-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnoxfile.py
More file actions
123 lines (93 loc) · 3.01 KB
/
noxfile.py
File metadata and controls
123 lines (93 loc) · 3.01 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
from os import environ
from pathlib import Path
import nox
package_path = Path(__file__).parent
is_circleci = 'CIRCLECI' in environ
nox.options.default_venv_backend = 'uv'
@nox.session
def pytest(session: nox.Session):
uv_sync(session)
pytest_run(session)
@nox.session
def prek(session: nox.Session):
uv_sync(session)
session.run(
'prek',
'run',
'--all-files',
)
@nox.session
def audit(session: nox.Session):
# Much faster to install the deps first and have pip-audit run against the venv
uv_sync(session)
session.run(
'pip-audit',
'--desc',
'--skip-editable',
*pip_audit_ignore_args(),
)
def pytest_run(session: nox.Session, *args, **env):
"""
Using functions for pytest and uv enables more advanced uses cases. Examples:
py_all = ['3.10', '3.11', '3.12', '3.13']
@session(py=py_all)
@parametrize('db', ['pg', 'sqlite'])
def pytest(session: Session, db: str):
uv_sync(session)
pytest_run(session, WEBTEST_DB=db)
@session(py=py_single)
def pytest_mssql(session: Session):
uv_sync(session, 'pytest', 'mssql')
pytest_run(session, WEBTEST_DB='mssql')
@session(py=py_single)
def pytest_i18n(session: Session):
uv_sync(session, extra='i18n')
pytest_run(session, WEBTEST_DB='sqlite')
"""
# CircleCI will give additional info in the UI about tests if junit-xml is output.
junit_opt = f'--junit-xml={package_path}/ci/test-reports/{session.name}.pytests.xml'
junit_args = (junit_opt,) if is_circleci else ()
session.run(
'pytest',
'-ra',
'--tb=native',
'--strict-markers',
'--cov',
'--cov-report=xml',
'--no-cov-on-fail',
*junit_args,
package_path / 'tests',
*args,
*session.posargs,
env=env,
)
def uv_sync(session: nox.Session, *groups, project=False, extra=None):
# If no group given, assume group shares name of session.
if not groups:
groups = (session.name,)
# At least pytest needs the project installed.
project_args = () if project or session.name.startswith('pytest') else ('--no-install-project',)
group_args = [arg for group in groups for arg in ('--group', group)]
extra_args = ('--extra', extra) if extra else ()
run_args = (
'uv',
'sync',
'--active',
'--frozen',
'--exact',
# Use --no-default-groups instead of --only-group as the latter implies
# --no-install-project.
'--no-default-groups',
*project_args,
*group_args,
*extra_args,
)
session.run(*run_args)
def pip_audit_ignore_args() -> list | tuple:
ignore_fpath = package_path / 'pip-audit-ignore.txt'
if not ignore_fpath.exists():
return ()
vuln_ids = [
line for line in ignore_fpath.read_text().strip().splitlines() if not line.startswith('#')
]
return [arg for vuln_id in vuln_ids for arg in ('--ignore-vuln', vuln_id)]