-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnoxfile.py
168 lines (136 loc) · 4.67 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
import os
from typing import List, Optional
import nox
CI = bool(os.getenv("CI"))
PYTHON = ["3.11", "3.12"] if not CI else None
SESSIONS = "ruff", "mypy", "lockfile", "pytest"
nox.options.sessions = SESSIONS
os.environ.update({"PDM_IGNORE_SAVED_PYTHON": "1"})
def pdm_sync(
session: nox.Session,
*,
self: bool = False,
default: bool = False,
editable: bool = True,
groups: Optional[List[str]] = None,
) -> None:
"""Install dependencies using PDM.
Args:
session: The nox session.
self: Whether to install the package itself.
default: Whether to install the default dependencies.
editable: Whether to install packages in editable mode.
groups: The dependency groups to install.
"""
cmd = ["pdm", "sync"]
if not self:
cmd.append("--no-self")
if not default:
cmd.append("--no-default")
if not editable:
cmd.append("--no-editable")
if groups:
for group in groups:
cmd.append("-G")
cmd.append(group)
session.run(*cmd, external=True)
def pdm_check_lockfile(session: nox.Session) -> None:
"""Check if the lockfile is up-to-date."""
session.run("pdm", "lock", "--check", external=True)
@nox.session(python=PYTHON)
def ruff(session: nox.Session) -> None:
"""Lint code and ensure formatting using ruff."""
pdm_sync(session, groups=["lint"])
session.run("ruff", "check", "src", "tests")
session.run("ruff", "format", "--check", "src", "tests")
@nox.session(python=PYTHON)
def black(session: nox.Session) -> None:
"""Check if style adheres to black."""
ruff(session)
@nox.session(python=PYTHON)
def mypy(session: nox.Session) -> None:
"""Static type checking using mypy."""
pdm_sync(session, default=True, groups=["typecheck", "type_stubs"])
session.run("mypy", "src")
@nox.session(python=PYTHON)
def lockfile(session: nox.Session) -> None:
"""Check if the lockfile is up-to-date."""
pdm_check_lockfile(session)
@nox.session(python=PYTHON)
def pytest(session: nox.Session) -> None:
"""Run fast pytest tests if not running in CI, otherwise run all."""
if not CI:
pytest_fast(session)
else:
pytest_full(session)
@nox.session(python=PYTHON)
def pytest_fast(session: nox.Session) -> None:
"""Run pytest tests that are fast to execute.
This session excludes e2e and integration tests since they are slow
to execute and might require external dependencies.
It is intended to be run multiple times during development.
"""
pdm_sync(session, self=True, default=True, groups=["test"])
session.warn(
"Skipping e2e tests for faster execution. "
"To include them, run `nox -s pytest_full`."
)
session.run(
"pytest", "tests", "-m", "not e2e and not integration", *session.posargs
)
@nox.session(python=PYTHON)
def pytest_full(session: nox.Session) -> None:
"""Run all pytest tests.
This session includes all tests and is intended to be
run in CI or before a commit.
"""
pdm_sync(session, self=True, default=True, groups=["test"])
args = session.posargs if not CI else ["--cov"]
session.run(
"pytest",
"tests",
"-m",
# FRRouting integration tests have their own session
"not (frrouting and integration)",
*args,
)
session.notify(
"pytest_frrouting_integration"
) # TODO: Fix that only one session is run
@nox.session(python=PYTHON)
@nox.parametrize(
"frrouting",
[
"7.3.1",
"7.4.0",
"7.5.1",
"8.1.0",
"8.2.2",
"8.3.1",
"8.4.2",
"8.5.3",
"9.0.1",
],
)
def pytest_frrouting_integration(session: nox.Session, frrouting: str) -> None:
"""Run pytest FRRouting integration tests.
This session runs the integration tests for all supported FRRouting
versions using the FRRouting docker image.
"""
pdm_sync(session, self=True, default=True, groups=["test"])
session.env["FRR_VERSION"] = frrouting
session.run(
"pytest", "tests", "-m", "(frrouting and integration)", *session.posargs
)
@nox.session(python=PYTHON)
def safety(session: nox.Session) -> None:
"""Scan dependencies for known security vulnerabilities using safety."""
session.install("safety")
session.run("pdm", "export", "-o", "requirements.txt", external=True)
session.run("safety", "check", "--file=requirements.txt", "--full-report")
@nox.session(python=PYTHON)
def codecov(session: nox.Session) -> None:
"""Upload codecov coverage data."""
session.install("coverage", "codecov")
session.run("coverage", "xml", "--fail-under=0")
session.run("codecov", "-f", "coverage.xml")