forked from tweag/FawltyDeps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
131 lines (109 loc) · 4.22 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
import hashlib
import sys
from pathlib import Path
from typing import Iterable
import nox
python_versions = ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
def install_groups(
session: nox.Session,
*,
include: Iterable[str] = (),
exclude: Iterable[str] = (),
include_self: bool = True,
) -> None:
"""Install Poetry dependency groups
This function installs the given dependency groups into the session's
virtual environment. When 'include_self' is true (the default), the
function also installs this package (".") and its default dependencies.
We cannot use `poetry install` directly here, because it ignores the
session's virtualenv and installs into Poetry's own virtualenv. Instead, we
use `poetry export` with suitable options to generate a requirements.txt
file which we can then pass to session.install().
Auto-skip the `poetry export` step if the poetry.lock file is unchanged
since the last time this session was run.
"""
if isinstance(session.virtualenv, nox.virtualenv.PassthroughEnv):
session.warn(
"Running outside a Nox virtualenv! We will skip installation here, "
"and simply assume that the necessary dependency groups have "
"already been installed by other means!"
)
return
lockdata = Path("poetry.lock").read_bytes()
digest = hashlib.blake2b(lockdata).hexdigest()
requirements_txt = Path(session.cache_dir, session.name, "reqs_from_poetry.txt")
hashfile = requirements_txt.with_suffix(".hash")
if not hashfile.is_file() or hashfile.read_text() != digest:
requirements_txt.parent.mkdir(parents=True, exist_ok=True)
argv = [
"poetry",
"export",
"--format=requirements.txt",
f"--output={requirements_txt}",
]
if include:
option = "only" if not include_self else "with"
argv.append(f"--{option}={','.join(include)}")
if exclude:
argv.append(f"--without={','.join(exclude)}")
session.run_always(*argv, external=True)
hashfile.write_text(digest)
session.install("-r", str(requirements_txt))
if include_self:
session.install("-e", ".")
@nox.session(python=python_versions)
def tests(session):
install_groups(session, include=["test"])
session.run(
"pytest",
"-x",
"--log-level=debug",
"--durations=10",
"--hypothesis-show-statistics",
*session.posargs,
)
@nox.session(python=python_versions)
def integration_tests(session):
install_groups(session, include=["test"])
session.run("pytest", "-x", "-m", "integration", "--durations=10", *session.posargs)
@nox.session(python=python_versions)
def self_test(session):
# Install all optional dependency groups for a self test
install_groups(session, include=["nox", "test", "lint", "format", "dev"])
# For --pyenv, use Nox's virtualenv, or fall back to current virtualenv
venv_path = getattr(session.virtualenv, "location", sys.prefix)
session.run("fawltydeps", f"--pyenv={venv_path}")
@nox.session(python=python_versions)
def lint(session):
install_groups(session, include=["lint"])
session.run("mypy")
session.run("pylint", "fawltydeps")
test_extra_pylint_disable = [
"invalid-name",
"missing-function-docstring",
"protected-access",
"redefined-outer-name",
"too-many-arguments",
"too-many-instance-attributes",
"too-many-lines",
"unused-argument",
"use-dict-literal",
"use-implicit-booleaness-not-comparison",
]
session.run(
"pylint",
f"--disable={','.join(test_extra_pylint_disable)}",
"tests",
)
@nox.session
def format(session):
install_groups(session, include=["format"], include_self=False)
session.run("codespell", "--enable-colors")
session.run("isort", "fawltydeps", "tests", "--check", "--diff", "--color")
session.run("black", ".", "--check", "--diff", "--color")
@nox.session
def reformat(session):
install_groups(session, include=["format"], include_self=False)
session.run("codespell", "--write-changes")
session.run("isort", "fawltydeps", "tests")
session.run("black", ".")