| 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 |
"""Files excluded with --exclude/-X are not listed in the generated copyright. |
| 18 |
|
| 19 |
-X used to gate parsing only, so an excluded file stayed in the tree with no |
| 20 |
license of its own and inherited the nearest enclosing group: the output listed |
| 21 |
it under a license that was never detected for it (Debian bug #997814). |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import annotations |
| 25 |
|
| 26 |
import contextlib |
| 27 |
import io |
| 28 |
import shutil |
| 29 |
import tempfile |
| 30 |
import unittest |
| 31 |
from pathlib import Path |
| 32 |
|
| 33 |
from decopy import decopy |
| 34 |
from decopy.cmdoptions import process_options |
| 35 |
|
| 36 |
_TESTDATA = Path(__file__).parent / "testdata" |
| 37 |
|
| 38 |
# art.txt at the top level and bsd.txt under vendor/ carry different licenses, |
| 39 |
# so an excluded vendor/bsd.txt that gets absorbed by the Artistic-1 group is |
| 40 |
# visibly misattributed rather than merely present. |
| 41 |
_NO_GLOB = ["--no-glob"] |
| 42 |
_EXCLUDE_VENDOR = ["-X", "^vendor/"] |
| 43 |
|
| 44 |
|
| 45 |
def _write_tree(root: Path) -> None: |
| 46 |
"""A top-level Artistic-1 file and a vendor/ subdir holding a BSD one.""" |
| 47 |
src = _TESTDATA / "one" |
| 48 |
shutil.copy(src / "art.txt", root / "art.txt") |
| 49 |
vendor = root / "vendor" |
| 50 |
vendor.mkdir() |
| 51 |
shutil.copy(src / "bsd.txt", vendor / "bsd.txt") |
| 52 |
|
| 53 |
|
| 54 |
def _write_tricky_tree(root: Path) -> None: |
| 55 |
"""Directory names holding characters that re.VERBOSE would eat. |
| 56 |
|
| 57 |
A space and a '#' are literal in an ordinary regex; under re.VERBOSE the |
| 58 |
first is stripped and the second starts a comment. tagtwo/ exists so a |
| 59 |
pattern truncated at the '#' ('^tag') is caught excluding too much. |
| 60 |
""" |
| 61 |
src = _TESTDATA / "one" |
| 62 |
for dirname in ("my docs", "tag#one", "tagtwo"): |
| 63 |
(root / dirname).mkdir() |
| 64 |
shutil.copy(src / "art.txt", root / "art.txt") |
| 65 |
shutil.copy(src / "bsd.txt", root / "my docs" / "bsd.txt") |
| 66 |
shutil.copy(src / "bsd.txt", root / "tag#one" / "bsd.txt") |
| 67 |
shutil.copy(src / "paren.txt", root / "tagtwo" / "paren.txt") |
| 68 |
|
| 69 |
|
| 70 |
_DEBIAN_COPYRIGHT = """\ |
| 71 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 72 |
Source: TODO |
| 73 |
|
| 74 |
Files: * |
| 75 |
License: Artistic-1 |
| 76 |
|
| 77 |
Files: debian/copyright |
| 78 |
License: Artistic-1 |
| 79 |
""" |
| 80 |
|
| 81 |
|
| 82 |
def _write_debianized_tree(root: Path, copyright_text: str = _DEBIAN_COPYRIGHT) -> None: |
| 83 |
"""A source file plus a debian/ dir holding copyright, changelog and rules.""" |
| 84 |
shutil.copy(_TESTDATA / "one" / "art.txt", root / "art.txt") |
| 85 |
debian = root / "debian" |
| 86 |
debian.mkdir() |
| 87 |
(debian / "copyright").write_text(copyright_text, encoding="utf-8") |
| 88 |
(debian / "changelog").write_text("pkg (1.0-1) unstable; urgency=low\n", "utf-8") |
| 89 |
(debian / "rules").write_text("#!/usr/bin/make -f\n", encoding="utf-8") |
| 90 |
|
| 91 |
|
| 92 |
def _write_nested_tree(root: Path) -> None: |
| 93 |
"""A top-level doc/, a nested src/doc/ and a .jpg, for the README's claims.""" |
| 94 |
src = _TESTDATA / "one" |
| 95 |
(root / "doc").mkdir() |
| 96 |
(root / "src" / "doc").mkdir(parents=True) |
| 97 |
shutil.copy(src / "art.txt", root / "doc" / "a.txt") |
| 98 |
shutil.copy(src / "bsd.txt", root / "src" / "doc" / "b.txt") |
| 99 |
shutil.copy(src / "paren.txt", root / "top.jpg") |
| 100 |
|
| 101 |
|
| 102 |
def _generate(root: Path, extra: list[str] | None = None) -> str: |
| 103 |
"""Render the copyright for `root` and return the generated text.""" |
| 104 |
with tempfile.TemporaryDirectory() as tmp: |
| 105 |
out = Path(tmp) / "copyright" |
| 106 |
options = process_options( |
| 107 |
[ |
| 108 |
"--no-progress", |
| 109 |
"--no-expand-archives", |
| 110 |
*(extra or []), |
| 111 |
"--root", |
| 112 |
str(root), |
| 113 |
"-o", |
| 114 |
str(out), |
| 115 |
] |
| 116 |
) |
| 117 |
_ = decopy.run_pipeline(options) |
| 118 |
return out.read_text(encoding="utf-8") |
| 119 |
|
| 120 |
|
| 121 |
def _explain(root: Path, extra: list[str] | None = None) -> tuple[int, str]: |
| 122 |
"""Run explain mode over `root`, returning (exit code, stdout).""" |
| 123 |
options = process_options( |
| 124 |
[ |
| 125 |
"--no-progress", |
| 126 |
"--no-expand-archives", |
| 127 |
"--explain", |
| 128 |
*(extra or []), |
| 129 |
"--root", |
| 130 |
str(root), |
| 131 |
] |
| 132 |
) |
| 133 |
out = io.StringIO() |
| 134 |
with contextlib.redirect_stdout(out): |
| 135 |
code = decopy.run_pipeline(options) |
| 136 |
return code, out.getvalue() |
| 137 |
|
| 138 |
|
| 139 |
class ExcludedFileTest(unittest.TestCase): |
| 140 |
"""An -X excluded file gets no entry in the generated copyright.""" |
| 141 |
|
| 142 |
def test_control_without_exclude(self): |
| 143 |
# Guards the fixture: without -X the file is listed under its own |
| 144 |
# license, so the assertions below test the exclusion, not a typo. |
| 145 |
with tempfile.TemporaryDirectory() as tmp: |
| 146 |
root = Path(tmp) |
| 147 |
_write_tree(root) |
| 148 |
text = _generate(root, _NO_GLOB) |
| 149 |
self.assertIn("vendor/bsd.txt", text) |
| 150 |
self.assertIn("BSD-3-clause", text) |
| 151 |
|
| 152 |
def test_excluded_file_is_not_listed(self): |
| 153 |
with tempfile.TemporaryDirectory() as tmp: |
| 154 |
root = Path(tmp) |
| 155 |
_write_tree(root) |
| 156 |
text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) |
| 157 |
self.assertNotIn("vendor/bsd.txt", text) |
| 158 |
|
| 159 |
def test_excluded_file_is_not_absorbed_by_another_group(self): |
| 160 |
# The regression: an excluded file has no license of its own, so it |
| 161 |
# used to inherit the top-level Artistic-1 and be listed with it. |
| 162 |
with tempfile.TemporaryDirectory() as tmp: |
| 163 |
root = Path(tmp) |
| 164 |
_write_tree(root) |
| 165 |
text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) |
| 166 |
self.assertIn("Files: art.txt\nLicense: Artistic-1", text) |
| 167 |
self.assertNotIn("vendor", text) |
| 168 |
|
| 169 |
def test_excluded_file_leaves_no_license_stanza(self): |
| 170 |
with tempfile.TemporaryDirectory() as tmp: |
| 171 |
root = Path(tmp) |
| 172 |
_write_tree(root) |
| 173 |
text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) |
| 174 |
self.assertNotIn("BSD-3-clause", text) |
| 175 |
|
| 176 |
def test_siblings_are_still_listed(self): |
| 177 |
with tempfile.TemporaryDirectory() as tmp: |
| 178 |
root = Path(tmp) |
| 179 |
_write_tree(root) |
| 180 |
text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) |
| 181 |
self.assertIn("art.txt", text) |
| 182 |
self.assertIn("Artistic-1", text) |
| 183 |
|
| 184 |
|
| 185 |
class ExcludedFileExplainTest(unittest.TestCase): |
| 186 |
"""Explain mode agrees with what generation would produce.""" |
| 187 |
|
| 188 |
def test_excluded_file_is_not_reported_as_missing(self): |
| 189 |
# A copyright that covers everything except the excluded file is up to |
| 190 |
# date: explain must not ask for a paragraph generation would not write. |
| 191 |
with tempfile.TemporaryDirectory() as tmp: |
| 192 |
root = Path(tmp) |
| 193 |
_write_tree(root) |
| 194 |
debian = root / "debian" |
| 195 |
debian.mkdir() |
| 196 |
(debian / "copyright").write_text( |
| 197 |
"Format: https://www.debian.org/doc/packaging-manuals" |
| 198 |
"/copyright-format/1.0/\n" |
| 199 |
"Source: TODO\n" |
| 200 |
"\n" |
| 201 |
"Files: art.txt\n" |
| 202 |
"License: Artistic-1\n", |
| 203 |
encoding="utf-8", |
| 204 |
) |
| 205 |
code, out = _explain(root, _EXCLUDE_VENDOR) |
| 206 |
self.assertEqual(out, "debian/copyright is up to date.\n") |
| 207 |
self.assertEqual(code, 0) |
| 208 |
|
| 209 |
|
| 210 |
class ExcludePatternSyntaxTest(unittest.TestCase): |
| 211 |
"""-X takes an ordinary regex, not a re.VERBOSE one.""" |
| 212 |
|
| 213 |
def test_pattern_with_a_space_is_matched_literally(self): |
| 214 |
# Under re.VERBOSE this pattern collapsed to ^mydocs/ and matched |
| 215 |
# nothing, so -X silently did nothing at all. |
| 216 |
with tempfile.TemporaryDirectory() as tmp: |
| 217 |
root = Path(tmp) |
| 218 |
_write_tricky_tree(root) |
| 219 |
text = _generate(root, [*_NO_GLOB, "-X", "^my docs/"]) |
| 220 |
self.assertNotIn("my docs/bsd.txt", text) |
| 221 |
self.assertIn("art.txt", text) |
| 222 |
|
| 223 |
def test_pattern_with_a_hash_is_matched_literally(self): |
| 224 |
# Under re.VERBOSE the '#' started a comment, leaving ^tag, which also |
| 225 |
# swallowed tagtwo/. |
| 226 |
with tempfile.TemporaryDirectory() as tmp: |
| 227 |
root = Path(tmp) |
| 228 |
_write_tricky_tree(root) |
| 229 |
text = _generate(root, [*_NO_GLOB, "-X", "^tag#one/"]) |
| 230 |
self.assertNotIn("tag#one/bsd.txt", text) |
| 231 |
self.assertIn("tagtwo/paren.txt", text) |
| 232 |
|
| 233 |
def test_default_pattern_excludes_nothing(self): |
| 234 |
# The default relies on VERBOSE stripping its own indentation; guard |
| 235 |
# the no--X case against a regression in how it is written. |
| 236 |
with tempfile.TemporaryDirectory() as tmp: |
| 237 |
root = Path(tmp) |
| 238 |
_write_tricky_tree(root) |
| 239 |
text = _generate(root, _NO_GLOB) |
| 240 |
for name in ("art.txt", "my docs/bsd.txt", "tag#one/bsd.txt"): |
| 241 |
with self.subTest(name=name): |
| 242 |
self.assertIn(name, text) |
| 243 |
|
| 244 |
|
| 245 |
class UnusedExcludePatternTest(unittest.TestCase): |
| 246 |
"""An -X that matches nothing says so instead of doing nothing quietly.""" |
| 247 |
|
| 248 |
def test_pattern_matching_nothing_warns(self): |
| 249 |
with tempfile.TemporaryDirectory() as tmp: |
| 250 |
root = Path(tmp) |
| 251 |
_write_tree(root) |
| 252 |
with self.assertLogs("decopy", level="WARNING") as caught: |
| 253 |
_ = _generate(root, [*_NO_GLOB, "-X", "^nosuchdir/"]) |
| 254 |
self.assertIn("matched no files", "\n".join(caught.output)) |
| 255 |
|
| 256 |
def test_pattern_matching_something_is_quiet(self): |
| 257 |
with tempfile.TemporaryDirectory() as tmp: |
| 258 |
root = Path(tmp) |
| 259 |
_write_tree(root) |
| 260 |
with self.assertNoLogs("decopy", level="WARNING"): |
| 261 |
_ = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) |
| 262 |
|
| 263 |
def test_default_is_quiet(self): |
| 264 |
# The warning must not fire on every ordinary run that passes no -X. |
| 265 |
with tempfile.TemporaryDirectory() as tmp: |
| 266 |
root = Path(tmp) |
| 267 |
_write_tree(root) |
| 268 |
with self.assertNoLogs("decopy", level="WARNING"): |
| 269 |
_ = _generate(root, _NO_GLOB) |
| 270 |
|
| 271 |
def test_match_outside_a_partial_scope_still_counts(self): |
| 272 |
# tag_listed_files walks the whole tree, so a pattern matching a file |
| 273 |
# the scope leaves out is still "used" and must not warn. |
| 274 |
with tempfile.TemporaryDirectory() as tmp: |
| 275 |
root = Path(tmp) |
| 276 |
_write_tree(root) |
| 277 |
with self.assertNoLogs("decopy", level="WARNING"): |
| 278 |
_ = _generate( |
| 279 |
root, |
| 280 |
[ |
| 281 |
*_NO_GLOB, |
| 282 |
*_EXCLUDE_VENDOR, |
| 283 |
"--mode", |
| 284 |
"partial", |
| 285 |
str(root / "art.txt"), |
| 286 |
], |
| 287 |
) |
| 288 |
|
| 289 |
|
| 290 |
class AlwaysExcludedFileTest(unittest.TestCase): |
| 291 |
"""debian/copyright and debian/changelog get no entry of their own. |
| 292 |
|
| 293 |
Both are excluded from parsing by exclude_special_re -- debian/copyright |
| 294 |
must be, it is the tool's own input -- so listing them would attribute them |
| 295 |
to whatever group they inherit from, and the manpage says they are excluded. |
| 296 |
""" |
| 297 |
|
| 298 |
def test_copyright_is_not_listed(self): |
| 299 |
with tempfile.TemporaryDirectory() as tmp: |
| 300 |
root = Path(tmp) |
| 301 |
_write_debianized_tree(root) |
| 302 |
text = _generate(root, _NO_GLOB) |
| 303 |
self.assertNotIn("debian/copyright", text) |
| 304 |
|
| 305 |
def test_changelog_is_not_listed(self): |
| 306 |
with tempfile.TemporaryDirectory() as tmp: |
| 307 |
root = Path(tmp) |
| 308 |
_write_debianized_tree(root) |
| 309 |
text = _generate(root, _NO_GLOB) |
| 310 |
self.assertNotIn("debian/changelog", text) |
| 311 |
|
| 312 |
def test_other_packaging_files_are_still_listed(self): |
| 313 |
with tempfile.TemporaryDirectory() as tmp: |
| 314 |
root = Path(tmp) |
| 315 |
_write_debianized_tree(root) |
| 316 |
text = _generate(root, _NO_GLOB) |
| 317 |
self.assertIn("debian/rules", text) |
| 318 |
|
| 319 |
def test_existing_entry_is_dropped(self): |
| 320 |
# The stored copyright names debian/copyright in a paragraph of its |
| 321 |
# own; regenerating must not carry that entry over. |
| 322 |
with tempfile.TemporaryDirectory() as tmp: |
| 323 |
root = Path(tmp) |
| 324 |
_write_debianized_tree(root) |
| 325 |
text = _generate(root, _NO_GLOB) |
| 326 |
self.assertNotIn("Files: debian/copyright", text) |
| 327 |
|
| 328 |
def test_glob_output_is_unchanged(self): |
| 329 |
# The default glob mode covered both files under debian/* already, so |
| 330 |
# dropping their entries must not disturb that paragraph. |
| 331 |
with tempfile.TemporaryDirectory() as tmp: |
| 332 |
root = Path(tmp) |
| 333 |
_write_debianized_tree(root) |
| 334 |
text = _generate(root) |
| 335 |
self.assertIn("Files: debian/*", text) |
| 336 |
|
| 337 |
|
| 338 |
class DocumentedSemanticsTest(unittest.TestCase): |
| 339 |
"""The behaviours the -X manpage entry promises (README.md, decopy(1)).""" |
| 340 |
|
| 341 |
def test_pattern_is_anchored_at_the_start_of_the_path(self): |
| 342 |
# "-X 'doc/' excludes everything under a top-level doc/ but nothing |
| 343 |
# under src/doc/". |
| 344 |
with tempfile.TemporaryDirectory() as tmp: |
| 345 |
root = Path(tmp) |
| 346 |
_write_nested_tree(root) |
| 347 |
text = _generate(root, [*_NO_GLOB, "-X", "doc/"]) |
| 348 |
self.assertNotIn("doc/a.txt", text) |
| 349 |
self.assertIn("src/doc/b.txt", text) |
| 350 |
|
| 351 |
def test_extension_match_needs_a_leading_wildcard(self): |
| 352 |
# "matching by extension needs a leading .*, as in -X '.*\.jpg$'". |
| 353 |
with tempfile.TemporaryDirectory() as tmp: |
| 354 |
root = Path(tmp) |
| 355 |
_write_nested_tree(root) |
| 356 |
text = _generate(root, [*_NO_GLOB, "-X", r".*\.jpg$"]) |
| 357 |
self.assertNotIn("top.jpg", text) |
| 358 |
|
| 359 |
def test_unanchored_extension_match_warns(self): |
| 360 |
# The trap the sentence above warns about is itself reported, so the |
| 361 |
# reader who skips it still finds out. |
| 362 |
with tempfile.TemporaryDirectory() as tmp: |
| 363 |
root = Path(tmp) |
| 364 |
_write_nested_tree(root) |
| 365 |
with self.assertLogs("decopy", level="WARNING"): |
| 366 |
text = _generate(root, [*_NO_GLOB, "-X", r"\.jpg$"]) |
| 367 |
self.assertIn("top.jpg", text) |
| 368 |
|
| 369 |
def test_pattern_is_case_insensitive(self): |
| 370 |
# "an ordinary, case-insensitive regular expression". |
| 371 |
with tempfile.TemporaryDirectory() as tmp: |
| 372 |
root = Path(tmp) |
| 373 |
_write_nested_tree(root) |
| 374 |
text = _generate(root, [*_NO_GLOB, "-X", "^DOC/"]) |
| 375 |
self.assertNotIn("doc/a.txt", text) |
| 376 |
|
| 377 |
|
| 378 |
if __name__ == "__main__": |
| 379 |
unittest.main() |
| 380 |
|
| 381 |
|
| 382 |
class RepeatedExcludePatternTest(unittest.TestCase): |
| 383 |
"""-X is repeatable: every pattern given applies, not just the last one.""" |
| 384 |
|
| 385 |
def test_both_patterns_are_applied(self): |
| 386 |
with tempfile.TemporaryDirectory() as tmp: |
| 387 |
root = Path(tmp) |
| 388 |
_write_nested_tree(root) |
| 389 |
text = _generate(root, [*_NO_GLOB, "-X", "^doc/", "-X", "^src/"]) |
| 390 |
self.assertNotIn("doc/a.txt", text) |
| 391 |
self.assertNotIn("src/doc/b.txt", text) |
| 392 |
self.assertIn("top.jpg", text) |
| 393 |
|
| 394 |
def test_unused_pattern_names_itself(self): |
| 395 |
# Only the pattern that matched nothing is reported, so a mix of a used |
| 396 |
# and an unused pattern still points at the right one. |
| 397 |
with tempfile.TemporaryDirectory() as tmp: |
| 398 |
root = Path(tmp) |
| 399 |
_write_tree(root) |
| 400 |
with self.assertLogs("decopy", level="WARNING") as caught: |
| 401 |
text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR, "-X", "^nosuch/"]) |
| 402 |
logged = "\n".join(caught.output) |
| 403 |
self.assertIn("^nosuch/", logged) |
| 404 |
self.assertNotIn("^vendor/", logged) |
| 405 |
# The used pattern kept excluding, so it was not dropped to make room. |
| 406 |
self.assertNotIn("vendor/bsd.txt", text) |
| 407 |
|
| 408 |
def test_all_patterns_used_is_quiet(self): |
| 409 |
with tempfile.TemporaryDirectory() as tmp: |
| 410 |
root = Path(tmp) |
| 411 |
_write_nested_tree(root) |
| 412 |
with self.assertNoLogs("decopy", level="WARNING"): |
| 413 |
_ = _generate(root, [*_NO_GLOB, "-X", "^doc/", "-X", "^src/"]) |