Debian Codegraph @

tests/changed_cli_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 """End-to-end --changed git scoping.
18
19 Builds a git repository whose one commit touches debian/copyright (the
20 baseline), then asserts --changed scopes the explain report and generation to
21 the files changed since that baseline, that the git-shorthand equals
22 --mode=changed, and that an unavailable git degrades to a full scan.
23 """
24
25 from __future__ import annotations
26
27 import contextlib
28 import io
29 import logging
30 import os
31 import shutil
32 import subprocess
33 from pathlib import Path
34
35 import pytest
36
37 from decopy import decopy
38 from decopy.cmdoptions import process_options
39 from decopy.fstree import vcs
40
41 _TESTDATA = Path(__file__).parent / "testdata" / "one"
42
43 pytestmark = pytest.mark.skipif(
44 shutil.which("git") is None, reason="git is not installed"
45 )
46
47 # A wildcard paragraph declaring a license none of the files detect, so every
48 # wildcard-matched file reads as relicensed (license-changed only surfaces under
49 # a wildcard Files: pattern).
50 _STALE = """\
51 Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
52 Source: TODO
53
54 Files: *
55 License: GPL-2+
56 """
57
58
59 # bsd.txt (BSD-3-clause) is covered cleanly; legacy/* matches no current file, so
60 # it is a whole-tree stale paragraph that changed mode must keep and a plain
61 # partial subset must drop.
62 _STALE_PARAGRAPH = """\
63 Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
64 Source: TODO
65
66 Files: *
67 License: BSD-3-clause
68
69 Files: legacy/*
70 License: MIT
71 """
72
73
74 def _run_git(repo, *args):
75 env = dict(os.environ)
76 env["GIT_CONFIG_GLOBAL"] = os.devnull
77 env["GIT_CONFIG_SYSTEM"] = os.devnull
78 env.update(
79 GIT_AUTHOR_NAME="Test",
80 GIT_AUTHOR_EMAIL="[email protected]",
81 GIT_COMMITTER_NAME="Test",
82 GIT_COMMITTER_EMAIL="[email protected]",
83 )
84 cmd = ["git", "-C", str(repo), *args]
85 subprocess.run(cmd, check=True, capture_output=True, env=env) # noqa: S603
86
87
88 def _build_repo(root, copyright_text=_STALE):
89 """A committed repo with art.txt (Artistic-1), bsd.txt (BSD-3-clause) and the
90 given debian/copyright; the single commit is the baseline."""
91 for name in ("art.txt", "bsd.txt"):
92 shutil.copy(_TESTDATA / name, root / name)
93 debian = root / "debian"
94 debian.mkdir()
95 (debian / "copyright").write_text(copyright_text, encoding="utf-8")
96 _run_git(root, "init")
97 _run_git(root, "add", "-A")
98 _run_git(root, "commit", "-m", "base")
99
100
101 # legacy/old.c is the sole match of Files: legacy/*; removing it empties that
102 # paragraph, so a deletion since the baseline makes it stale.
103 _LEGACY_COPYRIGHT = """\
104 Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
105 Source: TODO
106
107 Files: *
108 License: BSD-3-clause
109
110 Files: legacy/*
111 License: Expat
112 """
113
114
115 def _build_legacy_repo(root):
116 """Like _build_repo, plus a legacy/old.c covered only by Files: legacy/*."""
117 for name in ("art.txt", "bsd.txt"):
118 shutil.copy(_TESTDATA / name, root / name)
119 (root / "legacy").mkdir()
120 (root / "legacy" / "old.c").write_text("int old;\n", encoding="utf-8")
121 debian = root / "debian"
122 debian.mkdir()
123 (debian / "copyright").write_text(_LEGACY_COPYRIGHT, encoding="utf-8")
124 _run_git(root, "init")
125 _run_git(root, "add", "-A")
126 _run_git(root, "commit", "-m", "base")
127
128
129 def _run(root, extra_args):
130 argv = [
131 "--no-progress",
132 "--no-expand-archives",
133 "--root",
134 str(root),
135 *extra_args,
136 ]
137 options = process_options(argv)
138 out, err = io.StringIO(), io.StringIO()
139 with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
140 code = decopy.run_pipeline(options)
141 return code, out.getvalue(), err.getvalue()
142
143
144 def test_explain_changed_scopes_to_changed_file(tmp_path):
145 _build_repo(tmp_path)
146 # Modify only bsd.txt in the working tree; art.txt is equally relicensed but
147 # unchanged, so only bsd.txt must be reported.
148 (tmp_path / "bsd.txt").write_text(
149 (tmp_path / "bsd.txt").read_text() + "\n// touched\n", encoding="utf-8"
150 )
151 code, out, _ = _run(tmp_path, ["--explain", "--changed"])
152 assert code == 1
153 assert "bsd.txt" in out
154 assert "art.txt" not in out
155
156
157 def test_empty_diff_reports_nothing_and_exits_zero(tmp_path):
158 _build_repo(tmp_path)
159 # Nothing changed since the baseline, though the committed copyright is
160 # stale: --changed must report nothing (the staleness is not recent).
161 code, out, _ = _run(tmp_path, ["--explain", "--changed"])
162 assert code == 0
163 assert "bsd.txt" not in out
164 assert "art.txt" not in out
165 # A full explain of the same stale tree would flag the relicenses.
166 full_code, full_out, _ = _run(tmp_path, ["--explain"])
167 assert full_code == 1
168 assert "bsd.txt" in full_out
169
170
171 def test_not_a_repo_warns_and_falls_back_to_full(tmp_path, caplog):
172 # No git init: --changed cannot resolve a baseline.
173 for name in ("art.txt", "bsd.txt"):
174 shutil.copy(_TESTDATA / name, tmp_path / name)
175 (tmp_path / "debian").mkdir()
176 (tmp_path / "debian" / "copyright").write_text(_STALE, encoding="utf-8")
177
178 with caplog.at_level(logging.WARNING, logger="decopy"):
179 changed_code, changed_out, _ = _run(tmp_path, ["--explain", "--changed"])
180 full_code, full_out, _ = _run(tmp_path, ["--explain"])
181 assert changed_code == full_code
182 assert changed_out == full_out
183 assert "not a git repository" in caplog.text
184
185
186 def test_git_missing_warns_and_falls_back_to_full(tmp_path, monkeypatch, caplog):
187 _build_repo(tmp_path)
188 monkeypatch.setattr(vcs.shutil, "which", lambda _cmd: None)
189 with caplog.at_level(logging.WARNING, logger="decopy"):
190 changed_code, changed_out, _ = _run(tmp_path, ["--explain", "--changed"])
191 # With git unavailable the run degrades to a full explain of the stale tree.
192 assert changed_code == 1
193 assert "bsd.txt" in changed_out
194 assert "art.txt" in changed_out
195 assert "git was not found" in caplog.text
196
197
198 def test_changed_flag_equals_mode_changed(tmp_path):
199 _build_repo(tmp_path)
200 (tmp_path / "bsd.txt").write_text(
201 (tmp_path / "bsd.txt").read_text() + "\n// touched\n", encoding="utf-8"
202 )
203 shorthand = _run(tmp_path, ["--explain", "--changed"])
204 explicit = _run(tmp_path, ["--explain", "--mode=changed"])
205 assert shorthand[0] == explicit[0]
206 assert shorthand[1] == explicit[1]
207
208
209 def test_changed_reports_stale_paragraph_that_partial_drops(tmp_path):
210 # ChangedDelta keeps the whole-tree stale-paragraph signal (a paragraph that
211 # matches no current file); a generic partial subset (PartialDelta) drops it.
212 _build_repo(tmp_path, _STALE_PARAGRAPH)
213 (tmp_path / "bsd.txt").write_text(
214 (tmp_path / "bsd.txt").read_text() + "\n// touched\n", encoding="utf-8"
215 )
216 changed_code, changed_out, _ = _run(tmp_path, ["--explain", "--changed"])
217 assert changed_code == 1
218 assert "legacy/*" in changed_out
219 _, partial_out, _ = _run(
220 tmp_path, ["--explain", "--mode", "partial", str(tmp_path / "bsd.txt")]
221 )
222 assert "legacy/*" not in partial_out
223
224
225 def test_generation_changed_matches_named_partial(tmp_path):
226 _build_repo(tmp_path)
227 (tmp_path / "bsd.txt").write_text(
228 (tmp_path / "bsd.txt").read_text() + "\n// touched\n", encoding="utf-8"
229 )
230 _, changed_out, _ = _run(tmp_path, ["--changed"])
231 _, partial_out, _ = _run(tmp_path, ["--mode", "partial", str(tmp_path / "bsd.txt")])
232 assert changed_out == partial_out
233
234
235 def test_deletion_reports_stale_paragraph_with_provenance(tmp_path):
236 _build_legacy_repo(tmp_path)
237 (tmp_path / "legacy" / "old.c").unlink()
238 code, out, _ = _run(tmp_path, ["--explain", "--changed"])
239 assert code == 1
240 assert "Files: legacy/*" in out
241 assert "legacy/old.c (deleted)" in out
242 # Nothing was added/modified, so the file-level scope is empty: art.txt's
243 # relicense under the wildcard paragraph is not reported.
244 assert "art.txt" not in out
245
246
247 def test_deletion_stale_paragraph_dropped_by_plain_partial(tmp_path):
248 _build_legacy_repo(tmp_path)
249 (tmp_path / "legacy" / "old.c").unlink()
250 _, out, _ = _run(
251 tmp_path, ["--explain", "--mode", "partial", str(tmp_path / "bsd.txt")]
252 )
253 assert "legacy/*" not in out
254
255
256 def test_full_explain_reports_stale_paragraph_without_provenance(tmp_path):
257 _build_legacy_repo(tmp_path)
258 (tmp_path / "legacy" / "old.c").unlink()
259 code, out, _ = _run(tmp_path, ["--explain"])
260 assert code == 1
261 assert "Files: legacy/*" in out
262 # The deleted set is computed only in changed mode, so no provenance here.
263 assert "(deleted)" not in out
264
265
266 def test_rename_names_old_side_as_provenance(tmp_path):
267 _build_legacy_repo(tmp_path)
268 _run_git(tmp_path, "mv", "legacy/old.c", "moved.c")
269 code, out, _ = _run(tmp_path, ["--explain", "--changed"])
270 assert code == 1
271 assert "legacy/old.c (deleted)" in out
272
273
274 def test_empty_diff_still_reports_pre_existing_stale_paragraph(tmp_path):
275 # A paragraph stale already at the baseline (no legacy/ dir ever): changed
276 # mode reports it even with an empty diff, but without provenance since no
277 # deletion caused it.
278 _build_repo(tmp_path, _STALE_PARAGRAPH)
279 code, out, _ = _run(tmp_path, ["--explain", "--changed"])
280 assert code == 1
281 assert "legacy/*" in out
282 assert "(deleted)" not in out