-
Notifications
You must be signed in to change notification settings - Fork 3
/
noxfile.py
103 lines (81 loc) · 3.12 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
import shutil
import sys
from pathlib import Path
from typing import Optional, Union
import nox
THIS_DIR = Path(__file__).parent
WINDOWS = sys.platform.startswith("win")
SUPPORTED_PYTHONS = ["3.7", "3.8", "3.9", "3.10"]
nox.needs_version = ">=2021.10.1"
nox.options.error_on_external_run = True
def wipe(session: nox.Session, path: Union[str, Path]) -> None:
if "--install-only" in sys.argv:
return
if isinstance(path, str):
path = Path.cwd() / path
normalized = path.relative_to(Path.cwd())
if not path.exists():
return
if path.is_file():
session.log(f"Deleting '{normalized}' file.")
path.unlink()
elif path.is_dir():
session.log(f"Deleting '{normalized}' directory.")
shutil.rmtree(path)
def get_flag(session: nox.Session, flag: str) -> bool:
if flag in session.posargs:
index = session.posargs.index(flag)
del session.posargs[index]
return True
return False
def get_option(session: nox.Session, name: str) -> Optional[str]:
assert name.startswith("--")
if name in session.posargs:
index = session.posargs.index(name)
try:
value = session.posargs[index + 1]
except IndexError:
session.warn(f"[WARN] missing argument to {name}")
else:
del session.posargs[index : index + 2]
assert isinstance(value, str)
return value
return None
@nox.session(name="lint")
def lint(session: nox.Session) -> None:
"""Run pre-commit."""
session.install("pre-commit")
session.run("pre-commit", "run", "--all-files", "--show-diff-on-failure")
@nox.session(name="tests", python=SUPPORTED_PYTHONS)
def tests(session: nox.Session) -> None:
"""A proper unit and functional test suite."""
session.install("-e", ".[test]")
session.run("diff-shades", "--version")
black_req = get_option(session, "--black-req")
if black_req:
session.install(black_req)
else:
session.install("black")
coverage = not get_flag(session, "--no-cov")
cmd = ["pytest", "tests"]
if coverage:
wipe(session, "htmlcov")
cmd.extend(["--cov", "--cov-context", "test"])
session.run(*cmd, *session.posargs)
if coverage:
session.run("coverage", "html")
# For some reason, a stray empty coverage is left behind, let's delete it.
# TODO: figure out why it is created in the first place and fix the underlying issue
for c in THIS_DIR.glob(".coverage.*"):
if not c.read_bytes():
wipe(session, c)
@nox.session(name="setup-env", venv_backend="none")
def setup_env(session: nox.Session) -> None:
"""Setup a basic (virtual) environment for manual testing."""
env_dir = THIS_DIR / ".venv"
bin_dir = env_dir / ("Scripts" if WINDOWS else "bin")
wipe(session, env_dir)
session.run(sys.executable, "-m", "virtualenv", str(env_dir))
session.run(str(bin_dir / "python"), "-m", "pip", "install", "-e", ".")
session.run(str(bin_dir / "python"), "-m", "pip", "install", "black")
session.log("Virtual environment at project root under '.venv' ready to go!")