#!/usr/bin/env python3 # Copyright: 2026, Maximiliano Curia # # License: ISC # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # . # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # PERFORMANCE OF THIS SOFTWARE. """Files excluded with --exclude/-X are not listed in the generated copyright. -X used to gate parsing only, so an excluded file stayed in the tree with no license of its own and inherited the nearest enclosing group: the output listed it under a license that was never detected for it (Debian bug #997814). """ from __future__ import annotations import contextlib import io import shutil import tempfile import unittest from pathlib import Path from decopy import decopy from decopy.cmdoptions import process_options _TESTDATA = Path(__file__).parent / "testdata" # art.txt at the top level and bsd.txt under vendor/ carry different licenses, # so an excluded vendor/bsd.txt that gets absorbed by the Artistic-1 group is # visibly misattributed rather than merely present. _NO_GLOB = ["--no-glob"] _EXCLUDE_VENDOR = ["-X", "^vendor/"] def _write_tree(root: Path) -> None: """A top-level Artistic-1 file and a vendor/ subdir holding a BSD one.""" src = _TESTDATA / "one" shutil.copy(src / "art.txt", root / "art.txt") vendor = root / "vendor" vendor.mkdir() shutil.copy(src / "bsd.txt", vendor / "bsd.txt") def _write_tricky_tree(root: Path) -> None: """Directory names holding characters that re.VERBOSE would eat. A space and a '#' are literal in an ordinary regex; under re.VERBOSE the first is stripped and the second starts a comment. tagtwo/ exists so a pattern truncated at the '#' ('^tag') is caught excluding too much. """ src = _TESTDATA / "one" for dirname in ("my docs", "tag#one", "tagtwo"): (root / dirname).mkdir() shutil.copy(src / "art.txt", root / "art.txt") shutil.copy(src / "bsd.txt", root / "my docs" / "bsd.txt") shutil.copy(src / "bsd.txt", root / "tag#one" / "bsd.txt") shutil.copy(src / "paren.txt", root / "tagtwo" / "paren.txt") _DEBIAN_COPYRIGHT = """\ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Source: TODO Files: * License: Artistic-1 Files: debian/copyright License: Artistic-1 """ def _write_debianized_tree(root: Path, copyright_text: str = _DEBIAN_COPYRIGHT) -> None: """A source file plus a debian/ dir holding copyright, changelog and rules.""" shutil.copy(_TESTDATA / "one" / "art.txt", root / "art.txt") debian = root / "debian" debian.mkdir() (debian / "copyright").write_text(copyright_text, encoding="utf-8") (debian / "changelog").write_text("pkg (1.0-1) unstable; urgency=low\n", "utf-8") (debian / "rules").write_text("#!/usr/bin/make -f\n", encoding="utf-8") def _write_nested_tree(root: Path) -> None: """A top-level doc/, a nested src/doc/ and a .jpg, for the README's claims.""" src = _TESTDATA / "one" (root / "doc").mkdir() (root / "src" / "doc").mkdir(parents=True) shutil.copy(src / "art.txt", root / "doc" / "a.txt") shutil.copy(src / "bsd.txt", root / "src" / "doc" / "b.txt") shutil.copy(src / "paren.txt", root / "top.jpg") def _generate(root: Path, extra: list[str] | None = None) -> str: """Render the copyright for `root` and return the generated text.""" with tempfile.TemporaryDirectory() as tmp: out = Path(tmp) / "copyright" options = process_options( [ "--no-progress", "--no-expand-archives", *(extra or []), "--root", str(root), "-o", str(out), ] ) _ = decopy.run_pipeline(options) return out.read_text(encoding="utf-8") def _explain(root: Path, extra: list[str] | None = None) -> tuple[int, str]: """Run explain mode over `root`, returning (exit code, stdout).""" options = process_options( [ "--no-progress", "--no-expand-archives", "--explain", *(extra or []), "--root", str(root), ] ) out = io.StringIO() with contextlib.redirect_stdout(out): code = decopy.run_pipeline(options) return code, out.getvalue() class ExcludedFileTest(unittest.TestCase): """An -X excluded file gets no entry in the generated copyright.""" def test_control_without_exclude(self): # Guards the fixture: without -X the file is listed under its own # license, so the assertions below test the exclusion, not a typo. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) text = _generate(root, _NO_GLOB) self.assertIn("vendor/bsd.txt", text) self.assertIn("BSD-3-clause", text) def test_excluded_file_is_not_listed(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) self.assertNotIn("vendor/bsd.txt", text) def test_excluded_file_is_not_absorbed_by_another_group(self): # The regression: an excluded file has no license of its own, so it # used to inherit the top-level Artistic-1 and be listed with it. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) self.assertIn("Files: art.txt\nLicense: Artistic-1", text) self.assertNotIn("vendor", text) def test_excluded_file_leaves_no_license_stanza(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) self.assertNotIn("BSD-3-clause", text) def test_siblings_are_still_listed(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) self.assertIn("art.txt", text) self.assertIn("Artistic-1", text) class ExcludedFileExplainTest(unittest.TestCase): """Explain mode agrees with what generation would produce.""" def test_excluded_file_is_not_reported_as_missing(self): # A copyright that covers everything except the excluded file is up to # date: explain must not ask for a paragraph generation would not write. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) debian = root / "debian" debian.mkdir() (debian / "copyright").write_text( "Format: https://www.debian.org/doc/packaging-manuals" "/copyright-format/1.0/\n" "Source: TODO\n" "\n" "Files: art.txt\n" "License: Artistic-1\n", encoding="utf-8", ) code, out = _explain(root, _EXCLUDE_VENDOR) self.assertEqual(out, "debian/copyright is up to date.\n") self.assertEqual(code, 0) class ExcludePatternSyntaxTest(unittest.TestCase): """-X takes an ordinary regex, not a re.VERBOSE one.""" def test_pattern_with_a_space_is_matched_literally(self): # Under re.VERBOSE this pattern collapsed to ^mydocs/ and matched # nothing, so -X silently did nothing at all. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tricky_tree(root) text = _generate(root, [*_NO_GLOB, "-X", "^my docs/"]) self.assertNotIn("my docs/bsd.txt", text) self.assertIn("art.txt", text) def test_pattern_with_a_hash_is_matched_literally(self): # Under re.VERBOSE the '#' started a comment, leaving ^tag, which also # swallowed tagtwo/. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tricky_tree(root) text = _generate(root, [*_NO_GLOB, "-X", "^tag#one/"]) self.assertNotIn("tag#one/bsd.txt", text) self.assertIn("tagtwo/paren.txt", text) def test_default_pattern_excludes_nothing(self): # The default relies on VERBOSE stripping its own indentation; guard # the no--X case against a regression in how it is written. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tricky_tree(root) text = _generate(root, _NO_GLOB) for name in ("art.txt", "my docs/bsd.txt", "tag#one/bsd.txt"): with self.subTest(name=name): self.assertIn(name, text) class UnusedExcludePatternTest(unittest.TestCase): """An -X that matches nothing says so instead of doing nothing quietly.""" def test_pattern_matching_nothing_warns(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) with self.assertLogs("decopy", level="WARNING") as caught: _ = _generate(root, [*_NO_GLOB, "-X", "^nosuchdir/"]) self.assertIn("matched no files", "\n".join(caught.output)) def test_pattern_matching_something_is_quiet(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) with self.assertNoLogs("decopy", level="WARNING"): _ = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR]) def test_default_is_quiet(self): # The warning must not fire on every ordinary run that passes no -X. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) with self.assertNoLogs("decopy", level="WARNING"): _ = _generate(root, _NO_GLOB) def test_match_outside_a_partial_scope_still_counts(self): # tag_listed_files walks the whole tree, so a pattern matching a file # the scope leaves out is still "used" and must not warn. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) with self.assertNoLogs("decopy", level="WARNING"): _ = _generate( root, [ *_NO_GLOB, *_EXCLUDE_VENDOR, "--mode", "partial", str(root / "art.txt"), ], ) class AlwaysExcludedFileTest(unittest.TestCase): """debian/copyright and debian/changelog get no entry of their own. Both are excluded from parsing by exclude_special_re -- debian/copyright must be, it is the tool's own input -- so listing them would attribute them to whatever group they inherit from, and the manpage says they are excluded. """ def test_copyright_is_not_listed(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_debianized_tree(root) text = _generate(root, _NO_GLOB) self.assertNotIn("debian/copyright", text) def test_changelog_is_not_listed(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_debianized_tree(root) text = _generate(root, _NO_GLOB) self.assertNotIn("debian/changelog", text) def test_other_packaging_files_are_still_listed(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_debianized_tree(root) text = _generate(root, _NO_GLOB) self.assertIn("debian/rules", text) def test_existing_entry_is_dropped(self): # The stored copyright names debian/copyright in a paragraph of its # own; regenerating must not carry that entry over. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_debianized_tree(root) text = _generate(root, _NO_GLOB) self.assertNotIn("Files: debian/copyright", text) def test_glob_output_is_unchanged(self): # The default glob mode covered both files under debian/* already, so # dropping their entries must not disturb that paragraph. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_debianized_tree(root) text = _generate(root) self.assertIn("Files: debian/*", text) class DocumentedSemanticsTest(unittest.TestCase): """The behaviours the -X manpage entry promises (README.md, decopy(1)).""" def test_pattern_is_anchored_at_the_start_of_the_path(self): # "-X 'doc/' excludes everything under a top-level doc/ but nothing # under src/doc/". with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_nested_tree(root) text = _generate(root, [*_NO_GLOB, "-X", "doc/"]) self.assertNotIn("doc/a.txt", text) self.assertIn("src/doc/b.txt", text) def test_extension_match_needs_a_leading_wildcard(self): # "matching by extension needs a leading .*, as in -X '.*\.jpg$'". with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_nested_tree(root) text = _generate(root, [*_NO_GLOB, "-X", r".*\.jpg$"]) self.assertNotIn("top.jpg", text) def test_unanchored_extension_match_warns(self): # The trap the sentence above warns about is itself reported, so the # reader who skips it still finds out. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_nested_tree(root) with self.assertLogs("decopy", level="WARNING"): text = _generate(root, [*_NO_GLOB, "-X", r"\.jpg$"]) self.assertIn("top.jpg", text) def test_pattern_is_case_insensitive(self): # "an ordinary, case-insensitive regular expression". with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_nested_tree(root) text = _generate(root, [*_NO_GLOB, "-X", "^DOC/"]) self.assertNotIn("doc/a.txt", text) if __name__ == "__main__": unittest.main() class RepeatedExcludePatternTest(unittest.TestCase): """-X is repeatable: every pattern given applies, not just the last one.""" def test_both_patterns_are_applied(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_nested_tree(root) text = _generate(root, [*_NO_GLOB, "-X", "^doc/", "-X", "^src/"]) self.assertNotIn("doc/a.txt", text) self.assertNotIn("src/doc/b.txt", text) self.assertIn("top.jpg", text) def test_unused_pattern_names_itself(self): # Only the pattern that matched nothing is reported, so a mix of a used # and an unused pattern still points at the right one. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) with self.assertLogs("decopy", level="WARNING") as caught: text = _generate(root, [*_NO_GLOB, *_EXCLUDE_VENDOR, "-X", "^nosuch/"]) logged = "\n".join(caught.output) self.assertIn("^nosuch/", logged) self.assertNotIn("^vendor/", logged) # The used pattern kept excluding, so it was not dropped to make room. self.assertNotIn("vendor/bsd.txt", text) def test_all_patterns_used_is_quiet(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_nested_tree(root) with self.assertNoLogs("decopy", level="WARNING"): _ = _generate(root, [*_NO_GLOB, "-X", "^doc/", "-X", "^src/"])