| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Unit tests for the GroupingStrategy abstraction. |
| 4 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 5 |
# |
| 6 |
# License: ISC |
| 7 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
# purpose with or without fee is hereby granted, provided that the above |
| 9 |
# copyright notice and this permission notice appear in all copies. |
| 10 |
# . |
| 11 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 12 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 13 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 14 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 15 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 16 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 17 |
# PERFORMANCE OF THIS SOFTWARE. |
| 18 |
|
| 19 |
"""Unit tests for grouping_strategy. |
| 20 |
|
| 21 |
These pin the strategy classes in isolation. They must agree with the |
| 22 |
characterization tests in grouping_policy_test.py, which lock the equivalent |
| 23 |
behaviour at the (still-live) call sites the strategies will replace. |
| 24 |
""" |
| 25 |
|
| 26 |
import unittest |
| 27 |
|
| 28 |
from decopy import cmdoptions |
| 29 |
from decopy.common import core_types |
| 30 |
from decopy.fstree import nodes |
| 31 |
from decopy.grouping.grouping import GroupKey |
| 32 |
from decopy.grouping.grouping_strategy import ( |
| 33 |
CopyrightGrouping, |
| 34 |
GroupingStrategy, |
| 35 |
LicenseGrouping, |
| 36 |
grouping_strategy_for, |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def _options(*, group_by="license", split_on_license=True, split_debian=True): |
| 41 |
flags = ["--no-progress", "--root", "tests/testdata/one", "--group-by", group_by] |
| 42 |
if not split_on_license: |
| 43 |
flags.append("--no-split-on-license") |
| 44 |
if not split_debian: |
| 45 |
flags.append("--no-split-debian") |
| 46 |
return cmdoptions.process_options(flags) |
| 47 |
|
| 48 |
|
| 49 |
class FactoryTest(unittest.TestCase): |
| 50 |
def test_license_option_selects_license_grouping(self): |
| 51 |
strategy = grouping_strategy_for("license") |
| 52 |
self.assertIsInstance(strategy, LicenseGrouping) |
| 53 |
|
| 54 |
def test_copyright_option_selects_copyright_grouping(self): |
| 55 |
strategy = grouping_strategy_for("copyright") |
| 56 |
self.assertIsInstance(strategy, CopyrightGrouping) |
| 57 |
|
| 58 |
def test_strategies_are_shared_singletons(self): |
| 59 |
a = grouping_strategy_for("license") |
| 60 |
b = grouping_strategy_for("license") |
| 61 |
self.assertIs(a, b) |
| 62 |
|
| 63 |
|
| 64 |
class KeyCopyrightSlotTest(unittest.TestCase): |
| 65 |
def test_license_drops_the_copyright_slot(self): |
| 66 |
self.assertIsNone(LicenseGrouping().key_copyright_slot(("alice@x",))) |
| 67 |
|
| 68 |
def test_copyright_keeps_the_copyright_slot(self): |
| 69 |
self.assertEqual( |
| 70 |
CopyrightGrouping().key_copyright_slot(("alice@x",)), ("alice@x",) |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
class KeepsMisplacedTest(unittest.TestCase): |
| 75 |
def setUp(self): |
| 76 |
self.group_key = GroupKey("MIT", ("alice@x",), "") |
| 77 |
self.group_key_no_copyright = GroupKey("MIT", (), "") |
| 78 |
|
| 79 |
def test_license_keeps_only_on_exact_match(self): |
| 80 |
strategy = LicenseGrouping() |
| 81 |
self.assertTrue( |
| 82 |
strategy.keeps_misplaced( |
| 83 |
self.group_key, self.group_key, self.group_key_no_copyright |
| 84 |
) |
| 85 |
) |
| 86 |
self.assertFalse( |
| 87 |
strategy.keeps_misplaced( |
| 88 |
GroupKey("MIT", ("bob@x",), ""), |
| 89 |
self.group_key, |
| 90 |
self.group_key_no_copyright, |
| 91 |
) |
| 92 |
) |
| 93 |
|
| 94 |
def test_copyright_keeps_on_exact_match(self): |
| 95 |
strategy = CopyrightGrouping() |
| 96 |
self.assertTrue( |
| 97 |
strategy.keeps_misplaced( |
| 98 |
self.group_key, self.group_key, self.group_key_no_copyright |
| 99 |
) |
| 100 |
) |
| 101 |
|
| 102 |
def test_copyright_rescues_uncopyrighted_file(self): |
| 103 |
# An empty copyright slot matches the no-copyright key -> kept. |
| 104 |
strategy = CopyrightGrouping() |
| 105 |
self.assertTrue( |
| 106 |
strategy.keeps_misplaced( |
| 107 |
self.group_key_no_copyright, |
| 108 |
self.group_key, |
| 109 |
self.group_key_no_copyright, |
| 110 |
) |
| 111 |
) |
| 112 |
|
| 113 |
def test_copyright_drops_mismatched_nonempty_copyright(self): |
| 114 |
strategy = CopyrightGrouping() |
| 115 |
self.assertFalse( |
| 116 |
strategy.keeps_misplaced( |
| 117 |
GroupKey("MIT", ("bob@x",), ""), |
| 118 |
self.group_key, |
| 119 |
self.group_key_no_copyright, |
| 120 |
) |
| 121 |
) |
| 122 |
|
| 123 |
def test_copyright_keeps_file_whose_holders_are_a_subset(self): |
| 124 |
# A multi-holder paragraph's Copyright is the union of its files' |
| 125 |
# holders, so each file legitimately carries a subset. A file is kept |
| 126 |
# when its holders are a subset of the group's declared holders. |
| 127 |
strategy = CopyrightGrouping() |
| 128 |
union_group = GroupKey("MIT", ("alice@x", "bob@x"), "") |
| 129 |
self.assertTrue( |
| 130 |
strategy.keeps_misplaced( |
| 131 |
GroupKey("MIT", ("alice@x",), ""), union_group, GroupKey("MIT", (), "") |
| 132 |
) |
| 133 |
) |
| 134 |
|
| 135 |
def test_copyright_drops_file_with_an_undeclared_holder(self): |
| 136 |
# A holder the paragraph does not declare is not a subset -> removed |
| 137 |
# (the paragraph's Copyright is stale relative to the file). |
| 138 |
strategy = CopyrightGrouping() |
| 139 |
union_group = GroupKey("MIT", ("alice@x", "bob@x"), "") |
| 140 |
self.assertFalse( |
| 141 |
strategy.keeps_misplaced( |
| 142 |
GroupKey("MIT", ("alice@x", "carol@x"), ""), |
| 143 |
union_group, |
| 144 |
GroupKey("MIT", (), ""), |
| 145 |
) |
| 146 |
) |
| 147 |
|
| 148 |
def test_copyright_drops_subset_when_license_differs(self): |
| 149 |
# Subset applies to the copyright slot only; a different license (or |
| 150 |
# path) still removes the file. |
| 151 |
strategy = CopyrightGrouping() |
| 152 |
self.assertFalse( |
| 153 |
strategy.keeps_misplaced( |
| 154 |
GroupKey("BSD", ("alice@x",), ""), |
| 155 |
GroupKey("MIT", ("alice@x", "bob@x"), ""), |
| 156 |
GroupKey("MIT", (), ""), |
| 157 |
) |
| 158 |
) |
| 159 |
|
| 160 |
|
| 161 |
class ReportsCopyrightDriftTest(unittest.TestCase): |
| 162 |
"""Only license grouping reports copyright drift. |
| 163 |
|
| 164 |
Under copyright grouping a holder change already surfaces as a relicensed |
| 165 |
entry (the copyright slot participates in the key), so reporting drift there |
| 166 |
would double-count. |
| 167 |
""" |
| 168 |
|
| 169 |
def test_license_grouping_reports_drift(self): |
| 170 |
self.assertTrue(LicenseGrouping().reports_copyright_drift()) |
| 171 |
|
| 172 |
def test_copyright_grouping_does_not_report_drift(self): |
| 173 |
self.assertFalse(CopyrightGrouping().reports_copyright_drift()) |
| 174 |
|
| 175 |
|
| 176 |
class SplittingPathTest(unittest.TestCase): |
| 177 |
"""The shared base-class splitting_path matches file_policy.splitting_path.""" |
| 178 |
|
| 179 |
def setUp(self): |
| 180 |
self.strategy = LicenseGrouping() |
| 181 |
self.root = nodes.DirInfo(parent=None, name="") |
| 182 |
self.src = nodes.DirInfo(parent=self.root, name="src") |
| 183 |
self.root.file_list["src"] = self.src |
| 184 |
self.debian = nodes.DirInfo(parent=self.root, name="debian") |
| 185 |
self.root.file_list["debian"] = self.debian |
| 186 |
self.lic = nodes.DirInfo(parent=self.src, name="lic") |
| 187 |
self.lic.add_licenses(["MIT"]) |
| 188 |
self.src.file_list["lic"] = self.lic |
| 189 |
|
| 190 |
def test_root_returns_its_own_name(self): |
| 191 |
self.assertEqual(self.strategy.splitting_path(self.root, _options()), "") |
| 192 |
|
| 193 |
def test_license_dir_with_split_on_license(self): |
| 194 |
opts = _options(split_on_license=True) |
| 195 |
self.assertEqual(self.strategy.splitting_path(self.lic, opts), "src/lic") |
| 196 |
|
| 197 |
def test_license_dir_recurses_without_split_on_license(self): |
| 198 |
opts = _options(split_on_license=False) |
| 199 |
self.assertEqual(self.strategy.splitting_path(self.lic, opts), "") |
| 200 |
|
| 201 |
def test_debian_dir_with_split_debian(self): |
| 202 |
opts = _options(split_debian=True) |
| 203 |
self.assertEqual(self.strategy.splitting_path(self.debian, opts), "debian") |
| 204 |
|
| 205 |
def test_debian_dir_recurses_without_split_debian(self): |
| 206 |
opts = _options(split_debian=False) |
| 207 |
self.assertEqual(self.strategy.splitting_path(self.debian, opts), "") |
| 208 |
|
| 209 |
def test_file_delegates_to_parent(self): |
| 210 |
f = nodes.FileInfo(parent=self.lic, name="a.txt") |
| 211 |
self.assertEqual( |
| 212 |
self.strategy.splitting_path(f, _options(split_on_license=True)), |
| 213 |
"src/lic", |
| 214 |
) |
| 215 |
|
| 216 |
def test_parentless_file_returns_none(self): |
| 217 |
orphan = nodes.FileInfo(parent=None, name="o.txt") |
| 218 |
self.assertIsNone(self.strategy.splitting_path(orphan, _options())) |
| 219 |
|
| 220 |
|
| 221 |
class SortBandTest(unittest.TestCase): |
| 222 |
def setUp(self): |
| 223 |
self.strategy = LicenseGrouping() |
| 224 |
|
| 225 |
def test_normal_group_is_band_zero(self): |
| 226 |
band = self.strategy.sort_band("MIT", "src", split_debian=True) |
| 227 |
self.assertEqual(band, 0) |
| 228 |
|
| 229 |
def test_debian_group_is_band_two_with_split_debian(self): |
| 230 |
band = self.strategy.sort_band("MIT", "debian", split_debian=True) |
| 231 |
self.assertEqual(band, 2) |
| 232 |
|
| 233 |
def test_debian_group_is_band_zero_without_split_debian(self): |
| 234 |
band = self.strategy.sort_band("MIT", "debian", split_debian=False) |
| 235 |
self.assertEqual(band, 0) |
| 236 |
|
| 237 |
def test_unknown_license_is_band_one(self): |
| 238 |
band = self.strategy.sort_band(core_types.UNKNOWN, "src", split_debian=True) |
| 239 |
self.assertEqual(band, 1) |
| 240 |
|
| 241 |
def test_unknown_overrides_debian_band(self): |
| 242 |
band = self.strategy.sort_band( |
| 243 |
core_types.UNKNOWN_COPYRIGHTED, "debian", split_debian=True |
| 244 |
) |
| 245 |
self.assertEqual(band, 1) |
| 246 |
|
| 247 |
|
| 248 |
class AbstractContractTest(unittest.TestCase): |
| 249 |
def test_base_class_cannot_be_instantiated(self): |
| 250 |
with self.assertRaises(TypeError): |
| 251 |
GroupingStrategy() # type: ignore[abstract] |
| 252 |
|
| 253 |
|
| 254 |
if __name__ == "__main__": |
| 255 |
unittest.main() |