-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathnoxfile.py
More file actions
208 lines (155 loc) · 7.13 KB
/
noxfile.py
File metadata and controls
208 lines (155 loc) · 7.13 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import shutil
from enum import StrEnum
from pathlib import Path
import nox
from packaging.version import Version
PROJECT_DIR = Path(__file__).parent
BENCHMARK_DIR = PROJECT_DIR / "tests" / "benchmarks"
DOCS_SRC_DIR = PROJECT_DIR / "docs"
DOCS_BUILD_DIR = PROJECT_DIR / "dist" / "docs"
AUTODOC_PORT = 8001
class Python(StrEnum):
v3_14 = "3.14"
v3_13 = "3.13"
v3_12 = "3.12"
v3_11 = "3.11"
v3_10 = "3.10"
@property
def tag(self):
return f"py{self.value.replace('.', '')}"
class Django(StrEnum):
v6_0 = "6.0"
v5_2 = "5.2"
v5_1 = "5.1"
v5_0 = "5.0"
v4_2 = "4.2"
@property
def tag(self):
return f"dj{self.value.replace('.', '')}"
def is_combination_supported(py: Python, dj: Django) -> bool:
"""Determines if the given Python / Django versions combination is supported.
This is a direct implementation of the table provided in Django FAQ:
- https://docs.djangoproject.com/en/4.2/faq/install/#what-python-version-can-i-use-with-django
- https://docs.djangoproject.com/en/5.2/faq/install/#what-python-version-can-i-use-with-django
"""
if dj == Django.v4_2:
return Version(py) <= Version("3.12")
if dj == Django.v5_0:
return Version(py) <= Version("3.12")
if dj == Django.v5_1:
return Version(py) <= Version("3.13")
if dj == Django.v5_2:
return True
if dj >= Django.v6_0:
return Version("3.12") <= Version(py)
return False
def is_test_enabled_on_cicd(py: Python):
# Use this to filter out some Python version from CICD
return True
def build_test_matrix():
for py in Python:
for dj in Django:
if not is_combination_supported(py, dj):
continue
tags = ["tests", py.tag, dj.tag]
if is_test_enabled_on_cicd(py):
tags.append("cicd-tests")
session_id = f"Python {py} × Django {dj}" # noqa: RUF001, RUF003 (disable warning about ambiguous × char)
yield nox.param(py, dj, id=session_id, tags=tags)
@nox.session(name="tests", venv_backend="uv")
@nox.parametrize(["python", "django"], build_test_matrix())
def tests(session, python, django):
"""Execute test suite using pytest"""
env = {"UV_PROJECT_ENVIRONMENT": session.virtualenv.location}
session.run_install("uv", "sync", "-p", python, "--no-install-package", "django", env=env)
session.install(f"django=={django}.*", "--prerelease=allow")
session.run("django-admin", "--version")
post_args = session.posargs or []
# Benchmarks cannot be run with parallelization enabled, run tests & benchmarks separately
session.run("pytest", "-n", "auto", *post_args)
@nox.session(name="tests:current-venv", venv_backend="none", default=False, tags=["tests"])
def tests_current_venv(session):
"""Run tests in the current virtualenv only"""
post_args = session.posargs or []
session.run("uv", "run", "pytest", "-n", "auto", *post_args)
@nox.session(name="tests:coverage", venv_backend="none", default=False, tags=["tests"])
def coverage(session):
"""Run tests and generate a coverage report (in terminal by default, or in format passed as posarg)"""
cov_type = "term"
if session.posargs:
cov_type = session.posargs.pop(0)
allowed_cov_types = ("term", "term-missing", "annotate", "html", "xml", "json", "markdown", "lcov")
if cov_type not in allowed_cov_types:
raise ValueError(f"Invalid coverage report type {cov_type}, possible values are {allowed_cov_types}")
session.run("uv", "run", "pytest", "-n", "auto", "--cov", f"--cov-report={cov_type}", *session.posargs)
@nox.session(name="tests:duration", venv_backend="none", default=False, tags=["tests"])
def tests_duration(session):
"""Run tests and report the 20 slowest tests"""
session.run("uv", "run", "pytest", "--durations=20")
bench_ids = [f"Python {py}" for py in Python if is_test_enabled_on_cicd(py)]
bench_tags = [[py.tag] for py in Python if is_test_enabled_on_cicd(py)]
@nox.session(name="benchmarks", venv_backend="uv", default=False, tags=["benchmarks"])
@nox.parametrize(["python"], bench_ids, tags=bench_tags)
def benchmarks(session, python):
"""Run benchmarks on all supported Python versions"""
env = {"UV_PROJECT_ENVIRONMENT": session.virtualenv.location}
session.run_install("uv", "sync", "-p", python, env=env)
session.run("pytest", BENCHMARK_DIR, "--benchmark-enable")
@nox.session(name="benchmarks:current-venv", venv_backend="none", default=False, tags=["benchmarks"])
def benchmarks_current_venv(session):
"""Run benchmarks in the current virtualenv only"""
session.run("uv", "run", "pytest", BENCHMARK_DIR, "--benchmark-enable")
@nox.session(name="lint", venv_backend="none", tags=["ruff", "checks"])
def lint(session):
"""Check the project for common issues"""
session.run("uv", "run", "ruff", "check", ".")
@nox.session(name="lint:fix", venv_backend="none", default=False, tags=["ruff"])
def lint_fix(session):
"""Check the project for common issues and apply obvious fixes"""
session.run("uv", "run", "ruff", "check", ".", "--fix")
@nox.session(name="format", venv_backend="none", default=False, tags=["ruff", "checks"])
def format_apply(session):
"""Apply formatting rules on all files"""
session.run("uv", "run", "ruff", "format", ".")
@nox.session(name="format:check", venv_backend="none", tags=["ruff", "checks"])
def format_check(session):
"""Check that all files are well formated"""
session.run("uv", "run", "ruff", "format", ".", "--check")
@nox.session(venv_backend="uv", tags=["checks"])
def ty(session):
"""Verify type hints"""
env_vars = {"UV_PROJECT_ENVIRONMENT": session.virtualenv.location}
session.run_install("uv", "sync", "--group", "type-checking", env=env_vars)
session.run("ty", "check", env=env_vars)
@nox.session(venv_backend="uv", tags=["checks"])
def mypy(session):
"""Verify type hints"""
env_vars = {"UV_PROJECT_ENVIRONMENT": session.virtualenv.location}
session.run_install("uv", "sync", "--group", "type-checking", env=env_vars)
session.run("mypy", ".", env=env_vars)
@nox.session(name="docs:build", venv_backend="uv", default=False)
def docs_build(session):
"""Build the project's documentation"""
env_vars = {"UV_PROJECT_ENVIRONMENT": session.virtualenv.location}
session.run_install("uv", "sync", "--group", "docs", env=env_vars)
session.run("sphinx-build", DOCS_SRC_DIR, DOCS_BUILD_DIR, *session.posargs, env=env_vars)
@nox.session(name="docs:serve", venv_backend="uv", default=False)
def docs_serve(session):
"""Continuously rebuild the project's documentation and serve it on localhost:8001"""
env_vars = {"UV_PROJECT_ENVIRONMENT": session.virtualenv.location}
session.run_install("uv", "sync", "--group", "docs", env=env_vars)
session.run(
"sphinx-autobuild",
"-b",
"html",
"--port",
str(AUTODOC_PORT),
DOCS_SRC_DIR,
DOCS_BUILD_DIR,
*session.posargs,
env=env_vars,
)
@nox.session(name="docs:clean", venv_backend="none", default=False)
def docs_cleanup(session):
"""Cleanup previously built docs files"""
shutil.rmtree(DOCS_BUILD_DIR)