-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathnoxfile.py
More file actions
374 lines (335 loc) · 10.3 KB
/
noxfile.py
File metadata and controls
374 lines (335 loc) · 10.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from __future__ import annotations
import json
import os
import re
import shlex
import shutil
import tempfile
from pathlib import Path
import nox
PROJECT_ROOT = Path(__file__).parent
PYPROJECT_PATH = PROJECT_ROOT / "pyproject.toml"
nox.options.reuse_existing_virtualenvs = True
nox.options.sessions = ["tests"]
def _load_pyproject() -> dict:
try:
import tomllib
except ImportError: # pragma: no cover - py<3.11
import tomli as tomllib
with PYPROJECT_PATH.open("rb") as f:
return tomllib.load(f)
def _version_range(requirement: str) -> list[str]:
min_match = re.search(r">=(\d+\.\d+)", requirement)
max_match = re.search(r"<(\d+\.\d+)", requirement)
if not (min_match and max_match):
return []
min_v = tuple(map(int, min_match.group(1).split(".")))
max_v = tuple(map(int, max_match.group(1).split(".")))
versions = []
current = min_v
while current < max_v:
versions.append(".".join(map(str, current)))
current = (current[0], current[1] + 1)
return versions
def _matrix_versions() -> tuple[list[str], list[str]]:
data = _load_pyproject()
python_req = data["project"]["requires-python"]
py_versions = _version_range(python_req)
mpl_req = next(
dep for dep in data["project"]["dependencies"] if dep.startswith("matplotlib")
)
mpl_versions = _version_range(mpl_req) or ["3.9"]
return py_versions, mpl_versions
PYTHON_VERSIONS, MPL_VERSIONS = _matrix_versions()
def _mamba_root() -> Path:
return PROJECT_ROOT / ".nox" / "micromamba"
def _mamba_exe(session: nox.Session) -> str:
exe = os.environ.get("MAMBA_EXE", "micromamba")
if shutil.which(exe):
return exe
session.error(
"micromamba not found; install it or set MAMBA_EXE to the micromamba path."
)
return exe
def _mamba_env_name(python_version: str, matplotlib_version: str) -> str:
return f"ultraplot-py{python_version}-mpl{matplotlib_version}"
def _ensure_mamba_env(
session: nox.Session, python_version: str, matplotlib_version: str
) -> str:
root = _mamba_root()
env_name = _mamba_env_name(python_version, matplotlib_version)
env_path = root / "envs" / env_name
if env_path.exists():
return env_name
exe = _mamba_exe(session)
env = os.environ.copy()
env["MAMBA_ROOT_PREFIX"] = str(root)
session.run(
exe,
"create",
"-y",
"-n",
env_name,
"-f",
str(PROJECT_ROOT / "environment.yml"),
f"python={python_version}",
f"matplotlib={matplotlib_version}",
external=True,
env=env,
)
return env_name
def _mamba_run(session: nox.Session, env_name: str, *args: str) -> None:
exe = _mamba_exe(session)
env = os.environ.copy()
env["MAMBA_ROOT_PREFIX"] = str(_mamba_root())
quoted = " ".join(shlex.quote(arg) for arg in args)
session.run(
"bash",
"-lc",
f'eval "$({shlex.quote(exe)} shell hook -s bash)"; '
f"micromamba activate {shlex.quote(env_name)}; {quoted}",
external=True,
env=env,
)
def _install_ultraplot(session: nox.Session, env_name: str, path: str) -> None:
_mamba_run(
session,
env_name,
"python",
"-m",
"pip",
"install",
"--no-build-isolation",
"--no-deps",
path,
)
def _selected_nodeids(env: dict[str, str]) -> list[str] | None:
if env.get("TEST_MODE", "full") != "selected":
return None
tokens = env.get("TEST_NODEIDS", "").split()
nodeids = [t for t in tokens if "::" in t or t.endswith(".py")]
return nodeids or None
@nox.session(venv_backend="none")
@nox.parametrize("python_version", PYTHON_VERSIONS)
@nox.parametrize("matplotlib_version", MPL_VERSIONS)
def tests(session: nox.Session, python_version: str, matplotlib_version: str) -> None:
env_name = _ensure_mamba_env(session, python_version, matplotlib_version)
_install_ultraplot(session, env_name, ".")
nodeids = _selected_nodeids(session.env)
if nodeids:
_mamba_run(
session,
env_name,
"pytest",
"--cov=ultraplot",
"--cov-branch",
"--cov-report",
"term-missing",
"--cov-report=xml",
*nodeids,
)
else:
_mamba_run(
session,
env_name,
"pytest",
"--cov=ultraplot",
"--cov-branch",
"--cov-report",
"term-missing",
"--cov-report=xml",
"ultraplot",
)
@nox.session
def select_tests(session: nox.Session) -> None:
if len(session.posargs) >= 2:
base, head = session.posargs[:2]
else:
base, head = "origin/main", "HEAD"
ci_dir = PROJECT_ROOT / ".ci"
ci_dir.mkdir(parents=True, exist_ok=True)
changed = ci_dir / "changed.txt"
with changed.open("w", encoding="utf-8") as changed_file:
session.run(
"git",
"diff",
"--name-only",
base,
head,
external=True,
stdout=changed_file,
)
selection = ci_dir / "selection.json"
session.run(
"python",
"tools/ci/select_tests.py",
"--map",
str(ci_dir / "test-map.json"),
"--changed-files",
str(changed),
"--output",
str(selection),
"--always-full",
"pyproject.toml",
"--always-full",
"environment.yml",
"--always-full",
"ultraplot/__init__.py",
"--ignore",
"docs/**",
"--ignore",
"README.rst",
)
data = json.loads(selection.read_text(encoding="utf-8"))
session.log("mode=%s", data.get("mode"))
session.log("tests=%s", " ".join(data.get("tests", [])))
@nox.session
def build_test_map(session: nox.Session) -> None:
env_name = _ensure_mamba_env(session, "3.11", "3.9")
_install_ultraplot(session, env_name, ".")
ci_dir = PROJECT_ROOT / ".ci"
ci_dir.mkdir(parents=True, exist_ok=True)
_mamba_run(
session,
env_name,
"pytest",
"-n",
"auto",
"--cov=ultraplot",
"--cov-branch",
"--cov-context=test",
"--cov-report=",
"ultraplot",
)
_mamba_run(
session,
env_name,
"python",
"tools/ci/build_test_map.py",
"--coverage-file",
".coverage",
"--output",
str(ci_dir / "test-map.json"),
"--root",
".",
)
@nox.session(venv_backend="none")
@nox.parametrize("python_version", PYTHON_VERSIONS)
@nox.parametrize("matplotlib_version", MPL_VERSIONS)
def compare_baseline(
session: nox.Session, python_version: str, matplotlib_version: str
) -> None:
base_ref = session.env.get("BASE_REF", "origin/main")
baseline_dir = Path(session.env.get("BASELINE_DIR", "ultraplot/tests/baseline"))
results_dir = Path(session.env.get("RESULTS_DIR", "results"))
baseline_dir.mkdir(parents=True, exist_ok=True)
results_dir.mkdir(parents=True, exist_ok=True)
env_name = _ensure_mamba_env(session, python_version, matplotlib_version)
_install_ultraplot(session, env_name, ".")
nodeids = _selected_nodeids(session.env)
with tempfile.TemporaryDirectory() as tmpdir:
session.run(
"git",
"worktree",
"add",
"--detach",
tmpdir,
base_ref,
external=True,
)
try:
_install_ultraplot(session, env_name, tmpdir)
_mamba_run(
session,
env_name,
"python",
"-c",
"import ultraplot as plt; plt.config.Configurator()._save_yaml('ultraplot.yml')",
)
if nodeids:
_mamba_run(
session,
env_name,
"pytest",
"-W",
"ignore",
"--mpl-generate-path",
str(baseline_dir),
"--mpl-default-style=./ultraplot.yml",
*nodeids,
)
else:
_mamba_run(
session,
env_name,
"pytest",
"-W",
"ignore",
"--mpl-generate-path",
str(baseline_dir),
"--mpl-default-style=./ultraplot.yml",
"ultraplot/tests",
)
finally:
session.run(
"git",
"worktree",
"remove",
"--force",
tmpdir,
external=True,
)
session.run("git", "worktree", "prune", external=True)
_install_ultraplot(session, env_name, ".")
_mamba_run(
session,
env_name,
"python",
"-c",
"import ultraplot as plt; plt.config.Configurator()._save_yaml('ultraplot.yml')",
)
if nodeids:
_mamba_run(
session,
env_name,
"pytest",
"-W",
"ignore",
"--mpl",
"--mpl-baseline-path",
str(baseline_dir),
"--mpl-results-path",
str(results_dir),
"--mpl-generate-summary=html",
"--mpl-default-style=./ultraplot.yml",
*nodeids,
)
else:
_mamba_run(
session,
env_name,
"pytest",
"-W",
"ignore",
"--mpl",
"--mpl-baseline-path",
str(baseline_dir),
"--mpl-results-path",
str(results_dir),
"--mpl-generate-summary=html",
"--mpl-default-style=./ultraplot.yml",
"ultraplot/tests",
)
@nox.session
def build_dist(session: nox.Session) -> None:
session.install(
"--upgrade", "pip", "wheel", "setuptools", "setuptools_scm", "build", "twine"
)
session.run("python", "-m", "build", "--sdist", "--wheel", ".", "--outdir", "dist")
session.run("python", "-m", "pip", "install", "dist/ultraplot*.whl")
session.run(
"python",
"-c",
"import ultraplot as u; assert not u.__version__.startswith('0.'), u.__version__",
)
session.run("python", "-m", "twine", "check", "dist/*")