Files
lai_automatisierung/tests/test_git.py
Christian Meyer 34bf645957
Some checks failed
CI / test (push) Has been cancelled
Preserve discarded patch context
2026-08-28 13:55:39 +02:00

107 lines
3.7 KiB
Python

import subprocess
from test_config import settings
from lai_automation.git import GitWorkspace
def test_test_command_does_not_interpret_shell_metacharacters(tmp_path) -> None:
workspace = GitWorkspace(settings(workspace_root=tmp_path), 1)
workspace.path.mkdir()
result = workspace.run("printf safe ; touch injected", check=False)
assert result.stdout == "safe"
assert result.returncode != 0
assert not (workspace.path / "injected").exists()
def test_summary_includes_untracked_files(tmp_path) -> None:
workspace = GitWorkspace(settings(workspace_root=tmp_path), 2)
workspace.path.mkdir()
subprocess.run(["git", "init"], cwd=workspace.path, check=True, capture_output=True)
(workspace.path / "new.php").write_text("<?php\n")
summary = workspace.summary()
assert summary.files == ("new.php",)
assert summary.added_lines == 1
def test_discard_changes_restores_clean_disposable_workspace(tmp_path) -> None:
workspace = GitWorkspace(settings(workspace_root=tmp_path), 3)
workspace.path.mkdir()
subprocess.run(["git", "init"], cwd=workspace.path, check=True, capture_output=True)
subprocess.run(
["git", "config", "user.email", "test@example.invalid"],
cwd=workspace.path,
check=True,
)
subprocess.run(
["git", "config", "user.name", "Test"],
cwd=workspace.path,
check=True,
)
tracked = workspace.path / "tracked.php"
tracked.write_text("<?php\nreturn [];\n")
subprocess.run(["git", "add", "."], cwd=workspace.path, check=True)
subprocess.run(["git", "commit", "-m", "base"], cwd=workspace.path, check=True, capture_output=True)
tracked.write_text("<?php\ninvalid\n")
untracked = workspace.path / "extra.php"
untracked.write_text("<?php\n")
workspace.discard_changes()
assert tracked.read_text() == "<?php\nreturn [];\n"
assert not untracked.exists()
def test_checkpoint_restores_last_valid_changes(tmp_path) -> None:
workspace = GitWorkspace(settings(workspace_root=tmp_path), 4)
workspace.path.mkdir()
subprocess.run(["git", "init"], cwd=workspace.path, check=True, capture_output=True)
subprocess.run(
["git", "config", "user.email", "test@example.invalid"],
cwd=workspace.path,
check=True,
)
subprocess.run(
["git", "config", "user.name", "Test"],
cwd=workspace.path,
check=True,
)
tracked = workspace.path / "config.php"
tracked.write_text("<?php\nreturn [];\n")
subprocess.run(["git", "add", "."], cwd=workspace.path, check=True)
subprocess.run(["git", "commit", "-m", "base"], cwd=workspace.path, check=True, capture_output=True)
tracked.write_text("<?php\nreturn ['valid'];\n")
added = workspace.path / "test.php"
added.write_text("<?php\n")
checkpoint = workspace.checkpoint()
tracked.write_text("<?php\nreturn [broken;\n")
workspace.restore_checkpoint(checkpoint)
assert tracked.read_text() == "<?php\nreturn ['valid'];\n"
assert added.exists()
def test_diff_returns_discarded_patch_excerpt(tmp_path) -> None:
workspace = GitWorkspace(settings(workspace_root=tmp_path), 5)
workspace.path.mkdir()
subprocess.run(["git", "init"], cwd=workspace.path, check=True, capture_output=True)
tracked = workspace.path / "service.php"
tracked.write_text("<?php\nreturn 'old';\n")
subprocess.run(["git", "add", "."], cwd=workspace.path, check=True)
subprocess.run(
["git", "-c", "user.email=test@example.invalid", "-c", "user.name=Test", "commit", "-m", "base"],
cwd=workspace.path,
check=True,
capture_output=True,
)
tracked.write_text("<?php\nreturn 'new';\n")
patch = workspace.diff()
assert "-return 'old';" in patch
assert "+return 'new';" in patch