| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Characterization tests for the grouping/splitting policy. |
| 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 |
"""Pin the current grouping policy before the GroupingStrategy refactor. |
| 20 |
|
| 21 |
These tests lock the observable behaviour of the four scattered |
| 22 |
group_by/split_* conditionals that we will move behind a strategy: |
| 23 |
|
| 24 |
* grouping.make_group_key - the copyright slot decision |
| 25 |
* file_policy.splitting_path - the split_on_license/split_debian rules |
| 26 |
* copyright_processor.remove_misplaced_files - the group_by copyright fallback |
| 27 |
|
| 28 |
The fourth site, Group.sort_key's split_debian band, is already pinned by |
| 29 |
TestGroupSortKey in dep5_test.py, so it is not duplicated here. |
| 30 |
""" |
| 31 |
|
| 32 |
import unittest |
| 33 |
|
| 34 |
from decopy import cmdoptions |
| 35 |
from decopy.common.core_types import CopyrightHolder |
| 36 |
from decopy.dep5doc import dep5 |
| 37 |
from decopy.fstree import nodes |
| 38 |
from decopy.grouping import file_policy |
| 39 |
from decopy.grouping.grouping import make_group_key |
| 40 |
|
| 41 |
|
| 42 |
def _options(*, group_by="license", split_on_license=True, split_debian=True): |
| 43 |
flags = ["--no-progress", "--root", "tests/testdata/one", "--group-by", group_by] |
| 44 |
if not split_on_license: |
| 45 |
flags.append("--no-split-on-license") |
| 46 |
if not split_debian: |
| 47 |
flags.append("--no-split-debian") |
| 48 |
return cmdoptions.process_options(flags) |
| 49 |
|
| 50 |
|
| 51 |
class MakeGroupKeyTest(unittest.TestCase): |
| 52 |
"""grouping.make_group_key fills the copyright slot only under copyright.""" |
| 53 |
|
| 54 |
def test_copyright_slot_is_none_when_grouping_by_license(self): |
| 55 |
key = make_group_key("MIT", ("alice@x",), "path", _options(group_by="license")) |
| 56 |
self.assertEqual(key, ("MIT", None, "path")) |
| 57 |
|
| 58 |
def test_copyright_slot_is_kept_when_grouping_by_copyright(self): |
| 59 |
key = make_group_key( |
| 60 |
"MIT", ("alice@x",), "path", _options(group_by="copyright") |
| 61 |
) |
| 62 |
self.assertEqual(key, ("MIT", ("alice@x",), "path")) |
| 63 |
|
| 64 |
def test_license_and_path_pass_through_unchanged(self): |
| 65 |
key = make_group_key("GPL-2", (), None, _options(group_by="copyright")) |
| 66 |
self.assertEqual(key, ("GPL-2", (), None)) |
| 67 |
|
| 68 |
def test_key_exposes_named_slots_and_still_equals_the_tuple_form(self): |
| 69 |
# The key is a NamedTuple: named-slot access reads clearer than key[0..2] |
| 70 |
# while staying tuple-compatible (equality, unpacking, hashing) so it |
| 71 |
# remains a drop-in dict key. |
| 72 |
key = make_group_key( |
| 73 |
"MIT", ("alice@x",), "path", _options(group_by="copyright") |
| 74 |
) |
| 75 |
self.assertEqual(key.license, "MIT") |
| 76 |
self.assertEqual(key.copyright, ("alice@x",)) |
| 77 |
self.assertEqual(key.path, "path") |
| 78 |
# Tuple-compatible: equals the plain tuple and unpacks positionally. |
| 79 |
self.assertEqual(key, ("MIT", ("alice@x",), "path")) |
| 80 |
license_, copyright_, path = key |
| 81 |
self.assertEqual((license_, copyright_, path), ("MIT", ("alice@x",), "path")) |
| 82 |
|
| 83 |
|
| 84 |
class SplittingPathTest(unittest.TestCase): |
| 85 |
"""file_policy.splitting_path resolves the per-node split boundary.""" |
| 86 |
|
| 87 |
def setUp(self): |
| 88 |
# root/ |
| 89 |
# src/lic/ (lic holds a license) |
| 90 |
# debian/ |
| 91 |
self.root = nodes.DirInfo(parent=None, name="") |
| 92 |
self.src = nodes.DirInfo(parent=self.root, name="src") |
| 93 |
self.root.file_list["src"] = self.src |
| 94 |
self.debian = nodes.DirInfo(parent=self.root, name="debian") |
| 95 |
self.root.file_list["debian"] = self.debian |
| 96 |
self.lic = nodes.DirInfo(parent=self.src, name="lic") |
| 97 |
self.lic.add_licenses(["MIT"]) |
| 98 |
self.src.file_list["lic"] = self.lic |
| 99 |
|
| 100 |
def test_root_returns_its_own_name_regardless_of_flags(self): |
| 101 |
self.assertEqual(file_policy.splitting_path(self.root, _options()), "") |
| 102 |
|
| 103 |
def test_license_dir_returns_itself_with_split_on_license(self): |
| 104 |
opts = _options(split_on_license=True) |
| 105 |
self.assertEqual(file_policy.splitting_path(self.lic, opts), "src/lic") |
| 106 |
|
| 107 |
def test_license_dir_recurses_to_root_without_split_on_license(self): |
| 108 |
opts = _options(split_on_license=False) |
| 109 |
self.assertEqual(file_policy.splitting_path(self.lic, opts), "") |
| 110 |
|
| 111 |
def test_debian_dir_is_special_with_split_debian(self): |
| 112 |
opts = _options(split_debian=True) |
| 113 |
self.assertEqual(file_policy.splitting_path(self.debian, opts), "debian") |
| 114 |
|
| 115 |
def test_debian_dir_recurses_to_root_without_split_debian(self): |
| 116 |
opts = _options(split_debian=False) |
| 117 |
self.assertEqual(file_policy.splitting_path(self.debian, opts), "") |
| 118 |
|
| 119 |
def test_file_delegates_to_its_parent_directory(self): |
| 120 |
opts = _options(split_on_license=True) |
| 121 |
f = nodes.FileInfo(parent=self.lic, name="a.txt") |
| 122 |
self.assertEqual(file_policy.splitting_path(f, opts), "src/lic") |
| 123 |
|
| 124 |
def test_parentless_file_has_no_splitting_path(self): |
| 125 |
orphan = nodes.FileInfo(parent=None, name="o.txt") |
| 126 |
self.assertIsNone(file_policy.splitting_path(orphan, _options())) |
| 127 |
|
| 128 |
# node subclasses follow their base kind (ArchiveDirInfo -> DirInfo, |
| 129 |
# VirtualFileInfo -> FileInfo); this is the distinction Item 5 makes |
| 130 |
# polymorphic, so pin it before the refactor. |
| 131 |
|
| 132 |
def test_archive_dir_is_split_boundary_like_a_directory(self): |
| 133 |
arch = nodes.ArchiveDirInfo( |
| 134 |
parent=self.src, name="pkg.tar", archive_path="/x/pkg.tar" |
| 135 |
) |
| 136 |
arch.add_licenses(["MIT"]) |
| 137 |
self.assertEqual( |
| 138 |
file_policy.splitting_path(arch, _options(split_on_license=True)), |
| 139 |
"src/pkg.tar", |
| 140 |
) |
| 141 |
|
| 142 |
def test_archive_dir_recurses_when_not_a_boundary(self): |
| 143 |
arch = nodes.ArchiveDirInfo( |
| 144 |
parent=self.src, name="pkg.tar", archive_path="/x/pkg.tar" |
| 145 |
) |
| 146 |
arch.add_licenses(["MIT"]) |
| 147 |
self.assertEqual( |
| 148 |
file_policy.splitting_path(arch, _options(split_on_license=False)), |
| 149 |
"", |
| 150 |
) |
| 151 |
|
| 152 |
def test_virtual_file_delegates_to_parent_like_a_file(self): |
| 153 |
vf = nodes.VirtualFileInfo(parent=self.lic, name="a.py", content=b"x") |
| 154 |
self.assertEqual( |
| 155 |
file_policy.splitting_path(vf, _options(split_on_license=True)), |
| 156 |
"src/lic", |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
class RemoveMisplacedFilesTest(unittest.TestCase): |
| 161 |
"""remove_misplaced_files: the group_by copyright fallback vs license.""" |
| 162 |
|
| 163 |
def _file(self, name, holder=None): |
| 164 |
f = nodes.FileInfo(parent=self.root, name=name) |
| 165 |
f.add_licenses(["MIT"]) |
| 166 |
if holder is not None: |
| 167 |
f.add_copyrights([holder]) |
| 168 |
return f |
| 169 |
|
| 170 |
def _copyright_with_group(self, files): |
| 171 |
copyright_ = dep5.Copyright() |
| 172 |
copyright_.groups = [] |
| 173 |
group = dep5.Group(position=0) |
| 174 |
for f in files: |
| 175 |
group.add_file(f) |
| 176 |
copyright_.groups.append(group) |
| 177 |
return copyright_, group |
| 178 |
|
| 179 |
def setUp(self): |
| 180 |
self.root = nodes.DirInfo(parent=None, name="") |
| 181 |
self.alice = CopyrightHolder("Alice", "alice@x") |
| 182 |
self.bob = CopyrightHolder("Bob", "bob@x") |
| 183 |
|
| 184 |
def test_uncopyrighted_file_is_kept_in_copyrighted_group(self): |
| 185 |
# Same-holder files plus an uncopyrighted one: under copyright grouping |
| 186 |
# the uncopyrighted file is rescued by the no-copyright fallback. |
| 187 |
files = [ |
| 188 |
self._file("a.txt", self.alice), |
| 189 |
self._file("b.txt", self.alice), |
| 190 |
self._file("c.txt", None), |
| 191 |
] |
| 192 |
copyright_, group = self._copyright_with_group(files) |
| 193 |
copyright_.remove_misplaced_files(_options(group_by="copyright")) |
| 194 |
self.assertEqual(sorted(group.files.files), ["a.txt", "b.txt", "c.txt"]) |
| 195 |
|
| 196 |
def test_same_files_all_kept_under_license_grouping(self): |
| 197 |
files = [ |
| 198 |
self._file("a.txt", self.alice), |
| 199 |
self._file("b.txt", self.alice), |
| 200 |
self._file("c.txt", None), |
| 201 |
] |
| 202 |
copyright_, group = self._copyright_with_group(files) |
| 203 |
copyright_.remove_misplaced_files(_options(group_by="license")) |
| 204 |
self.assertEqual(sorted(group.files.files), ["a.txt", "b.txt", "c.txt"]) |
| 205 |
|
| 206 |
def test_union_copyright_files_kept_under_copyright_grouping(self): |
| 207 |
# Distinct holders sharing a group: the group's Copyright is the union of |
| 208 |
# its files' holders, so each file carries a subset and both are kept. |
| 209 |
# (Splitting distinct holders is a fresh-grouping concern, not a reason |
| 210 |
# to tear apart an existing union paragraph.) |
| 211 |
files = [self._file("a.txt", self.alice), self._file("b.txt", self.bob)] |
| 212 |
copyright_, group = self._copyright_with_group(files) |
| 213 |
copyright_.remove_misplaced_files(_options(group_by="copyright")) |
| 214 |
self.assertEqual(sorted(group.files.files), ["a.txt", "b.txt"]) |
| 215 |
|
| 216 |
def test_file_with_undeclared_holder_dropped_under_copyright_grouping(self): |
| 217 |
# A file whose holder the group does not declare is not a subset, so it |
| 218 |
# is removed. The group key is pinned to Alice before the Bob file is |
| 219 |
# added, mirroring the pipeline (the key reflects the declared holders, |
| 220 |
# not the kept-file merge). |
| 221 |
copyright_, group = self._copyright_with_group( |
| 222 |
[self._file("a.txt", self.alice)] |
| 223 |
) |
| 224 |
group.get_key(_options(group_by="copyright")) |
| 225 |
group.add_file(self._file("b.txt", self.bob)) |
| 226 |
copyright_.remove_misplaced_files(_options(group_by="copyright")) |
| 227 |
self.assertEqual(sorted(group.files.files), ["a.txt"]) |
| 228 |
|
| 229 |
def test_mismatched_copyright_files_kept_under_license_grouping(self): |
| 230 |
# Under license grouping copyright is irrelevant, so both survive. |
| 231 |
files = [self._file("a.txt", self.alice), self._file("b.txt", self.bob)] |
| 232 |
copyright_, group = self._copyright_with_group(files) |
| 233 |
copyright_.remove_misplaced_files(_options(group_by="license")) |
| 234 |
self.assertEqual(sorted(group.files.files), ["a.txt", "b.txt"]) |
| 235 |
|
| 236 |
|
| 237 |
class LicenseSourceTest(unittest.TestCase): |
| 238 |
"""file_policy.license_source: provenance phrase mirroring license_key.""" |
| 239 |
|
| 240 |
def setUp(self): |
| 241 |
self.root = nodes.DirInfo(parent=None, name="") |
| 242 |
self.src = nodes.DirInfo(parent=self.root, name="src") |
| 243 |
self.root.file_list["src"] = self.src |
| 244 |
|
| 245 |
def _file(self, name, parent=None): |
| 246 |
return nodes.FileInfo(parent=parent or self.root, name=name) |
| 247 |
|
| 248 |
def test_own_detected_license_names_the_file(self): |
| 249 |
f = self._file("a.txt") |
| 250 |
f.add_licenses(["MIT"]) |
| 251 |
self.assertEqual(file_policy.license_source(f), "detected in a.txt") |
| 252 |
|
| 253 |
def test_own_license_from_separate_file_names_that_file(self): |
| 254 |
f = self._file("a.txt") |
| 255 |
f.add_licenses(["MIT"], ["LICENSE"]) |
| 256 |
self.assertEqual(file_policy.license_source(f), "from LICENSE") |
| 257 |
|
| 258 |
def test_inherited_from_parent_directory_names_the_dir(self): |
| 259 |
self.src.add_licenses(["MIT"]) |
| 260 |
f = self._file("a.txt", parent=self.src) |
| 261 |
self.assertEqual(file_policy.license_source(f), "inherited from src/") |
| 262 |
|
| 263 |
def test_inherited_from_dir_license_file_names_that_file(self): |
| 264 |
# A dir whose license came from a LICENSE child names the file, not the |
| 265 |
# dir: the file path is the more precise evidence. |
| 266 |
self.src.add_licenses(["MIT"], ["src/LICENSE"]) |
| 267 |
f = self._file("a.txt", parent=self.src) |
| 268 |
self.assertEqual(file_policy.license_source(f), "from src/LICENSE") |
| 269 |
|
| 270 |
def test_multiple_license_files_are_sorted(self): |
| 271 |
f = self._file("a.txt") |
| 272 |
f.add_licenses(["MIT"], ["COPYING", "LICENSE"]) |
| 273 |
self.assertEqual(file_policy.license_source(f), "from COPYING, LICENSE") |
| 274 |
|
| 275 |
def test_no_license_and_no_copyright(self): |
| 276 |
f = self._file("a.txt") |
| 277 |
self.assertEqual(file_policy.license_source(f), "no license detected") |
| 278 |
|
| 279 |
def test_copyright_present_but_no_license(self): |
| 280 |
f = self._file("a.txt") |
| 281 |
f.add_copyrights([CopyrightHolder("Alice", "alice@x")]) |
| 282 |
self.assertEqual(file_policy.license_source(f), "copyright present, no license") |
| 283 |
|
| 284 |
|
| 285 |
class HolderSourcesTest(unittest.TestCase): |
| 286 |
"""file_policy.holder_sources: holder key -> sorted files it was found in.""" |
| 287 |
|
| 288 |
def setUp(self): |
| 289 |
self.root = nodes.DirInfo(parent=None, name="") |
| 290 |
|
| 291 |
def _file(self, name, holders=()): |
| 292 |
f = nodes.FileInfo(parent=self.root, name=name) |
| 293 |
f.add_copyrights(list(holders)) |
| 294 |
return f |
| 295 |
|
| 296 |
def test_holder_in_two_files_maps_to_both(self): |
| 297 |
a = self._file("a.txt", [CopyrightHolder("Alice", "alice@x")]) |
| 298 |
b = self._file("b.txt", [CopyrightHolder("Alice", "alice@x")]) |
| 299 |
self.assertEqual( |
| 300 |
file_policy.holder_sources([a, b]), {"alice@x": ("a.txt", "b.txt")} |
| 301 |
) |
| 302 |
|
| 303 |
def test_holder_in_one_file_maps_to_one(self): |
| 304 |
a = self._file("a.txt", [CopyrightHolder("Alice", "alice@x")]) |
| 305 |
self.assertEqual(file_policy.holder_sources([a]), {"alice@x": ("a.txt",)}) |
| 306 |
|
| 307 |
def test_distinct_holders_map_separately(self): |
| 308 |
a = self._file("a.txt", [CopyrightHolder("Alice", "alice@x")]) |
| 309 |
b = self._file("b.txt", [CopyrightHolder("Bob", "bob@x")]) |
| 310 |
self.assertEqual( |
| 311 |
file_policy.holder_sources([a, b]), |
| 312 |
{"alice@x": ("a.txt",), "bob@x": ("b.txt",)}, |
| 313 |
) |
| 314 |
|
| 315 |
def test_key_is_person_when_no_email(self): |
| 316 |
# Matches CopyrightHolders.key() (email or person), so the map lines up |
| 317 |
# with the holder strings the explain +/- lines render. |
| 318 |
a = self._file("a.txt", [CopyrightHolder("Acme Inc", "")]) |
| 319 |
self.assertEqual(file_policy.holder_sources([a]), {"Acme Inc": ("a.txt",)}) |
| 320 |
|
| 321 |
def test_files_without_copyright_contribute_nothing(self): |
| 322 |
a = self._file("a.txt") |
| 323 |
b = self._file("b.txt", [CopyrightHolder("Alice", "alice@x")]) |
| 324 |
self.assertEqual(file_policy.holder_sources([a, b]), {"alice@x": ("b.txt",)}) |
| 325 |
|
| 326 |
def test_empty_input_is_empty_map(self): |
| 327 |
self.assertEqual(file_policy.holder_sources([]), {}) |
| 328 |
|
| 329 |
def test_source_files_are_sorted_regardless_of_input_order(self): |
| 330 |
b = self._file("b.txt", [CopyrightHolder("Alice", "alice@x")]) |
| 331 |
a = self._file("a.txt", [CopyrightHolder("Alice", "alice@x")]) |
| 332 |
self.assertEqual( |
| 333 |
file_policy.holder_sources([b, a]), {"alice@x": ("a.txt", "b.txt")} |
| 334 |
) |
| 335 |
|
| 336 |
|
| 337 |
if __name__ == "__main__": |
| 338 |
unittest.main() |