-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
215 lines (165 loc) · 5.1 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
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
import nox
nox.options.envdir = ".cache/nox"
nox.options.default_venv_backend = "uv"
# all external utility tools used by the nox sessions
BUILD_TOOLS = ["build"]
COVERAGE_TOOLS = [
"coverage[toml]",
"coverage-badge",
"setuptools",
] # setuptools required with uv as coverage-badge imports pkg_resources
FORMATTING_TOOLS = ["ruff~=0.4.0"]
LINTING_TOOLS = FORMATTING_TOOLS
LOCKFILE_TOOLS = ["pip-tools>=7.0.0"]
TYPING_TOOLS = ["mypy"]
LOCKFILE_PATH = "pylock.txt"
COVERAGE_REPORTS_DIR = "artefacts/reports/coverage"
TEST_REPORTS_DIR = "artefacts/reports"
@nox.session
def dist_build(session: nox.Session) -> None:
"""Builds distributions (sdist and wheel).
Examples:
nox -s dist_build
"""
session.run("rm", "-rf", "dist", external=True)
session.install(*BUILD_TOOLS)
session.run("python", "-m", "build")
@nox.session
def formatting_check(session: nox.Session) -> None:
"""Checks codebase formatting.
Examples:
nox -s formatting_check
"""
session.install(*FORMATTING_TOOLS)
session.run("ruff", "check", "--select", "I")
session.run("ruff", "format", "--check", *session.posargs)
@nox.session
def formatting_fix(session: nox.Session) -> None:
"""Fixes codebase formatting.
Examples:
nox -s formatting_fix
"""
session.install(*FORMATTING_TOOLS)
session.run("ruff", "check", "--select", "I", "--fix")
session.run("ruff", "format", *session.posargs)
@nox.session
def linting_check(session: nox.Session) -> None:
"""Checks codebase lint quality.
Examples:
nox -s linting_check
"""
session.install(*LINTING_TOOLS)
session.run("ruff", "check", *session.posargs)
@nox.session
def linting_fix(session: nox.Session) -> None:
"""Fixes codebase lint quality where possible.
Examples:
nox -s linting_fix
"""
session.install(*LINTING_TOOLS)
session.run("ruff", "--fix", *session.posargs)
@nox.session
def typing_check(session: nox.Session) -> None:
"""Checks codebase static typing.
Examples:
nox -s typing_check
"""
session.install(".[tests]", *TYPING_TOOLS, "--constraint", LOCKFILE_PATH)
session.run("mypy", *session.posargs)
@nox.session
def dependencies_pin(session: nox.Session) -> None:
"""Generates a package dependencies' lockfile.
Examples:
nox -s dependencies_pin
"""
session.install(*LOCKFILE_TOOLS)
session.run(
"pip-compile",
"--verbose",
"--strip-extras",
"--no-emit-index-url",
"--no-emit-trusted-host",
"pyproject.toml",
"-o",
LOCKFILE_PATH,
"--upgrade",
env={"CUSTOM_COMPILE_COMMAND": f"nox -s {session.name}"},
)
session.log(f"Lockfile generated at {LOCKFILE_PATH!r} ✨")
@nox.session
def tests_run(session: nox.Session) -> None:
"""Runs tests and doctests.
Examples:
nox -s tests_run
"""
# Run tests
session.install(".[tests]", "--constraint", LOCKFILE_PATH)
doctests_target_dir = "src/"
tests_target_dir = "tests/"
coverage_datafile_path = f"{COVERAGE_REPORTS_DIR}/.coverage"
junitxml_path = f"{TEST_REPORTS_DIR}/.junitxml.xml"
session.run(
"coverage",
"run",
f"--data-file={coverage_datafile_path}",
"-m",
"pytest",
f"--junitxml={junitxml_path}",
doctests_target_dir,
tests_target_dir,
*session.posargs,
)
session.notify("coverage_report")
@nox.session
def coverage_report(session: nox.Session) -> None:
"""Generates coverage reports.
This session is usually triggered following a pytest coverage session generating
the coverage data files to build the reports on.
Examples:
nox -s coverage_report
"""
session.install(*COVERAGE_TOOLS)
# Output coverage reports in same folder (for convenience)
data_file = f"{COVERAGE_REPORTS_DIR}/.coverage"
session.run("coverage", "report", "--data-file", data_file)
session.run(
"coverage",
"html",
"--data-file",
data_file,
"-d",
f"{COVERAGE_REPORTS_DIR}/html",
)
session.run(
"coverage",
"xml",
"--data-file",
data_file,
"-o",
f"{COVERAGE_REPORTS_DIR}/coverage.xml",
)
session.run(
"coverage",
"json",
"--data-file",
data_file,
"-o",
f"{COVERAGE_REPORTS_DIR}/coverage.json",
)
session.notify("coverage_build_badge")
@nox.session
def coverage_build_badge(session: nox.Session) -> None:
"""Generates a coverage badge.
This session is usually triggered following a pytest coverage session generating
the coverage data files for which to build the badge.
Examples:
nox -s coverage_build_badge
"""
# coverage-badge only works from the same directory where the .coverage
# data file is located.
session.chdir(COVERAGE_REPORTS_DIR)
badge_filename = "coverage.svg"
# cleanup old badge
session.run("rm", "-rf", badge_filename, external=True)
session.install(*COVERAGE_TOOLS)
session.run("coverage-badge", "-o", badge_filename)