Project: pygitweb

053f925 """Tests for the actions TUI page."""
from __future__ import annotations
import sys
import time
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from pygit2 import Repository, init_repository
from pyratatui import Rect
from pygittools.tui.git_colors import init_tui_color_theme
from pygittools.tui.keys import key
966394d from pygittools.tui.pages.actions import ActionsPage, _RunState, actions_page
053f925 from pygittools.tui.types import PageAction, PageContext
def _repo_with_actions(tmp_path: Path, actions_body: str) -> PageContext:
73c9c30 repo_dir = tmp_path / "repo"
repo_dir.mkdir()
repo = init_repository(str(repo_dir), bare=False)
(repo_dir / "pyproject.toml").write_text(
f'[project]\nname = "demo"\nversion = "0"\n\n[tool.pgt]\nactions = {actions_body}\n',
encoding="utf-8",
)
venv_bin = repo_dir / ".venv" / "bin"
venv_bin.mkdir(parents=True)
(venv_bin / "python").symlink_to(sys.executable)
return PageContext(repo=repo, repo_path=Path(repo.path))
053f925
def test_actions_page_empty(tmp_path: Path) -> None:
73c9c30 repo_dir = tmp_path / "repo"
repo_dir.mkdir()
repo = init_repository(str(repo_dir), bare=False)
ctx = PageContext(repo=repo, repo_path=Path(repo.path))
page = actions_page()
page.on_enter(ctx)
assert "No pyproject" in page.status_text() or "No [tool.pgt]" in page.status_text()
page.draw(MagicMock(), Rect(0, 0, 80, 20))
assert page.handle_key(key("Esc")).action == PageAction.POP
053f925
@pytest.mark.skipif(sys.platform == "win32", reason="symlink venv setup requires SeCreateSymbolicLinkPrivilege")
def test_actions_page_lists_and_run(tmp_path: Path) -> None:
8fa7f0b ctx = _repo_with_actions(tmp_path, '[{ name = "ok", file = "ok.py" }]')
73c9c30 init_tui_color_theme(ctx.repo)
workdir = Path(ctx.repo.workdir)
(workdir / "ok.py").write_text("print('done')\n", encoding="utf-8")
page = ActionsPage()
page.on_enter(ctx)
assert "Enter run" in page.status_text()
page.draw(MagicMock(), Rect(0, 0, 80, 20))
# select the action row (index 1; 0 is Run all)
page.handle_key(key("Down"))
page.handle_key(key("Enter"))
deadline = time.monotonic() + 5.0
while time.monotonic() < deadline:
page.on_idle()
with page._lock:
if page._active is None and page._states.get(("default", "ok")) is not _RunState.IDLE:
break
time.sleep(0.05)
assert page._states[("default", "ok")].name == "SUCCESS"
assert page._messages[("default", "ok")] == "done"
page.draw(MagicMock(), Rect(0, 0, 80, 20))
053f925
@pytest.mark.skipif(sys.platform == "win32", reason="symlink venv setup requires SeCreateSymbolicLinkPrivilege")
def test_actions_page_run_all_stops_on_failure(tmp_path: Path) -> None:
73c9c30 ctx = _repo_with_actions(
tmp_path,
"""[
8fa7f0b { name = "bad", file = "bad.py" },
{ name = "good", file = "good.py" },
053f925 ]""",
73c9c30 )
init_tui_color_theme(ctx.repo)
workdir = Path(ctx.repo.workdir)
(workdir / "bad.py").write_text("import sys\nsys.exit(1)\n", encoding="utf-8")
(workdir / "good.py").write_text("print('should-not-run')\n", encoding="utf-8")
page = ActionsPage()
page.on_enter(ctx)
page.handle_key(key("a"))
deadline = time.monotonic() + 5.0
while time.monotonic() < deadline:
page.on_idle()
with page._lock:
if page._active is None:
break
time.sleep(0.05)
assert page._states[("default", "bad")] is _RunState.FAILURE
assert page._states[("default", "good")] is _RunState.IDLE
assert ("default", "good") not in page._messages
053f925
@pytest.mark.skipif(sys.platform == "win32", reason="symlink venv setup requires SeCreateSymbolicLinkPrivilege")
def test_actions_page_opens_from_factory(tmp_path: Path) -> None:
73c9c30 ctx = _repo_with_actions(tmp_path, "[]")
page = actions_page()
page.on_enter(ctx)
assert page.title == "actions"
assert isinstance(ctx.repo, Repository)
assert "No actions configured" in page.status_text()
966394d
def _repo_dir(tmp_path: Path) -> tuple[PageContext, Path]:
73c9c30 repo_dir = tmp_path / "repo"
repo_dir.mkdir()
repo = init_repository(str(repo_dir), bare=False)
return PageContext(repo=repo, repo_path=Path(repo.path)), repo_dir
966394d
04f3c66 def test_actions_page_cycles_groups(tmp_path: Path) -> None:
73c9c30 ctx, repo_dir = _repo_dir(tmp_path)
(repo_dir / "pyproject.toml").write_text(
8fa7f0b '[project]\nname = "demo"\n\n[tool.pgt]\nactions = [{ name = "lint", file = "lint.py" }]\n\n'
'[tool.pgt.sets.release]\nactions = [{ name = "build", file = "build.py" }]\n',
73c9c30 encoding="utf-8",
)
page = ActionsPage()
page.on_enter(ctx)
assert "Tab/Shift-Tab" in page.status_text()
assert [label for _, label in page._rows()] == ["Run all", "lint"]
page.handle_key(key("Down"))
page.handle_key(key("Tab"))
assert [label for _, label in page._rows()] == ["Run all", "build"]
assert page._list_state.selected == 0
page.handle_key(key("Tab", shift=True))
assert [label for _, label in page._rows()] == ["Run all", "lint"]
page.handle_key(key("BackTab"))
assert [label for _, label in page._rows()] == ["Run all", "build"]
page.handle_key(key("Left"))
assert [label for _, label in page._rows()] == ["Run all", "lint"]
page.handle_key(key("Right"))
assert [label for _, label in page._rows()] == ["Run all", "build"]
page.draw(MagicMock(), Rect(0, 0, 80, 20))
966394d
04f3c66 def test_actions_page_run_all_uses_root_hubs(tmp_path: Path) -> None:
73c9c30 ctx, repo_dir = _repo_dir(tmp_path)
04f3c66 (repo_dir / "pyproject.toml").write_text(
73c9c30 """
04f3c66 [tool.pgt]
actions = [
8fa7f0b { name = "one", file = "one.py" },
{ name = "two", file = "two.py" },
04f3c66 { name = "check", run = [{ task = "one" }, { task = "two" }] },
]
966394d """,
73c9c30 encoding="utf-8",
)
page = ActionsPage()
page.on_enter(ctx)
04f3c66 assert [label for _, label in page._rows()] == ["Run all", "one", "two", "check"]
73c9c30 page.handle_key(key("a"))
deadline = time.monotonic() + 5.0
while time.monotonic() < deadline:
page.on_idle()
with page._lock:
if page._active is None:
break
time.sleep(0.05)
04f3c66 assert page._states[("default", "check")] is _RunState.FAILURE
assert "Script not found" in page._messages[("default", "check")]
assert page._states[("default", "one")] is _RunState.FAILURE
assert "Script not found" in page._messages[("default", "one")]
assert page._states[("default", "two")] is _RunState.IDLE
966394d
04f3c66 def _wait_until_idle(page: ActionsPage) -> None:
deadline = time.monotonic() + 5.0
while time.monotonic() < deadline:
page.on_idle()
with page._lock:
if page._active is None:
return
time.sleep(0.05)
raise AssertionError("action run did not finish")
def test_actions_page_run_all_marks_referenced_tasks(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 ctx, repo_dir = _repo_dir(tmp_path)
04f3c66 (repo_dir / "pyproject.toml").write_text(
"""
[tool.pgt]
actions = [
8fa7f0b { name = "one", file = "one.py" },
{ name = "two", file = "two.py" },
04f3c66 { name = "check", run = [{ task = "one" }, { task = "two" }] },
]
""",
encoding="utf-8",
)
calls: list[str] = []
8fa7f0b def fake_file(root: Path, script: str, args: tuple[str, ...] = (), *, environ: object = None) -> tuple[bool, str]:
bae6568 del root, args, environ
04f3c66 calls.append(script)
return True, script
8fa7f0b monkeypatch.setattr("pygittools.actions.run_action_file", fake_file)
73c9c30 page = ActionsPage()
page.on_enter(ctx)
04f3c66 page.handle_key(key("a"))
_wait_until_idle(page)
assert calls == ["one.py", "two.py"]
assert page._states[("default", "one")] is _RunState.SUCCESS
assert page._states[("default", "two")] is _RunState.SUCCESS
assert page._states[("default", "check")] is _RunState.SUCCESS
assert page._messages[("default", "one")] == "one.py"
assert page._messages[("default", "two")] == "two.py"
9f80683
04f3c66 def test_actions_page_group_name_collision_stays_in_group(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
9f80683 ctx, repo_dir = _repo_dir(tmp_path)
(repo_dir / "pyproject.toml").write_text(
04f3c66 """
[tool.pgt]
actions = [
8fa7f0b { name = "lint", file = "default_lint.py" },
04f3c66 { name = "check", depends = ["lint"] },
]
[tool.pgt.sets.extra]
actions = [
8fa7f0b { name = "lint", file = "extra_lint.py" },
04f3c66 ]
""",
9f80683 encoding="utf-8",
)
04f3c66 calls: list[str] = []
8fa7f0b def fake_file(root: Path, script: str, args: tuple[str, ...] = (), *, environ: object = None) -> tuple[bool, str]:
bae6568 del root, args, environ
04f3c66 calls.append(script)
return True, script
8fa7f0b monkeypatch.setattr("pygittools.actions.run_action_file", fake_file)
9f80683 page = ActionsPage()
page.on_enter(ctx)
04f3c66 page.handle_key(key("Down"))
page.handle_key(key("Down"))
page.handle_key(key("Enter"))
_wait_until_idle(page)
assert calls == ["default_lint.py"]
assert page._states[("default", "lint")] is _RunState.SUCCESS
assert page._states[("extra", "lint")] is _RunState.IDLE
9f80683 page.handle_key(key("Tab"))
04f3c66 page.handle_key(key("Down"))
page.handle_key(key("Enter"))
_wait_until_idle(page)
assert calls == ["default_lint.py", "extra_lint.py"]
assert page._states[("extra", "lint")] is _RunState.SUCCESS
assert page._messages[("extra", "lint")] == "extra_lint.py"
def test_actions_page_bad_actions_toml(tmp_path: Path) -> None:
ctx, repo_dir = _repo_dir(tmp_path)
(repo_dir / "actions.toml").write_text("quality = 1\n", encoding="utf-8")
page = ActionsPage()
page.on_enter(ctx)
assert "unexpected key" in page.status_text()
assert page.handle_key(key("Esc")).action == PageAction.POP
8ac8a11
def test_actions_page_hides_hidden_actions(tmp_path: Path) -> None:
ctx, repo_dir = _repo_dir(tmp_path)
(repo_dir / "pyproject.toml").write_text(
"""
[tool.pgt]
actions = [
8fa7f0b { name = "unit", file = "unit.py", hide = true },
04f3c66 { name = "check", depends = ["unit"] },
8ac8a11 ]
""",
encoding="utf-8",
)
page = ActionsPage()
page.on_enter(ctx)
assert [label for _, label in page._rows()] == ["Run all", "check"]