Project: pygitweb

f84f28d """Tests for the per-version nox task wrapper."""
from __future__ import annotations
import importlib.util
import sys
from pathlib import Path
from types import ModuleType
import pytest
def _load_script() -> ModuleType:
73c9c30 path = Path(__file__).resolve().parent / "pytest_version.py"
spec = importlib.util.spec_from_file_location("pytest_version_script", path)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
966394d
f84f28d def test_session_name() -> None:
module = _load_script()
assert module.session_name("3.14") == "tests-3.14"
def test_run_invokes_nox_session(monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 module = _load_script()
commands: list[list[str]] = []
envs: list[dict[str, str]] = []
class _Proc:
def __init__(self, returncode: int) -> None:
self.returncode = returncode
def fake_run(cmd: list[str], **kwargs: object) -> _Proc:
commands.append(list(cmd))
env = kwargs["env"]
assert isinstance(env, dict)
envs.append({str(key): str(value) for key, value in env.items()})
return _Proc(0)
f84f28d monkeypatch.setenv("usage_version", "3.12")
monkeypatch.setenv("usage_pytest_args", "-q")
73c9c30 monkeypatch.setattr(module.subprocess, "run", fake_run)
assert module.run("3.12", ["-k", "foo"]) == 0
f84f28d assert commands == [["nox", "-s", "tests-3.12", "--", "-k", "foo"]]
assert "usage_version" not in envs[0]
assert "usage_pytest_args" not in envs[0]
966394d
f84f28d def test_run_propagates_nox_failure(monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 module = _load_script()
966394d
73c9c30 class _Proc:
returncode = 4
966394d
73c9c30 def fake_run(cmd: list[str], **kwargs: object) -> _Proc:
f84f28d del cmd, kwargs
73c9c30 return _Proc()
966394d
73c9c30 monkeypatch.setattr(module.subprocess, "run", fake_run)
assert module.run("3.11", []) == 4
966394d
def test_main_requires_version(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
73c9c30 module = _load_script()
f84f28d monkeypatch.delenv("usage_version", raising=False)
monkeypatch.delenv("usage_pytest_args", raising=False)
73c9c30 monkeypatch.setattr(sys, "argv", ["pytest_version.py"])
assert module.main() == 2
assert "usage:" in capsys.readouterr().err
966394d
def test_main_forwards_pytest_args(monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 module = _load_script()
seen: list[tuple[str, list[str]]] = []
966394d
73c9c30 def fake_run(version: str, pytest_args: list[str]) -> int:
seen.append((version, pytest_args))
return 0
966394d
73c9c30 monkeypatch.setattr(module, "run", fake_run)
monkeypatch.setattr(sys, "argv", ["pytest_version.py", "3.14", "-q"])
assert module.main() == 0
assert seen == [("3.14", ["-q"])]
bae6568
def test_main_reads_usage_version(monkeypatch: pytest.MonkeyPatch) -> None:
module = _load_script()
seen: list[tuple[str, list[str]]] = []
def fake_run(version: str, pytest_args: list[str]) -> int:
seen.append((version, pytest_args))
return 0
monkeypatch.setattr(module, "run", fake_run)
monkeypatch.setattr(sys, "argv", ["pytest_version.py"])
monkeypatch.setenv("usage_version", "3.13")
monkeypatch.delenv("usage_pytest_args", raising=False)
assert module.main() == 0
assert seen == [("3.13", [])]