Debian Codegraph @

tests/holders_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 """Tests for the holder-parsing helpers in decopy.licensing.holders."""
18
19 import unittest
20
21 from decopy.common._regex import rex
22 from decopy.common.core_types import CopyrightHolder, YearRange
23 from decopy.licensing import holders
24
25
26 def _triples(holder_list: list[CopyrightHolder]) -> list[tuple[str, str, str]]:
27 """(name, email, years-as-string) for each holder, for readable asserts."""
28 return [(h.name, h.email, str(h.years)) for h in holder_list]
29
30
31 class TestReSplitIter(unittest.TestCase):
32 """`_re_split_iter` must split on the pattern it is given."""
33
34 def test_splits_on_the_passed_pattern(self):
35 """The separators are consumed, the between-parts are yielded."""
36 pattern = rex.compile(r";")
37 self.assertEqual(
38 list(holders._re_split_iter(pattern, "a;b;c")),
39 ["a", "b", "c"],
40 )
41
42 def test_no_match_yields_whole_text(self):
43 """With no separator present the entire text comes back as one part."""
44 pattern = rex.compile(r";")
45 self.assertEqual(
46 list(holders._re_split_iter(pattern, "abc")),
47 ["abc"],
48 )
49
50 def test_uses_the_argument_not_a_hardcoded_global(self):
51 """A pattern that differs from COPYRIGHT_INDICATOR_RE drives the split.
52
53 Guards against the helper ignoring its parameter and always splitting on
54 the module-level copyright-indicator regex instead.
55 """
56 pattern = rex.compile(r"\|")
57 self.assertEqual(
58 list(holders._re_split_iter(pattern, "one|two")),
59 ["one", "two"],
60 )
61
62
63 class TestGetYear(unittest.TestCase):
64 """`_get_year` normalises short/2-digit years at fixed century pivots.
65
66 Pins the pivot values so the named constants that replace the inline magic
67 numbers cannot silently drift.
68 """
69
70 def test_single_digit_borrows_reference_decade(self):
71 # "1990-2" -> the trailing "2" resolves to 1992 via the ref year.
72 self.assertEqual(holders._get_year("2", ref=1990), 1992)
73
74 def test_two_digit_below_pivot_maps_to_2000s(self):
75 self.assertEqual(holders._get_year("49"), 2049)
76
77 def test_two_digit_at_pivot_maps_to_1900s(self):
78 self.assertEqual(holders._get_year("50"), 1950)
79 self.assertEqual(holders._get_year("99"), 1999)
80
81 def test_three_digit_is_not_a_plausible_year(self):
82 self.assertIsNone(holders._get_year("500"))
83
84 def test_four_digit_year_passes_through(self):
85 self.assertEqual(holders._get_year("2023"), 2023)
86
87
88 class TestCopyrightHoldersAdd(unittest.TestCase):
89 """CopyrightHolders.add merge-vs-store decision, per identifying signal."""
90
91 def test_same_email_merges_regardless_of_name(self):
92 collection = holders.CopyrightHolders()
93 collection.add(CopyrightHolder("Alice", "a@x"))
94 collection.add(CopyrightHolder("Alicia", "a@x"))
95 self.assertEqual(len(collection), 1)
96
97 def test_same_name_merges_and_adopts_missing_email(self):
98 collection = holders.CopyrightHolders()
99 collection.add(CopyrightHolder("Alice", ""))
100 collection.add(CopyrightHolder("Alice", "a@x"))
101 self.assertEqual(len(collection), 1)
102 self.assertIn("a@x", collection.by_email)
103
104 def test_same_name_different_emails_kept_separate(self):
105 collection = holders.CopyrightHolders()
106 collection.add(CopyrightHolder("Alice", "a1@x"))
107 collection.add(CopyrightHolder("Alice", "a2@x"))
108 self.assertEqual(len(collection), 2)
109
110 def test_legal_suffix_is_ignored_when_deduping(self):
111 # "Example Inc." and "Example" normalise to the same key and merge; the
112 # first-seen display form is kept.
113 collection = holders.CopyrightHolders()
114 collection.add(CopyrightHolder("Example Inc.", ""))
115 collection.add(CopyrightHolder("Example", ""))
116 self.assertEqual(len(collection), 1)
117 self.assertEqual(list(collection.sorted_members()), ["Example Inc."])
118
119
120 class TestProcessNameEmailPairs(unittest.TestCase):
121 """The (name, email, years) continuation-vs-new-holder decision table."""
122
123 def test_email_only_pair_completes_previous_holder(self):
124 holder_list = [CopyrightHolder("Alice", "")]
125 leftover = holders._process_name_email_pairs([("", "a@x")], holder_list, None)
126 self.assertEqual(len(holder_list), 1)
127 self.assertEqual(holder_list[0].email, "a@x")
128 self.assertIsNone(leftover)
129
130 def test_name_only_pair_completes_previous_holder(self):
131 holder_list = [CopyrightHolder("", "a@x")]
132 leftover = holders._process_name_email_pairs([("Alice", "")], holder_list, None)
133 self.assertEqual(len(holder_list), 1)
134 self.assertEqual(holder_list[0].name, "Alice")
135 self.assertIsNone(leftover)
136
137 def test_years_only_pair_backfills_previous_holder(self):
138 previous = CopyrightHolder("Alice", "a@x")
139 holder_list = [previous]
140 leftover = holders._process_name_email_pairs(
141 [("", "")], holder_list, YearRange(2020)
142 )
143 self.assertEqual(len(holder_list), 1)
144 self.assertEqual(previous.years.low, 2020)
145 self.assertIsNone(leftover)
146
147 def test_complete_pair_appends_a_new_holder(self):
148 holder_list: list[CopyrightHolder] = []
149 leftover = holders._process_name_email_pairs(
150 [("Alice", "a@x")], holder_list, None
151 )
152 self.assertEqual(len(holder_list), 1)
153 self.assertEqual(holder_list[0].name, "Alice")
154 self.assertIsNone(leftover)
155
156
157 class TestParseHoldersCharacterization(unittest.TestCase):
158 """End-to-end `parse_holders` behaviour on realistic copyright notices.
159
160 These pin the observable contract of the public entry point before any
161 internal refactor of the notice parser. Each expectation is the parser's
162 *current* output (characterization), not an independent spec; a future change
163 that alters any of these is a signal to review, not necessarily a break.
164 """
165
166 def test_multiple_holders_after_one_indicator(self):
167 # A single "Copyright:" indicator followed by two continuation lines
168 # yields both holders (no email, no years).
169 text = "Copyright:\n Maximiliano Curia\n Margarita Manterola\n"
170 self.assertEqual(
171 _triples(holders.parse_holders(text)),
172 [("Maximiliano Curia", "", ""), ("Margarita Manterola", "", "")],
173 )
174
175 def test_years_before_holder_are_carried_onto_it(self):
176 text = "Copyright 2018\n Maximiliano Curia\n"
177 self.assertEqual(
178 _triples(holders.parse_holders(text)),
179 [("Maximiliano Curia", "", "2018")],
180 )
181
182 def test_year_after_holder_is_backfilled(self):
183 text = "Copyright Maximiliano Curia (c) 2018\n"
184 self.assertEqual(
185 _triples(holders.parse_holders(text)),
186 [("Maximiliano Curia", "", "2018")],
187 )
188
189 def test_c_comment_first_line_is_parsed(self):
190 # The `/*` comment prefix on the Copyright line is stripped and the first
191 # holder parsed with email and year. Current behaviour: the follow-on
192 # ` * Bob ...` continuation line is NOT captured -- pinned so a future
193 # NoticeParser change that starts capturing it surfaces here.
194 text = "/* Copyright 2019 Alice <alice@x>\n * Bob <bob@y>\n */\n"
195 self.assertEqual(
196 _triples(holders.parse_holders(text)),
197 [("Alice", "alice@x", "2019")],
198 )
199
200 def test_same_name_distinct_emails_stay_separate_end_to_end(self):
201 text = "Copyright 2020 Alice <a1@x>\nCopyright 2021 Alice <a2@x>\n"
202 self.assertEqual(
203 _triples(holders.parse_holders(text)),
204 [("Alice", "a1@x", "2020"), ("Alice", "a2@x", "2021")],
205 )
206
207 def test_year_range_and_email_are_parsed(self):
208 text = "Copyright (C) 2015-2018 Alice <[email protected]>\n"
209 self.assertEqual(
210 _triples(holders.parse_holders(text)),
211 [("Alice", "[email protected]", "2015-2018")],
212 )
213
214 def test_empty_text_has_no_holders(self):
215 self.assertEqual(holders.parse_holders(""), [])
216
217 def test_text_without_a_copyright_indicator_has_no_holders(self):
218 self.assertEqual(
219 holders.parse_holders("Just some text\nwith no copyright\n"), []
220 )
221
222
223 if __name__ == "__main__":
224 unittest.main()