-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
184 lines (153 loc) · 4.56 KB
/
noxfile.py
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
from __future__ import annotations
import json
import os
from pathlib import Path
import nox
nox.options.default_venv_backend = "uv|virtualenv"
nox.options.reuse_existing_virtualenvs = True
PY39 = "3.9"
PY310 = "3.10"
PY311 = "3.11"
PY312 = "3.12"
PY313 = "3.13"
PY_VERSIONS = [PY39, PY310, PY311, PY312, PY313]
PY_DEFAULT = PY_VERSIONS[0]
PY_LATEST = PY_VERSIONS[-1]
DJ42 = "4.2"
DJ50 = "5.0"
DJ51 = "5.1"
DJMAIN = "main"
DJMAIN_MIN_PY = PY310
DJ_VERSIONS = [DJ42, DJ50, DJ51, DJMAIN]
DJ_LTS = [DJ42]
DJ_DEFAULT = DJ_LTS[0]
DJ_LATEST = DJ_VERSIONS[-2]
def version(ver: str) -> tuple[int, ...]:
"""Convert a string version to a tuple of ints, e.g. "3.10" -> (3, 10)"""
return tuple(map(int, ver.split(".")))
def should_skip(python: str, django: str) -> bool:
"""Return True if the test should be skipped"""
if django == DJMAIN and version(python) < version(DJMAIN_MIN_PY):
# Django main requires Python 3.10+
return True
if django == DJ50 and version(python) < version(PY310):
# Django 5.0 requires Python 3.10+
return True
if django == DJ51 and version(python) < version(PY310):
# Django 5.1 requires Python 3.10+
return True
return False
@nox.session
def test(session):
session.notify(f"tests(python='{PY_DEFAULT}', django='{DJ_DEFAULT}')")
@nox.session
@nox.parametrize(
"python,django",
[
(python, django)
for python in PY_VERSIONS
for django in DJ_VERSIONS
if not should_skip(python, django)
],
)
def tests(session, django):
session.run_install(
"uv",
"sync",
"--extra",
"tests",
"--frozen",
"--inexact",
"--no-install-package",
"django",
"--python",
session.python,
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
if django == DJMAIN:
session.install(
"django @ https://github.com/django/django/archive/refs/heads/main.zip"
)
else:
session.install(f"django=={django}")
command = ["python", "-m", "pytest"]
if session.posargs and all(arg for arg in session.posargs):
command.append(*session.posargs)
session.run(*command)
@nox.session
def coverage(session):
session.run_install(
"uv",
"sync",
"--extra",
"tests",
"--frozen",
"--python",
PY_LATEST,
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
try:
session.run("python", "-m", "pytest", "--cov", "--cov-report=")
finally:
report_cmd = ["python", "-m", "coverage", "report"]
session.run(*report_cmd)
if summary := os.getenv("GITHUB_STEP_SUMMARY"):
report_cmd.extend(["--skip-covered", "--skip-empty", "--format=markdown"])
with Path(summary).open("a") as output_buffer:
output_buffer.write("")
output_buffer.write("### Coverage\n\n")
output_buffer.flush()
session.run(*report_cmd, stdout=output_buffer)
else:
session.run(
"python", "-m", "coverage", "html", "--skip-covered", "--skip-empty"
)
@nox.session
def types(session):
session.run_install(
"uv",
"sync",
"--extra",
"types",
"--frozen",
"--python",
PY_LATEST,
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
command = ["python", "-m", "mypy", "."]
if session.posargs and all(arg for arg in session.posargs):
command.append(*session.posargs)
session.run(*command)
@nox.session
def demo(session):
session.run_install(
"uv",
"sync",
"--extra",
"types",
"--frozen",
"--python",
PY_DEFAULT,
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
command = ["python", "example/demo.py", "runserver"]
if session.posargs and all(arg for arg in session.posargs):
command.append(*session.posargs)
else:
command.append("localhost:8000")
session.run(*command)
@nox.session
def gha_matrix(session):
sessions = session.run("nox", "-l", "--json", silent=True)
matrix = {
"include": [
{
"python-version": session["python"],
"django-version": session["call_spec"]["django"],
}
for session in json.loads(sessions)
if session["name"] == "tests"
]
}
with Path(os.environ["GITHUB_OUTPUT"]).open("a") as fh:
print(f"matrix={matrix}", file=fh)