-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathnoxfile.py
400 lines (332 loc) · 11.4 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
from __future__ import annotations
import argparse
import sys
import webbrowser
from argparse import ArgumentParser
from pathlib import Path
from shutil import rmtree
from tempfile import TemporaryDirectory
# fmt: off
PROJECT_ROOT = Path(__file__).parent
# scripts path also contains administrative code/modules which are used by some nox targets
SCRIPTS = PROJECT_ROOT / "scripts"
DOC = PROJECT_ROOT / "doc"
DOC_BUILD = DOC / "build"
sys.path.append(f"{SCRIPTS}")
# fmt: on
from typing import Iterator
import nox
from git_helpers import tags
from links import check as _check
from links import documentation as _documentation
from links import urls as _urls
from nox import Session
from nox.sessions import SessionRunner
from version_check import (
version_from_poetry,
version_from_python_module,
version_from_string,
)
from exasol.odbc import (
ODBC_DRIVER,
odbcconfig,
)
# default actions to be run if nothing is explicitly specified with the -s option
nox.options.sessions = ["project:fix"]
from noxconfig import PROJECT_CONFIG
def find_session_runner(session: Session, name: str) -> SessionRunner:
"""Helper function to find parameterized action by name"""
for s, _ in session._runner.manifest.list_all_sessions():
if name in s.signatures:
return s
session.error(f"Could not find a nox session by the name {name!r}")
def _python_files(path: Path) -> Iterator[Path]:
files = filter(lambda path: "dist" not in path.parts, PROJECT_ROOT.glob("**/*.py"))
files = filter(lambda path: ".eggs" not in path.parts, files)
files = filter(lambda path: "venv" not in path.parts, files)
return files
from exasol.toolbox.nox._format import (
Mode,
_code_format,
_pyupgrade,
_version,
fix,
)
@nox.session(name="project:check", python=False)
def check(session: Session) -> None:
"""Runs all available checks on the project"""
from exasol.toolbox.nox._lint import (
_pylint,
_type_check,
)
py_files = [f"{file}" for file in _python_files(PROJECT_CONFIG.root)]
_version(session, Mode.Check, PROJECT_CONFIG.version_file)
_code_format(session, Mode.Check, py_files)
_pylint(session, py_files)
_type_check(session, py_files)
from exasol.toolbox.nox._lint import (
lint,
type_check,
)
from exasol.toolbox.nox._metrics import report
@nox.session(name="db:start", python=False)
def start_db(session: Session) -> None:
"""Start a test database. For more details append '-- -h'"""
def parser() -> ArgumentParser:
p = ArgumentParser(
usage="nox -s db:start -- [-h] [--db-version]",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
p.add_argument(
"--db-version",
choices=PROJECT_CONFIG.exasol_versions,
default=PROJECT_CONFIG.exasol_versions[0],
help="which will be used",
)
return p
def start(db_version: str) -> None:
session.run(
"itde",
"spawn-test-environment",
"--environment-name",
f"{PROJECT_CONFIG.environment_name}",
"--database-port-forward",
f"{PROJECT_CONFIG.db_port}",
"--bucketfs-port-forward",
f"{PROJECT_CONFIG.bucketfs_port}",
"--docker-db-image-version",
db_version,
"--db-mem-size",
"4GB",
external=True,
)
args = parser().parse_args(session.posargs)
start(args.db_version)
@nox.session(name="db:stop", python=False)
def stop_db(session: Session) -> None:
"""Stop the test database"""
session.run("docker", "kill", "db_container_test", external=True)
def _coverage_command():
coverage_command = [
"coverage",
"run",
"-a",
f"--rcfile={PROJECT_ROOT / 'pyproject.toml'}",
"-m",
]
return coverage_command
@nox.session(name="test:sqla", python=False)
def sqlalchemy_tests(session: Session) -> None:
"""
Run the sqlalchemy integration tests suite. For more details append '-- -h'
Attention:
Make sure the sqla compliance suite is run in isolation, to avoid side effects from custom tests
e.g. because of unintended implicit schema open/closes.
Expects a running test db
"""
def parser() -> ArgumentParser:
p = ArgumentParser(
usage="nox -s test:sqla -- [-h] [--connector]",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
p.add_argument(
"--connector",
choices=PROJECT_CONFIG.connectors,
default=PROJECT_CONFIG.connectors[0],
help="which will be used",
)
return p
with odbcconfig(ODBC_DRIVER) as (config, env):
args = parser().parse_args(session.posargs)
connector = args.connector
session.run(
*_coverage_command(),
"pytest",
"--dropfirst",
"--db",
f"exasol-{connector}",
f"{PROJECT_ROOT / 'test' / 'integration' / 'sqlalchemy'}",
external=True,
env=env,
)
@nox.session(name="test:unit", python=False)
def unit_tests(session: Session) -> None:
"""Run the unit tests"""
session.run(
"pytest",
f"{PROJECT_ROOT / 'test' / 'unit'}",
external=True,
)
@nox.session(name="test:exasol", python=False)
def exasol_tests(session: Session) -> None:
"""Run the integration tests with a specific connector. For more details append '-- -h'"""
def parser() -> ArgumentParser:
p = ArgumentParser(
usage="nox -s test:exasol -- [-h] [--connector]",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
p.add_argument(
"--connector",
choices=PROJECT_CONFIG.connectors,
default=PROJECT_CONFIG.connectors[0],
help="which will be used",
)
return p
with odbcconfig(ODBC_DRIVER) as (config, env):
args = parser().parse_args(session.posargs)
connector = args.connector
session.run(
*_coverage_command(),
"pytest",
"--dropfirst",
"--db",
f"exasol-{connector}",
f"{PROJECT_ROOT / 'test' / 'integration' / 'exasol'}",
external=True,
env=env,
)
@nox.session(name="test:regression", python=False)
def regression_tests(session: Session) -> None:
"""Run regression tests"""
session.run(
*_coverage_command(),
"pytest",
f"{PROJECT_ROOT / 'test' / 'integration' / 'regression'}",
)
@nox.session(name="test:integration", python=False)
def integration_tests(session: Session) -> None:
"""Run integration tests with a specific configuration. For more details append '-- -h'"""
def parser() -> ArgumentParser:
p = ArgumentParser(
usage="nox -s test:integration -- [-h] [--connector] [--db-version]",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
p.add_argument(
"--connector",
choices=PROJECT_CONFIG.connectors,
default=PROJECT_CONFIG.connectors[0],
help="which will be used",
)
p.add_argument(
"--db-version",
choices=PROJECT_CONFIG.exasol_versions,
default=PROJECT_CONFIG.exasol_versions[0],
help="which will be used",
)
p.add_argument(
"--coverage",
action="store_true",
help="This is only here for compatibility. Coverage will always be collected.",
)
return p
coverage_file = PROJECT_ROOT / ".coverage"
coverage_file.unlink(missing_ok=True)
args = parser().parse_args(session.posargs)
session.notify(
find_session_runner(session, "db:start"),
posargs=["--db-version", f"{args.db_version}"],
)
session.notify(
find_session_runner(session, f"test:sqla"),
posargs=["--connector", f"{args.connector}"],
)
session.notify(
find_session_runner(session, f"test:exasol"),
posargs=["--connector", f"{args.connector}"],
)
session.notify(
find_session_runner(session, f"test:regression"),
)
session.notify(find_session_runner(session, "db:stop"))
@nox.session(name="test:skipped", python=False)
def report_skipped(session: Session) -> None:
"""
Runs all tests for all supported connectors and creates a csv report of skipped tests for each connector.
Attention: This task expects a running test database (db-start).
"""
with TemporaryDirectory() as tmp_dir:
for connector in PROJECT_CONFIG.connectors:
report = Path(tmp_dir) / f"test-report{connector}.json"
with odbcconfig(ODBC_DRIVER) as (config, env):
session.run(
"pytest",
"--dropfirst",
"--db",
f"exasol-{connector}",
f"{PROJECT_ROOT / 'test' / 'integration' / 'sqlalchemy'}",
"--json-report",
f"--json-report-file={report}",
external=True,
env=env,
)
session.run(
"python",
f"{SCRIPTS / 'report.py'}",
"-f",
"csv",
"--output",
f"skipped-tests-{connector}.csv",
f"{connector}",
f"{report}",
)
# fmt: off
from exasol.toolbox.nox._documentation import (
build_docs,
build_multiversion,
clean_docs,
open_docs,
)
# fmt: on
@nox.session(name="docs:links", python=False)
def list_links(session: Session) -> None:
"""List all the links within the documentation."""
for path, url in _urls(_documentation(PROJECT_ROOT)):
session.log(f"Url: {url}, File: {path}")
@nox.session(name="docs:links:check", python=False)
def check_links(session: Session) -> None:
"""Checks whether all links in the documentation are accessible."""
errors = []
for path, url in _urls(_documentation(PROJECT_ROOT)):
status, details = _check(url)
if status != 200:
errors.append((path, url, status, details))
if errors:
session.error(
"\n"
+ "\n".join(f"Url: {e[1]}, File: {e[0]}, Error: {e[3]}" for e in errors)
)
# fmt: off
from exasol.toolbox.nox._documentation import (
build_docs,
build_multiversion,
clean_docs,
open_docs,
)
def _connector_matrix(config: Config):
CONNECTORS = ['websocket']
attr = "connectors"
connectors = getattr(config, attr, CONNECTORS)
if not hasattr(config, attr):
_log.warning(
"Config does not contain '%s' setting. Using default: %s",
attr,
CONNECTORS,
)
return {"connector": connectors}
from exasol.toolbox.nox._ci import (
exasol_matrix,
python_matrix,
)
@nox.session(name="matrix:all", python=False)
def full_matrix(session: Session) -> None:
"""Output the full build matrix for Python & Exasol versions as JSON."""
import json
from exasol.toolbox.nox._ci import (
_exasol_matrix,
_python_matrix,
)
matrix = _python_matrix(PROJECT_CONFIG)
matrix.update(_exasol_matrix(PROJECT_CONFIG))
matrix.update(_connector_matrix(PROJECT_CONFIG))
print(json.dumps(matrix))
# fmt: on