Debian Codegraph @

tests/license_notice_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 """License-notice files are not listed in the generated copyright.
18
19 A file that only carries a license text (COPYING, LICENSE, ...) needs no entry
20 of its own: lintian reports license-file-listed-in-debian-copyright for it and
21 excuses it from needing coverage. decopy drops those entries by default;
22 --list-license-files restores the old behaviour (Debian bug #962309).
23 """
24
25 from __future__ import annotations
26
27 import contextlib
28 import io
29 import shutil
30 import tempfile
31 import unittest
32 from pathlib import Path
33
34 from decopy import decopy
35 from decopy.cmdoptions import process_options
36 from decopy.fstree import nodes
37
38 _TESTDATA = Path(__file__).parent / "testdata"
39
40 _NO_CATCHALL_COPYRIGHT = """\
41 Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
42 Source: TODO
43
44 Files: art.txt
45 License: Artistic-1
46
47 Files: bsd.txt
48 License: BSD-3-clause
49
50 Files: paren.txt
51 License: GPL-3+
52 """
53
54 _COPYING_LISTED_COPYRIGHT = """\
55 Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
56 Source: TODO
57
58 Files: *
59 License: GPL-2+
60
61 Files: COPYING
62 License: GPL-2+
63 """
64
65
66 _STANZA_COPYRIGHT = """\
67 Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
68 Source: TODO
69
70 Files: *
71 License: Artistic-1
72
73 Files: COPYING
74 License: GPL-2+
75
76 License: GPL-2+
77 Old stored text for the GPL-2+ stanza.
78 """
79
80
81 def _write_tree(root: Path, copyright_text: str | None = None) -> None:
82 """Copy the known-license fixtures plus COPYING into `root`."""
83 src = _TESTDATA / "one"
84 for name in ("COPYING", "art.txt", "bsd.txt", "paren.txt"):
85 shutil.copy(src / name, root / name)
86 if copyright_text is not None:
87 debian = root / "debian"
88 debian.mkdir()
89 (debian / "copyright").write_text(copyright_text, encoding="utf-8")
90
91
92 def _generate(root: Path, extra: list[str] | None = None) -> str:
93 """Render the copyright for `root` and return the generated text."""
94 with tempfile.TemporaryDirectory() as tmp:
95 out = Path(tmp) / "copyright"
96 options = process_options(
97 [
98 "--no-progress",
99 "--no-expand-archives",
100 *(extra or []),
101 "--root",
102 str(root),
103 "-o",
104 str(out),
105 ]
106 )
107 _ = decopy.run_pipeline(options)
108 return out.read_text(encoding="utf-8")
109
110
111 def _explain(root: Path, extra: list[str] | None = None) -> tuple[int, str]:
112 """Run explain mode over `root`, returning (exit code, stdout)."""
113 options = process_options(
114 [
115 "--no-progress",
116 "--no-expand-archives",
117 "--explain",
118 *(extra or []),
119 "--root",
120 str(root),
121 ]
122 )
123 out = io.StringIO()
124 with contextlib.redirect_stdout(out):
125 code = decopy.run_pipeline(options)
126 return code, out.getvalue()
127
128
129 class LicenseNoticeNameTest(unittest.TestCase):
130 """Which names count as a license notice file."""
131
132 def test_notice_names(self):
133 names = (
134 "COPYING",
135 "COPYING.GPL",
136 "COPYING.txt",
137 "COPYRIGHT",
138 "LICENSE",
139 "LICENSE.md",
140 )
141 for name in names:
142 with self.subTest(name=name):
143 self.assertTrue(nodes.FileInfo(None, name).is_license_notice())
144
145 def test_other_names(self):
146 names = (
147 "copying",
148 "license.txt",
149 "COPYRIGHT.txt",
150 "README",
151 "AUTHORS",
152 "COPY",
153 "NOTICE",
154 )
155 for name in names:
156 with self.subTest(name=name):
157 self.assertFalse(nodes.FileInfo(None, name).is_license_notice())
158
159 def test_directory_is_never_a_notice(self):
160 self.assertFalse(nodes.DirInfo(None, "LICENSES").is_license_notice())
161
162
163 class GeneratedOutputTest(unittest.TestCase):
164 """The generated copyright drops the license-notice entries."""
165
166 def test_notice_file_is_not_listed(self):
167 text = _generate(_TESTDATA / "one")
168 self.assertNotIn("Files: COPYING", text)
169
170 def test_flag_lists_the_notice_file(self):
171 text = _generate(_TESTDATA / "one", ["--list-license-files"])
172 self.assertIn("Files: COPYING", text)
173
174 def test_notice_licenses_still_reach_the_output(self):
175 # Dropping the entry must not drop what the notice file taught the tree:
176 # the catch-all keeps the inherited license and names its source.
177 text = _generate(_TESTDATA / "one")
178 self.assertIn("Files: *\nLicense: GPL-2+", text)
179 self.assertIn(
180 "No explicit license found, using license(s) from:\n COPYING", text
181 )
182
183 def test_existing_entry_is_dropped(self):
184 with tempfile.TemporaryDirectory() as tmp:
185 root = Path(tmp)
186 _write_tree(root, _COPYING_LISTED_COPYRIGHT)
187 text = _generate(root)
188 self.assertNotIn("Files: COPYING", text)
189
190 def test_existing_entry_is_kept_with_the_flag(self):
191 with tempfile.TemporaryDirectory() as tmp:
192 root = Path(tmp)
193 _write_tree(root, _COPYING_LISTED_COPYRIGHT)
194 text = _generate(root, ["--list-license-files"])
195 self.assertIn("Files: COPYING", text)
196
197
198 class LicenseStanzaTest(unittest.TestCase):
199 """A dropped notice file leaves no orphan License stanza behind."""
200
201 # The fixture tree is COPYING (GPL-2+) plus three files that each carry
202 # their own license, so nothing inherits GPL-2+: once COPYING loses its
203 # entry, no Files paragraph names that license any more.
204
205 def test_unused_notice_license_has_no_stanza(self):
206 with tempfile.TemporaryDirectory() as tmp:
207 root = Path(tmp)
208 _write_tree(root)
209 text = _generate(root)
210 self.assertNotIn("GPL-2+", text)
211
212 def test_flag_keeps_the_stanza(self):
213 with tempfile.TemporaryDirectory() as tmp:
214 root = Path(tmp)
215 _write_tree(root)
216 text = _generate(root, ["--list-license-files"])
217 self.assertIn("License: GPL-2+", text)
218
219 def test_stored_stanza_of_a_dropped_entry_goes_too(self):
220 # Updating a copyright that already carries the stanza text: the text
221 # is stored while reading, and must not reach the output either.
222 with tempfile.TemporaryDirectory() as tmp:
223 root = Path(tmp) / "src"
224 root.mkdir()
225 _write_tree(root)
226 old = Path(tmp) / "copyright"
227 old.write_text(_STANZA_COPYRIGHT, encoding="utf-8")
228 text = _generate(root, ["--copyright-file", str(old)])
229 self.assertNotIn("GPL-2+", text)
230 self.assertNotIn("Old stored text", text)
231
232
233 class ExplainModeTest(unittest.TestCase):
234 """Explain mode agrees with what generation would produce."""
235
236 def test_uncovered_notice_file_is_not_stale(self):
237 with tempfile.TemporaryDirectory() as tmp:
238 root = Path(tmp)
239 _write_tree(root, _NO_CATCHALL_COPYRIGHT)
240 code, out = _explain(root)
241 self.assertEqual(out, "debian/copyright is up to date.\n")
242 self.assertEqual(code, 0)
243
244 def test_uncovered_notice_file_is_reported_with_the_flag(self):
245 with tempfile.TemporaryDirectory() as tmp:
246 root = Path(tmp)
247 _write_tree(root, _NO_CATCHALL_COPYRIGHT)
248 code, out = _explain(root, ["--list-license-files"])
249 self.assertEqual(code, 1)
250 self.assertIn("COPYING", out)
251
252 def test_listed_notice_entry_is_reported(self):
253 # The entry generation would drop has to show up in the report, or
254 # explain would call an out-of-date file clean.
255 with tempfile.TemporaryDirectory() as tmp:
256 root = Path(tmp)
257 _write_tree(root, _COPYING_LISTED_COPYRIGHT)
258 code, out = _explain(root)
259 self.assertEqual(code, 1)
260 self.assertIn("COPYING", out)
261
262
263 if __name__ == "__main__":
264 unittest.main()