|
| 1 | +from pathlib import Path |
| 2 | +from tempfile import TemporaryDirectory |
| 3 | +import os |
| 4 | + |
| 5 | +import nox |
| 6 | + |
| 7 | +ROOT = Path(__file__).parent |
| 8 | +PYPROJECT = ROOT / "pyproject.toml" |
| 9 | +DOCS = ROOT / "docs" |
| 10 | + |
| 11 | +PACKAGE = ROOT / "jsonschema_lexer" |
| 12 | + |
| 13 | +REQUIREMENTS = dict( |
| 14 | + tests=ROOT / "test-requirements.txt", |
| 15 | +) |
| 16 | +REQUIREMENTS_IN = { |
| 17 | + path.parent / f"{path.stem}.in" for path in REQUIREMENTS.values() |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +SUPPORTED = ["pypy3.10", "3.10", "3.11", "3.12"] |
| 22 | +LATEST = SUPPORTED[-1] |
| 23 | + |
| 24 | +nox.options.sessions = [] |
| 25 | + |
| 26 | + |
| 27 | +def session(default=True, python=LATEST, **kwargs): # noqa: D103 |
| 28 | + def _session(fn): |
| 29 | + if default: |
| 30 | + nox.options.sessions.append(kwargs.get("name", fn.__name__)) |
| 31 | + return nox.session(python=python, **kwargs)(fn) |
| 32 | + |
| 33 | + return _session |
| 34 | + |
| 35 | + |
| 36 | +@session(python=SUPPORTED) |
| 37 | +def tests(session): |
| 38 | + """ |
| 39 | + Run the test suite. |
| 40 | + """ |
| 41 | + session.install("pytest") |
| 42 | + |
| 43 | + if session.posargs and session.posargs[0] == "coverage": |
| 44 | + if len(session.posargs) > 1 and session.posargs[1] == "github": |
| 45 | + github = Path(os.environ["GITHUB_STEP_SUMMARY"]) |
| 46 | + else: |
| 47 | + github = None |
| 48 | + |
| 49 | + session.install("coverage[toml]") |
| 50 | + session.run("coverage", "run", "-m", "pytest", PACKAGE) |
| 51 | + if github is None: |
| 52 | + session.run("coverage", "report") |
| 53 | + else: |
| 54 | + with github.open("a") as summary: |
| 55 | + summary.write("### Coverage\n\n") |
| 56 | + summary.flush() # without a flush, output seems out of order. |
| 57 | + session.run( |
| 58 | + "coverage", |
| 59 | + "report", |
| 60 | + "--format=markdown", |
| 61 | + stdout=summary, |
| 62 | + ) |
| 63 | + else: |
| 64 | + session.run("python", "-m", "pytest", *session.posargs, PACKAGE) |
| 65 | + |
| 66 | + |
| 67 | +@session(python=SUPPORTED) |
| 68 | +def audit(session): |
| 69 | + """ |
| 70 | + Audit Python dependencies for vulnerabilities. |
| 71 | + """ |
| 72 | + session.install("pip-audit", ROOT) |
| 73 | + session.run("python", "-m", "pip_audit") |
| 74 | + |
| 75 | + |
| 76 | +@session(tags=["build"]) |
| 77 | +def build(session): |
| 78 | + """ |
| 79 | + Build a distribution suitable for PyPI and check its validity. |
| 80 | + """ |
| 81 | + session.install("build", "twine") |
| 82 | + with TemporaryDirectory() as tmpdir: |
| 83 | + session.run("python", "-m", "build", ROOT, "--outdir", tmpdir) |
| 84 | + session.run("twine", "check", "--strict", tmpdir + "/*") |
| 85 | + |
| 86 | + |
| 87 | +@session() |
| 88 | +def secrets(session): |
| 89 | + """ |
| 90 | + Check for accidentally included secrets. |
| 91 | + """ |
| 92 | + session.install("detect-secrets") |
| 93 | + session.run("detect-secrets", "scan", ROOT) |
| 94 | + |
| 95 | + |
| 96 | +@session(tags=["style"]) |
| 97 | +def style(session): |
| 98 | + """ |
| 99 | + Check for coding style. |
| 100 | + """ |
| 101 | + session.install("ruff") |
| 102 | + session.run("ruff", "check", ROOT) |
| 103 | + |
| 104 | + |
| 105 | +@session() |
| 106 | +def typing(session): |
| 107 | + """ |
| 108 | + Statically check typing annotations. |
| 109 | + """ |
| 110 | + session.install("pyright", ROOT) |
| 111 | + session.run("pyright", PACKAGE) |
| 112 | + |
| 113 | + |
| 114 | +@session(default=False) |
| 115 | +def requirements(session): |
| 116 | + """ |
| 117 | + Update requirements files. |
| 118 | + """ |
| 119 | + session.install("pip-tools") |
| 120 | + for each in REQUIREMENTS_IN: |
| 121 | + session.run( |
| 122 | + "pip-compile", |
| 123 | + "--resolver", |
| 124 | + "backtracking", |
| 125 | + "--strip-extras", |
| 126 | + "-U", |
| 127 | + each.relative_to(ROOT), |
| 128 | + ) |
0 commit comments