-
Notifications
You must be signed in to change notification settings - Fork 23
/
noxfile.py
174 lines (141 loc) · 4.73 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
"""Nox sessions."""
from contextlib import contextmanager
from pathlib import Path
import tempfile
from typing import List
from uuid import uuid4
import nox
import toml
nox.options.sessions = "lint", "mypy", "tests", "xdoctest", "mindeps"
PY_VERSIONS = ["3.11", "3.10", "3.9", "3.8", "3.7"]
PY_LATEST = "3.11"
@nox.session(python=PY_VERSIONS)
def tests(session):
"""Run the test suite."""
args = session.posargs or [
"--cov",
"--cov-report",
"term-missing",
"--cov-report",
"xml",
"-ra",
"-vv",
]
session.run("poetry", "install", "--no-dev", external=True)
install_with_constraints(
session,
"coverage[toml]",
"grpcio-tools",
"pytest",
"pytest-asyncio",
"pytest-cov",
)
session.run("pytest", *args)
@nox.session(python=PY_VERSIONS)
def xdoctest(session) -> None:
"""Run examples with xdoctest."""
args = session.posargs or ["all"]
session.run("poetry", "install", "--no-dev", external=True)
install_with_constraints(session, "xdoctest")
session.run("python", "-m", "xdoctest", "grpc_interceptor", *args)
@nox.session(python=PY_LATEST)
def docs(session):
"""Build the documentation."""
session.run("poetry", "install", "--no-dev", "-E", "testing", external=True)
install_with_constraints(session, "sphinx")
session.run("sphinx-build", "docs", "docs/_build")
SOURCE_CODE = ["src", "tests", "noxfile.py", "docs/conf.py"]
@nox.session(python=PY_LATEST)
def black(session):
"""Run black code formatter."""
args = session.posargs or SOURCE_CODE
install_with_constraints(session, "black")
session.run("black", *args)
@nox.session(python=PY_LATEST)
def lint(session):
"""Lint using flake8."""
args = session.posargs or SOURCE_CODE
install_with_constraints(
session,
"flake8",
"flake8-bandit",
"flake8-bugbear",
"flake8-docstrings",
"flake8-import-order",
)
session.run("flake8", *args)
@nox.session(python=PY_VERSIONS)
def mypy(session):
"""Type-check using mypy."""
args = session.posargs or SOURCE_CODE
install_with_constraints(session, "mypy")
session.run("mypy", "--install-types", "--non-interactive", *args)
session.run("mypy", *args)
@nox.session(python=PY_LATEST)
def safety(session):
"""Scan dependencies for insecure packages."""
with _temp_file() as requirements:
session.run(
"poetry",
"export",
"--dev",
"--format=requirements.txt",
"--without-hashes",
f"--output={requirements}",
external=True,
)
install_with_constraints(session, "safety")
session.run("safety", "check", f"--file={requirements}", "--full-report")
@nox.session(python="3.7")
def mindeps(session):
"""Run test with minimum versions of dependencies."""
deps = _parse_minimum_dependency_versions()
session.install(*deps)
session.run("pytest", env={"PYTHONPATH": "src"})
def install_with_constraints(session, *args, **kwargs):
"""Install packages constrained by Poetry's lock file."""
with _temp_file() as requirements:
session.run(
"poetry",
"export",
"--dev",
"--format=requirements.txt",
f"--output={requirements}",
"--without-hashes",
external=True,
)
session.install(f"--constraint={requirements}", *args, **kwargs)
@contextmanager
def _temp_file():
# NamedTemporaryFile doesn't work on Windows.
path = Path(tempfile.gettempdir()) / str(uuid4())
try:
yield path
finally:
try:
path.unlink()
except FileNotFoundError:
pass
def _parse_minimum_dependency_versions() -> List[str]:
pyproj = toml.load("pyproject.toml")
dependencies = pyproj["tool"]["poetry"]["dependencies"]
dev_dependencies = pyproj["tool"]["poetry"]["dev-dependencies"]
min_deps = []
for deps in (dependencies, dev_dependencies):
for dep, constraint in deps.items():
if dep == "python":
continue
if not isinstance(constraint, str):
# Don't install deps with python contraints, because they're always for
# newer versions on python.
if "python" in constraint:
continue
constraint = constraint["version"]
if constraint.startswith("^") or constraint.startswith("~"):
version = constraint[1:]
elif constraint.startswith(">="):
version = constraint[2:]
else:
version = constraint
min_deps.append(f"{dep}=={version}")
return min_deps