Debian Codegraph @

tests/vcs_test.py

raw
1 #!/usr/bin/env python3
2 # Copyright: 2026, Maximiliano Curia <[email protected]>
3 #
4 # License: ISC
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 # .
9 # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 # PERFORMANCE OF THIS SOFTWARE.
16
17 """Contract of the git changed-paths computation.
18
19 Builds throwaway git repositories under tmp_path (isolated from the developer's
20 global git config) and asserts changed_paths classifies working-tree changes and
21 untracked files against the baseline, and returns a typed Fallback with the right
22 reason when git or the baseline is unavailable.
23 """
24
25 import os
26 import shutil
27 import subprocess
28
29 import pytest
30
31 from decopy.fstree import vcs
32 from decopy.fstree.vcs import (
33 BAD_REF,
34 COPYRIGHT_UNTRACKED,
35 GIT_MISSING,
36 NOT_A_REPO,
37 ChangedPaths,
38 Fallback,
39 )
40
41 pytestmark = pytest.mark.skipif(
42 shutil.which("git") is None, reason="git is not installed"
43 )
44
45
46 def _run_git(repo, *args):
47 env = dict(os.environ)
48 env["GIT_CONFIG_GLOBAL"] = os.devnull
49 env["GIT_CONFIG_SYSTEM"] = os.devnull
50 env.update(
51 GIT_AUTHOR_NAME="Test",
52 GIT_AUTHOR_EMAIL="[email protected]",
53 GIT_COMMITTER_NAME="Test",
54 GIT_COMMITTER_EMAIL="[email protected]",
55 )
56 cmd = ["git", "-C", str(repo), *args]
57 subprocess.run( # noqa: S603
58 cmd,
59 check=True,
60 capture_output=True,
61 env=env,
62 )
63
64
65 def _write(repo, relpath, content):
66 path = repo / relpath
67 path.parent.mkdir(parents=True, exist_ok=True)
68 path.write_text(content)
69
70
71 def _commit(repo, message):
72 _run_git(repo, "add", "-A")
73 _run_git(repo, "commit", "-m", message)
74
75
76 @pytest.fixture
77 def repo(tmp_path):
78 """A repo whose only commit touches debian/copyright (the baseline)."""
79 _run_git(tmp_path, "init")
80 _write(tmp_path, "debian/copyright", "initial\n")
81 _write(tmp_path, "src/a.c", "int a;\n")
82 _write(tmp_path, "src/b.c", "int b;\n")
83 _commit(tmp_path, "base: copyright and sources")
84 return tmp_path
85
86
87 def test_no_changes_returns_empty_changedpaths(repo):
88 result = vcs.changed_paths(str(repo), "debian/copyright")
89 assert isinstance(result, ChangedPaths)
90 assert result.added_or_modified == ()
91 assert result.deleted == ()
92 assert result.renamed == ()
93
94
95 def test_reports_change_committed_after_baseline(repo):
96 # A later commit modifies a.c only, never touching copyright, so the working
97 # tree equals HEAD. The change still shows because the baseline is the last
98 # commit that touched copyright, not HEAD.
99 _write(repo, "src/a.c", "int a2;\n")
100 _commit(repo, "later: modify a.c")
101 result = vcs.changed_paths(str(repo), "debian/copyright")
102 assert isinstance(result, ChangedPaths)
103 assert "src/a.c" in result.added_or_modified
104 assert "src/b.c" not in result.added_or_modified
105
106
107 def test_untracked_file_is_reported(repo):
108 _write(repo, "src/new.c", "int n;\n")
109 result = vcs.changed_paths(str(repo), "debian/copyright")
110 assert isinstance(result, ChangedPaths)
111 assert "src/new.c" in result.added_or_modified
112
113
114 def test_deleted_file_lands_in_deleted(repo):
115 (repo / "src" / "b.c").unlink()
116 result = vcs.changed_paths(str(repo), "debian/copyright")
117 assert isinstance(result, ChangedPaths)
118 assert "src/b.c" in result.deleted
119 assert "src/b.c" not in result.added_or_modified
120
121
122 def test_rename_lands_in_renamed(repo):
123 _run_git(repo, "mv", "src/a.c", "src/a2.c")
124 result = vcs.changed_paths(str(repo), "debian/copyright")
125 assert isinstance(result, ChangedPaths)
126 assert ("src/a.c", "src/a2.c") in result.renamed
127
128
129 def test_since_override_uses_given_ref(repo):
130 _write(repo, "src/a.c", "int a2;\n")
131 _commit(repo, "later: modify a.c")
132 # Since the copyright baseline, a.c changed; since HEAD, nothing changed.
133 since_head = vcs.changed_paths(str(repo), "debian/copyright", since="HEAD")
134 assert isinstance(since_head, ChangedPaths)
135 assert "src/a.c" not in since_head.added_or_modified
136 default = vcs.changed_paths(str(repo), "debian/copyright")
137 assert isinstance(default, ChangedPaths)
138 assert "src/a.c" in default.added_or_modified
139
140
141 def test_not_a_git_repo_returns_fallback(tmp_path):
142 result = vcs.changed_paths(str(tmp_path), "debian/copyright")
143 assert isinstance(result, Fallback)
144 assert result.reason == NOT_A_REPO
145
146
147 def test_copyright_untracked_returns_fallback(repo):
148 result = vcs.changed_paths(str(repo), "debian/absent-copyright")
149 assert isinstance(result, Fallback)
150 assert result.reason == COPYRIGHT_UNTRACKED
151
152
153 def test_bad_since_ref_returns_fallback(repo):
154 result = vcs.changed_paths(str(repo), "debian/copyright", since="no-such-ref")
155 assert isinstance(result, Fallback)
156 assert result.reason == BAD_REF
157
158
159 def test_git_missing_returns_fallback(repo, monkeypatch):
160 monkeypatch.setattr(vcs.shutil, "which", lambda _cmd: None)
161 result = vcs.changed_paths(str(repo), "debian/copyright")
162 assert isinstance(result, Fallback)
163 assert result.reason == GIT_MISSING
164 assert "git" in result.message.lower()