-
-
Notifications
You must be signed in to change notification settings - Fork 600
/
noxfile.py
236 lines (188 loc) · 7.29 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
# type: ignore
import copy
import os
import nox
BASE = os.path.abspath(os.path.dirname(__file__))
DEFAULT_PYTHON_VERSIONS = ["3.6", "3.7", "3.8"]
PYTHON_VERSIONS = os.environ.get(
"NOX_PYTHON_VERSIONS", ",".join(DEFAULT_PYTHON_VERSIONS)
).split(",")
PLUGINS_INSTALL_COMMANDS = (["pip", "install"], ["pip", "install", "-e"])
# Allow limiting testing to specific plugins
# The list ['ALL'] indicates all plugins
PLUGINS = os.environ.get("PLUGINS", "ALL").split(",")
SILENT = os.environ.get("VERBOSE", "0") == "0"
def install_hydra(session, cmd):
# clean install hydra
session.chdir(BASE)
session.run(*cmd, ".", silent=SILENT)
def install_pytest(session):
session.install("pytest")
# session.install("pytest", "pytest_parallel")
def run_pytest(session, directory="."):
session.run("pytest", directory, silent=False, *session.posargs)
# session.run("pytest", "--workers=30", silent=SILENT)
def plugin_names():
return [x["name"] for x in list_plugins()]
def list_plugins():
example_plugins = [
{"name": x, "path": "examples/{}".format(x)}
for x in sorted(os.listdir(os.path.join(BASE, "plugins/examples")))
]
plugins = [
{"name": x, "path": x}
for x in sorted(os.listdir(os.path.join(BASE, "plugins")))
if x != "examples"
]
return plugins + example_plugins
def get_all_plugins():
return [
{
"name": plugin["name"],
"path": plugin["path"],
"module": "hydra_plugins." + plugin["name"],
}
for plugin in list_plugins()
if plugin["name"] in PLUGINS or PLUGINS == ["ALL"]
]
def test_example_app(session, install_cmd):
# Install and test example app
session.run(*install_cmd, "examples/advanced/hydra_app_example", silent=SILENT)
session.run(
"pytest", "examples/advanced/hydra_app_example", silent=SILENT, *session.posargs
)
@nox.session
def lint(session):
session.install("--upgrade", "setuptools", "pip")
# needed for isort to properly identify pytest as third party
session.install("pytest")
session.install(
"mypy",
"flake8",
"flake8-copyright",
"git+git://github.com/timothycrosley/isort.git@c54b3dd4620f9b3e7ac127c583ecb029fb90f1b7",
)
session.run("pip", "install", "-e", ".", silent=SILENT)
session.run("flake8", "--config", ".circleci/flake8_py3.cfg")
session.install("black")
# if this fails you need to format your code with black
session.run("black", "--check", ".")
session.run("isort", "--check", ".")
# Mypy
session.run("mypy", ".", "--strict")
# Mypy for plugins
for plugin in get_all_plugins():
session.run("mypy", os.path.join("plugins", plugin["path"]), "--strict")
@nox.session(python=PYTHON_VERSIONS)
@nox.parametrize(
"install_cmd",
PLUGINS_INSTALL_COMMANDS,
ids=[" ".join(x) for x in PLUGINS_INSTALL_COMMANDS],
)
def test_core(session, install_cmd):
session.install("--upgrade", "setuptools", "pip")
install_hydra(session, install_cmd)
install_pytest(session)
run_pytest(session, "tests")
test_example_app(session, install_cmd)
def get_setup_python_versions(session, setup_py):
out = session.run("python", setup_py, "--classifiers", silent=True).splitlines()
pythons = filter(lambda line: "Programming Language :: Python" in line, out)
return [p[len("Programming Language :: Python :: ") :] for p in pythons]
def get_plugin_python_version(session, plugin):
return get_setup_python_versions(
session, os.path.join(BASE, "plugins", plugin["path"], "setup.py")
)
@nox.session(python=PYTHON_VERSIONS)
@nox.parametrize(
"install_cmd",
PLUGINS_INSTALL_COMMANDS,
ids=[" ".join(x) for x in PLUGINS_INSTALL_COMMANDS],
)
def test_plugins(session, install_cmd):
session.install("--upgrade", "setuptools", "pip")
install_pytest(session)
install_hydra(session, install_cmd)
plugin_enabled = {}
all_plugins = get_all_plugins()
# Install all supported plugins in session
for plugin in all_plugins:
pythons = get_plugin_python_version(session, plugin)
# Verify this plugin supports the python we are testing on, skip otherwise
plugin_python_versions = get_plugin_python_version(session, plugin)
plugin_enabled[plugin["path"]] = session.python in pythons
if not plugin_enabled[plugin["path"]]:
py_str = ",".join(plugin_python_versions)
session.log(
f"Not testing {plugin['name']} on Python {session.python}, supports [{py_str}]"
)
continue
cmd = list(install_cmd) + [os.path.join("plugins", plugin["path"])]
session.run(*cmd, silent=SILENT)
# Test that we can import Hydra
session.run("python", "-c", "from hydra import main", silent=SILENT)
# Test that we can import all installed plugins
for plugin in all_plugins:
# install all other plugins that are compatible with the current Python version
if plugin_enabled[plugin["path"]]:
session.run("python", "-c", "import {}".format(plugin["module"]))
# Run Hydra tests to verify installed plugins did not break anything
run_pytest(session, "tests")
# Run tests for all installed plugins
for plugin in all_plugins:
# install all other plugins that are compatible with the current Python version
if plugin_enabled[plugin["path"]]:
session.chdir(os.path.join(BASE, "plugins", plugin["path"]))
run_pytest(session)
# code coverage runs with python 3.6
@nox.session(python="3.6")
def coverage(session):
session.install("--upgrade", "setuptools", "pip")
session.install("coverage", "pytest")
session.run("pip", "install", "-e", ".", silent=SILENT)
# Install all plugins in session
for plugin in get_all_plugins():
session.run(
"pip",
"install",
"-e",
os.path.join("plugins", plugin["path"]),
silent=SILENT,
)
session.run("coverage", "erase")
session.run("coverage", "run", "--append", "-m", "pytest", silent=SILENT)
for plugin in list_plugins():
plugin_python_versions = get_plugin_python_version(session, plugin)
if session.python not in plugin_python_versions:
continue
session.run(
"coverage",
"run",
"--append",
"-m",
"pytest",
os.path.join("plugins", plugin["path"]),
silent=SILENT,
)
# Increase the fail_under as coverage improves
session.run("coverage", "report", "--fail-under=80")
session.run("coverage", "erase")
@nox.session(python=PYTHON_VERSIONS)
def test_jupyter_notebook(session):
versions = copy.copy(DEFAULT_PYTHON_VERSIONS)
if session.python not in versions:
session.skip(
"Not testing Jupyter notebook on Python {}, supports [{}]".format(
session.python, ",".join(versions)
)
)
session.install("--upgrade", "setuptools", "pip")
session.install("jupyter", "nbval")
install_hydra(session, ["pip", "install", "-e"])
session.run(
"pytest",
"--nbval",
"examples/notebook/hydra_notebook_example.ipynb",
silent=SILENT,
)