Project: pygitweb

"""Tests for project actions API."""
from __future__ import annotations
import stat
import sys
from pathlib import Path
import pytest
from pygittools.actions import (
Action,
ActionSet,
ActionSpec,
ProjectActions,
catalog_registry,
find_project_root,
load_action_catalog,
load_action_sets,
load_action_specs,
load_mise_action_specs,
mise_available,
project_actions,
project_venv_python,
run_action,
run_action_script,
run_mise_task,
visible_specs,
)
def _write_pyproject(root: Path, actions_toml: str) -> None:
(root / "pyproject.toml").write_text(
f'[project]\nname = "demo"\nversion = "0"\n\n[tool.pgt]\nactions = {actions_toml}\n',
encoding="utf-8",
)
def _make_venv(root: Path) -> Path:
venv = root / ".venv"
bin_dir = venv / "bin"
bin_dir.mkdir(parents=True)
python = bin_dir / "python"
python.symlink_to(sys.executable)
return python
def test_find_project_root(tmp_path: Path) -> None:
nested = tmp_path / "a" / "b"
nested.mkdir(parents=True)
(tmp_path / "pyproject.toml").write_text("[project]\nname='x'\n", encoding="utf-8")
(nested / "actions.toml").write_text("[local]\nactions = []\n", encoding="utf-8")
assert find_project_root(nested) == tmp_path.resolve()
assert find_project_root(tmp_path) == tmp_path.resolve()
def test_find_project_root_actions_toml_only(tmp_path: Path) -> None:
nested = tmp_path / "a" / "b"
nested.mkdir(parents=True)
(tmp_path / "actions.toml").write_text("[quality]\nactions = []\n", encoding="utf-8")
assert find_project_root(nested) == tmp_path.resolve()
def test_find_project_root_mise_toml_only(tmp_path: Path) -> None:
nested = tmp_path / "a" / "b"
nested.mkdir(parents=True)
(tmp_path / "mise.toml").write_text("[tasks.hello]\nrun = 'echo hi'\n", encoding="utf-8")
assert find_project_root(nested) == tmp_path.resolve()
def test_load_action_specs_inline_tables(tmp_path: Path) -> None:
_write_pyproject(
tmp_path,
"""[
{ name = "lint", script = "scripts/lint.py" },
{ name = "test", script = "scripts/test.py" },
]""",
)
specs = load_action_specs(tmp_path)
assert specs == [
ActionSpec(name="lint", script="scripts/lint.py"),
ActionSpec(name="test", script="scripts/test.py"),
]
def test_load_action_specs_pair_arrays(tmp_path: Path) -> None:
_write_pyproject(tmp_path, '[["lint", "scripts/lint.py"], ["test", "scripts/test.sh"]]')
specs = load_action_specs(tmp_path)
assert [s.name for s in specs] == ["lint", "test"]
def test_load_action_specs_args(tmp_path: Path) -> None:
_write_pyproject(
tmp_path,
'[{ name = "py", script = "scripts/pytest_version.py", args = ["3.12", "-q"] }]',
)
specs = load_action_specs(tmp_path)
assert specs == [
ActionSpec(name="py", script="scripts/pytest_version.py", args=("3.12", "-q")),
]
def test_load_action_specs_description_depends_hide(tmp_path: Path) -> None:
_write_pyproject(
tmp_path,
"""[
{ name = "unit", script = "unit.py", description = "fast tests", hide = true },
{ name = "check", script = "check.py", depends = ["unit"], description = "all checks" },
]""",
)
assert load_action_specs(tmp_path) == [
ActionSpec(name="unit", script="unit.py", description="fast tests", hide=True),
ActionSpec(
name="check",
script="check.py",
description="all checks",
depends=("unit",),
),
]
assert [spec.name for spec in visible_specs(load_action_specs(tmp_path))] == ["check"]
def test_workspace_python_version_set() -> None:
root = Path(__file__).resolve().parents[3]
sets = {item.name: item for item in load_action_sets(root)}
versions = sets["testversions"]
assert [spec.args for spec in versions.specs] == [("3.12",), ("3.13",), ("3.14",), ("3.15",)]
assert {spec.script for spec in versions.specs} == {"scripts/pytest_version.py"}
def test_load_action_specs_invalid_args(tmp_path: Path) -> None:
_write_pyproject(tmp_path, '[{ name = "py", script = "x.py", args = "3.12" }]')
with pytest.raises(ValueError, match="args"):
load_action_specs(tmp_path)
def test_run_action_script_passes_args(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
script = tmp_path / "echo_args.py"
script.write_text("print('unused')\n", encoding="utf-8")
bin_dir = tmp_path / ".venv" / "bin"
win_dir = tmp_path / ".venv" / "Scripts"
bin_dir.mkdir(parents=True)
win_dir.mkdir()
(bin_dir / "python").write_text("", encoding="utf-8")
(win_dir / "python.exe").write_text("", encoding="utf-8")
captured: list[list[str]] = []
class _Proc:
returncode = 0
stdout = "ok"
stderr = ""
def fake_run(cmd: list[str], **kwargs: object) -> _Proc:
del kwargs
captured.append(list(cmd))
return _Proc()
monkeypatch.setattr("pygittools.actions.subprocess.run", fake_run)
spec = ActionSpec(name="py", script="echo_args.py", args=("3.12", "-q"))
ok, message = ProjectActions(tmp_path, [spec]).action("py")
python = project_venv_python(tmp_path)
assert python is not None
assert ok is True
assert message == "ok"
assert captured == [[str(python), str(script.resolve()), "3.12", "-q"]]
def test_load_action_specs_invalid(tmp_path: Path) -> None:
_write_pyproject(tmp_path, '{ name = "lint" }')
with pytest.raises(ValueError, match="must be an array"):
load_action_specs(tmp_path)
def test_load_action_sets_from_pyproject_and_actions_toml(tmp_path: Path) -> None:
_write_pyproject(tmp_path, '[{ name = "lint", script = "scripts/lint.py" }]')
pyproject = (tmp_path / "pyproject.toml").read_text(encoding="utf-8")
(tmp_path / "pyproject.toml").write_text(
pyproject
+ """
[tool.pgt.sets.release]
actions = [
["build", "scripts/build.py"],
]
""",
encoding="utf-8",
)
(tmp_path / "actions.toml").write_text(
"""
name = "tools"
[[actions]]
name = "fmt"
script = "scripts/fmt.py"
[quality]
actions = [
{ name = "test", script = "scripts/test.py" },
]
""",
encoding="utf-8",
)
assert load_action_sets(tmp_path) == [
ActionSet(name="default", specs=(ActionSpec(name="lint", script="scripts/lint.py"),)),
ActionSet(name="release", specs=(ActionSpec(name="build", script="scripts/build.py"),)),
ActionSet(name="tools", specs=(ActionSpec(name="fmt", script="scripts/fmt.py"),)),
ActionSet(name="quality", specs=(ActionSpec(name="test", script="scripts/test.py"),)),
]
def test_load_action_sets_named_pyproject_flat_set(tmp_path: Path) -> None:
(tmp_path / "pyproject.toml").write_text(
"""
[tool.pgt]
name = "checks"
actions = [{ name = "lint", script = "lint.py" }]
""",
encoding="utf-8",
)
sets = load_action_sets(tmp_path)
assert [item.name for item in sets] == ["checks"]
def test_load_action_sets_duplicate_name(tmp_path: Path) -> None:
_write_pyproject(tmp_path, '[{ name = "lint", script = "lint.py" }]')
(tmp_path / "actions.toml").write_text(
'[default]\nactions = [{ name = "other", script = "other.py" }]\n',
encoding="utf-8",
)
with pytest.raises(ValueError, match="Duplicate action set name: default"):
load_action_sets(tmp_path)
def test_load_action_sets_invalid_table(tmp_path: Path) -> None:
(tmp_path / "actions.toml").write_text('quality = "nope"\n', encoding="utf-8")
with pytest.raises(ValueError, match="unexpected key"):
load_action_sets(tmp_path)
def test_project_actions_is_action(tmp_path: Path) -> None:
_write_pyproject(tmp_path, '[{ name = "ok", script = "ok.py" }]')
runner = project_actions(tmp_path)
assert isinstance(runner, Action)
assert issubclass(ProjectActions, Action)
@pytest.mark.skipif(sys.platform == "win32", reason="symlink venv setup requires SeCreateSymbolicLinkPrivilege")
def test_run_action_script_python_success(tmp_path: Path) -> None:
_make_venv(tmp_path)
script = tmp_path / "ok.py"
script.write_text("print('hello-ok')\n", encoding="utf-8")
ok, message = run_action_script(tmp_path, "ok.py")
assert ok is True
assert message == "hello-ok"
@pytest.mark.skipif(sys.platform == "win32", reason="symlink venv setup requires SeCreateSymbolicLinkPrivilege")
def test_run_action_script_python_failure(tmp_path: Path) -> None:
_make_venv(tmp_path)
script = tmp_path / "fail.py"
script.write_text("import sys\nprint('boom', file=sys.stderr)\nsys.exit(2)\n", encoding="utf-8")
ok, message = run_action_script(tmp_path, "fail.py")
assert ok is False
assert "boom" in message
def test_run_action_script_missing_venv(tmp_path: Path) -> None:
script = tmp_path / "ok.py"
script.write_text("print('x')\n", encoding="utf-8")
ok, message = run_action_script(tmp_path, "ok.py")
assert ok is False
assert "venv not found" in message
@pytest.mark.skipif(sys.platform == "win32", reason="symlink venv setup requires SeCreateSymbolicLinkPrivilege")
def test_run_action_script_shell(tmp_path: Path) -> None:
_make_venv(tmp_path)
script = tmp_path / "hi.sh"
script.write_text("#!/bin/bash\necho shell-ok\n", encoding="utf-8")
script.chmod(script.stat().st_mode | stat.S_IXUSR)
ok, message = run_action_script(tmp_path, "hi.sh")
assert ok is True
assert message == "shell-ok"
assert project_venv_python(tmp_path) is not None
def test_project_actions_unknown(tmp_path: Path) -> None:
_write_pyproject(tmp_path, "[]")
ok, message = ProjectActions(tmp_path).action("missing")
assert ok is False
assert "Unknown action" in message
@pytest.mark.skipif(sys.platform == "win32", reason="symlink venv setup requires SeCreateSymbolicLinkPrivilege")
def test_project_actions_runs_named(tmp_path: Path) -> None:
_make_venv(tmp_path)
_write_pyproject(tmp_path, '[{ name = "greet", script = "greet.py" }]')
(tmp_path / "greet.py").write_text("print('hi')\n", encoding="utf-8")
ok, message = ProjectActions(tmp_path).action("greet")
assert ok is True
assert message == "hi"
def test_load_mise_action_specs_without_mise(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("pygittools.actions.shutil.which", lambda _name: None)
assert load_mise_action_specs(tmp_path) == []
def test_load_mise_action_specs_parses_json(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("pygittools.actions.shutil.which", lambda _name: "/usr/bin/mise")
class _Proc:
returncode = 0
stdout = """[
{"name": "lint", "description": "Run lint", "file": "scripts/lint.py", "hide": false},
{"name": "hidden", "description": "", "hide": true},
{"name": "test", "description": " ", "hide": false}
]"""
stderr = ""
monkeypatch.setattr("pygittools.actions.subprocess.run", lambda *_a, **_k: _Proc())
assert load_mise_action_specs(tmp_path) == [
ActionSpec(name="lint", script="scripts/lint.py", source="mise", description="Run lint"),
ActionSpec(name="test", script="", source="mise", description=""),
]
def test_load_mise_action_specs_soft_fails(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("pygittools.actions.shutil.which", lambda _name: "/usr/bin/mise")
class _Proc:
returncode = 1
stdout = ""
stderr = "boom"
monkeypatch.setattr("pygittools.actions.subprocess.run", lambda *_a, **_k: _Proc())
assert load_mise_action_specs(tmp_path) == []
def test_load_action_catalog_appends_mise(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
_write_pyproject(tmp_path, '[{ name = "lint", script = "scripts/lint.py" }]')
monkeypatch.setattr(
"pygittools.actions.load_mise_action_specs",
lambda _root: [ActionSpec(name="build", script="", source="mise", description="Build")],
)
assert load_action_catalog(tmp_path) == [
ActionSet(name="default", specs=(ActionSpec(name="lint", script="scripts/lint.py"),)),
ActionSet(
name="mise",
specs=(ActionSpec(name="build", script="", source="mise", description="Build"),),
),
]
def test_load_action_catalog_duplicate_mise_set(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
_write_pyproject(tmp_path, '[{ name = "lint", script = "lint.py" }]')
(tmp_path / "actions.toml").write_text(
'[mise]\nactions = [{ name = "x", script = "x.py" }]\n',
encoding="utf-8",
)
monkeypatch.setattr(
"pygittools.actions.load_mise_action_specs",
lambda _root: [ActionSpec(name="build", script="", source="mise")],
)
with pytest.raises(ValueError, match="Duplicate action set name: mise"):
load_action_catalog(tmp_path)
def test_run_mise_task_invokes_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("pygittools.actions.shutil.which", lambda _name: "/usr/bin/mise")
captured: list[list[str]] = []
class _Proc:
returncode = 0
stdout = "built\n"
stderr = ""
def fake_run(cmd: list[str], **kwargs: object) -> _Proc:
captured.append(list(cmd))
assert kwargs["cwd"] == tmp_path
return _Proc()
monkeypatch.setattr("pygittools.actions.subprocess.run", fake_run)
ok, message = run_mise_task(tmp_path, "build", ("--release",))
assert ok is True
assert message == "built"
assert captured == [["mise", "run", "build", "--", "--release"]]
def test_run_action_dispatches_by_source(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
calls: list[str] = []
def fake_mise(root: Path, name: str, args: tuple[str, ...] = ()) -> tuple[bool, str]:
del root, args
calls.append(f"mise:{name}")
return True, "mise-ok"
def fake_script(root: Path, script: str, args: tuple[str, ...] = ()) -> tuple[bool, str]:
del root, args
calls.append(f"pgt:{script}")
return True, "pgt-ok"
monkeypatch.setattr("pygittools.actions.run_mise_task", fake_mise)
monkeypatch.setattr("pygittools.actions.run_action_script", fake_script)
assert run_action(tmp_path, ActionSpec(name="build", script="", source="mise")) == (True, "mise-ok")
assert run_action(tmp_path, ActionSpec(name="lint", script="lint.py")) == (True, "pgt-ok")
assert calls == ["mise:build", "pgt:lint.py"]
def test_project_actions_runs_mise_spec(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"pygittools.actions.run_mise_task",
lambda root, name, args=(): (True, f"{root.name}:{name}:{args}"),
)
spec = ActionSpec(name="build", script="", source="mise", args=("--quiet",))
ok, message = ProjectActions(tmp_path, [spec]).action("build")
assert ok is True
assert message == f"{tmp_path.name}:build:('--quiet',)"
def test_depends_requires_unique_names(tmp_path: Path) -> None:
_write_pyproject(tmp_path, '[{ name = "lint", script = "lint.py", depends = ["fmt"] }]')
(tmp_path / "actions.toml").write_text(
"""
[quality]
actions = [
{ name = "fmt", script = "fmt.py" },
{ name = "lint", script = "other.py" },
]
""",
encoding="utf-8",
)
with pytest.raises(ValueError, match="Duplicate action name 'lint'"):
load_action_sets(tmp_path)
def test_depends_unknown_name(tmp_path: Path) -> None:
_write_pyproject(
tmp_path,
'[{ name = "check", script = "check.py", depends = ["missing"] }]',
)
with pytest.raises(ValueError, match="Unknown depends 'missing'"):
load_action_sets(tmp_path)
def test_run_action_runs_depends_first(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
calls: list[str] = []
def fake_script(root: Path, script: str, args: tuple[str, ...] = ()) -> tuple[bool, str]:
del root, args
calls.append(script)
return True, "ok"
monkeypatch.setattr("pygittools.actions.run_action_script", fake_script)
unit = ActionSpec(name="unit", script="unit.py", hide=True)
check = ActionSpec(name="check", script="check.py", depends=("unit",))
ok, message = run_action(tmp_path, check, registry={"unit": unit, "check": check})
assert ok is True
assert message == "ok"
assert calls == ["unit.py", "check.py"]
def test_run_action_circular_depends(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"pygittools.actions.run_action_script",
lambda *_a, **_k: (True, "ok"),
)
a = ActionSpec(name="a", script="a.py", depends=("b",))
b = ActionSpec(name="b", script="b.py", depends=("a",))
ok, message = run_action(tmp_path, a, registry={"a": a, "b": b})
assert ok is False
assert "Circular depends" in message
def test_catalog_registry_last_wins() -> None:
sets = [
ActionSet(name="default", specs=(ActionSpec(name="lint", script="a.py"),)),
ActionSet(name="extra", specs=(ActionSpec(name="lint", script="b.py"),)),
]
assert catalog_registry(sets)["lint"].script == "b.py"
@pytest.mark.skipif(not mise_available(), reason="mise not installed")
def test_workspace_catalog_includes_mise_tasks() -> None:
root = Path(__file__).resolve().parents[3]
sets = {item.name: item for item in load_action_catalog(root)}
assert "default" in sets
assert "mise" in sets
names = {spec.name for spec in sets["mise"].specs}
assert {"ruff-check", "ruff-format", "typecheck", "test", "check"} <= names
check = next(spec for spec in sets["mise"].specs if spec.name == "check")
assert check.depends == ("ruff-check", "ruff-format", "typecheck", "test")