Skip to content

Commit 23e9a5e

Browse files
committed
chore: build, lint, and test the package with Nox
1 parent e2e9df4 commit 23e9a5e

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
__pycache__/
22
.mypy_cache/
3+
.nox/
34
.pytest_cache/
45
.ruff_cache/
56
*.egg-info/

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Pre-commit.ci](https://results.pre-commit.ci/badge/github/paduszyk/django-management-commands/main.svg)][pre-commit.ci]
44

5+
[![Nox](https://img.shields.io/badge/%f0%9f%a6%8a-Nox-d85e00)][nox]
56
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)][ruff]
67
[![Mypy](https://img.shields.io/badge/type--checked-mypy-blue?logo=python)][mypy]
78
[![Prettier](https://img.shields.io/badge/code_style-Prettier-1e2b33?logo=prettier)][prettier]
@@ -14,6 +15,7 @@ Released under the [MIT license][license].
1415
[conventional-commits]: https://www.conventionalcommits.org/
1516
[license]: https://github.com/paduszyk/django-management-commands/blob/main/LICENSE
1617
[mypy]: https://mypy.readthedocs.io
18+
[nox]: https://nox.thea.codes/
1719
[pre-commit.ci]: https://results.pre-commit.ci/latest/github/paduszyk/django-management-commands/main
1820
[prettier]: https://prettier.io
1921
[ruff]: https://docs.astral.sh/ruff/

noxfile.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
from __future__ import annotations
2+
3+
import re
4+
from functools import cache, partial
5+
from itertools import chain
6+
from typing import Any
7+
8+
import nox
9+
10+
DJANGO_PYTHONS = {
11+
"4.2": [
12+
"3.9",
13+
"3.10",
14+
"3.11",
15+
"3.12",
16+
],
17+
"5.0": [
18+
"3.10",
19+
"3.11",
20+
"3.12",
21+
],
22+
"5.1": [
23+
"3.10",
24+
"3.11",
25+
"3.12",
26+
],
27+
}
28+
29+
30+
@cache
31+
def load_dev_dependencies() -> list[str]:
32+
project: dict[str, Any] = nox.project.load_toml("pyproject.toml")["project"]
33+
34+
optional_dependencies: dict[str, list[str]] = project["optional-dependencies"]
35+
36+
return optional_dependencies["dev"]
37+
38+
39+
def match_dev_dependencies(*patterns: str) -> list[str]:
40+
dev_dependencies = load_dev_dependencies()
41+
42+
return list(
43+
chain.from_iterable(
44+
filter(partial(re.match, pattern), dev_dependencies) for pattern in patterns
45+
),
46+
)
47+
48+
49+
nox.options.sessions = ["ruff", "mypy", "pytest"]
50+
51+
52+
@nox.session(tags=["build"])
53+
@nox.parametrize(
54+
"python",
55+
[
56+
"3.9",
57+
"3.10",
58+
"3.11",
59+
"3.12",
60+
],
61+
)
62+
def build(session: nox.Session) -> None:
63+
session.install("build")
64+
65+
session.run("python", "-m", "build")
66+
session.run("rm", "-rf", "dist", external=True)
67+
68+
69+
@nox.session(tags=["lint"])
70+
@nox.parametrize(
71+
"command",
72+
[
73+
"check",
74+
"format",
75+
],
76+
)
77+
def ruff(session: nox.Session, command: str) -> None:
78+
session.install(
79+
*match_dev_dependencies(
80+
r"^ruff ~=",
81+
),
82+
)
83+
84+
extra_options = session.posargs or []
85+
86+
session.run("ruff", command, *extra_options, ".")
87+
88+
89+
@nox.session(tags=["lint"])
90+
def mypy(session: nox.Session) -> None:
91+
session.install(
92+
*match_dev_dependencies(
93+
r"^django\-stubs\[compatible-mypy\] ~=",
94+
),
95+
"-e",
96+
".",
97+
)
98+
99+
extra_options = session.posargs or ["--ignore-missing-imports"]
100+
101+
session.run("mypy", *extra_options, ".")
102+
103+
104+
@nox.session(tags=["test"])
105+
@nox.parametrize(
106+
("django", "python"),
107+
[
108+
(django, python)
109+
for django, pythons in DJANGO_PYTHONS.items()
110+
for python in pythons
111+
],
112+
)
113+
def pytest(session: nox.Session, django: str) -> None:
114+
session.install(
115+
*match_dev_dependencies(
116+
r"^pytest(-[\w\-]+)? ~=",
117+
),
118+
f"django == {django}.*",
119+
"-e",
120+
".",
121+
)
122+
123+
extra_options = session.posargs or []
124+
125+
session.run("pytest", *extra_options)

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dynamic = ["version"]
4343
[project.optional-dependencies]
4444
dev = [
4545
"django-stubs[compatible-mypy] ~= 5.0",
46+
"nox == 2024.4.15",
4647
"pre-commit ~= 3.8",
4748
"pytest ~= 8.3",
4849
"pytest-custom-exit-code ~= 0.3",
@@ -103,6 +104,7 @@ ignore_missing_imports = true
103104

104105
[[tool.mypy.overrides]]
105106
module = [
107+
"noxfile",
106108
"tests.*",
107109
]
108110
disallow_untyped_decorators = false

0 commit comments

Comments
 (0)