-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
131 lines (110 loc) · 4.16 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
"""Nox sessions."""
import os
import random
import shutil
from pathlib import Path
import nox
os.environ.update({"PDM_IGNORE_SAVED_PYTHON": "1"})
package = "svante"
python_versions = ["3.12", "3.11", "3.10", "3.9"]
primary_python_version = "3.12"
nox.needs_version = ">= 2021.10.1"
nox.options.sessions = (
"pre-commit",
"safety",
"mypy",
"tests",
"typeguard",
"xdoctest",
"docs-build",
)
nox.options.reuse_existing_virtualenvs = True
@nox.session(name="pre-commit", python=primary_python_version)
def precommit(session: nox.Session) -> None:
"""Lint using pre-commit."""
args = session.posargs or [
"run",
"--all-files",
"--hook-stage=manual",
"--show-diff-on-failure",
]
session.run_always("pdm", "install", "-G", "pre-commit", external=True)
session.run("pre-commit", *args)
@nox.session(python=primary_python_version)
def safety(session: nox.Session) -> None:
"""Scan dependencies for insecure packages."""
session.run_always("pdm", "install", "-G", "safety", external=True)
session.run_always("pdm", "export", "-o", "requirements.txt",
"--without-hashes", external=True)
session.run("safety", "check", "--full-report")
Path("requirements.txt").unlink()
@nox.session(python=primary_python_version)
def mypy(session: nox.Session) -> None:
"""Type-check using mypy."""
args = session.posargs or ["svante"]
session.run_always("pdm", "install", "-G", "tests", external=True)
session.run("mypy", "--check-untyped-defs", *args)
@nox.session(python=python_versions)
def tests(session: nox.Session) -> None:
"""Run the test suite."""
session.run_always("pdm", "install", "-G", "tests", external=True)
try:
session.run(
"coverage",
"run",
"-m",
"pytest",
*session.posargs,
)
cov_path = Path(".coverage")
if cov_path.exists():
cov_path.rename(f".coverage.{random.randrange(100000)}") # noqa: S311
finally:
if session.interactive:
session.notify("coverage", posargs=[])
@nox.session(python=primary_python_version)
def coverage(session: nox.Session) -> None:
"""Produce the coverage report."""
args = session.posargs or ["report"]
session.run_always("pdm", "install", "-G", "coverage", external=True)
if not session.posargs and any(Path().glob(".coverage.*")):
session.run("coverage", "combine")
session.run("coverage", *args)
@nox.session(python=primary_python_version)
def typeguard(session: nox.Session) -> None:
"""Runtime type checking using Typeguard."""
session.run_always(
"pdm", "install", "-G", "tests", external=True
)
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)
@nox.session(python=primary_python_version)
def xdoctest(session: nox.Session) -> None:
"""Run examples with xdoctest."""
if session.posargs:
args = [package, *session.posargs]
else:
args = [f"--modname={package}", "--command=all"]
if "FORCE_COLOR" in os.environ:
args.append("--colored=1")
session.run_always("pdm", "install", "-G", "xdoctest", external=True)
session.run("python", "-m", "xdoctest", *args)
@nox.session(name="docs-build", python=primary_python_version)
def docs_build(session: nox.Session) -> None:
"""Build the documentation."""
args = session.posargs or ["docs", "docs/_build"]
if not session.posargs and "FORCE_COLOR" in os.environ:
args.insert(0, "--color")
session.run_always("pdm", "install", "-G", "docs", external=True)
build_dir = Path("docs", "_build")
if build_dir.exists():
shutil.rmtree(build_dir)
session.run("sphinx-build", *args)
@nox.session(python=primary_python_version)
def docs(session: nox.Session) -> None:
"""Build and serve the documentation with live reloading on file changes."""
args = session.posargs or ["--open-browser", "docs", "docs/_build"]
session.run_always("pdm", "install", "-G", "docs", external=True)
build_dir = Path("docs", "_build")
if build_dir.exists():
shutil.rmtree(build_dir)
session.run("sphinx-autobuild", *args)