-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathnoxfile.py
More file actions
165 lines (130 loc) · 4.86 KB
/
Copy pathnoxfile.py
File metadata and controls
165 lines (130 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Flexible test automation with Python for this project.
To run all sessions, run the following command:
$ nox
To run a specific session, run the following command:
$ nox -s <session-name>
To run a session with additional arguments, run the following command:
$ nox -s <session-name> -- <additional-arguments>
To list all available sessions, run the following command:
$ nox -l
"""
import os
import re
from pathlib import Path
import nox
from nox.sessions import Session
# default sessions to run (sorted alphabetically)
nox.options.sessions = ["lint", "python"]
# reuse virtual environment for all sessions
nox.options.reuse_venv = "always"
# use venv as the default virtual environment backend
nox.options.default_venv_backend = "venv"
# do not download missing Python interpreter
nox.options.download_python = "never"
def install_requirements(session: Session) -> None:
"""Install requirements for all sessions."""
session.install("--no-deps", "-r", "requirements-extras.txt")
def parse_supported_python_versions() -> list[str]:
"""Parse supported Python versions from pyproject.toml."""
pyproject = Path("pyproject.toml").read_text()
versions = re.findall(r'"Programming Language :: Python :: (3\.\d+)"', pyproject)
return versions
@nox.session()
def lint(session: Session) -> None:
"""Run linters."""
exc = None
install_requirements(session)
cmds = [
"ruff check hermeto tests noxfile.py",
"ruff format --check --diff hermeto tests noxfile.py",
"mypy --install-types --non-interactive hermeto tests noxfile.py",
]
for cmd in cmds:
try:
session.run(*cmd.split(), *session.posargs, silent=True)
except Exception as e:
exc = e
if exc:
raise exc
@nox.session(name="ruff-fix")
def ruff_fix(session: Session) -> None:
"""Run ruff with auto-fix for linting and formatting."""
exc = None
install_requirements(session)
cmds = [
"ruff check --fix hermeto tests noxfile.py",
"ruff format hermeto tests noxfile.py",
]
for cmd in cmds:
try:
session.run(*cmd.split(), *session.posargs, silent=True)
except Exception as e:
exc = e
if exc:
raise exc
@nox.session(name="python", python=parse_supported_python_versions())
def unit_tests(session: Session) -> None:
"""Run unit tests and generate coverage report."""
install_requirements(session)
# install the application package
session.install(".")
# disable color output in GitHub Actions
env = {"TERM": "dumb"} if os.getenv("CI") == "true" else None
cmd = "pytest --log-level=DEBUG --doctest-modules -W ignore::DeprecationWarning hermeto tests/unit"
if not session.posargs:
# enable coverage when no pytest positional arguments are passed through
cmd += " --cov=hermeto --cov-config=pyproject.toml --cov-report=term --cov-report=html --cov-report=xml --no-cov-on-fail"
session.run(*cmd.split(), *session.posargs, env=env)
def _run_integration_tests(session: Session, env: dict[str, str]) -> None:
install_requirements(session)
cmd = "pytest --log-cli-level=WARNING -W ignore::DeprecationWarning tests/integration"
session.run(*cmd.split(), *session.posargs, env=env)
@nox.session(name="integration-tests")
def integration_tests(session: Session) -> None:
"""Run integration tests."""
_run_integration_tests(session, {})
@nox.session(name="all-integration-tests")
def all_integration_tests(session: Session) -> None:
"""Run all integration tests that are available."""
_run_integration_tests(
session,
{
"HERMETO_TEST_LOCAL_NEXUS": "1",
"HERMETO_TEST_LOCAL_NEXUS_PROXY": "1",
},
)
@nox.session(name="generate-test-data")
def generate_test_data(session: Session) -> None:
"""Run all integration tests that are available and update SBOMs."""
_run_integration_tests(
session,
{
"HERMETO_TEST_GENERATE_DATA": "1",
},
)
@nox.session(name="pip-compile")
def pip_compile(session: Session) -> None:
"""Update requirements.txt and requirements-extras.txt files."""
PWD = os.environ["PWD"]
uv_pip_compile_cmd = (
"pip install uv && "
# requirements.txt
"uv pip compile --generate-hashes --output-file=requirements.txt --python=3.10 --refresh --no-strip-markers pyproject.toml && "
# requirements-extras.txt
"uv pip compile --all-extras --generate-hashes --output-file=requirements-extras.txt --python=3.10 --refresh --no-strip-markers pyproject.toml"
)
cmd = [
"podman",
"run",
"--rm",
"--volume",
f"{PWD}:/hermeto:rw,Z",
"--workdir",
"/hermeto",
"mirror.gcr.io/library/python:3.10-alpine",
"sh",
"-c",
uv_pip_compile_cmd,
]
session.run(*cmd, external=True)