Debian Codegraph @

tests/metadata_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 """Embedded metadata fields to copyright notices (Debian bug #1022330)."""
18
19 import unittest
20
21 from decopy.fstree.metadata import metadata_notices, rdf_fields
22 from decopy.licensing.holders import parse_holders
23
24
25 class TestMetadataNotices(unittest.TestCase):
26 """metadata_notices() maps a field mapping to synthesised notice lines."""
27
28 @staticmethod
29 def _holders(fields: dict[str, str]) -> list[str]:
30 return [str(h) for h in parse_holders(metadata_notices(fields))]
31
32 def test_creator_becomes_a_yearless_notice(self):
33 fields = {"dc:creator": "Ramon Miranda"}
34 self.assertEqual(self._holders(fields), ["Ramon Miranda"])
35
36 def test_creator_keeps_an_email(self):
37 fields = {"Author": "Luca Ferretti <[email protected]>"}
38 self.assertEqual(self._holders(fields), ["Luca Ferretti <[email protected]>"])
39
40 def test_every_creator_field_is_read(self):
41 for field in ("dc:creator", "Author", "Artist", "Creator"):
42 with self.subTest(field=field):
43 holders = self._holders({field: "Jakub Steiner"})
44 self.assertEqual(holders, ["Jakub Steiner"])
45
46 def test_an_organisation_is_a_holder(self):
47 fields = {"Author": "GNOME Design Team"}
48 self.assertEqual(self._holders(fields), ["GNOME Design Team"])
49
50 def test_rights_is_emitted_raw_so_it_self_filters(self):
51 # A real notice passes; a licence string or placeholder does not.
52 self.assertEqual(
53 self._holders({"dc:rights": "Copyright 2007 Niko Kiirala"}),
54 ["2007, Niko Kiirala"],
55 )
56 licencey = ("see the license tab", "CC BY-SA 4.0", "GPL version 2")
57 for value in licencey:
58 with self.subTest(value=value):
59 self.assertEqual(self._holders({"dc:rights": value}), [])
60
61 def test_unknown_fields_are_ignored(self):
62 fields = {"dc:date": "13 Jan 2019", "dc:publisher": "Inkscape"}
63 self.assertEqual(metadata_notices(fields), "")
64
65 def test_authoring_tools_are_rejected(self):
66 tools = ("Inkscape", "Sodipodi", "Matplotlib v3.5.1, https://matplotlib.org/")
67 for tool in tools:
68 with self.subTest(tool=tool):
69 self.assertEqual(self._holders({"Author": tool}), [])
70
71 def test_template_markers_are_rejected(self):
72 for junk in ("$committer", "%USERNAME%", "{{author}}"):
73 with self.subTest(junk=junk):
74 self.assertEqual(self._holders({"dc:creator": junk}), [])
75
76 def test_machine_artefacts_are_rejected(self):
77 for junk in ("HASH(0x86d2470)", "x-default", "www.tonytextures.com"):
78 with self.subTest(junk=junk):
79 self.assertEqual(self._holders({"Artist": junk}), [])
80
81 def test_a_value_that_would_fabricate_a_year_is_rejected(self):
82 # "(doofus-01)" parses as the year 2001 plus a truncated name; no year
83 # is ever synthesised, so one appearing means the value is not a name.
84 for junk in ("(doofus-01)", "doofus-01"):
85 with self.subTest(junk=junk):
86 self.assertEqual(self._holders({"Creator": junk}), [])
87
88 def test_bracketed_handles_are_kept(self):
89 self.assertEqual(self._holders({"dc:creator": "[maxwxyz]"}), ["maxwxyz"])
90
91 def test_no_fields_gives_no_notices(self):
92 self.assertEqual(metadata_notices({}), "")
93
94 def test_repeated_names_are_emitted_once(self):
95 fields = {"Author": "Jakub Steiner", "Creator": "Jakub Steiner"}
96 self.assertEqual(self._holders(fields), ["Jakub Steiner"])
97
98
99 class TestRdfFields(unittest.TestCase):
100 """rdf_fields() reads the RDF metadata block Inkscape and friends write."""
101
102 @staticmethod
103 def _svg(work: str) -> str:
104 return (
105 '<?xml version="1.0"?>\n'
106 '<svg xmlns:dc="http://purl.org/dc/elements/1.1/"\n'
107 ' xmlns:cc="http://creativecommons.org/ns#"\n'
108 ' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">\n'
109 ' <metadata><rdf:RDF><cc:Work rdf:about="">\n'
110 " <dc:format>image/svg+xml</dc:format>\n"
111 f" {work}\n"
112 " </cc:Work></rdf:RDF></metadata>\n"
113 ' <rect width="1" height="1"/>\n'
114 "</svg>\n"
115 )
116
117 @staticmethod
118 def _agent(field: str, name: str) -> str:
119 return (
120 f"<dc:{field}><cc:Agent><dc:title>{name}</dc:title>"
121 f"</cc:Agent></dc:{field}>"
122 )
123
124 def test_creator(self):
125 svg = self._svg(self._agent("creator", "GNOME Design Team"))
126 self.assertEqual(rdf_fields(svg), {"dc:creator": "GNOME Design Team"})
127
128 def test_rights(self):
129 svg = self._svg(self._agent("rights", "Copyright 2007 Niko Kiirala"))
130 self.assertEqual(rdf_fields(svg), {"dc:rights": "Copyright 2007 Niko Kiirala"})
131
132 def test_both_fields(self):
133 svg = self._svg(
134 self._agent("creator", "Andy Fitzsimon")
135 + self._agent("rights", "Andrew Michael Fitzsimon"),
136 )
137 self.assertEqual(
138 rdf_fields(svg),
139 {"dc:creator": "Andy Fitzsimon", "dc:rights": "Andrew Michael Fitzsimon"},
140 )
141
142 def test_entities_are_unescaped(self):
143 svg = self._svg(
144 self._agent("creator", "Luca Ferretti &lt;[email protected]&gt;"),
145 )
146 self.assertEqual(
147 rdf_fields(svg),
148 {"dc:creator": "Luca Ferretti <[email protected]>"},
149 )
150
151 def test_truncated_entity_is_read_as_written(self):
152 # An upstream value that never closed its angle bracket.
153 svg = self._svg(self._agent("creator", "Luca Ferretti &lt;[email protected]"))
154 self.assertEqual(
155 rdf_fields(svg),
156 {"dc:creator": "Luca Ferretti <[email protected]"},
157 )
158
159 def test_the_works_own_title_is_not_a_creator(self):
160 svg = self._svg("<dc:title>Application Exit</dc:title>")
161 self.assertEqual(rdf_fields(svg), {})
162
163 def test_wrapped_attributes(self):
164 # Inkscape splits an element name from its attributes across lines.
165 svg = self._svg(
166 '<dc:creator>\n <cc:Agent\n rdf:about="">\n'
167 " <dc:title>Jakub Steiner</dc:title>\n"
168 " </cc:Agent>\n </dc:creator>",
169 )
170 self.assertEqual(rdf_fields(svg), {"dc:creator": "Jakub Steiner"})
171
172 def test_direct_text_content(self):
173 # Dublin Core allows the value without a cc:Agent wrapper.
174 svg = self._svg("<dc:creator>Ramon Miranda</dc:creator>")
175 self.assertEqual(rdf_fields(svg), {"dc:creator": "Ramon Miranda"})
176
177 def test_empty_value_is_dropped(self):
178 svg = self._svg(self._agent("creator", "") + self._agent("rights", " "))
179 self.assertEqual(rdf_fields(svg), {})
180
181 def test_no_metadata(self):
182 self.assertEqual(rdf_fields('<svg><rect width="1"/></svg>'), {})
183
184 def test_malformed_xml_does_not_raise(self):
185 broken = "<svg><metadata><rdf:RDF><cc:Work><dc:creator><cc:Agent>"
186 self.assertEqual(rdf_fields(broken), {})
187
188 def test_empty_text(self):
189 self.assertEqual(rdf_fields(""), {})
190
191
192 if __name__ == "__main__":
193 unittest.main()