test_guard_git.py¶
tests/guard/test_guard_git.py
Tests for clew.guard.git (absorbed from doxygen-guard).
test_branch_diff_range_builds_range_string
test_guard_git.TestGetMergeBase.test_branch_diff_range_builds_range_string(self)
tests/guard/test_guard_git.py:180
Checks
assert result == "abc123...HEAD"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_branch_diff_range_builds_range_string → get_branch_diff_range → get_merge_base → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_branch_diff_range_builds_range_string → get_branch_diff_range |
Source · 9 lines
def test_branch_diff_range_builds_range_string(self):
def mock_runner(cmd):
if "rev-parse" in cmd:
return "def456\n"
return "abc123\n"
result = get_branch_diff_range("origin/main", run_command=mock_runner)
assert result == "abc123...HEAD"
Activity · 3 steps
test_branch_diff_range_returns_none_on_failure
test_guard_git.TestGetMergeBase.test_branch_diff_range_returns_none_on_failure(self)
tests/guard/test_guard_git.py:198
Checks
assert result is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_branch_diff_range_returns_none_on_failure → get_branch_diff_range → get_merge_base → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_branch_diff_range_returns_none_on_failure → get_branch_diff_range |
Source · 10 lines
def test_branch_diff_range_returns_none_on_failure(self):
import subprocess
def mock_runner(cmd):
raise subprocess.CalledProcessError(1, cmd)
result = get_branch_diff_range("origin/main", run_command=mock_runner)
assert result is None
Activity · 4 steps
test_branch_diff_range_returns_none_on_main
test_guard_git.TestGetMergeBase.test_branch_diff_range_returns_none_on_main(self)
When merge-base == HEAD (on main), returns None for staged fallback.
tests/guard/test_guard_git.py:189
Checks
assert result is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_branch_diff_range_returns_none_on_main → get_branch_diff_range → get_merge_base → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_branch_diff_range_returns_none_on_main → get_branch_diff_range |
Source · 9 lines
def test_branch_diff_range_returns_none_on_main(self):
"""When merge-base == HEAD (on main), returns None for staged fallback."""
def mock_runner(cmd):
return "abc123\n"
result = get_branch_diff_range("origin/main", run_command=mock_runner)
assert result is None
Activity · 3 steps
test_calls_git_diff_cached
test_guard_git.TestGetStagedDiff.test_calls_git_diff_cached(self)
tests/guard/test_guard_git.py:111
Checks
assert result == "mock diff output"assert calls[0][0] == "git" and "core.fsmonitor=" in calls[0]assert calls[0][-6:] == ["diff", "--no-ext-diff", "--cached", "-U0", "--", "src/main.c"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_calls_git_diff_cached → get_staged_diff → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_calls_git_diff_cached → get_staged_diff |
Source · 15 lines
def test_calls_git_diff_cached(self):
calls = []
def mock_runner(cmd):
calls.append(cmd)
return "mock diff output"
result = get_staged_diff("src/main.c", run_command=mock_runner)
assert result == "mock diff output"
assert calls[0][0] == "git" and "core.fsmonitor=" in calls[0]
# round 3, P6c: `--no-ext-diff` follows `diff`. Without it the hardened argv's empty
# `diff.external=` made git run "" and every diff failed (impact read `[]`).
assert calls[0][-6:] == ["diff", "--no-ext-diff", "--cached", "-U0", "--", "src/main.c"]
Activity · 6 steps
test_calls_git_diff_with_range
test_guard_git.TestGetDiff.test_calls_git_diff_with_range(self)
tests/guard/test_guard_git.py:129
Checks
assert result == "mock diff output"assert calls[0][0] == "git" and "core.fsmonitor=" in calls[0]assert calls[0][-6:] == ["diff", "--no-ext-diff", "-U0", "HEAD~3..HEAD", "--", "src/main.c"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_calls_git_diff_with_range → get_diff → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_calls_git_diff_with_range → get_diff |
Source · 15 lines
def test_calls_git_diff_with_range(self):
calls = []
def mock_runner(cmd):
calls.append(cmd)
return "mock diff output"
result = get_diff("src/main.c", "HEAD~3..HEAD", run_command=mock_runner)
assert result == "mock diff output"
assert calls[0][0] == "git" and "core.fsmonitor=" in calls[0]
# round 3, P6c: `--no-ext-diff` follows `diff`. Without it the hardened argv's empty
# `diff.external=` made git run "" and every diff failed (impact read `[]`).
assert calls[0][-6:] == ["diff", "--no-ext-diff", "-U0", "HEAD~3..HEAD", "--", "src/main.c"]
Activity · 6 steps
test_combines_diff_and_parse
test_guard_git.TestGetChangedLinesForFile.test_combines_diff_and_parse(self)
tests/guard/test_guard_git.py:147
Checks
assert result == {4}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_combines_diff_and_parse → get_changed_lines_for_file → get_staged_diff → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_combines_diff_and_parse → get_changed_lines_for_file |
Source · 12 lines
def test_combines_diff_and_parse(self):
def mock_runner(cmd):
return """\
@@ -5,1 +5,1 @@
-old
+new
"""
result = get_changed_lines_for_file("test.c", run_command=mock_runner)
assert result == {4}
test_diff_range_that_looks_like_an_option_is_refused
test_guard_git.TestGitFailureContract.test_diff_range_that_looks_like_an_option_is_refused(self)
git diff supports --output=<file>, an arbitrary write primitive.
tests/guard/test_guard_git.py:258
Checks
assert get_diff("a.c", "--output=/tmp/pwned", capture) is Noneassert called == []
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | inferred | test_diff_range_that_looks_like_an_option_is_refused → get_diff → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_diff_range_that_looks_like_an_option_is_refused → get_diff |
Source · 10 lines
def test_diff_range_that_looks_like_an_option_is_refused(self):
"""git diff supports --output=<file>, an arbitrary write primitive."""
called: list[list[str]] = []
def capture(cmd):
called.append(cmd)
return ""
assert get_diff("a.c", "--output=/tmp/pwned", capture) is None
assert called == []1 more lines, not shown
Activity · 4 steps
test_empty_diff
test_guard_git.TestParseChangedLines.test_empty_diff(self)
tests/guard/test_guard_git.py:82
Checks
assert result == set()
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-023 | measured | test_empty_diff → parse_changed_lines |
Source · 4 lines
def test_empty_diff(self):
result = parse_changed_lines("")
assert result == set()
Activity · 2 steps
test_git_add_uses_double_dash
test_guard_git.TestGitFailureContract.test_git_add_uses_double_dash(self)
tests/guard/test_guard_git.py:247
Checks
assert "--" in seen[0]assert seen[0].index("--") < seen[0].index("-weird-name.md")
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_git_add_uses_double_dash → git_add → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_git_add_uses_double_dash → git_add → run_git |
Source · 11 lines
def test_git_add_uses_double_dash(self):
seen: list[list[str]] = []
def capture(cmd):
seen.append(cmd)
return ""
git_add(["-weird-name.md"], capture)
assert "--" in seen[0]
assert seen[0].index("--") < seen[0].index("-weird-name.md")
Activity · 5 steps
test_modification_in_middle
test_guard_git.TestParseChangedLines.test_modification_in_middle(self)
tests/guard/test_guard_git.py:50
Checks
assert result == {9, 10} # lines 10-11 → 0-indexed
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-023 | measured | test_modification_in_middle → parse_changed_lines |
Source · 11 lines
def test_modification_in_middle(self):
diff = """\
@@ -10,2 +10,2 @@
-old line A
-old line B
+new line A
+new line B
"""
result = parse_changed_lines(diff)
assert result == {9, 10} # lines 10-11 → 0-indexed
test_multi_line_addition
test_guard_git.TestParseChangedLines.test_multi_line_addition(self)
tests/guard/test_guard_git.py:40
Checks
assert result == {0, 1, 2}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-023 | measured | test_multi_line_addition → parse_changed_lines |
Source · 10 lines
def test_multi_line_addition(self):
diff = """\
@@ -0,0 +1,3 @@
+line one
+line two
+line three
"""
result = parse_changed_lines(diff)
assert result == {0, 1, 2}
test_multiple_hunks
test_guard_git.TestParseChangedLines.test_multiple_hunks(self)
tests/guard/test_guard_git.py:61
Checks
assert result == {4, 19}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-023 | measured | test_multiple_hunks → parse_changed_lines |
Source · 12 lines
def test_multiple_hunks(self):
diff = """\
@@ -5,1 +5,1 @@
-old
+new
@@ -20,1 +20,1 @@
-old
+new
"""
result = parse_changed_lines(diff)
assert result == {4, 19}
test_pure_deletion
test_guard_git.TestParseChangedLines.test_pure_deletion(self)
tests/guard/test_guard_git.py:73
Checks
assert result == set()
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-023 | measured | test_pure_deletion → parse_changed_lines |
Source · 9 lines
def test_pure_deletion(self):
diff = """\
@@ -5,2 +5,0 @@
-deleted line 1
-deleted line 2
"""
result = parse_changed_lines(diff)
assert result == set()
test_real_world_diff
test_guard_git.TestParseChangedLines.test_real_world_diff(self)
tests/guard/test_guard_git.py:86
Checks
assert {9, 10, 11, 12, 13}.issubset(result)assert 26 in result
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-023 | measured | test_real_world_diff → parse_changed_lines |
Source · 22 lines
def test_real_world_diff(self):
diff = """\
diff --git a/src/main.c b/src/main.c
index abc123..def456 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,3 +10,5 @@ void Module_Init(void) {
- old_setup();
+ new_setup();
+ configure();
+ validate();
@@ -25,1 +27,1 @@ int Module_Process(const char *data) {
- return process(data);
+ return process_v2(data);
"""
result = parse_changed_lines(diff)
# First hunk: lines 10-14 (5 lines starting at 10)
assert {9, 10, 11, 12, 13}.issubset(result)
# Second hunk: line 27
assert 26 in result
test_returns_commit_hash
test_guard_git.TestGetMergeBase.test_returns_commit_hash(self)
tests/guard/test_guard_git.py:162
Checks
assert cmd[0] == "git" and "core.fsmonitor=" in cmdassert cmd[-3:] == ["merge-base", "origin/main", "HEAD"]assert result == "abc123def456"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_returns_commit_hash → get_merge_base → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_returns_commit_hash → get_merge_base |
Source · 9 lines
def test_returns_commit_hash(self):
def mock_runner(cmd):
assert cmd[0] == "git" and "core.fsmonitor=" in cmd
assert cmd[-3:] == ["merge-base", "origin/main", "HEAD"]
return "abc123def456\n"
result = get_merge_base("origin/main", run_command=mock_runner)
assert result == "abc123def456"
Activity · 3 steps
test_returns_none_on_failure
test_guard_git.TestGetMergeBase.test_returns_none_on_failure(self)
tests/guard/test_guard_git.py:171
Checks
assert result is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_returns_none_on_failure → get_merge_base → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_returns_none_on_failure → get_merge_base |
Source · 9 lines
def test_returns_none_on_failure(self):
import subprocess
def mock_runner(cmd):
raise subprocess.CalledProcessError(1, cmd)
result = get_merge_base("origin/main", run_command=mock_runner)
assert result is None
Activity · 4 steps
test_run_git_returns_none_on_called_process_error
test_guard_git.TestGitFailureContract.test_run_git_returns_none_on_called_process_error(self)
tests/guard/test_guard_git.py:229
Checks
assert run_git(["git", "diff"], fail) is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_run_git_returns_none_on_called_process_error → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_run_git_returns_none_on_called_process_error → run_git |
Source · 6 lines
def test_run_git_returns_none_on_called_process_error(self):
def fail(cmd):
raise subprocess.CalledProcessError(1, cmd)
assert run_git(["git", "diff"], fail) is None
Activity · 2 steps
test_run_git_returns_none_on_timeout
test_guard_git.TestGitFailureContract.test_run_git_returns_none_on_timeout(self)
tests/guard/test_guard_git.py:223
Checks
assert run_git(["git", "diff"], hang) is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_run_git_returns_none_on_timeout → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_run_git_returns_none_on_timeout → run_git |
Source · 6 lines
def test_run_git_returns_none_on_timeout(self):
def hang(cmd):
raise subprocess.TimeoutExpired(cmd, 30)
assert run_git(["git", "diff"], hang) is None
Activity · 2 steps
test_run_git_returns_none_when_git_missing
test_guard_git.TestGitFailureContract.test_run_git_returns_none_when_git_missing(self)
tests/guard/test_guard_git.py:235
Checks
assert run_git(["git", "diff"], missing) is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_run_git_returns_none_when_git_missing → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_run_git_returns_none_when_git_missing → run_git |
Source · 6 lines
def test_run_git_returns_none_when_git_missing(self):
def missing(cmd):
raise FileNotFoundError("git")
assert run_git(["git", "diff"], missing) is None
Activity · 2 steps
test_single_line_addition
test_guard_git.TestParseChangedLines.test_single_line_addition(self)
tests/guard/test_guard_git.py:32
Checks
assert result == {0} # line 1 → 0-indexed
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-GUARD-023 | measured | test_single_line_addition → parse_changed_lines |
Source · 8 lines
def test_single_line_addition(self):
diff = """\
@@ -0,0 +1 @@
+new line
"""
result = parse_changed_lines(diff)
assert result == {0} # line 1 → 0-indexed