|
| 1 | +from pathlib import Path |
| 2 | +from textwrap import dedent |
| 3 | + |
| 4 | +from nox import parametrize |
| 5 | +from nox_poetry import Session, session |
| 6 | + |
| 7 | +pydantic1 = ">=1.10.0,<2.0.0" |
| 8 | +pydantic2 = ">=2.0.2,<3.0.0" |
| 9 | +pydantic_versions = { |
| 10 | + pydantic1: "pyd1", |
| 11 | + pydantic2: "pyd2", |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +@session(python=["3.12", "3.11", "3.10", "3.9", "3.8", "3.7"]) |
| 16 | +@parametrize("pydantic", list(pydantic_versions.keys()), ids=pydantic_versions.values()) |
| 17 | +def tests(session: Session, pydantic: str) -> None: |
| 18 | + """Run pytest with coverage.""" |
| 19 | + session.run_always("poetry", "install", external=True) |
| 20 | + session.install(".") |
| 21 | + session.run_always( |
| 22 | + "pip", "install", "--upgrade", f"pydantic{pydantic}", external=True |
| 23 | + ) |
| 24 | + my_env = { |
| 25 | + # Need to set PYTHONPATH because some tests depend on docs_src |
| 26 | + "PYTHONPATH": session.invoked_from, |
| 27 | + "COVERAGE_FILE": f"coverage/.coverage.py{session.python}-{pydantic_versions[pydantic]}", |
| 28 | + "CONTEXT": f"py{session.python}-{pydantic_versions[pydantic]}", |
| 29 | + } |
| 30 | + session.log(f"Using env: {my_env}") |
| 31 | + session.run("bash", "scripts/test.sh", env=my_env, external=True) |
| 32 | + session.notify("coverage") |
| 33 | + |
| 34 | + |
| 35 | +@session(python=["3.8"]) |
| 36 | +def coverage(session: Session) -> None: |
| 37 | + """Gather coverage from test runs.""" |
| 38 | + session.install("coverage[toml]", ".") |
| 39 | + session.run("coverage", "combine", "coverage") |
| 40 | + session.run("coverage", "report") |
| 41 | + session.run("coverage", "html", "--show-contexts", "--title", "Coverage") |
| 42 | + |
| 43 | + |
| 44 | +@session |
| 45 | +@parametrize("python", ["3.12", "3.11", "3.10", "3.9", "3.8", "3.7"]) |
| 46 | +@parametrize("pydantic", list(pydantic_versions.keys()), ids=pydantic_versions.values()) |
| 47 | +def lint(session: Session, python: str, pydantic: str) -> None: |
| 48 | + """Run lint checks.""" |
| 49 | + if python != "3.7" and pydantic == pydantic2: |
| 50 | + session.run_always("poetry", "install", external=True) |
| 51 | + session.install(".") |
| 52 | + session.run_always( |
| 53 | + "pip", "install", "--upgrade", f"pydantic{pydantic}", external=True |
| 54 | + ) |
| 55 | + session.run("bash", "scripts/lint.sh", external=True) |
| 56 | + |
| 57 | + |
| 58 | +def activate_virtualenv_in_precommit_hooks(session: Session) -> None: |
| 59 | + """Activate virtualenv in hooks installed by pre-commit. |
| 60 | +
|
| 61 | + This function patches git hooks installed by pre-commit to activate the |
| 62 | + session's virtual environment. This allows pre-commit to locate hooks in |
| 63 | + that environment when invoked from git. |
| 64 | +
|
| 65 | + Args: |
| 66 | + session: The Session object. |
| 67 | + """ |
| 68 | + if session.bin is None: |
| 69 | + return |
| 70 | + |
| 71 | + virtualenv = session.env.get("VIRTUAL_ENV") |
| 72 | + if virtualenv is None: |
| 73 | + return |
| 74 | + |
| 75 | + hookdir = Path(".git") / "hooks" |
| 76 | + if not hookdir.is_dir(): |
| 77 | + return |
| 78 | + |
| 79 | + for hook in hookdir.iterdir(): |
| 80 | + if hook.name.endswith(".sample") or not hook.is_file(): |
| 81 | + continue |
| 82 | + |
| 83 | + text = hook.read_text() |
| 84 | + bindir = repr(session.bin)[1:-1] # strip quotes |
| 85 | + if not ( |
| 86 | + Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text |
| 87 | + ): |
| 88 | + continue |
| 89 | + |
| 90 | + lines = text.splitlines() |
| 91 | + if not (lines[0].startswith("#!") and "python" in lines[0].lower()): |
| 92 | + continue |
| 93 | + |
| 94 | + header = dedent( |
| 95 | + f"""\ |
| 96 | + import os |
| 97 | + os.environ["VIRTUAL_ENV"] = {virtualenv!r} |
| 98 | + os.environ["PATH"] = os.pathsep.join(( |
| 99 | + {session.bin!r}, |
| 100 | + os.environ.get("PATH", ""), |
| 101 | + )) |
| 102 | + """ |
| 103 | + ) |
| 104 | + |
| 105 | + lines.insert(1, header) |
| 106 | + hook.write_text("\n".join(lines)) |
| 107 | + |
| 108 | + |
| 109 | +@session(name="pre-commit", python="3.12") |
| 110 | +def precommit(session: Session) -> None: |
| 111 | + """Lint using pre-commit.""" |
| 112 | + args = session.posargs or ["run", "--all-files", "--show-diff-on-failure"] |
| 113 | + session.install("ruff", "pre-commit", "pre-commit-hooks") |
| 114 | + session.run("pre-commit", *args) |
| 115 | + if args and args[0] == "install": |
| 116 | + activate_virtualenv_in_precommit_hooks(session) |
0 commit comments