#!/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. """License-notice files are not listed in the generated copyright. A file that only carries a license text (COPYING, LICENSE, ...) needs no entry of its own: lintian reports license-file-listed-in-debian-copyright for it and excuses it from needing coverage. decopy drops those entries by default; --list-license-files restores the old behaviour (Debian bug #962309). """ 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 from decopy.fstree import nodes _TESTDATA = Path(__file__).parent / "testdata" _NO_CATCHALL_COPYRIGHT = """\ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Source: TODO Files: art.txt License: Artistic-1 Files: bsd.txt License: BSD-3-clause Files: paren.txt License: GPL-3+ """ _COPYING_LISTED_COPYRIGHT = """\ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Source: TODO Files: * License: GPL-2+ Files: COPYING License: GPL-2+ """ _STANZA_COPYRIGHT = """\ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Source: TODO Files: * License: Artistic-1 Files: COPYING License: GPL-2+ License: GPL-2+ Old stored text for the GPL-2+ stanza. """ def _write_tree(root: Path, copyright_text: str | None = None) -> None: """Copy the known-license fixtures plus COPYING into `root`.""" src = _TESTDATA / "one" for name in ("COPYING", "art.txt", "bsd.txt", "paren.txt"): shutil.copy(src / name, root / name) if copyright_text is not None: debian = root / "debian" debian.mkdir() (debian / "copyright").write_text(copyright_text, encoding="utf-8") 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 LicenseNoticeNameTest(unittest.TestCase): """Which names count as a license notice file.""" def test_notice_names(self): names = ( "COPYING", "COPYING.GPL", "COPYING.txt", "COPYRIGHT", "LICENSE", "LICENSE.md", ) for name in names: with self.subTest(name=name): self.assertTrue(nodes.FileInfo(None, name).is_license_notice()) def test_other_names(self): names = ( "copying", "license.txt", "COPYRIGHT.txt", "README", "AUTHORS", "COPY", "NOTICE", ) for name in names: with self.subTest(name=name): self.assertFalse(nodes.FileInfo(None, name).is_license_notice()) def test_directory_is_never_a_notice(self): self.assertFalse(nodes.DirInfo(None, "LICENSES").is_license_notice()) class GeneratedOutputTest(unittest.TestCase): """The generated copyright drops the license-notice entries.""" def test_notice_file_is_not_listed(self): text = _generate(_TESTDATA / "one") self.assertNotIn("Files: COPYING", text) def test_flag_lists_the_notice_file(self): text = _generate(_TESTDATA / "one", ["--list-license-files"]) self.assertIn("Files: COPYING", text) def test_notice_licenses_still_reach_the_output(self): # Dropping the entry must not drop what the notice file taught the tree: # the catch-all keeps the inherited license and names its source. text = _generate(_TESTDATA / "one") self.assertIn("Files: *\nLicense: GPL-2+", text) self.assertIn( "No explicit license found, using license(s) from:\n COPYING", text ) def test_existing_entry_is_dropped(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root, _COPYING_LISTED_COPYRIGHT) text = _generate(root) self.assertNotIn("Files: COPYING", text) def test_existing_entry_is_kept_with_the_flag(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root, _COPYING_LISTED_COPYRIGHT) text = _generate(root, ["--list-license-files"]) self.assertIn("Files: COPYING", text) class LicenseStanzaTest(unittest.TestCase): """A dropped notice file leaves no orphan License stanza behind.""" # The fixture tree is COPYING (GPL-2+) plus three files that each carry # their own license, so nothing inherits GPL-2+: once COPYING loses its # entry, no Files paragraph names that license any more. def test_unused_notice_license_has_no_stanza(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) text = _generate(root) self.assertNotIn("GPL-2+", text) def test_flag_keeps_the_stanza(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root) text = _generate(root, ["--list-license-files"]) self.assertIn("License: GPL-2+", text) def test_stored_stanza_of_a_dropped_entry_goes_too(self): # Updating a copyright that already carries the stanza text: the text # is stored while reading, and must not reach the output either. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) / "src" root.mkdir() _write_tree(root) old = Path(tmp) / "copyright" old.write_text(_STANZA_COPYRIGHT, encoding="utf-8") text = _generate(root, ["--copyright-file", str(old)]) self.assertNotIn("GPL-2+", text) self.assertNotIn("Old stored text", text) class ExplainModeTest(unittest.TestCase): """Explain mode agrees with what generation would produce.""" def test_uncovered_notice_file_is_not_stale(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root, _NO_CATCHALL_COPYRIGHT) code, out = _explain(root) self.assertEqual(out, "debian/copyright is up to date.\n") self.assertEqual(code, 0) def test_uncovered_notice_file_is_reported_with_the_flag(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root, _NO_CATCHALL_COPYRIGHT) code, out = _explain(root, ["--list-license-files"]) self.assertEqual(code, 1) self.assertIn("COPYING", out) def test_listed_notice_entry_is_reported(self): # The entry generation would drop has to show up in the report, or # explain would call an out-of-date file clean. with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) _write_tree(root, _COPYING_LISTED_COPYRIGHT) code, out = _explain(root) self.assertEqual(code, 1) self.assertIn("COPYING", out) if __name__ == "__main__": unittest.main()