| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Unit tests for decopy |
| 4 |
# Copyright: 2016, Margarita Manterola <[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 |
import dataclasses |
| 20 |
import tempfile |
| 21 |
import unittest |
| 22 |
from concurrent import futures |
| 23 |
from pathlib import Path |
| 24 |
from unittest.mock import patch |
| 25 |
|
| 26 |
from decopy import cmdoptions |
| 27 |
from decopy.common.core_types import ( |
| 28 |
UNKNOWN, |
| 29 |
UNKNOWN_COPYRIGHTED, |
| 30 |
CopyrightHolder, |
| 31 |
License, |
| 32 |
YearRange, |
| 33 |
) |
| 34 |
from decopy.dep5doc import dep5 |
| 35 |
from decopy.fstree import nodes, tree |
| 36 |
from decopy.grouping import group |
| 37 |
from decopy.grouping.file_policy import group_key, license_key, splitting_path |
| 38 |
from decopy.licensing import holders |
| 39 |
|
| 40 |
|
| 41 |
class TestFileInfo(unittest.TestCase): |
| 42 |
def setUp(self): |
| 43 |
# Create one root with one dir with one file |
| 44 |
self.r = tree.RootInfo(root="root") |
| 45 |
self.d = nodes.DirInfo(name="dir", parent=self.r) |
| 46 |
self.f = nodes.FileInfo(name="file", parent=self.d) |
| 47 |
|
| 48 |
def test_fullname(self): |
| 49 |
self.assertEqual(self.r.fullname, "") |
| 50 |
self.assertEqual(self.d.fullname, "dir") |
| 51 |
self.assertEqual(self.f.fullname, "dir/file") |
| 52 |
|
| 53 |
def test_add_licenses_stores_full_filename(self): |
| 54 |
self.d.add_licenses({"GPL"}, {"COPYING"}) |
| 55 |
self.assertIn("COPYING", self.d.license_filenames) |
| 56 |
self.assertNotIn("C", self.d.license_filenames) |
| 57 |
|
| 58 |
def test_get_licenses(self): |
| 59 |
# Set a license for the directory |
| 60 |
self.d.add_licenses({"GPL"}, {"COPYING"}) |
| 61 |
# The license for the file should be the one from the directory |
| 62 |
self.assertEqual(self.f.get_licenses(), {"GPL"}) |
| 63 |
|
| 64 |
# Set a different license for the file |
| 65 |
self.f.add_licenses({"BSD"}) |
| 66 |
# Now the license is the one for the file |
| 67 |
self.assertEqual(self.f.get_licenses(), {"BSD"}) |
| 68 |
|
| 69 |
# Add a second license to the file |
| 70 |
self.f.add_licenses({"PHP"}) |
| 71 |
# Now it should have both licenses |
| 72 |
self.assertEqual(self.f.get_licenses(), {"BSD", "PHP"}) |
| 73 |
|
| 74 |
def test_splitting_path(self): |
| 75 |
# Set a license for the directory |
| 76 |
self.d.add_licenses({"GPL"}, {"COPYING"}) |
| 77 |
# Create a child directory with a different license |
| 78 |
self.o = nodes.DirInfo(name="other", parent=self.d) |
| 79 |
self.o.add_licenses({"BSD"}, {"COPYING"}) |
| 80 |
|
| 81 |
# First test with splitting on license |
| 82 |
options = cmdoptions.process_options(["--no-progress", "--split-on-license"]) |
| 83 |
self.assertEqual(splitting_path(self.d, options), "dir") |
| 84 |
self.assertEqual(splitting_path(self.o, options), "dir/other") |
| 85 |
|
| 86 |
# Then test without splitting on license |
| 87 |
options = cmdoptions.process_options(["--no-progress", "--no-split-on-license"]) |
| 88 |
self.assertEqual(splitting_path(self.d, options), "") |
| 89 |
self.assertEqual(splitting_path(self.o, options), "") |
| 90 |
|
| 91 |
# Create a debian directory, with another license |
| 92 |
self.deb = nodes.DirInfo(name="debian", parent=self.r) |
| 93 |
self.deb.add_licenses({"LGPL"}, {"debian/copyright"}) |
| 94 |
options = cmdoptions.process_options( |
| 95 |
["--no-progress", "--split-debian", "--no-split-on-license"], |
| 96 |
) |
| 97 |
self.assertEqual(splitting_path(self.deb, options), "debian") |
| 98 |
|
| 99 |
options = cmdoptions.process_options( |
| 100 |
["--no-progress", "--split-debian", "--split-on-license"], |
| 101 |
) |
| 102 |
self.assertEqual(splitting_path(self.deb, options), "debian") |
| 103 |
|
| 104 |
options = cmdoptions.process_options( |
| 105 |
["--no-progress", "--no-split-debian", "--split-on-license"], |
| 106 |
) |
| 107 |
self.assertEqual(splitting_path(self.deb, options), "debian") |
| 108 |
|
| 109 |
options = cmdoptions.process_options( |
| 110 |
["--no-progress", "--no-split-debian", "--no-split-on-license"], |
| 111 |
) |
| 112 |
self.assertEqual(splitting_path(self.deb, options), "") |
| 113 |
|
| 114 |
def test_splitting_path_file_delegates_to_parent(self): |
| 115 |
# A FileInfo has no splitting path of its own; it uses its parent dir's. |
| 116 |
self.d.add_licenses({"GPL"}, {"COPYING"}) |
| 117 |
options = cmdoptions.process_options(["--no-progress", "--split-on-license"]) |
| 118 |
self.assertEqual( |
| 119 |
splitting_path(self.f, options), |
| 120 |
splitting_path(self.d, options), |
| 121 |
) |
| 122 |
self.assertEqual(splitting_path(self.f, options), "dir") |
| 123 |
|
| 124 |
def test_splitting_path_file_no_parent_returns_none(self): |
| 125 |
orphan = nodes.FileInfo(parent=None, name="orphan") |
| 126 |
options = cmdoptions.process_options(["--no-progress"]) |
| 127 |
self.assertIsNone(splitting_path(orphan, options)) |
| 128 |
|
| 129 |
def test_group_key_license_mode_has_no_copyright_slot(self): |
| 130 |
self.d.add_licenses({"GPL"}, {"COPYING"}) |
| 131 |
self.f.add_licenses({"MIT"}) |
| 132 |
options = cmdoptions.process_options(["--no-progress", "--group-by", "license"]) |
| 133 |
# (license_key, copyright slot is None in license mode, splitting path) |
| 134 |
self.assertEqual(group_key(self.f, options), ("MIT", None, "dir")) |
| 135 |
|
| 136 |
def test_group_key_copyright_mode_fills_copyright_slot(self): |
| 137 |
self.d.add_licenses({"GPL"}, {"COPYING"}) |
| 138 |
self.f.add_licenses({"MIT"}) |
| 139 |
self.f.add_copyrights([CopyrightHolder("Alice", "[email protected]")]) |
| 140 |
options = cmdoptions.process_options( |
| 141 |
["--no-progress", "--group-by", "copyright"], |
| 142 |
) |
| 143 |
self.assertEqual( |
| 144 |
group_key(self.f, options), |
| 145 |
("MIT", ("[email protected]",), "dir"), |
| 146 |
) |
| 147 |
|
| 148 |
def test_included(self): |
| 149 |
self.assertEqual(self.f.included, False) |
| 150 |
# Remove the cache |
| 151 |
self.f.included = None |
| 152 |
self.d.included = True |
| 153 |
self.assertEqual(self.f.included, True) |
| 154 |
|
| 155 |
|
| 156 |
class TestRootInfo(unittest.TestCase): |
| 157 |
def _check_expected_files(self, filetree, expected_files): |
| 158 |
"""Check that the parsed filetree has the expected files.""" |
| 159 |
self.longMessage = False |
| 160 |
for filename in expected_files: |
| 161 |
with self.subTest(msg=f'Checking "{filename}"'): |
| 162 |
try: |
| 163 |
name = filetree[filename].fullname |
| 164 |
except IndexError: |
| 165 |
name = "" |
| 166 |
self.assertEqual( |
| 167 |
name, |
| 168 |
filename, |
| 169 |
msg=f'missing file: "{filename}"', |
| 170 |
) |
| 171 |
|
| 172 |
for fileinfo in filetree: |
| 173 |
with self.subTest(msg=f'Checking "{fileinfo.fullname}"'): |
| 174 |
self.assertTrue( |
| 175 |
fileinfo.fullname in expected_files, |
| 176 |
msg=f'unexpected file: "{fileinfo.fullname}"', |
| 177 |
) |
| 178 |
self.longMessage = True |
| 179 |
|
| 180 |
def test_simple_tree(self): |
| 181 |
"""Parses a tree with licensed files (testdata/one).""" |
| 182 |
|
| 183 |
options = cmdoptions.process_options( |
| 184 |
["--no-progress", "--root", "tests/testdata/one"], |
| 185 |
) |
| 186 |
expected_files = { |
| 187 |
"": "Inherited(GPL-2+)", |
| 188 |
"COPYING": "GPL-2+", |
| 189 |
"art.txt": "Artistic-1", |
| 190 |
"bsd.txt": "BSD-3-clause", |
| 191 |
"empty.txt": "Inherited(GPL-2+)", |
| 192 |
"no.txt": "Inherited(GPL-2+)", |
| 193 |
"paren.txt": "GPL-3+", |
| 194 |
} |
| 195 |
|
| 196 |
# First build the tree and check that it has the files and nothing else |
| 197 |
filetree = tree.RootInfo.build(options) |
| 198 |
self._check_expected_files(filetree, expected_files) |
| 199 |
|
| 200 |
# Now process the licenses |
| 201 |
filetree.process(options) |
| 202 |
|
| 203 |
# Check that the licenses match our expectations |
| 204 |
for filename, license_ in expected_files.items(): |
| 205 |
with self.subTest(msg=f'Checking "{filename}"'): |
| 206 |
self.assertEqual( |
| 207 |
license_key(filetree[filename]), |
| 208 |
license_, |
| 209 |
msg=f'license mismatch for: "{filename}"', |
| 210 |
) |
| 211 |
|
| 212 |
def test_copyrighted_tree(self): |
| 213 |
"""Parses a tree with licensed & copyrighted files (testdata/two).""" |
| 214 |
|
| 215 |
options = cmdoptions.process_options( |
| 216 |
["--no-progress", "--root", "tests/testdata/two"], |
| 217 |
) |
| 218 |
expected_files = { |
| 219 |
"": ["Inherited(GPL-2+)", ()], |
| 220 |
"COPYING": ["GPL-2+", ()], |
| 221 |
"art.txt": ["Artistic-1", ("Jane Doe",)], |
| 222 |
"bsd.txt": ["BSD-3-clause", ("John Doe",)], |
| 223 |
"empty.txt": ["Inherited(GPL-2+)", ()], |
| 224 |
"no.txt": ["Inherited(GPL-2+)", ("Juan Pérez", "Juana López")], |
| 225 |
"docbook.txt": ["Inherited(GPL-2+)", ()], |
| 226 |
} |
| 227 |
|
| 228 |
# First build the tree and check that it has the files and nothing else |
| 229 |
filetree = tree.RootInfo.build(options) |
| 230 |
self._check_expected_files(filetree, expected_files) |
| 231 |
|
| 232 |
# Now process the licenses |
| 233 |
filetree.process(options) |
| 234 |
|
| 235 |
# Check that the licenses and copyrights match our expectations |
| 236 |
for filename, values in expected_files.items(): |
| 237 |
license_, expected_copyright = values |
| 238 |
with self.subTest(msg=f'Checking "{filename}"'): |
| 239 |
self.assertEqual( |
| 240 |
license_key(filetree[filename]), |
| 241 |
license_, |
| 242 |
msg=f'license mismatch for: "{filename}"', |
| 243 |
) |
| 244 |
|
| 245 |
self.assertEqual( |
| 246 |
filetree[filename].copyrights.key(), |
| 247 |
tuple(sorted(expected_copyright)), |
| 248 |
msg=f'copyright mismatch for: "{filename}"', |
| 249 |
) |
| 250 |
|
| 251 |
def test_debianized_tree(self): |
| 252 |
"""Parses a tree that contains a debian/copyright (testdata/three).""" |
| 253 |
|
| 254 |
options = cmdoptions.process_options( |
| 255 |
["--no-progress", "--root", "tests/testdata/three"], |
| 256 |
) |
| 257 |
expected_files = { |
| 258 |
"": ["Inherited(GPL-2+)", ()], |
| 259 |
"COPYING": ["GPL-2+", ()], |
| 260 |
"src": ["Inherited(GPL-2+)", ()], |
| 261 |
"src/do-something.m": ["MIT/X11", ("[email protected]",)], |
| 262 |
"src/do-something.py": ["MIT/X11", ("[email protected]",)], |
| 263 |
"src/main.py": ["GPL-2+", ("[email protected]",)], |
| 264 |
"src/module.py": ["GPL-2+", ("[email protected]",)], |
| 265 |
"acinclude.m4": ["GPL-2+", ("Free Software Foundation, Inc",)], |
| 266 |
"data": ["Inherited(GPL-2+)", ()], |
| 267 |
"data/file.txt": ["MIT/X11", ("[email protected]",)], |
| 268 |
"debian": ["Inherited(GPL-2+)", ()], |
| 269 |
"debian/rules": ["LGPL-2+", ()], |
| 270 |
"debian/compat": ["LGPL-2+", ()], |
| 271 |
"debian/copyright": ["LGPL-2+", ()], |
| 272 |
} |
| 273 |
|
| 274 |
# First build the tree and check that it has the files and nothing else |
| 275 |
filetree = tree.RootInfo.build(options) |
| 276 |
self._check_expected_files(filetree, expected_files) |
| 277 |
|
| 278 |
# Instantiate and process the copyright |
| 279 |
copyright_ = dep5.read_copyright(options) |
| 280 |
copyright_.process(filetree) |
| 281 |
|
| 282 |
# Now process the licenses in the filetree |
| 283 |
filetree.process(options) |
| 284 |
|
| 285 |
# Check that the licenses and copyrights match our expectations |
| 286 |
for filename, values in expected_files.items(): |
| 287 |
license_, expected_copyright = values |
| 288 |
with self.subTest(msg=f'Checking "{filename}"'): |
| 289 |
self.assertEqual( |
| 290 |
license_key(filetree[filename]), |
| 291 |
license_, |
| 292 |
msg=f'license mismatch for: "{filename}"', |
| 293 |
) |
| 294 |
|
| 295 |
self.assertEqual( |
| 296 |
filetree[filename].copyrights.key(), |
| 297 |
tuple(sorted(expected_copyright)), |
| 298 |
msg=f'copyright mismatch for: "{filename}"', |
| 299 |
) |
| 300 |
|
| 301 |
def test_tree_exclude(self): |
| 302 |
"""Parse a tree with an exclusion expresion (testdata/three).""" |
| 303 |
|
| 304 |
options = cmdoptions.process_options( |
| 305 |
["--no-progress", "--root", "tests/testdata/three", "-X", "src.*"], |
| 306 |
) |
| 307 |
expected_files = { |
| 308 |
"": ["Inherited(GPL-2+)", ()], |
| 309 |
"COPYING": ["GPL-2+", ()], |
| 310 |
"src": ["Inherited(GPL-2+)", ()], |
| 311 |
"src/do-something.m": ["Inherited(GPL-2+)", ()], |
| 312 |
"src/do-something.py": ["Inherited(GPL-2+)", ()], |
| 313 |
"src/main.py": ["Inherited(GPL-2+)", ()], |
| 314 |
"src/module.py": ["Inherited(GPL-2+)", ()], |
| 315 |
"acinclude.m4": ["GPL-2+", ("Free Software Foundation, Inc",)], |
| 316 |
"data": ["Inherited(GPL-2+)", ()], |
| 317 |
"data/file.txt": ["MIT/X11", ("[email protected]",)], |
| 318 |
"debian": ["Inherited(GPL-2+)", ()], |
| 319 |
"debian/rules": ["Inherited(GPL-2+)", ()], |
| 320 |
"debian/compat": ["Inherited(GPL-2+)", ()], |
| 321 |
"debian/copyright": ["Inherited(GPL-2+)", ()], |
| 322 |
} |
| 323 |
|
| 324 |
# First build the tree and check that it has the files and nothing else |
| 325 |
filetree = tree.RootInfo.build(options) |
| 326 |
self._check_expected_files(filetree, expected_files) |
| 327 |
|
| 328 |
# Don't process the debian/copyright, so that we can test just the |
| 329 |
# parsed tree. |
| 330 |
|
| 331 |
# Now process the licenses in the filetree. Excluded files won't get |
| 332 |
# processed, so they get inherited values. |
| 333 |
filetree.process(options) |
| 334 |
|
| 335 |
# Check that the licenses and copyrights match our expectations |
| 336 |
for filename, values in expected_files.items(): |
| 337 |
license_, expected_copyright = values |
| 338 |
with self.subTest(msg=f'Checking "{filename}"'): |
| 339 |
self.assertEqual( |
| 340 |
license_key(filetree[filename]), |
| 341 |
license_, |
| 342 |
msg=f'license mismatch for: "{filename}"', |
| 343 |
) |
| 344 |
|
| 345 |
self.assertEqual( |
| 346 |
filetree[filename].copyrights.key(), |
| 347 |
tuple(sorted(expected_copyright)), |
| 348 |
msg=f'copyright mismatch for: "{filename}"', |
| 349 |
) |
| 350 |
|
| 351 |
|
| 352 |
class TestWildcardPattern(unittest.TestCase): |
| 353 |
def test_file_pattern_is_its_fullname(self): |
| 354 |
self.assertEqual(nodes.FileInfo(None, "foo").wildcard_pattern(), "foo") |
| 355 |
|
| 356 |
def test_nested_file_pattern_is_its_fullname(self): |
| 357 |
dirinfo = nodes.DirInfo(None, "dir") |
| 358 |
fileinfo = nodes.FileInfo(dirinfo, "foo") |
| 359 |
self.assertEqual(fileinfo.wildcard_pattern(), "dir/foo") |
| 360 |
|
| 361 |
def test_dir_pattern_appends_slash_star(self): |
| 362 |
self.assertEqual(nodes.DirInfo(None, "dir").wildcard_pattern(), "dir/*") |
| 363 |
|
| 364 |
def test_nested_dir_pattern_appends_slash_star(self): |
| 365 |
dirinfo = nodes.DirInfo(nodes.DirInfo(None, "a"), "b") |
| 366 |
self.assertEqual(dirinfo.wildcard_pattern(), "a/b/*") |
| 367 |
|
| 368 |
def test_root_dir_pattern_is_star(self): |
| 369 |
self.assertEqual(tree.RootInfo("root").wildcard_pattern(), "*") |
| 370 |
|
| 371 |
|
| 372 |
class TestNodeTraversal(unittest.TestCase): |
| 373 |
def _small_tree(self): |
| 374 |
root = nodes.DirInfo(None, "") |
| 375 |
sub = nodes.DirInfo(root, "sub") |
| 376 |
root.file_list["sub"] = sub |
| 377 |
top = nodes.FileInfo(root, "a") |
| 378 |
root.file_list["a"] = top |
| 379 |
nested = nodes.FileInfo(sub, "b") |
| 380 |
sub.file_list["b"] = nested |
| 381 |
return root, sub, top, nested |
| 382 |
|
| 383 |
def test_file_has_no_children(self): |
| 384 |
self.assertEqual(list(nodes.FileInfo(None, "foo").children), []) |
| 385 |
|
| 386 |
def test_dir_children_are_its_members(self): |
| 387 |
root, sub, top, _nested = self._small_tree() |
| 388 |
self.assertEqual(list(root.children), [sub, top]) |
| 389 |
|
| 390 |
def test_file_walk_yields_only_itself(self): |
| 391 |
fileinfo = nodes.FileInfo(None, "foo") |
| 392 |
self.assertEqual(list(fileinfo.walk()), [fileinfo]) |
| 393 |
|
| 394 |
def test_dir_walk_yields_self_then_descendants(self): |
| 395 |
root, sub, top, nested = self._small_tree() |
| 396 |
self.assertEqual(list(root.walk()), [root, sub, nested, top]) |
| 397 |
|
| 398 |
def test_walk_matches_iter(self): |
| 399 |
root, *_ = self._small_tree() |
| 400 |
self.assertEqual(list(root.walk()), list(root)) |
| 401 |
|
| 402 |
def test_walk_files_yields_only_leaves(self): |
| 403 |
root, _sub, top, nested = self._small_tree() |
| 404 |
self.assertEqual(list(root.walk_files()), [nested, top]) |
| 405 |
|
| 406 |
def test_file_walk_files_yields_itself(self): |
| 407 |
fileinfo = nodes.FileInfo(None, "foo") |
| 408 |
self.assertEqual(list(fileinfo.walk_files()), [fileinfo]) |
| 409 |
|
| 410 |
def test_file_recompute_total_is_one(self): |
| 411 |
self.assertEqual(nodes.FileInfo(None, "foo").recompute_total(), 1) |
| 412 |
|
| 413 |
def test_empty_dir_recompute_total_is_zero(self): |
| 414 |
self.assertEqual(nodes.DirInfo(None, "dir").recompute_total(), 0) |
| 415 |
|
| 416 |
def test_dir_recompute_total_counts_leaves_and_sets_total(self): |
| 417 |
root, *_ = self._small_tree() |
| 418 |
self.assertEqual(root.recompute_total(), 2) |
| 419 |
self.assertEqual(root.total, 2) |
| 420 |
|
| 421 |
|
| 422 |
class TestIsContainer(unittest.TestCase): |
| 423 |
def test_file_is_not_a_container(self): |
| 424 |
self.assertFalse(nodes.FileInfo(None, "foo").is_container) |
| 425 |
|
| 426 |
def test_dir_is_a_container(self): |
| 427 |
self.assertTrue(nodes.DirInfo(None, "dir").is_container) |
| 428 |
|
| 429 |
def test_archive_dir_is_a_container(self): |
| 430 |
archive = nodes.ArchiveDirInfo( |
| 431 |
parent=nodes.DirInfo(None, ""), name="a.tar", archive_path="/x" |
| 432 |
) |
| 433 |
self.assertTrue(archive.is_container) |
| 434 |
|
| 435 |
|
| 436 |
class TestAsContainer(unittest.TestCase): |
| 437 |
def test_file_as_container_is_none(self): |
| 438 |
self.assertIsNone(nodes.FileInfo(None, "foo").as_container()) |
| 439 |
|
| 440 |
def test_dir_as_container_is_itself(self): |
| 441 |
dirinfo = nodes.DirInfo(None, "dir") |
| 442 |
self.assertIs(dirinfo.as_container(), dirinfo) |
| 443 |
|
| 444 |
def test_archive_dir_as_container_is_itself(self): |
| 445 |
archive = nodes.ArchiveDirInfo( |
| 446 |
parent=nodes.DirInfo(None, ""), name="a.tar", archive_path="/x" |
| 447 |
) |
| 448 |
self.assertIs(archive.as_container(), archive) |
| 449 |
|
| 450 |
|
| 451 |
class TestGetChild(unittest.TestCase): |
| 452 |
def test_file_has_no_child(self): |
| 453 |
self.assertIsNone(nodes.FileInfo(None, "foo").get_child("x")) |
| 454 |
|
| 455 |
def test_dir_returns_member(self): |
| 456 |
root = nodes.DirInfo(None, "") |
| 457 |
leaf = nodes.FileInfo(root, "a") |
| 458 |
root.file_list["a"] = leaf |
| 459 |
self.assertIs(root.get_child("a"), leaf) |
| 460 |
|
| 461 |
def test_dir_returns_none_for_missing(self): |
| 462 |
self.assertIsNone(nodes.DirInfo(None, "").get_child("nope")) |
| 463 |
|
| 464 |
|
| 465 |
class TestParallelProcessing(unittest.TestCase): |
| 466 |
"""RootInfo.process: the process-pool path matches the serial path.""" |
| 467 |
|
| 468 |
@staticmethod |
| 469 |
def _run(root: str, jobs: int) -> dict[str, tuple[list[str], list[str]]]: |
| 470 |
opts = cmdoptions.process_options( |
| 471 |
[ |
| 472 |
"--no-progress", |
| 473 |
"--no-expand-archives", |
| 474 |
"--root", |
| 475 |
root, |
| 476 |
"--jobs", |
| 477 |
str(jobs), |
| 478 |
] |
| 479 |
) |
| 480 |
# Force the parallel path for this tiny tree by lowering the threshold. |
| 481 |
with patch.object(tree, "_PARALLEL_MIN", 1): |
| 482 |
filetree = tree.RootInfo.build(opts) |
| 483 |
filetree.process(opts) |
| 484 |
return { |
| 485 |
item.fullname: ( |
| 486 |
sorted(item.licenses), |
| 487 |
sorted(str(h) for h in item.copyrights), |
| 488 |
) |
| 489 |
for item in filetree.walk_files() |
| 490 |
} |
| 491 |
|
| 492 |
def test_parallel_matches_serial(self): |
| 493 |
with tempfile.TemporaryDirectory() as tmp: |
| 494 |
root = Path(tmp) |
| 495 |
(root / "a.py").write_text( |
| 496 |
"# Copyright 2020 Alice <[email protected]>\n" |
| 497 |
"# SPDX-License-Identifier: MIT\n" |
| 498 |
) |
| 499 |
(root / "b.txt").write_text("Copyright 2019 Bob <[email protected]>\n") |
| 500 |
(root / "COPYING").write_text( |
| 501 |
"Permission is hereby granted, free of charge, to any person\n" |
| 502 |
) |
| 503 |
serial = self._run(str(root), jobs=1) # workers==1 -> serial path |
| 504 |
parallel = self._run(str(root), jobs=2) # workers>1 -> process pool |
| 505 |
self.assertEqual(serial, parallel) |
| 506 |
# Sanity: the run actually detected something to compare. |
| 507 |
self.assertTrue(any(lic for lic, _ in parallel.values())) |
| 508 |
self.assertTrue(any(cr for _, cr in parallel.values())) |
| 509 |
|
| 510 |
|
| 511 |
class TestCollectChildLicenses(unittest.TestCase): |
| 512 |
@staticmethod |
| 513 |
def _completed(fileinfo): |
| 514 |
fut: futures.Future[nodes.FileInfo | None] = futures.Future() |
| 515 |
fut.set_result(fileinfo) |
| 516 |
return fut |
| 517 |
|
| 518 |
def test_aggregates_licenses_from_completed_tasks(self): |
| 519 |
root = nodes.DirInfo(None, "") |
| 520 |
leaf = nodes.FileInfo(root, "COPYING") |
| 521 |
leaf.add_licenses({"GPL-2+"}) |
| 522 |
root.tasks = {self._completed(leaf)} |
| 523 |
|
| 524 |
result = root.collect_child_licenses() |
| 525 |
|
| 526 |
self.assertIs(result, root) |
| 527 |
self.assertIn("GPL-2+", root.licenses) |
| 528 |
self.assertIn("COPYING", root.license_filenames) |
| 529 |
self.assertEqual(root.tasks, set()) |
| 530 |
|
| 531 |
def test_skips_children_without_licenses(self): |
| 532 |
root = nodes.DirInfo(None, "") |
| 533 |
leaf = nodes.FileInfo(root, "README") |
| 534 |
root.tasks = {self._completed(leaf)} |
| 535 |
|
| 536 |
root.collect_child_licenses() |
| 537 |
|
| 538 |
self.assertEqual(root.licenses, set()) |
| 539 |
|
| 540 |
|
| 541 |
class TestSubdirHelpers(unittest.TestCase): |
| 542 |
def test_get_subdir_returns_dir(self): |
| 543 |
root = nodes.DirInfo(None, "") |
| 544 |
sub = nodes.DirInfo(root, "sub") |
| 545 |
root.file_list["sub"] = sub |
| 546 |
self.assertIs(root.get_subdir("sub"), sub) |
| 547 |
|
| 548 |
def test_get_subdir_none_for_file(self): |
| 549 |
root = nodes.DirInfo(None, "") |
| 550 |
root.file_list["f"] = nodes.FileInfo(root, "f") |
| 551 |
self.assertIsNone(root.get_subdir("f")) |
| 552 |
|
| 553 |
def test_get_subdir_none_for_missing(self): |
| 554 |
self.assertIsNone(nodes.DirInfo(None, "").get_subdir("nope")) |
| 555 |
|
| 556 |
def test_get_or_create_subdir_returns_existing(self): |
| 557 |
root = nodes.DirInfo(None, "") |
| 558 |
sub = nodes.DirInfo(root, "sub") |
| 559 |
root.file_list["sub"] = sub |
| 560 |
self.assertIs(root.get_or_create_subdir("sub"), sub) |
| 561 |
|
| 562 |
def test_get_or_create_subdir_creates_missing(self): |
| 563 |
root = nodes.DirInfo(None, "") |
| 564 |
created = root.get_or_create_subdir("new") |
| 565 |
assert isinstance(created, nodes.DirInfo) |
| 566 |
self.assertIs(root.file_list["new"], created) |
| 567 |
self.assertIs(created.parent, root) |
| 568 |
|
| 569 |
def test_get_or_create_subdir_none_on_file_collision(self): |
| 570 |
root = nodes.DirInfo(None, "") |
| 571 |
root.file_list["f"] = nodes.FileInfo(root, "f") |
| 572 |
self.assertIsNone(root.get_or_create_subdir("f")) |
| 573 |
|
| 574 |
|
| 575 |
class TestDirAdd(unittest.TestCase): |
| 576 |
def test_add_creates_nested_dirs(self): |
| 577 |
root = nodes.DirInfo(None, "") |
| 578 |
root.add(["a", "b"], [], ["leaf"]) |
| 579 |
a = root.get_subdir("a") |
| 580 |
assert a is not None |
| 581 |
b = a.get_subdir("b") |
| 582 |
assert b is not None |
| 583 |
self.assertIn("leaf", b.file_list) |
| 584 |
|
| 585 |
def test_add_does_not_descend_into_a_file(self): |
| 586 |
root = nodes.DirInfo(None, "") |
| 587 |
leaf = nodes.FileInfo(root, "x") |
| 588 |
root.file_list["x"] = leaf |
| 589 |
# "x" is a file; descending into it as a dir is a no-op, not a crash. |
| 590 |
root.add(["x"], [], ["child"]) |
| 591 |
self.assertIs(root.file_list["x"], leaf) |
| 592 |
|
| 593 |
|
| 594 |
class TestRootIndexing(unittest.TestCase): |
| 595 |
def setUp(self): |
| 596 |
self.root = tree.RootInfo("root") |
| 597 |
self.root.add([], ["dir"], ["top"]) |
| 598 |
self.root.add(["dir"], [], ["leaf"]) |
| 599 |
|
| 600 |
def test_empty_key_returns_root(self): |
| 601 |
self.assertIs(self.root[""], self.root) |
| 602 |
self.assertIs(self.root["."], self.root) |
| 603 |
|
| 604 |
def test_nested_lookup(self): |
| 605 |
self.assertEqual(self.root["dir/leaf"].fullname, "dir/leaf") |
| 606 |
|
| 607 |
def test_missing_key_raises(self): |
| 608 |
with self.assertRaises(IndexError): |
| 609 |
self.root["missing"] |
| 610 |
|
| 611 |
def test_descend_through_file_raises(self): |
| 612 |
with self.assertRaises(IndexError): |
| 613 |
self.root["top/x"] |
| 614 |
|
| 615 |
|
| 616 |
class TestTallyTracker(unittest.TestCase): |
| 617 |
def test_first_untallied_is_topmost_ancestor(self): |
| 618 |
r = tree.RootInfo("root") |
| 619 |
d = nodes.DirInfo(name="dir", parent=r) |
| 620 |
f = nodes.FileInfo(name="file", parent=d) |
| 621 |
tracker = group.TallyTracker() |
| 622 |
self.assertIs(tracker.first_untallied(f), r) |
| 623 |
tracker.tally(d) |
| 624 |
self.assertIs(tracker.first_untallied(f), f) |
| 625 |
|
| 626 |
def test_tally_is_shared_across_lookups(self): |
| 627 |
d = nodes.DirInfo(None, "dir") |
| 628 |
foo = nodes.FileInfo(d, "foo") |
| 629 |
bar = nodes.FileInfo(d, "bar") |
| 630 |
tracker = group.TallyTracker() |
| 631 |
# foo's topmost untallied ancestor is the dir. |
| 632 |
self.assertIs(tracker.first_untallied(foo), d) |
| 633 |
# Tallying foo marks foo and the dir, so a sibling now resolves to itself. |
| 634 |
tracker.tally(foo) |
| 635 |
self.assertIs(tracker.first_untallied(bar), bar) |
| 636 |
|
| 637 |
|
| 638 |
class TestFileGroup(unittest.TestCase): |
| 639 |
def setUp(self): |
| 640 |
self.group = group.FileGroup() |
| 641 |
|
| 642 |
def test_get_patterns_empty_group(self): |
| 643 |
self.assertEqual(self.group.get_patterns(group.TallyTracker()), []) |
| 644 |
|
| 645 |
def test_get_patterns_one_file(self): |
| 646 |
fileinfo = nodes.FileInfo(None, "foo") |
| 647 |
self.group.add_file(fileinfo) |
| 648 |
self.assertEqual(self.group.get_patterns(group.TallyTracker()), ["foo"]) |
| 649 |
|
| 650 |
def test_get_patterns_one_dir_one_file(self): |
| 651 |
dirinfo = nodes.DirInfo(None, "dir") |
| 652 |
fileinfo = nodes.FileInfo(dirinfo, "foo") |
| 653 |
self.group.add_file(fileinfo) |
| 654 |
self.assertEqual(self.group.get_patterns(group.TallyTracker()), ["dir/*"]) |
| 655 |
|
| 656 |
def test_get_patterns_separate_groups(self): |
| 657 |
dirinfo = nodes.DirInfo(None, "dir") |
| 658 |
foo = nodes.FileInfo(dirinfo, "foo") |
| 659 |
bar = nodes.FileInfo(dirinfo, "bar") |
| 660 |
self.group.add_file(foo) |
| 661 |
other = group.FileGroup() |
| 662 |
other.add_file(bar) |
| 663 |
tracker = group.TallyTracker() |
| 664 |
self.assertEqual(self.group.get_patterns(tracker), ["dir/*"]) |
| 665 |
self.assertEqual(other.get_patterns(tracker), ["dir/bar"]) |
| 666 |
|
| 667 |
def test_get_patterns_root(self): |
| 668 |
dirinfo = tree.RootInfo("root") |
| 669 |
fileinfo = nodes.FileInfo(dirinfo, "foo") |
| 670 |
self.group.add_file(fileinfo) |
| 671 |
self.assertEqual(self.group.get_patterns(group.TallyTracker()), ["*"]) |
| 672 |
|
| 673 |
def test_get_patterns_separate_groups_more_files(self): |
| 674 |
dirinfo = nodes.DirInfo(None, "dir") |
| 675 |
foo = nodes.FileInfo(dirinfo, "foo") |
| 676 |
bar = nodes.FileInfo(dirinfo, "bar") |
| 677 |
baz = nodes.FileInfo(dirinfo, "baz") |
| 678 |
self.group.add_file(foo) |
| 679 |
other = group.FileGroup() |
| 680 |
other.add_file(bar) |
| 681 |
other.add_file(baz) |
| 682 |
tracker = group.TallyTracker() |
| 683 |
self.assertEqual(self.group.get_patterns(tracker), ["dir/*"]) |
| 684 |
self.assertEqual(other.get_patterns(tracker), ["dir/bar", "dir/baz"]) |
| 685 |
|
| 686 |
def test_common_path_empty(self): |
| 687 |
self.assertEqual(self.group.commonpath(), "") |
| 688 |
|
| 689 |
def test_common_path_one_file(self): |
| 690 |
fileinfo = nodes.FileInfo(None, "foo") |
| 691 |
self.group.add_file(fileinfo) |
| 692 |
self.assertEqual(self.group.commonpath(), "foo") |
| 693 |
|
| 694 |
def test_common_path_one_dir_two_files(self): |
| 695 |
dirinfo = nodes.DirInfo(None, "dir") |
| 696 |
foo = nodes.FileInfo(dirinfo, "foo") |
| 697 |
bar = nodes.FileInfo(dirinfo, "bar") |
| 698 |
self.group.add_file(foo) |
| 699 |
self.group.add_file(bar) |
| 700 |
self.assertEqual(self.group.commonpath(), "dir") |
| 701 |
|
| 702 |
|
| 703 |
class TestCopyrightGroup(unittest.TestCase): |
| 704 |
def setUp(self): |
| 705 |
self.group = holders.CopyrightHolders() |
| 706 |
|
| 707 |
def test_add_two_people(self): |
| 708 |
self.group.add( |
| 709 |
CopyrightHolder("Jane Doe", "[email protected]", YearRange(2015, 2015)), |
| 710 |
) |
| 711 |
self.group.add( |
| 712 |
CopyrightHolder("John Doe", "[email protected]", YearRange(2016, 2016)), |
| 713 |
) |
| 714 |
|
| 715 |
self.assertEqual( |
| 716 |
list(self.group.sorted_members()), |
| 717 |
["2015, Jane Doe <[email protected]>", "2016, John Doe <[email protected]>"], |
| 718 |
) |
| 719 |
|
| 720 |
def test_merge_names(self): |
| 721 |
self.group.add(CopyrightHolder("Jane", "[email protected]", YearRange())) |
| 722 |
self.group.add(CopyrightHolder("Jane Doe", "[email protected]", YearRange())) |
| 723 |
|
| 724 |
self.assertEqual(list(self.group.sorted_members()), ["Jane <[email protected]>"]) |
| 725 |
|
| 726 |
def test_merge_names_case_insensitive(self): |
| 727 |
# "the Wine project" (from source file) and "The Wine project" (from |
| 728 |
# debian/copyright) must be treated as the same holder. |
| 729 |
self.group.add(CopyrightHolder("the Wine project", "", YearRange())) |
| 730 |
self.group.add(CopyrightHolder("The Wine project", "", YearRange())) |
| 731 |
|
| 732 |
self.assertEqual(len(self.group), 1) |
| 733 |
|
| 734 |
def test_merge_emails(self): |
| 735 |
self.group.add(CopyrightHolder("Jane Doe", "[email protected]", YearRange())) |
| 736 |
self.group.add(CopyrightHolder("Jane Doe", "[email protected]", YearRange())) |
| 737 |
|
| 738 |
self.assertEqual( |
| 739 |
list(self.group.sorted_members()), |
| 740 |
["Jane Doe <[email protected]>", "Jane Doe <[email protected]>"], |
| 741 |
) |
| 742 |
|
| 743 |
def test_merge_years(self): |
| 744 |
self.group.add( |
| 745 |
CopyrightHolder("Jane Doe", "[email protected]", YearRange(2010, 2011)), |
| 746 |
) |
| 747 |
self.group.add( |
| 748 |
CopyrightHolder("Jane Doe", "[email protected]", YearRange(2015, 2016)), |
| 749 |
) |
| 750 |
|
| 751 |
self.assertEqual( |
| 752 |
list(self.group.sorted_members()), |
| 753 |
["2010-2016, Jane Doe <[email protected]>"], |
| 754 |
) |
| 755 |
|
| 756 |
def test_merge_names_with_years(self): |
| 757 |
self.group.add( |
| 758 |
CopyrightHolder("Jane Doe", "[email protected]", YearRange(1999, 1999)), |
| 759 |
) |
| 760 |
self.group.add( |
| 761 |
CopyrightHolder("Janine Doe", "[email protected]", YearRange(2016, 2016)), |
| 762 |
) |
| 763 |
|
| 764 |
self.assertEqual( |
| 765 |
list(self.group.sorted_members()), |
| 766 |
["1999-2016, Janine Doe <[email protected]>"], |
| 767 |
) |
| 768 |
|
| 769 |
def test_key_empty_group(self): |
| 770 |
self.assertEqual(self.group.key(), ()) |
| 771 |
|
| 772 |
def test_key_uses_email_when_present(self): |
| 773 |
self.group.add(CopyrightHolder("Alice", "[email protected]")) |
| 774 |
self.group.add(CopyrightHolder("Bob", "[email protected]")) |
| 775 |
self.assertEqual(self.group.key(), ("[email protected]", "[email protected]")) |
| 776 |
|
| 777 |
def test_key_falls_back_to_person_when_no_email(self): |
| 778 |
self.group.add(CopyrightHolder("Alice Smith", "")) |
| 779 |
self.assertEqual(self.group.key(), ("Alice Smith",)) |
| 780 |
|
| 781 |
def test_key_mixed_email_and_no_email(self): |
| 782 |
self.group.add(CopyrightHolder("Alice", "[email protected]")) |
| 783 |
self.group.add(CopyrightHolder("Bob NoEmail", "")) |
| 784 |
key = self.group.key() |
| 785 |
self.assertIn("[email protected]", key) |
| 786 |
self.assertIn("Bob NoEmail", key) |
| 787 |
self.assertEqual(len(key), 2) |
| 788 |
|
| 789 |
def test_key_is_sorted(self): |
| 790 |
self.group.add(CopyrightHolder("Z Person", "[email protected]")) |
| 791 |
self.group.add(CopyrightHolder("A Person", "[email protected]")) |
| 792 |
self.assertEqual(self.group.key(), ("[email protected]", "[email protected]")) |
| 793 |
|
| 794 |
def test_merge_legal_suffix_with_email_then_bare(self): |
| 795 |
# Holder with email + bare name without email → should merge |
| 796 |
self.group.add( |
| 797 |
CopyrightHolder("Aptible, Inc", "[email protected]", YearRange(2017, 2017)) |
| 798 |
) |
| 799 |
self.group.add(CopyrightHolder("Aptible", "", YearRange(2019, 2019))) |
| 800 |
self.assertEqual(len(self.group), 1) |
| 801 |
members = list(self.group.sorted_members()) |
| 802 |
# We keep the new name form |
| 803 |
self.assertIn("Aptible", members[0]) |
| 804 |
self.assertIn("2017-2019", members[0]) |
| 805 |
|
| 806 |
def test_merge_legal_suffix_bare_then_with_email(self): |
| 807 |
# Bare name without email + holder with email → should merge, adopting the email |
| 808 |
self.group.add(CopyrightHolder("Aptible", "", YearRange(2019, 2019))) |
| 809 |
self.group.add( |
| 810 |
CopyrightHolder("Aptible, Inc", "[email protected]", YearRange(2017, 2017)) |
| 811 |
) |
| 812 |
self.assertEqual(len(self.group), 1) |
| 813 |
members = list(self.group.sorted_members()) |
| 814 |
# We keep the new name form (copyright 2019 > 2017) |
| 815 |
self.assertIn("Aptible", members[0]) |
| 816 |
self.assertIn("[email protected]", members[0]) |
| 817 |
self.assertIn("2017-2019", members[0]) |
| 818 |
|
| 819 |
def test_merge_legal_suffix_new_then_old(self): |
| 820 |
# "Aptible, Inc" added first, then bare "Aptible" → one holder with long name |
| 821 |
self.group.add(CopyrightHolder("Aptible, Inc", "", YearRange(2017, 2019))) |
| 822 |
self.group.add(CopyrightHolder("Aptible", "", YearRange(2018, 2018))) |
| 823 |
self.assertEqual(len(self.group), 1) |
| 824 |
self.assertEqual(list(self.group.sorted_members()), ["2017-2019, Aptible, Inc"]) |
| 825 |
|
| 826 |
def test_merge_legal_suffix_old_then_new(self): |
| 827 |
# Bare "Aptible" added first, then "Aptible, Inc" → one holder with long name |
| 828 |
self.group.add(CopyrightHolder("Aptible", "", YearRange(2018, 2018))) |
| 829 |
self.group.add(CopyrightHolder("Aptible, Inc", "", YearRange(2017, 2019))) |
| 830 |
self.assertEqual(len(self.group), 1) |
| 831 |
self.assertEqual(list(self.group.sorted_members()), ["2017-2019, Aptible, Inc"]) |
| 832 |
|
| 833 |
def test_merge_two_different_suffixes(self): |
| 834 |
# Both "Foo LLC" and "Foo Inc" normalise to "foo", so they merge. |
| 835 |
# In practice a single copyright file listing both is essentially |
| 836 |
# unheard-of, and keeping one dict keyed by the normalised name is |
| 837 |
# simpler than tracking suffix variants separately. |
| 838 |
self.group.add(CopyrightHolder("Foo LLC", "", YearRange(2020, 2020))) |
| 839 |
self.group.add(CopyrightHolder("Foo Inc", "", YearRange(2021, 2021))) |
| 840 |
self.assertEqual(len(self.group), 1) |
| 841 |
|
| 842 |
def test_no_merge_unrelated_names(self): |
| 843 |
# "Foo" and "Foobar Inc" share no suffix relationship - must not be merged |
| 844 |
self.group.add(CopyrightHolder("Foo", "", YearRange(2020, 2020))) |
| 845 |
self.group.add(CopyrightHolder("Foobar Inc", "", YearRange(2021, 2021))) |
| 846 |
self.assertEqual(len(self.group), 2) |
| 847 |
|
| 848 |
|
| 849 |
class TestEffectiveLicense(unittest.TestCase): |
| 850 |
def test_file_parsed_license_is_raw(self): |
| 851 |
fi = nodes.FileInfo(None, "foo") |
| 852 |
fi.add_licenses({"GPL-2+"}) |
| 853 |
self.assertEqual(fi.parsed_license, "GPL-2+") |
| 854 |
|
| 855 |
def test_file_effective_license_is_raw(self): |
| 856 |
fi = nodes.FileInfo(None, "foo") |
| 857 |
fi.add_licenses({"GPL-2+"}) |
| 858 |
self.assertEqual(fi.effective_license, "GPL-2+") |
| 859 |
|
| 860 |
def test_dir_parsed_license_is_raw_not_inherited(self): |
| 861 |
dirinfo = nodes.DirInfo(None, "dir") |
| 862 |
dirinfo.add_licenses({"GPL-2+"}) |
| 863 |
self.assertEqual(dirinfo.parsed_license, "GPL-2+") |
| 864 |
|
| 865 |
def test_dir_effective_license_is_inherited(self): |
| 866 |
dirinfo = nodes.DirInfo(None, "dir") |
| 867 |
dirinfo.add_licenses({"GPL-2+"}) |
| 868 |
self.assertEqual(dirinfo.effective_license, "Inherited(GPL-2+)") |
| 869 |
|
| 870 |
def test_dir_effective_license_joins_multiple_sorted(self): |
| 871 |
dirinfo = nodes.DirInfo(None, "dir") |
| 872 |
dirinfo.add_licenses({"MIT", "GPL-2+"}) |
| 873 |
self.assertEqual(dirinfo.effective_license, "Inherited(GPL-2+ or MIT)") |
| 874 |
|
| 875 |
def test_dir_effective_license_empty_without_licenses(self): |
| 876 |
self.assertEqual(nodes.DirInfo(None, "dir").effective_license, "") |
| 877 |
|
| 878 |
|
| 879 |
class TestGetLicenseKeyPriority(unittest.TestCase): |
| 880 |
"""Unit tests for license_key(FileInfo) priority order. |
| 881 |
|
| 882 |
Documented priority (highest first): |
| 883 |
1. Explicit stored-group entry where fullname == matching_pattern |
| 884 |
2. License detected by parsing the file (parsed_license) |
| 885 |
3. Wildcarded stored-group entry (fullname != matching_pattern) |
| 886 |
4. Parent directory's get_license_key() |
| 887 |
fallback: UNKNOWN_COPYRIGHTED if copyrights set; UNKNOWN otherwise |
| 888 |
|
| 889 |
These tests use parent=None or minimal DirInfo parents so that the level |
| 890 |
under test is isolated from other levels. |
| 891 |
""" |
| 892 |
|
| 893 |
def _group(self, license_name): |
| 894 |
"""Return a Group whose only license is license_name.""" |
| 895 |
g = dep5.Group() |
| 896 |
g.licenses[license_name] = License(license_name) |
| 897 |
return g |
| 898 |
|
| 899 |
# fallback baseline |
| 900 |
|
| 901 |
def test_no_license_no_parent_returns_unknown(self): |
| 902 |
fi = nodes.FileInfo(parent=None, name="foo.c") |
| 903 |
self.assertEqual(license_key(fi), UNKNOWN) |
| 904 |
|
| 905 |
def test_copyrights_no_license_no_parent_returns_unknown_copyrighted(self): |
| 906 |
fi = nodes.FileInfo(parent=None, name="foo.c") |
| 907 |
fi.copyrights.add(CopyrightHolder("Alice", "[email protected]")) |
| 908 |
self.assertEqual(license_key(fi), UNKNOWN_COPYRIGHTED) |
| 909 |
|
| 910 |
# level 2: parsed_license |
| 911 |
|
| 912 |
def test_parsed_license_returned(self): |
| 913 |
fi = nodes.FileInfo(parent=None, name="foo.c") |
| 914 |
fi.add_licenses({"MIT"}) |
| 915 |
self.assertEqual(license_key(fi), "MIT") |
| 916 |
|
| 917 |
def test_parsed_multiple_licenses_joined_and_sorted(self): |
| 918 |
fi = nodes.FileInfo(parent=None, name="foo.c") |
| 919 |
fi.add_licenses({"MIT", "GPL-2+"}) |
| 920 |
self.assertEqual(license_key(fi), "GPL-2+ or MIT") |
| 921 |
|
| 922 |
# level 3: wildcarded existing_group |
| 923 |
|
| 924 |
def test_wildcarded_existing_group_returned_when_no_parsed_license(self): |
| 925 |
fi = nodes.FileInfo(parent=None, name="src/foo.c") |
| 926 |
fi.existing_group = self._group("GPL-2+") |
| 927 |
fi.matching_pattern = "src/*" |
| 928 |
self.assertEqual(license_key(fi), "GPL-2+") |
| 929 |
|
| 930 |
def test_parsed_license_beats_wildcarded_existing_group(self): |
| 931 |
"""Level 2 (parsed_license) takes priority over level 3 (wildcard match).""" |
| 932 |
fi = nodes.FileInfo(parent=None, name="src/foo.c") |
| 933 |
fi.existing_group = self._group("GPL-2+") |
| 934 |
fi.matching_pattern = "src/*" |
| 935 |
fi.add_licenses({"MIT"}) |
| 936 |
self.assertEqual(license_key(fi), "MIT") |
| 937 |
|
| 938 |
# level 1: exact existing_group |
| 939 |
|
| 940 |
def test_exact_match_existing_group_returned(self): |
| 941 |
fi = nodes.FileInfo(parent=None, name="src/foo.c") |
| 942 |
fi.existing_group = self._group("GPL-2+") |
| 943 |
fi.matching_pattern = "src/foo.c" |
| 944 |
self.assertEqual(license_key(fi), "GPL-2+") |
| 945 |
|
| 946 |
def test_exact_match_existing_group_beats_parsed_license(self): |
| 947 |
"""Level 1 (exact match) takes priority over level 2 (parsed_license). |
| 948 |
|
| 949 |
This is the critical ordering: an explicit debian/copyright entry for |
| 950 |
this exact filename overrides whatever license was detected in the file. |
| 951 |
""" |
| 952 |
fi = nodes.FileInfo(parent=None, name="src/foo.c") |
| 953 |
fi.existing_group = self._group("GPL-2+") |
| 954 |
fi.matching_pattern = "src/foo.c" |
| 955 |
fi.add_licenses({"MIT"}) |
| 956 |
self.assertEqual(license_key(fi), "GPL-2+") |
| 957 |
|
| 958 |
# level 4: parent |
| 959 |
|
| 960 |
def test_parent_license_inherited_when_file_has_none(self): |
| 961 |
src = nodes.DirInfo(parent=None, name="src") |
| 962 |
src.add_licenses({"GPL-2+"}) |
| 963 |
fi = nodes.FileInfo(parent=src, name="foo.c") |
| 964 |
self.assertEqual(license_key(fi), "Inherited(GPL-2+)") |
| 965 |
|
| 966 |
def test_parsed_license_beats_parent_license(self): |
| 967 |
src = nodes.DirInfo(parent=None, name="src") |
| 968 |
src.add_licenses({"GPL-2+"}) |
| 969 |
fi = nodes.FileInfo(parent=src, name="foo.c") |
| 970 |
fi.add_licenses({"MIT"}) |
| 971 |
self.assertEqual(license_key(fi), "MIT") |
| 972 |
|
| 973 |
def test_grandparent_license_inherited_through_unlicensed_dir(self): |
| 974 |
root = nodes.DirInfo(parent=None, name="") |
| 975 |
root.add_licenses({"Apache-2"}) |
| 976 |
lib = nodes.DirInfo(parent=root, name="lib") # no license on lib itself |
| 977 |
fi = nodes.FileInfo(parent=lib, name="util.c") |
| 978 |
self.assertEqual(license_key(fi), "Inherited(Apache-2)") |
| 979 |
|
| 980 |
|
| 981 |
class TestIgnoreLists(unittest.TestCase): |
| 982 |
"""The build walk prunes .gitignore / .git/info/exclude / Files-Excluded |
| 983 |
paths before they enter the tree.""" |
| 984 |
|
| 985 |
def _build(self, files, *, files_excluded=(), use_gitignore=True): |
| 986 |
tmp = tempfile.mkdtemp() |
| 987 |
for rel, content in files.items(): |
| 988 |
path = Path(tmp) / rel |
| 989 |
path.parent.mkdir(parents=True, exist_ok=True) |
| 990 |
path.write_text(content) |
| 991 |
args = ["--no-progress", "--root", tmp] |
| 992 |
if not use_gitignore: |
| 993 |
args.append("--no-gitignore") |
| 994 |
options = cmdoptions.process_options(args) |
| 995 |
options = dataclasses.replace(options, files_excluded=tuple(files_excluded)) |
| 996 |
filetree = tree.RootInfo.build(options) |
| 997 |
return {f.fullname for f in filetree.walk_files()} |
| 998 |
|
| 999 |
def test_gitignore_prunes_files_and_dirs(self): |
| 1000 |
names = self._build( |
| 1001 |
{ |
| 1002 |
"keep.c": "x", |
| 1003 |
"skip.log": "x", |
| 1004 |
"build/out.o": "x", |
| 1005 |
"src/main.c": "x", |
| 1006 |
".gitignore": "*.log\nbuild/\n", |
| 1007 |
} |
| 1008 |
) |
| 1009 |
self.assertIn("keep.c", names) |
| 1010 |
self.assertIn("src/main.c", names) |
| 1011 |
self.assertNotIn("skip.log", names) |
| 1012 |
self.assertNotIn("build/out.o", names) |
| 1013 |
|
| 1014 |
def test_no_gitignore_keeps_gitignored_but_drops_files_excluded(self): |
| 1015 |
names = self._build( |
| 1016 |
{"skip.log": "x", "drop.txt": "x", "keep.c": "x", ".gitignore": "*.log\n"}, |
| 1017 |
files_excluded=("drop.txt",), |
| 1018 |
use_gitignore=False, |
| 1019 |
) |
| 1020 |
self.assertIn("skip.log", names) |
| 1021 |
self.assertIn("keep.c", names) |
| 1022 |
self.assertNotIn("drop.txt", names) |
| 1023 |
|
| 1024 |
def test_nested_gitignore_scopes_to_subtree(self): |
| 1025 |
names = self._build( |
| 1026 |
{ |
| 1027 |
"tmp/a.c": "x", |
| 1028 |
"src/tmp/b.c": "x", |
| 1029 |
"src/keep.c": "x", |
| 1030 |
"src/.gitignore": "tmp/\n", |
| 1031 |
} |
| 1032 |
) |
| 1033 |
self.assertIn("tmp/a.c", names) |
| 1034 |
self.assertIn("src/keep.c", names) |
| 1035 |
self.assertNotIn("src/tmp/b.c", names) |
| 1036 |
|
| 1037 |
def test_files_excluded_directory_excludes_subtree(self): |
| 1038 |
names = self._build( |
| 1039 |
{"vendor/lib/x.c": "x", "keep.c": "x"}, |
| 1040 |
files_excluded=("vendor",), |
| 1041 |
) |
| 1042 |
self.assertIn("keep.c", names) |
| 1043 |
self.assertNotIn("vendor/lib/x.c", names) |
| 1044 |
|
| 1045 |
def test_git_info_exclude_honoured(self): |
| 1046 |
names = self._build( |
| 1047 |
{ |
| 1048 |
"notes.bak": "x", |
| 1049 |
"keep.c": "x", |
| 1050 |
".git/info/exclude": "*.bak\n", |
| 1051 |
} |
| 1052 |
) |
| 1053 |
self.assertIn("keep.c", names) |
| 1054 |
self.assertNotIn("notes.bak", names) |
| 1055 |
|
| 1056 |
|
| 1057 |
if __name__ == "__main__": |
| 1058 |
unittest.main() |