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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_branch_diff_range_builds_range_string → get_branch_diff_range → get_merge_base → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 181–184def mock_runner(cmd): if "rev-parse" in cmd: return "def456\n" return "abc123\n"lines 186–186result = get_branch_diff_range("origin/main", run_command=mock_runner)lines 187–187assert result == "abc123...HEAD"

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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_branch_diff_range_returns_none_on_failure → get_branch_diff_range → get_merge_base → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 199–199import subprocesslines 201–202def mock_runner(cmd): raise subprocess.CalledProcessError(1, cmd)lines 204–204result = get_branch_diff_range("origin/main", run_command=mock_runner)lines 205–205assert result is None

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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_branch_diff_range_returns_none_on_main → get_branch_diff_range → get_merge_base → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 192–193def mock_runner(cmd): return "abc123\n"lines 195–195result = get_branch_diff_range("origin/main", run_command=mock_runner)lines 196–196assert result is None

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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_calls_git_diff_cached → get_staged_diff → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 112–112calls = []lines 114–116def mock_runner(cmd): calls.append(cmd) return "mock diff output"lines 118–118result = get_staged_diff("src/main.c", run_command=mock_runner)lines 119–119assert result == "mock diff output"lines 120–120assert calls[0][0] == "git" and "core.fsmonitor=" in calls[0]lines 123–123assert calls[0][-6:] == ["diff", "--no-ext-diff", "--cached", "-U0", "--", "src/main.c"]

test_calls_git_diff_with_range

test_guard_git.TestGetDiff.test_calls_git_diff_with_range(self)

tests/guard/test_guard_git.py:129

Show on the map →

Called by none

Calls get_diff

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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_calls_git_diff_with_range → get_diff → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 130–130calls = []lines 132–134def mock_runner(cmd): calls.append(cmd) return "mock diff output"lines 136–136result = get_diff("src/main.c", "HEAD~3..HEAD", run_command=mock_runner)lines 137–137assert result == "mock diff output"lines 138–138assert calls[0][0] == "git" and "core.fsmonitor=" in calls[0]lines 141–141assert calls[0][-6:] == ["diff", "--no-ext-diff", "-U0", "HEAD~3..HEAD", "--", "src/main.c"]

test_changed_lines_is_empty_when_diff_unavailable

test_guard_git.TestGitFailureContract.test_changed_lines_is_empty_when_diff_unavailable(self)

tests/guard/test_guard_git.py:241

Checks

  • assert get_changed_lines_for_file("a.c", hang) == set()

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_changed_lines_is_empty_when_diff_unavailable → get_changed_lines_for_file → get_staged_diff → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_changed_lines_is_empty_when_diff_unavailable → get_changed_lines_for_file
Source · 6 lines
    def test_changed_lines_is_empty_when_diff_unavailable(self):
        def hang(cmd):
            raise subprocess.TimeoutExpired(cmd, 30)

        assert get_changed_lines_for_file("a.c", hang) == set()
Activity · 2 steps
lines 242–243def hang(cmd): raise subprocess.TimeoutExpired(cmd, 30)lines 245–245assert get_changed_lines_for_file("a.c", hang) == set()

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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_combines_diff_and_parse → get_changed_lines_for_file → get_staged_diff → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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

Show on the map →

Called by none

Calls get_diff

Checks

  • assert get_diff("a.c", "--output=/tmp/pwned", capture) is None
  • assert called == []

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001inferredtest_diff_range_that_looks_like_an_option_is_refused → get_diff → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 260–260called: list[list[str]] = []lines 262–264def capture(cmd): called.append(cmd) return ""lines 266–266assert get_diff("a.c", "--output=/tmp/pwned", capture) is Nonelines 267–267assert called == []

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

requirementlinkedpath to the implementer
REQ-DDB-GUARD-023measuredtest_empty_diff → parse_changed_lines
Source · 4 lines
    def test_empty_diff(self):
        result = parse_changed_lines("")
        assert result == set()
Activity · 2 steps
lines 83–83result = parse_changed_lines("")lines 84–84assert result == set()

test_git_add_uses_double_dash

test_guard_git.TestGitFailureContract.test_git_add_uses_double_dash(self)

tests/guard/test_guard_git.py:247

Show on the map →

Called by none

Calls git_add

Checks

  • assert "--" in seen[0]
  • assert seen[0].index("--") < seen[0].index("-weird-name.md")

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_git_add_uses_double_dash → git_add → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 248–248seen: list[list[str]] = []lines 250–252def capture(cmd): seen.append(cmd) return ""lines 254–254git_add(["-weird-name.md"], capture)lines 255–255assert "--" in seen[0]lines 256–256assert seen[0].index("--") < seen[0].index("-weird-name.md")

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

requirementlinkedpath to the implementer
REQ-DDB-GUARD-023measuredtest_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

requirementlinkedpath to the implementer
REQ-DDB-GUARD-023measuredtest_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

requirementlinkedpath to the implementer
REQ-DDB-GUARD-023measuredtest_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

requirementlinkedpath to the implementer
REQ-DDB-GUARD-023measuredtest_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

requirementlinkedpath to the implementer
REQ-DDB-GUARD-023measuredtest_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 cmd
  • assert cmd[-3:] == ["merge-base", "origin/main", "HEAD"]
  • assert result == "abc123def456"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_returns_commit_hash → get_merge_base → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 163–166def mock_runner(cmd): assert cmd[0] == "git" and "core.fsmonitor=" in cmd assert cmd[-3:] == ["merge-base", "origin/main", "HEAD"] return "abc123def456\n"lines 168–168result = get_merge_base("origin/main", run_command=mock_runner)lines 169–169assert result == "abc123def456"

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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_returns_none_on_failure → get_merge_base → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 172–172import subprocesslines 174–175def mock_runner(cmd): raise subprocess.CalledProcessError(1, cmd)lines 177–177result = get_merge_base("origin/main", run_command=mock_runner)lines 178–178assert result is None

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

Show on the map →

Called by none

Calls run_git

Checks

  • assert run_git(["git", "diff"], fail) is None

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_run_git_returns_none_on_called_process_error → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 230–231def fail(cmd): raise subprocess.CalledProcessError(1, cmd)lines 233–233assert run_git(["git", "diff"], fail) is None

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

Show on the map →

Called by none

Calls run_git

Checks

  • assert run_git(["git", "diff"], hang) is None

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_run_git_returns_none_on_timeout → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 224–225def hang(cmd): raise subprocess.TimeoutExpired(cmd, 30)lines 227–227assert run_git(["git", "diff"], hang) is None

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

Show on the map →

Called by none

Calls run_git

Checks

  • assert run_git(["git", "diff"], missing) is None

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_run_git_returns_none_when_git_missing → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_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
lines 236–237def missing(cmd): raise FileNotFoundError("git")lines 239–239assert run_git(["git", "diff"], missing) is None

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

requirementlinkedpath to the implementer
REQ-DDB-GUARD-023measuredtest_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