| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Unit tests for the Renderer 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 renderer.Dep5Renderer. |
| 20 |
|
| 21 |
These pin each rendered piece in isolation; they must agree with the |
| 22 |
generate_output characterization tests in output_test.py, which lock the |
| 23 |
equivalent text at the (still-live) orchestration the renderer will replace. |
| 24 |
""" |
| 25 |
|
| 26 |
import contextlib |
| 27 |
import io |
| 28 |
import unittest |
| 29 |
|
| 30 |
import debian.copyright as dc |
| 31 |
|
| 32 |
from decopy import cmdoptions |
| 33 |
from decopy.common.core_types import CopyrightHolder, LicenseRegistry |
| 34 |
from decopy.dep5doc import dep5 |
| 35 |
from decopy.dep5doc.renderer import Dep5Renderer, Renderer, renderer_for |
| 36 |
from decopy.fstree import nodes |
| 37 |
from decopy.licensing.license_texts import LicenseTextResolver |
| 38 |
|
| 39 |
_CURRENT_FORMAT = ( |
| 40 |
"Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\n" |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
def _options(args): |
| 45 |
base = ["--no-progress", "--root", "tests/testdata/one"] |
| 46 |
return cmdoptions.process_options([*base, *args]) |
| 47 |
|
| 48 |
|
| 49 |
class HeaderTest(unittest.TestCase): |
| 50 |
def setUp(self): |
| 51 |
self.renderer = Dep5Renderer() |
| 52 |
|
| 53 |
def test_full_mode_without_copyright(self): |
| 54 |
header = self.renderer.header(None, _options([])) |
| 55 |
self.assertEqual(header, _CURRENT_FORMAT.rstrip("\n") + "\nSource: TODO") |
| 56 |
|
| 57 |
def test_partial_mode_lists_files(self): |
| 58 |
options = cmdoptions.process_options(["--mode=partial", "."]) |
| 59 |
header = self.renderer.header(None, options) |
| 60 |
self.assertEqual( |
| 61 |
header, |
| 62 |
_CURRENT_FORMAT.rstrip("\n") + "\nSource: TODO\nComment: *** only: . ***", |
| 63 |
) |
| 64 |
|
| 65 |
def test_existing_copyright_overrides_format_keeps_fields(self): |
| 66 |
text = ( |
| 67 |
"Format: https://example.org/old-format\n" |
| 68 |
"Upstream-Name: example\n" |
| 69 |
"Source: https://example.org/dl\n" |
| 70 |
) |
| 71 |
with contextlib.redirect_stderr(io.StringIO()): |
| 72 |
copyright_ = dep5.Copyright(dc.Copyright(io.StringIO(text), strict=False)) |
| 73 |
header = self.renderer.header(copyright_, _options([])) |
| 74 |
self.assertEqual( |
| 75 |
header, |
| 76 |
_CURRENT_FORMAT.rstrip("\n") |
| 77 |
+ "\nUpstream-Name: example\nSource: https://example.org/dl", |
| 78 |
) |
| 79 |
|
| 80 |
def test_header_does_not_mutate_the_copyright(self): |
| 81 |
"""header() is a query: it must not write the canonical Format or a |
| 82 |
default Source back into the caller's document.""" |
| 83 |
text = "Format: https://example.org/old-format\nUpstream-Name: example\n" |
| 84 |
with contextlib.redirect_stderr(io.StringIO()): |
| 85 |
copyright_ = dep5.Copyright(dc.Copyright(io.StringIO(text), strict=False)) |
| 86 |
before_format = copyright_.header.format |
| 87 |
before_source = copyright_.header.source |
| 88 |
|
| 89 |
rendered = self.renderer.header(copyright_, _options([])) |
| 90 |
|
| 91 |
# The input is untouched ... |
| 92 |
self.assertEqual(copyright_.header.format, before_format) |
| 93 |
self.assertEqual(copyright_.header.source, before_source) |
| 94 |
# ... yet the rendered text still canonicalises Format and defaults |
| 95 |
# the missing Source to TODO. |
| 96 |
self.assertEqual( |
| 97 |
rendered, |
| 98 |
_CURRENT_FORMAT.rstrip("\n") + "\nUpstream-Name: example\nSource: TODO", |
| 99 |
) |
| 100 |
# A second render sees the same input and produces the same text. |
| 101 |
self.assertEqual(rendered, self.renderer.header(copyright_, _options([]))) |
| 102 |
|
| 103 |
|
| 104 |
class FileBlockTest(unittest.TestCase): |
| 105 |
def setUp(self): |
| 106 |
self.renderer = Dep5Renderer() |
| 107 |
|
| 108 |
def _group(self): |
| 109 |
root = nodes.DirInfo(parent=None, name="") |
| 110 |
group = dep5.Group(position=0) |
| 111 |
f = nodes.FileInfo(parent=root, name="README") |
| 112 |
f.add_licenses(["MIT"]) |
| 113 |
f.add_copyrights([CopyrightHolder("Alice", "alice@x")]) |
| 114 |
group.add_file(f) |
| 115 |
return group |
| 116 |
|
| 117 |
def test_file_block_renders_dep5_files_paragraph(self): |
| 118 |
options = _options(["--no-glob"]) |
| 119 |
grp = self._group() |
| 120 |
block = self.renderer.file_block(grp, options, grp.files.sorted_members()) |
| 121 |
self.assertEqual( |
| 122 |
block, |
| 123 |
"Files: README\nCopyright: Alice <alice@x>\nLicense: MIT", |
| 124 |
) |
| 125 |
|
| 126 |
def test_file_block_passes_through_extra_stored_fields(self): |
| 127 |
options = _options(["--no-glob"]) |
| 128 |
grp = self._group() |
| 129 |
# The created paragraph already carries Files/Copyright/License; those |
| 130 |
# standard fields must NOT be echoed (the renderer emits them from the |
| 131 |
# group's own data), only the extra X-Foo passes through. |
| 132 |
stored = dc.FilesParagraph.create( |
| 133 |
["README"], "Alice <alice@x>", dc.License("MIT") |
| 134 |
) |
| 135 |
stored["X-Foo"] = "bar" |
| 136 |
grp.stored = stored |
| 137 |
block = self.renderer.file_block(grp, options, grp.files.sorted_members()) |
| 138 |
self.assertIn("X-Foo: bar", block) |
| 139 |
self.assertEqual(block.count("Files:"), 1) |
| 140 |
self.assertEqual(block.count("License:"), 1) |
| 141 |
self.assertEqual(block.count("Copyright:"), 1) |
| 142 |
|
| 143 |
|
| 144 |
class LicenseBlockTest(unittest.TestCase): |
| 145 |
def setUp(self): |
| 146 |
self.registry = LicenseRegistry() |
| 147 |
self.renderer = Dep5Renderer() |
| 148 |
|
| 149 |
def test_no_stored_text_emits_placeholder(self): |
| 150 |
block = self.renderer.license_block(self.registry.get("MIT"), _options([])) |
| 151 |
self.assertEqual( |
| 152 |
block, "License: MIT\nComment: Add the corresponding license text here" |
| 153 |
) |
| 154 |
|
| 155 |
def test_stored_text_is_included(self): |
| 156 |
stored = dc.License(synopsis="MIT", text="Permission is granted ...") |
| 157 |
license_ = self.registry.get("MIT") |
| 158 |
license_.stored = dc.LicenseParagraph.create(stored) |
| 159 |
block = self.renderer.license_block(license_, _options([])) |
| 160 |
self.assertTrue(block.startswith("License: MIT")) |
| 161 |
self.assertIn("Permission is granted", block) |
| 162 |
|
| 163 |
def test_spdx_converts_display_name(self): |
| 164 |
block = self.renderer.license_block( |
| 165 |
self.registry.get("GPL-2"), _options(["--spdx"]) |
| 166 |
) |
| 167 |
self.assertTrue(block.startswith("License: GPL-2.0-only\n")) |
| 168 |
|
| 169 |
def test_no_spdx_keeps_dep5_name(self): |
| 170 |
block = self.renderer.license_block( |
| 171 |
self.registry.get("GPL-2"), _options(["--no-spdx"]) |
| 172 |
) |
| 173 |
self.assertTrue(block.startswith("License: GPL-2\n")) |
| 174 |
|
| 175 |
|
| 176 |
class LicenseBlockFillTest(unittest.TestCase): |
| 177 |
def setUp(self): |
| 178 |
self.registry = LicenseRegistry() |
| 179 |
self.renderer = Dep5Renderer(LicenseTextResolver()) |
| 180 |
|
| 181 |
def test_fills_common_licenses_reference(self): |
| 182 |
block = self.renderer.license_block(self.registry.get("GPL-2"), _options([])) |
| 183 |
self.assertTrue(block.startswith("License: GPL-2\n")) |
| 184 |
self.assertIn("/usr/share/common-licenses/GPL-2", block) |
| 185 |
self.assertNotIn("Add the corresponding license text here", block) |
| 186 |
|
| 187 |
def test_uncurated_name_keeps_placeholder(self): |
| 188 |
block = self.renderer.license_block(self.registry.get("Beerware"), _options([])) |
| 189 |
self.assertEqual( |
| 190 |
block, |
| 191 |
"License: Beerware\nComment: Add the corresponding license text here", |
| 192 |
) |
| 193 |
|
| 194 |
def test_reference_lines_within_78_columns(self): |
| 195 |
block = self.renderer.license_block(self.registry.get("GPL-2"), _options([])) |
| 196 |
for line in block.split("\n"): |
| 197 |
self.assertLessEqual(len(line), 78, line) |
| 198 |
|
| 199 |
def test_stored_text_wins_over_resolver(self): |
| 200 |
license_ = self.registry.get("GPL-2") |
| 201 |
stored = dc.License(synopsis="GPL-2", text="STORED-BODY-MARKER") |
| 202 |
license_.stored = dc.LicenseParagraph.create(stored) |
| 203 |
block = self.renderer.license_block(license_, _options([])) |
| 204 |
self.assertIn("STORED-BODY-MARKER", block) |
| 205 |
self.assertNotIn("/usr/share/common-licenses", block) |
| 206 |
|
| 207 |
def test_resolver_keys_on_dep5_name_under_spdx(self): |
| 208 |
block = self.renderer.license_block( |
| 209 |
self.registry.get("GPL-2"), _options(["--spdx"]) |
| 210 |
) |
| 211 |
# Display name is SPDX, but the body is resolved from the DEP-5 key. |
| 212 |
self.assertTrue(block.startswith("License: GPL-2.0-only\n")) |
| 213 |
self.assertIn("/usr/share/common-licenses/GPL-2", block) |
| 214 |
|
| 215 |
def test_no_resolver_keeps_placeholder(self): |
| 216 |
block = Dep5Renderer().license_block(self.registry.get("GPL-2"), _options([])) |
| 217 |
self.assertEqual( |
| 218 |
block, "License: GPL-2\nComment: Add the corresponding license text here" |
| 219 |
) |
| 220 |
|
| 221 |
|
| 222 |
class ProtocolConformanceTest(unittest.TestCase): |
| 223 |
def test_dep5renderer_is_a_renderer(self): |
| 224 |
self.assertIsInstance(Dep5Renderer(), Renderer) |
| 225 |
|
| 226 |
|
| 227 |
class RendererForTest(unittest.TestCase): |
| 228 |
def test_selects_dep5_renderer(self): |
| 229 |
self.assertIsInstance(renderer_for(_options([])), Dep5Renderer) |
| 230 |
|
| 231 |
def test_returns_a_renderer(self): |
| 232 |
self.assertIsInstance(renderer_for(_options([])), Renderer) |
| 233 |
|
| 234 |
def test_fill_on_fills_reference(self): |
| 235 |
options = _options([]) |
| 236 |
block = renderer_for(options).license_block( |
| 237 |
LicenseRegistry().get("GPL-2"), options |
| 238 |
) |
| 239 |
self.assertIn("/usr/share/common-licenses/GPL-2", block) |
| 240 |
|
| 241 |
def test_fill_off_keeps_placeholder(self): |
| 242 |
options = _options(["--no-fill-licenses"]) |
| 243 |
block = renderer_for(options).license_block( |
| 244 |
LicenseRegistry().get("GPL-2"), options |
| 245 |
) |
| 246 |
self.assertIn("Add the corresponding license text here", block) |
| 247 |
|
| 248 |
|
| 249 |
if __name__ == "__main__": |
| 250 |
unittest.main() |