| 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 |
"""Non-redundant, non-flaky module-structure checks.""" |
| 18 |
|
| 19 |
import importlib |
| 20 |
import sys |
| 21 |
import unittest |
| 22 |
|
| 23 |
import debian.copyright as dc |
| 24 |
|
| 25 |
from decopy.dep5doc.copyright_contracts import CopyrightProtocol |
| 26 |
from decopy.dep5doc.dep5 import Copyright, Group, read_copyright |
| 27 |
from decopy.grouping.group_contracts import GroupProtocol, LicenseKeyed |
| 28 |
from decopy.licensing.holders import CopyrightHolders |
| 29 |
|
| 30 |
_ALL_MODULES = [ |
| 31 |
"decopy.cmdoptions", |
| 32 |
"decopy.common.core_types", |
| 33 |
"decopy.dep5doc.dep5", |
| 34 |
"decopy.dep5doc.copyright_reader", |
| 35 |
"decopy.grouping.grouping", |
| 36 |
"decopy.grouping.grouping_strategy", |
| 37 |
"decopy.licensing.holders", |
| 38 |
"decopy.licensing.license_parsers", |
| 39 |
"decopy.licensing.license_subs", |
| 40 |
"decopy.licensing.licenses", |
| 41 |
"decopy.fstree.nodes", |
| 42 |
"decopy.fstree.vcs", |
| 43 |
"decopy.dep5doc.output", |
| 44 |
"decopy.common.re_types", |
| 45 |
"decopy.dep5doc.renderer", |
| 46 |
"decopy.licensing.res", |
| 47 |
"decopy.fstree.tree", |
| 48 |
] |
| 49 |
|
| 50 |
|
| 51 |
class TestImportOrder(unittest.TestCase): |
| 52 |
"""Modules can be imported in any order without circular-import errors.""" |
| 53 |
|
| 54 |
def setUp(self): |
| 55 |
# Snapshot every loaded decopy.* module so re-importing below cannot |
| 56 |
# leak a second copy of a class (e.g. a fresh nodes.DirInfo) into later |
| 57 |
# tests, which would break isinstance checks across module boundaries. |
| 58 |
self._saved_modules = { |
| 59 |
name: module |
| 60 |
for name, module in sys.modules.items() |
| 61 |
if name == "decopy" or name.startswith("decopy.") |
| 62 |
} |
| 63 |
|
| 64 |
def tearDown(self): |
| 65 |
for name in [ |
| 66 |
n for n in sys.modules if n == "decopy" or n.startswith("decopy.") |
| 67 |
]: |
| 68 |
if name not in self._saved_modules: |
| 69 |
del sys.modules[name] |
| 70 |
sys.modules.update(self._saved_modules) |
| 71 |
|
| 72 |
def _reimport_all(self, order): |
| 73 |
for name in _ALL_MODULES: |
| 74 |
sys.modules.pop(name, None) |
| 75 |
for name in order: |
| 76 |
importlib.import_module(name) |
| 77 |
|
| 78 |
def test_forward_order(self): |
| 79 |
self._reimport_all(_ALL_MODULES) |
| 80 |
|
| 81 |
def test_reverse_order(self): |
| 82 |
self._reimport_all(list(reversed(_ALL_MODULES))) |
| 83 |
|
| 84 |
def test_sorted_order(self): |
| 85 |
self._reimport_all(sorted(_ALL_MODULES)) |
| 86 |
|
| 87 |
|
| 88 |
class TestCopyrightProtocolImplementation(unittest.TestCase): |
| 89 |
"""Copyright implements CopyrightProtocol and dc.Copyright.""" |
| 90 |
|
| 91 |
def setUp(self): |
| 92 |
self.copyright = Copyright() |
| 93 |
|
| 94 |
def test_implements_copyright_protocol(self): |
| 95 |
self.assertIsInstance(self.copyright, CopyrightProtocol) |
| 96 |
|
| 97 |
def test_protocol_requires_rendering_members(self): |
| 98 |
"""The rendering path reads copyright_.header and .registry, so the |
| 99 |
protocol must require them, not just the processing methods.""" |
| 100 |
|
| 101 |
class WithoutRenderingMembers: |
| 102 |
groups = None |
| 103 |
|
| 104 |
@staticmethod |
| 105 |
def build(filetree, options, registry=None): ... |
| 106 |
def process(self, filetree): ... |
| 107 |
def remove_misplaced_files(self, options, assigned=None): ... |
| 108 |
def get_group_dict(self, options): |
| 109 |
return {} |
| 110 |
|
| 111 |
self.assertNotIsInstance(WithoutRenderingMembers(), CopyrightProtocol) |
| 112 |
|
| 113 |
def test_composes_not_inherits_dc_copyright(self): |
| 114 |
# Copyright holds a dc.Copyright by composition; it is not one. The |
| 115 |
# header (a dc.Header) is exposed by delegation. |
| 116 |
self.assertNotIsInstance(self.copyright, dc.Copyright) |
| 117 |
self.assertIsInstance(self.copyright.header, dc.Header) |
| 118 |
|
| 119 |
def test_groups_initially_none(self): |
| 120 |
self.assertIsNone(self.copyright.groups) |
| 121 |
|
| 122 |
def test_reader_builds_copyright(self): |
| 123 |
# Reading an existing file is the reader's job, not the class's; the |
| 124 |
# class no longer carries a build method. |
| 125 |
self.assertTrue(callable(read_copyright)) |
| 126 |
self.assertFalse(hasattr(Copyright, "build")) |
| 127 |
|
| 128 |
def test_has_process_method(self): |
| 129 |
self.assertTrue(callable(self.copyright.process)) |
| 130 |
|
| 131 |
def test_has_remove_misplaced_files_method(self): |
| 132 |
self.assertTrue(callable(self.copyright.remove_misplaced_files)) |
| 133 |
|
| 134 |
def test_has_get_group_dict_method(self): |
| 135 |
self.assertTrue(callable(self.copyright.get_group_dict)) |
| 136 |
|
| 137 |
|
| 138 |
class TestGroupProtocolImplementation(unittest.TestCase): |
| 139 |
"""Group implements GroupProtocol with expected attribute types.""" |
| 140 |
|
| 141 |
def setUp(self): |
| 142 |
self.group = Group(position=0) |
| 143 |
|
| 144 |
def test_implements_group_protocol(self): |
| 145 |
self.assertIsInstance(self.group, GroupProtocol) |
| 146 |
|
| 147 |
def test_license_is_string(self): |
| 148 |
self.assertIsInstance(self.group.license, str) |
| 149 |
|
| 150 |
def test_key_initially_none(self): |
| 151 |
self.assertIsNone(self.group.key) |
| 152 |
|
| 153 |
def test_position_is_int(self): |
| 154 |
self.assertEqual(self.group.position, 0) |
| 155 |
|
| 156 |
def test_copyrights_is_copyright_group(self): |
| 157 |
self.assertIsInstance(self.group.copyrights, CopyrightHolders) |
| 158 |
|
| 159 |
def test_licenses_is_dict(self): |
| 160 |
self.assertIsInstance(self.group.licenses, dict) |
| 161 |
|
| 162 |
def test_license_filenames_is_set(self): |
| 163 |
self.assertIsInstance(self.group.license_filenames, set) |
| 164 |
|
| 165 |
def test_comments_is_list(self): |
| 166 |
self.assertIsInstance(self.group.comments, list) |
| 167 |
|
| 168 |
def test_stored_initially_none(self): |
| 169 |
self.assertIsNone(self.group.stored) |
| 170 |
|
| 171 |
def test_group_protocol_has_get_license_key(self): |
| 172 |
self.assertTrue(hasattr(GroupProtocol, "get_license_key")) |
| 173 |
|
| 174 |
def test_implements_license_keyed(self): |
| 175 |
self.assertIsInstance(self.group, LicenseKeyed) |
| 176 |
|
| 177 |
def test_license_keyed_has_get_license_key(self): |
| 178 |
self.assertTrue(hasattr(LicenseKeyed, "get_license_key")) |
| 179 |
|
| 180 |
def test_get_license_key_returns_string(self): |
| 181 |
result = self.group.get_license_key() |
| 182 |
self.assertIsInstance(result, str) |
| 183 |
|
| 184 |
|
| 185 |
if __name__ == "__main__": |
| 186 |
unittest.main() |