-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnoxfile.py
More file actions
164 lines (126 loc) · 4.63 KB
/
noxfile.py
File metadata and controls
164 lines (126 loc) · 4.63 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
from __future__ import annotations
import os
import functools
import pathlib
import shutil
import nox
# Control factors for finding pieces of the module
MODULE_NAME = "module_name"
LINT_PATH = "./src"
TESTS_PATH = "./tests"
# What we allowed to clean (delete)
CLEANABLE_TARGETS = [
"./dist",
"./build",
"./.nox",
"./.coverage",
"./.coverage.*",
"./coverage.json",
"./htmlcov",
"./**/.mypy_cache",
"./**/.pytest_cache",
"./**/__pycache__",
"./**/*.pyc",
"./**/*.pyo",
]
# Define the default sessions run when `nox` is called on the CLI
nox.options.default_venv_backend = "uv"
nox.options.sessions = ["lock", "format", "lint", "test"]
# All linters and formatters are run with `uv run --active`
LINTERS: list[tuple[str, ...]] = [
("flake8", "--show-source", LINT_PATH, TESTS_PATH),
("mypy", "--pretty", "--package", MODULE_NAME),
("mypy", "--pretty", TESTS_PATH),
]
FORMATTERS: list[tuple[str, ...]] = [
(
"isort",
"--force-single-line-imports",
"--profile",
"black",
"--add-import",
"from __future__ import annotations",
LINT_PATH,
TESTS_PATH,
),
("black", LINT_PATH, TESTS_PATH),
]
# Default args for all 'uv sync' and 'uv run' calls
UV_ARGS = [
"--frozen",
"--quiet",
"--active",
]
@nox.session(name="dev", python=False)
def dev_session(session: nox.Session) -> None:
"""Create a development environment."""
session.run_install("uv", "sync")
@nox.session(name="test", python=False)
def run_tests_with_coverage(session: nox.Session) -> None:
"""Run pytest in isolated environment, display coverage. Extra arguements passed to pytest."""
uv_args = UV_ARGS
if "UV_PYTHON" in os.environ:
uv_args = UV_ARGS + [f"--python={os.environ['UV_PYTHON']}"]
partial = "partial-coverage" in session.posargs
session.run_install("uv", "sync", *uv_args)
coverage = functools.partial(session.run, "uv", "run", *uv_args, "coverage")
coverage("erase")
if partial:
session.posargs.remove("partial-coverage")
coverage("run", "--parallel-mode", "--module", "pytest", *session.posargs)
else:
coverage("run", "--module", "pytest", *session.posargs)
coverage("report", "--show-missing")
coverage("html")
@nox.session(name="combine", python=False)
def combine_coverage(session: nox.Session) -> None:
"""Combine parallel-mode coverage files and produce reports."""
session.run_install("uv", "sync", *UV_ARGS)
coverage = functools.partial(session.run, "uv", "run", *UV_ARGS, "coverage")
coverage("combine")
coverage("report", "--show-missing")
coverage("html")
coverage("json")
@nox.session(name="lint", python=False)
def run_linters(session: nox.Session) -> None:
"""Run code linters, and type checking against all files."""
session.run_install("uv", "sync", "--group", "lint", *UV_ARGS)
for linter_args in LINTERS:
session.run("uv", "run", *UV_ARGS, *linter_args)
@nox.session(name="format", python=False)
def run_formatters(session: nox.Session) -> None:
"""Run code formatters against all files."""
session.run_install("uv", "sync", "--group", "format", *UV_ARGS)
for formatter_args in FORMATTERS:
session.run("uv", "run", *UV_ARGS, *formatter_args)
@nox.session(name="build", python=False)
def build_artifacts(session: nox.Session) -> None:
"""Build a sdist and wheel."""
session.run("uv", "build")
@nox.session(name="lock", python=False)
def validate_lock_file(session: nox.Session) -> None:
"""Ensure the uv.lock file exists and is aligned with dependencies."""
session.run("uv", "lock")
@nox.session(name="upgrade", python=False)
def upgrade_dependencies(session: nox.Session) -> None:
"""Upgrade all versions of all dependencies."""
session.run("uv", "lock", "--upgrade")
@nox.session(name="upgrade-package", python=False)
def upgrade_specific_package(session: nox.Session) -> None:
"""Upgrade specific package name given in extra args."""
if not session.posargs:
session.log("No package name provided, nothing to do.")
else:
session.run("uv", "lock", "--upgrade-package", *session.posargs)
@nox.session(name="clean", python=False)
def clean_project_files(session: nox.Session) -> None:
"""Clean cache, .pyc, .pyo, and build artifact files from project."""
count = 0
for searchpath in CLEANABLE_TARGETS:
for filepath in pathlib.Path(".").glob(searchpath):
if filepath.is_dir():
shutil.rmtree(filepath)
else:
filepath.unlink()
count += 1
session.log(f"{count} files cleaned.")