-
Notifications
You must be signed in to change notification settings - Fork 2
/
noxfile.py
159 lines (121 loc) · 4.82 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
from __future__ import annotations
import os
import pathlib
import shutil
import sys
import nox
# Control factors for finding pieces of the module
MODULE_NAME = "module_name"
TESTS_PATH = "tests"
COVERAGE_FAIL_UNDER = 50
DEFAULT_PYTHON_VERSION = "3.12"
PYTHON_MATRIX = ["3.9", "3.10", "3.11", "3.12", "3.13"]
VENV_PATH = "venv"
REQUIREMENT_IN_FILES = [
pathlib.Path("requirements/requirements.in"),
]
# What we allowed to clean (delete)
CLEANABLE_TARGETS = [
"./dist",
"./build",
"./.nox",
"./.coverage",
"./.coverage.*",
"./coverage.json",
"./**/.mypy_cache",
"./**/.pytest_cache",
"./**/__pycache__",
"./**/*.pyc",
"./**/*.pyo",
]
# Define the default sessions run when `nox` is called on the CLI
nox.options.sessions = [
"tests_with_coverage",
"coverage_combine_and_report",
"mypy_check",
]
@nox.session(python=PYTHON_MATRIX)
def tests_with_coverage(session: nox.Session) -> None:
"""Run unit tests with coverage saved to partial file."""
print_standard_logs(session)
session.install(".[test]")
session.run("coverage", "run", "-p", "-m", "pytest", TESTS_PATH)
@nox.session(python=DEFAULT_PYTHON_VERSION)
def coverage_combine_and_report(session: nox.Session) -> None:
"""Combine all coverage partial files and generate JSON report."""
print_standard_logs(session)
fail_under = f"--fail-under={COVERAGE_FAIL_UNDER}"
session.install(".[test]")
session.run("python", "-m", "coverage", "combine")
session.run("python", "-m", "coverage", "report", "-m", fail_under)
session.run("python", "-m", "coverage", "json")
@nox.session(python=DEFAULT_PYTHON_VERSION)
def mypy_check(session: nox.Session) -> None:
"""Run mypy against package and all required dependencies."""
print_standard_logs(session)
session.install(".")
session.install("mypy")
session.run("mypy", "-p", MODULE_NAME, "--no-incremental")
@nox.session(python=False)
def coverage(session: nox.Session) -> None:
"""Generate a coverage report. Does not use a venv."""
session.run("coverage", "erase")
session.run("coverage", "run", "-m", "pytest", TESTS_PATH)
session.run("coverage", "report", "-m")
@nox.session(python=DEFAULT_PYTHON_VERSION)
def build(session: nox.Session) -> None:
"""Build distribution files."""
print_standard_logs(session)
session.install("build")
session.run("python", "-m", "build")
@nox.session(python=False)
def install(session: nox.Session) -> None:
"""Setup a development environment. Uses active venv if available, builds one if not."""
# Use the active environement if it exists, otherwise create a new one
venv_path = os.environ.get("VIRTUAL_ENV", VENV_PATH)
if sys.platform == "win32":
py_command = "py"
venv_path = f"{venv_path}/Scripts"
activate_command = f"{venv_path}/activate"
else:
py_command = f"python{DEFAULT_PYTHON_VERSION}"
venv_path = f"{venv_path}/bin"
activate_command = f"source {venv_path}/activate"
if not os.path.exists(VENV_PATH):
session.run(py_command, "-m", "venv", VENV_PATH)
session.run(f"{venv_path}/python", "-m", "pip", "install", "--upgrade", "pip")
session.run(f"{venv_path}/python", "-m", "pip", "install", "-e", ".[dev,test]")
session.run(f"{venv_path}/pre-commit", "install")
if not os.environ.get("VIRTUAL_ENV"):
session.log(f"\n\nRun '{activate_command}' to enter the virtual environment.\n")
@nox.session(python=DEFAULT_PYTHON_VERSION)
def update(session: nox.Session) -> None:
"""Process requirement*.in files, updating only additions/removals."""
print_standard_logs(session)
session.install("pip-tools")
for filename in REQUIREMENT_IN_FILES:
session.run("pip-compile", "--no-emit-index-url", str(filename))
@nox.session(python=DEFAULT_PYTHON_VERSION)
def upgrade(session: nox.Session) -> None:
"""Process requirement*.in files and upgrade all libraries as possible."""
print_standard_logs(session)
session.install("pip-tools")
for filename in REQUIREMENT_IN_FILES:
session.run("pip-compile", "--no-emit-index-url", "--upgrade", str(filename))
@nox.session(python=False)
def clean(_: nox.Session) -> None:
"""Clean cache, .pyc, .pyo, and test/build artifact files from project."""
count = 0
for searchpath in CLEANABLE_TARGETS:
for filepath in pathlib.Path(".").glob(searchpath):
if filepath.is_dir():
shutil.rmtree(filepath)
else:
filepath.unlink()
count += 1
print(f"{count} files cleaned.")
def print_standard_logs(session: nox.Session) -> None:
"""Reusable output for monitoring environment factors."""
version = session.run("python", "--version", silent=True)
session.log(f"Running from: {session.bin}")
session.log(f"Running with: {version}")