-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
179 lines (149 loc) · 4.22 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
from pathlib import Path
from tempfile import TemporaryDirectory
import os
import nox
ROOT = Path(__file__).parent
PYPROJECT = ROOT / "pyproject.toml"
DOCS = ROOT / "docs"
PACKAGE = ROOT / "referencing_http"
REQUIREMENTS = dict(
docs=DOCS / "requirements.txt",
tests=ROOT / "test-requirements.txt",
)
REQUIREMENTS_IN = {
path.parent / f"{path.stem}.in" for path in REQUIREMENTS.values()
}
SUPPORTED = ["pypy3.10", "3.10", "3.11", "3.12"]
LATEST = SUPPORTED[-1]
nox.options.sessions = []
def session(default=True, python=LATEST, **kwargs): # noqa: D103
def _session(fn):
if default:
nox.options.sessions.append(kwargs.get("name", fn.__name__))
return nox.session(python=python, **kwargs)(fn)
return _session
@session(python=SUPPORTED)
def tests(session):
"""
Run the test suite.
"""
session.install("-r", REQUIREMENTS["tests"])
if session.posargs and session.posargs[0] == "coverage":
if len(session.posargs) > 1 and session.posargs[1] == "github":
github = Path(os.environ["GITHUB_STEP_SUMMARY"])
else:
github = None
session.install("coverage[toml]")
session.run("coverage", "run", "-m", "pytest", PACKAGE)
if github is None:
session.run("coverage", "report")
else:
with github.open("a") as summary:
summary.write("### Coverage\n\n")
summary.flush() # without a flush, output seems out of order.
session.run(
"coverage",
"report",
"--format=markdown",
stdout=summary,
)
else:
session.run("python", "-m", "pytest", *session.posargs, PACKAGE)
@session(python=SUPPORTED)
def audit(session):
"""
Audit Python dependencies for vulnerabilities.
"""
session.install("pip-audit", ROOT)
session.run("python", "-m", "pip_audit")
@session(tags=["build"])
def build(session):
"""
Build a distribution suitable for PyPI and check its validity.
"""
session.install("build", "twine")
with TemporaryDirectory() as tmpdir:
session.run("python", "-m", "build", ROOT, "--outdir", tmpdir)
session.run("twine", "check", "--strict", tmpdir + "/*")
@session()
def secrets(session):
"""
Check for accidentally included secrets.
"""
session.install("detect-secrets")
session.run("detect-secrets", "scan", ROOT)
@session(tags=["style"])
def style(session):
"""
Check for coding style.
"""
session.install("ruff")
session.run("ruff", "check", ROOT)
@session()
def typing(session):
"""
Statically check typing annotations.
"""
session.install("pyright", ROOT)
session.run("pyright", PACKAGE)
@session(tags=["docs"])
@nox.parametrize(
"builder",
[
nox.param(name, id=name)
for name in [
"dirhtml",
"doctest",
"linkcheck",
"man",
"spelling",
]
],
)
def docs(session, builder):
"""
Build the documentation using various Sphinx builders.
"""
session.install("-r", DOCS / "requirements.txt")
with TemporaryDirectory() as tmpdir_str:
tmpdir = Path(tmpdir_str)
argv = ["-n", "-T", "-W"]
if builder != "spelling":
argv += ["-q"]
posargs = session.posargs or [tmpdir / builder]
session.run(
"python",
"-m",
"sphinx",
"-b",
builder,
DOCS,
*argv,
*posargs,
)
@session(tags=["docs", "style"], name="docs(style)")
def docs_style(session):
"""
Check the documentation source style.
"""
session.install(
"doc8",
"pygments",
"pygments-github-lexers",
)
session.run("python", "-m", "doc8", "--config", PYPROJECT, DOCS)
@session(default=False)
def requirements(session):
"""
Update requirements files.
"""
session.install("pip-tools")
for each in REQUIREMENTS_IN:
session.run(
"pip-compile",
"--resolver",
"backtracking",
"--strip-extras",
"-U",
each.relative_to(ROOT),
)