-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnoxfile.py
92 lines (72 loc) · 2.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
from __future__ import annotations
import functools
import os
import subprocess
from pathlib import Path
import nox
CI = os.environ.get("CI") is not None
ROOT = Path(".")
PYTHON_VERSIONS = ["3.12"]
PYTHON_DEFAULT_VERSION = PYTHON_VERSIONS[-1]
APP_ROOT = ROOT / "src"
nox.options.default_venv_backend = "uv"
nox.options.stop_on_first_error = True
nox.options.reuse_existing_virtualenvs = not CI
def install(session: nox.Session, *args):
groups = []
for group in args:
groups.extend(["--group", group])
uv_env = getattr(session.virtualenv, "location", os.getenv("VIRTUAL_ENV"))
session.run_install(
"uv",
"sync",
"--locked",
*groups,
env={"UV_PROJECT_ENVIRONMENT": uv_env},
)
@functools.lru_cache
def _list_files() -> list[Path]:
file_list = []
for cmd in (
["git", "ls-files"],
["git", "ls-files", "--others", "--exclude-standard"],
):
cmd_result = subprocess.run(cmd, check=True, text=True, capture_output=True)
file_list.extend(cmd_result.stdout.splitlines())
file_paths = [Path(p) for p in file_list]
return file_paths
def list_files(suffix: str | None = None) -> list[Path]:
"""List all non-files not-ignored by git."""
file_paths = _list_files()
if suffix is not None:
file_paths = [p for p in file_paths if p.suffix == suffix]
return file_paths
@nox.session(name="format", python=PYTHON_DEFAULT_VERSION)
def format_(session):
"""Lint the code and apply fixes in-place whenever possible."""
install(session, "format")
session.run("ruff", "check", "--fix", ".")
session.run("ruff", "format", ".")
@nox.session(python=PYTHON_DEFAULT_VERSION)
def lint(session):
"""Run linters in readonly mode."""
install(session, "lint")
session.run("ruff", "check", "--diff", "--unsafe-fixes", ".")
session.run("codespell", ".")
session.run("ruff", "format", "--diff", ".")
@nox.session(python=PYTHON_DEFAULT_VERSION)
def type_check(session):
install(session, "type_check")
session.run("mypy", str(APP_ROOT), *session.posargs)
@nox.session(python=PYTHON_VERSIONS)
def test(session):
install(session, "test")
session.run(
"pytest",
"-s",
"-x",
"-vv",
"-n",
"auto",
*session.posargs,
)