Debian Codegraph @

tests/test_copyright.py

raw
1 # Copyright (C) 2025 Siemens
2 #
3 # SPDX-License-Identifier: MIT
4
5 from debsbom.apt.copyright import Copyright, UnknownLicenseError
6 from pathlib import Path
7 import pytest
8
9
10 def test_copyright():
11 cr = Copyright(Path("tests/data/copyright"))
12
13 licenses = list(map(lambda lic: lic.synopsis, cr.licenses()))
14 assert len(licenses) == 4
15 assert "GPL-2+" in licenses
16 assert "GPL-2" in licenses
17 assert "BSD-3-clause" in licenses
18 assert "Expat" in licenses
19
20 spdx_licenses = list(map(lambda lic: str(lic), cr.spdx_license_expressions()))
21 assert len(spdx_licenses) == 4
22 assert "GPL-2.0-or-later" in spdx_licenses
23 assert "GPL-2.0-only" in spdx_licenses
24 assert "BSD-3-Clause" in spdx_licenses
25 assert "MIT" in spdx_licenses
26
27
28 def test_non_spdx_copyright():
29 cr = Copyright(Path("tests/data/non-spdx-copyright"))
30
31 licenses = list(map(lambda lic: lic.synopsis, cr.licenses()))
32 # we have some non-SPDX licenses, make sure they are in there
33 assert "OPL-1+" in licenses
34 assert "Vim-Regexp" in licenses
35 assert "Compaq" in licenses
36 assert "XPM" in licenses
37 assert "EDL-1" in licenses
38
39 with pytest.raises(UnknownLicenseError):
40 list(cr.spdx_license_expressions())
41
42
43 def test_spdx_lic_expressions():
44 cr = Copyright(Path("tests/data/lic-expr-copyright"))
45
46 spdx_licenses = set(map(lambda lic: str(lic), cr.spdx_license_expressions()))
47 assert "BSD-3-Clause OR GPL-2.0-or-later" in spdx_licenses
48 assert "BSD-3-Clause OR GPL-2.0-only" in spdx_licenses
49
50
51 def test_spdx_license_exceptions():
52 cr = Copyright(Path("tests/data/exception-copyright"))
53
54 spdx_licenses = set(map(str, cr.spdx_license_expressions()))
55 assert spdx_licenses == {
56 "GPL-3.0-or-later WITH Bison-exception-2.2",
57 "GPL-2.0-only WITH Linux-syscall-note",
58 "GPL-3.0-or-later WITH Texinfo-exception",
59 "GPL-3.0-or-later WITH Autoconf-exception-macro",
60 "BSD-3-Clause WITH PCRE2-exception",
61 "MIT",
62 "GPL-3.0-or-later WITH Bison-exception-2.2 OR MIT",
63 "MIT OR (GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause)",
64 }
65
66
67 def test_ambiguous_license_exception_rejects_all_licenses():
68 cr = Copyright(Path("tests/data/unknown-exception-copyright"))
69
70 with pytest.raises(UnknownLicenseError):
71 list(cr.spdx_license_expressions())
72
73
74 def test_license_exception_substring_rejects_all_licenses():
75 cr = Copyright(Path("tests/data/exception-substring-copyright"))
76
77 with pytest.raises(UnknownLicenseError):
78 list(cr.spdx_license_expressions())
79
80
81 def test_public_domain_with_supported_exception_rejects_all_licenses():
82 cr = Copyright(Path("tests/data/exception-public-domain-copyright"))
83
84 with pytest.raises(UnknownLicenseError):
85 list(cr.spdx_license_expressions())
86
87
88 def test_late_parse_error_yields_no_licenses():
89 """A late parse error must not leak licenses parsed before the error."""
90 cr = Copyright(Path("tests/data/late-parse-error-copyright"))
91
92 assert [lic.synopsis for lic in cr.licenses()] == []
93
94
95 def test_late_parse_error_rejects_all_licenses():
96 """A malformed copyright file must not emit a partial SPDX expression."""
97 cr = Copyright(Path("tests/data/late-parse-error-copyright"))
98
99 with pytest.raises(UnknownLicenseError):
100 list(cr.spdx_license_expressions())
101
102
103 def test_repaired_late_parse_error_emits_all_licenses():
104 """A valid equivalent must still expose every declared license."""
105 cr = Copyright(Path("tests/data/late-parse-ok-copyright"))
106
107 assert [lic.synopsis for lic in cr.licenses()] == ["Expat", "Apache-2.0", "GPL-2+"]
108 assert list(map(str, cr.spdx_license_expressions())) == [
109 "MIT",
110 "Apache-2.0",
111 "GPL-2.0-or-later",
112 ]
113
114
115 def test_copyright_is_parsed_as_utf8_regardless_of_locale(c_ctype_locale):
116 """DEP-5 mandates UTF-8, so parsing must not depend on the process locale."""
117 copyright = Copyright(Path("tests/data/utf8-copyright"))
118
119 assert [license.synopsis for license in copyright.licenses()] == ["BSD-3-clause"]