-
Notifications
You must be signed in to change notification settings - Fork 14
/
noxfile.py
185 lines (158 loc) · 5.73 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
185
from __future__ import annotations
import os
import sys
from pathlib import Path
import nox
HERE = Path(__file__).resolve().parent
nox.options.sessions = ["lint", "test"]
ALL_CPYTHON = [f"3.{minor}" for minor in range(8, 13 + 1)]
ALL_PYPY = [f"pypy3.{minor}" for minor in range(9, 10 + 1)]
ALL_PYTHON = ALL_CPYTHON + ALL_PYPY
@nox.session
def lint(session: nox.Session) -> None:
"""Run linters on the codebase."""
session.install("pre-commit")
session.run("pre-commit", "run", "-a")
def update_env_macos(session: nox.Session, env: dict[str, str]) -> None:
if sys.platform.startswith("darwin"):
# we don't support universal builds
machine = session.run( # type: ignore[union-attr]
"python", "-sSEc", "import platform; print(platform.machine())", silent=True
).strip()
env["ARCHFLAGS"] = f"-arch {machine}"
env["_PYTHON_HOST_PLATFORM"] = f"macosx-11.0-{machine}"
def remove_extension(session: nox.Session, in_place: bool = False) -> None:
if in_place:
where = HERE / "src" / "pybase64"
else:
command = "import sysconfig; print(sysconfig.get_path('platlib'))"
platlib = session.run("python", "-c", command, silent=True).strip() # type: ignore[union-attr]
where = Path(platlib) / "pybase64"
assert where.exists()
removed = False
for ext in ["*.so", "*.pyd"]:
for file in where.glob(ext):
session.log(f"removing '{file.relative_to(HERE)}'")
file.unlink()
removed = True
if not in_place:
assert removed
@nox.session(python="3.12")
def develop(session: nox.Session) -> None:
"""create venv for dev."""
session.install("nox", "setuptools", "-r", "requirements-test.txt")
# make extension mandatory by exporting CIBUILDWHEEL=1
env = {"CIBUILDWHEEL": "1"}
update_env_macos(session, env)
session.install("-e", ".", env=env)
@nox.session(python=ALL_PYTHON)
def test(session: nox.Session) -> None:
"""Run tests."""
session.install("-r", "requirements-test.txt")
# make extension mandatory by exporting CIBUILDWHEEL=1
env = {"CIBUILDWHEEL": "1"}
update_env_macos(session, env)
session.install(".", env=env)
session.run("pytest", *session.posargs, env=env)
# run without extension as well
env.pop("CIBUILDWHEEL")
remove_extension(session)
session.run("pytest", *session.posargs, env=env)
@nox.session(python=["3.13", "pypy3.10"])
def _coverage(session: nox.Session) -> None:
"""internal coverage run. Do not run manually"""
with_sde = "--with-sde" in session.posargs
clean = "--clean" in session.posargs
report = "--report" in session.posargs
coverage_args = (
"--cov=pybase64",
"--cov=tests",
"--cov-append",
"--cov-report=",
)
pytest_command = ("pytest", *coverage_args)
session.install("-r", "requirements-test.txt", "-r", "requirements-coverage.txt")
remove_extension(session, in_place=True)
# make extension mandatory by exporting CIBUILDWHEEL=1
env = {
"CIBUILDWHEEL": "1",
"CFLAGS": "-O0 -coverage",
"LDFLAGS": "-coverage",
"COVERAGE_PROCESS_START": "1",
}
update_env_macos(session, env)
session.install("-e", ".", env=env)
if clean:
session.run("coverage", "erase", env=env)
session.run(*pytest_command, env=env)
if with_sde:
cpu = "spr"
sde = ("sde", f"-{cpu}", "--")
session.run(*sde, *pytest_command, f"--sde-cpu={cpu}", env=env, external=True)
for cpu in ["p4p", "mrm", "pnr", "nhm", "snb", "hsw"]:
sde = ("sde", f"-{cpu}", "--")
pytest_addopt = (f"--sde-cpu={cpu}", "-k=test_flags")
session.run(*sde, *pytest_command, *pytest_addopt, env=env, external=True)
# run without extension as well
env.pop("CIBUILDWHEEL")
remove_extension(session, in_place=True)
session.run(*pytest_command, env=env)
# reports
if report:
threshold = 100.0 if "CI" in os.environ else 99.8
session.run("coverage", "report", "--show-missing", f"--fail-under={threshold}")
session.run("coverage", "xml", "-ocoverage-python.xml")
gcovr_config = ("-r=.", "-e=base64", "-e=.base64_build")
session.run(
"gcovr",
*gcovr_config,
"--fail-under-line=90",
"--txt",
"-s",
"--xml=coverage-native.xml",
)
@nox.session(venv_backend="none")
def coverage(session: nox.Session) -> None:
"""Coverage tests."""
posargs_ = set(session.posargs)
assert len(posargs_ & {"--clean", "--report"}) == 0
assert len(posargs_ - {"--with-sde"}) == 0
posargs = [*session.posargs, "--report"]
session.notify("_coverage-pypy3.10", ["--clean"])
session.notify("_coverage-3.13", posargs)
@nox.session(python="3.12")
def benchmark(session: nox.Session) -> None:
"""Benchmark tests."""
project_install: tuple[str, ...] = ("-e", ".")
posargs = session.posargs.copy()
if "--wheel" in posargs:
index = posargs.index("--wheel")
posargs.pop(index)
project_install = (posargs.pop(index),)
env = {"CIBUILDWHEEL": "1"}
update_env_macos(session, env)
session.install("-r", "requirements-benchmark.txt", *project_install, env=env)
session.run("pytest", "--codspeed", *posargs)
@nox.session(python="3.11")
def docs(session: nox.Session) -> None:
"""
Build the docs.
"""
session.install("-r", "requirements-doc.txt", ".")
session.run("pip", "list")
session.chdir("docs")
session.run(
"python",
"-m",
"sphinx",
"-T",
"-E",
"-b",
"html",
"-d",
"_build/doctrees",
"-D",
"language=en",
".",
"build",
)