-
Notifications
You must be signed in to change notification settings - Fork 45
/
noxfile.py
159 lines (121 loc) · 4.1 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
""" define nox sessions for running tests and checks """
# pylint: disable=missing-function-docstring
import glob
from pathlib import Path
import nox
PY_VERSION = "3.10"
def get_base_images(file_name):
"""would be cleaner to use yaml.load here, but want to avoid the dependency"""
out = []
with open(file_name, "r", encoding="utf-8") as yaml_fh:
for line in yaml_fh:
if line.strip() == "image:":
break
for line in yaml_fh:
if line.strip().startswith("- "):
out.append(line.strip()[2:])
else:
break
return out
def get_default_base_image(file_name):
with open(file_name, "r", encoding="utf-8") as yaml_fh:
for line in yaml_fh:
bare_line = line.strip().replace('"', "'")
if bare_line.startswith("DEFAULT_BASE_IMAGE:"):
return bare_line.split("'")[1]
raise ValueError("Did not find DEFAULT_BASE_IMAGE")
REPO_DIR = Path(__file__).resolve().parent
PUSH_WORKFLOW = REPO_DIR / ".github/workflows/push_latest.yml"
BASE_IMAGES = get_base_images(PUSH_WORKFLOW)
DEFAULT_BASE_IMAGE = get_default_base_image(PUSH_WORKFLOW)
assert DEFAULT_BASE_IMAGE in BASE_IMAGES
nox.options.sessions = [
f"image_tests(base_image='{DEFAULT_BASE_IMAGE}')",
"shellcheck",
"pylint",
"flake8",
"mypy",
"black",
"pytest",
"build_docs",
]
PYLINT_DEPS = [
"pylint==2.12.2",
"nox==2022.1.7",
"pytest==7.0.0", # so "import pytest" doesn't get reported
]
FLAKE8_DEPS = [
"flake8==4.0.1",
"flake8-bugbear==22.1.11",
"flake8-builtins==1.5.3",
"flake8-comprehensions==3.8.0",
]
PYTEST_DEPS = [
"pytest==7.0.0",
"pytest-mock==3.7.0",
"toml==0.10.2",
]
MYPY_DEPS = [
"mypy==1.9.0",
"types-requests",
]
py_files = set(glob.glob("*.py") + glob.glob("test/*.py")) - {"__init__.py"}
shell_scripts = set(glob.glob("*.sh") + glob.glob("test/*.bats") + glob.glob("test/test_helper/*.bash"))
@nox.session(python=PY_VERSION)
@nox.parametrize("base_image", BASE_IMAGES)
def image_tests(session, base_image):
"""Tests generation and use of docker images"""
session.run(str(REPO_DIR / "test_with_base_image.sh"), f"{base_image}", external=True)
@nox.session(python=PY_VERSION)
def default_base_image_tests(session):
"""Tests generation and use of docker images using default base image"""
image_tests(session, DEFAULT_BASE_IMAGE)
@nox.session(python=PY_VERSION)
def shellcheck(session):
"""lint all shell scripts with shellcheck"""
inputs = ["-x"] + list(shell_scripts)
try:
session.run("shellcheck", *inputs, external=True)
except FileNotFoundError:
session.run(
"docker",
"run",
"--rm",
"-v",
f"{REPO_DIR}:/mnt",
"koalaman/shellcheck:stable",
*inputs,
external=True,
)
# All sessions defined below here are for testing/linting python code
@nox.session(python=PY_VERSION)
def pylint(session):
session.install("-r", "requirements.txt", *PYLINT_DEPS)
session.run("pylint", *py_files)
@nox.session(python=PY_VERSION)
def flake8(session):
session.install(*FLAKE8_DEPS)
session.run("flake8", *py_files)
@nox.session(python=PY_VERSION)
def mypy(session):
session.install("-r", "requirements.txt", *MYPY_DEPS)
session.run("mypy", *py_files)
@nox.session(python=PY_VERSION)
def black(session):
session.install("black")
session.run("black", "--check", "--diff", "--color", *py_files)
@nox.session(python=PY_VERSION)
def blacken(session):
"""this modifies the files to meet black's requirements"""
session.install("black")
session.run("black", *py_files)
@nox.session(python=PY_VERSION)
def pytest(session):
"""tests python code, mainly check_version.py"""
session.install("-r", "requirements.txt", *PYTEST_DEPS)
session.run("pytest", *session.posargs, "test")
@nox.session(python=PY_VERSION)
def build_docs(session):
"""build the html version of the documentation"""
session.install("-r", "docs/requirements.txt")
session.run("make", "-C", "docs", "html")