This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
noxfile.py
114 lines (94 loc) · 3.6 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
"""
Nox sessions, which each run in an isolated Python environment with
minimal dependencies. This ensures that we never accidentally depend
on dev dependencies in production code (thus, we achieve a feature
similar to Maven's dependency scopes).
"""
import os
import subprocess
from tempfile import NamedTemporaryFile
from typing import Any
import nox
from nox.sessions import Session
PYTHON_VERSIONS = ["3.9", "3.10"]
nox.needs_version = ">=2021.10.1"
nox.options.sessions = ["lint", "tests"]
nox.options.stop_on_first_error = True
def install_with_constraints(session: Session, *args: str, **kwargs: Any) -> None:
"""Install packages constrained by poetry's lockfile."""
with NamedTemporaryFile() as constraints:
# This approach is by Claudio Jolowicz, see his excellect
# example at github.com/cjolowicz/hypermodern-python.
session.run(
"poetry",
"export",
"--with",
"dev",
"--without-hashes",
"--format=constraints.txt",
f"--output={constraints.name}",
external=True,
)
session.install(f"--constraint={constraints.name}", *args, **kwargs)
def is_docker_running() -> bool:
"""Check if the Docker daemon is running."""
args = ["docker", "info"]
result = subprocess.run(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return result.returncode == 0
def build_docker_image(session: Session) -> None:
"""Build the image required by our docker integration tests."""
session.run(
"docker",
"build",
"--tag",
"inloop-integration-test",
"tests/testrunner",
external=True,
)
def poetry_install(session: Session) -> None:
"""Install the runtime dependencies with poetry."""
session.run("poetry", "install", "--only", "main", external=True)
@nox.session(python=PYTHON_VERSIONS)
def tests(session: Session) -> None:
"""Run the complete test suite without dev dependencies."""
args = session.posargs or ["-v2", "--failfast"]
if is_docker_running():
build_docker_image(session)
elif "CI" in os.environ:
session.error("Docker daemon is not running")
else:
args.append("--exclude-tag=needs-docker")
session.warn("Docker daemon is not running, skipping docker tests")
poetry_install(session)
install_with_constraints(session, "coverage[toml]")
session.run("coverage", "run", "./runtests.py", "--", *args)
session.run("coverage", "report")
@nox.session(python=PYTHON_VERSIONS)
def lint(session: Session) -> None:
"""Check code style with flake8 and isort."""
locations = "inloop", "tests", "noxfile.py", "manage.py", "runtests.py"
install_with_constraints(
session,
"flake8",
"flake8-annotations",
"flake8-black",
"flake8-bugbear",
"flake8-docstrings",
"flake8-isort",
)
session.run("flake8", *locations)
@nox.session(python=PYTHON_VERSIONS)
def pytype(session: Session) -> None:
"""Statically check for type errors with pytype."""
args = session.posargs or ["--config", "pytype.cfg"]
poetry_install(session)
install_with_constraints(session, "pytype")
session.run("pytype", *args)
@nox.session(python=PYTHON_VERSIONS)
def typeguard(session: Session) -> None:
"""Run the test suite with run-time type checking of PEP-484 annotations."""
args = session.posargs or ["-v2", "--failfast"]
build_docker_image(session)
poetry_install(session)
install_with_constraints(session, "typeguard")
session.run("python", "runtests.py", "--with-typeguard", "--", *args)