| 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 |
"""Tests for the LicenseTextResolver stanza-body ladder.""" |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
from decopy.licensing.license_texts import WRAP_WIDTH, LicenseTextResolver |
| 22 |
|
| 23 |
|
| 24 |
def test_reference_gpl2(): |
| 25 |
body = LicenseTextResolver().body("GPL-2") |
| 26 |
assert body is not None |
| 27 |
assert "`/usr/share/common-licenses/GPL-2'" in body |
| 28 |
# The friendly name may span a wrap break, so compare whitespace-normalised. |
| 29 |
assert "the GNU General Public License version 2" in " ".join(body.split()) |
| 30 |
|
| 31 |
|
| 32 |
def test_reference_or_later_uses_same_file(): |
| 33 |
body = LicenseTextResolver().body("GPL-2+") |
| 34 |
assert body is not None |
| 35 |
assert "`/usr/share/common-licenses/GPL-2'" in body |
| 36 |
|
| 37 |
|
| 38 |
def test_none_for_uncurated(): |
| 39 |
# Neither in the common-licenses map nor the embedded set. |
| 40 |
assert LicenseTextResolver().body("Beerware") is None |
| 41 |
|
| 42 |
|
| 43 |
def test_embedded_full_text(): |
| 44 |
body = LicenseTextResolver().body("Expat") |
| 45 |
assert body is not None |
| 46 |
assert "Permission is hereby granted, free of charge" in body |
| 47 |
# An embedded text, not a common-licenses reference. |
| 48 |
assert "/usr/share/common-licenses" not in body |
| 49 |
|
| 50 |
|
| 51 |
def test_embedded_text_is_not_wrapped(): |
| 52 |
# Embedded texts are authoritative and kept verbatim (unlike generated |
| 53 |
# prose), so their original long lines are preserved. |
| 54 |
body = LicenseTextResolver().body("Expat") |
| 55 |
assert body is not None |
| 56 |
assert any(len(line) > WRAP_WIDTH for line in body.split("\n")) |
| 57 |
|
| 58 |
|
| 59 |
def test_reference_none_for_markers(): |
| 60 |
resolver = LicenseTextResolver() |
| 61 |
assert resolver.body("public-domain") is None |
| 62 |
assert resolver.body("Unknown") is None |
| 63 |
|
| 64 |
|
| 65 |
def test_reference_wrapped_to_content_width(): |
| 66 |
body = LicenseTextResolver().body("GPL-2") |
| 67 |
assert body is not None |
| 68 |
# 78-column target minus the renderer's single-space continuation indent. |
| 69 |
assert all(len(line) <= WRAP_WIDTH for line in body.split("\n")) |
| 70 |
# It really wrapped (the sentence does not fit on one line). |
| 71 |
assert "\n" in body |
| 72 |
|
| 73 |
|
| 74 |
def test_reference_path_not_split(): |
| 75 |
body = LicenseTextResolver().body("GPL-2") |
| 76 |
assert body is not None |
| 77 |
assert any( |
| 78 |
"`/usr/share/common-licenses/GPL-2'." in line for line in body.split("\n") |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
def test_reference_content_preserved(): |
| 83 |
body = LicenseTextResolver().body("GPL-2") |
| 84 |
assert body is not None |
| 85 |
# The reference sentence is the last block, after the header. |
| 86 |
reference = body.split("\n\n")[-1] |
| 87 |
joined = " ".join(line.strip() for line in reference.split("\n")) |
| 88 |
assert joined == ( |
| 89 |
"On Debian systems, the complete text of the GNU General Public " |
| 90 |
"License version 2 can be found in `/usr/share/common-licenses/GPL-2'." |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
def test_header_prepended_before_reference(): |
| 95 |
body = LicenseTextResolver().body("GPL-2") |
| 96 |
assert body is not None |
| 97 |
normalised = " ".join(body.split()) |
| 98 |
assert "This program is free software" in normalised |
| 99 |
# The header comes before the common-licenses reference. |
| 100 |
assert body.index("This program is free software") < body.index( |
| 101 |
"/usr/share/common-licenses/GPL-2" |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
def test_only_and_or_later_headers_differ(): |
| 106 |
only = LicenseTextResolver().body("GPL-2") |
| 107 |
later = LicenseTextResolver().body("GPL-2+") |
| 108 |
assert only is not None |
| 109 |
assert later is not None |
| 110 |
# The or-later notice grants "any later version"; the only one does not. |
| 111 |
assert "any later version" not in " ".join(only.split()) |
| 112 |
assert "any later version" in " ".join(later.split()) |
| 113 |
# Both still reference the same common-licenses file. |
| 114 |
assert "/usr/share/common-licenses/GPL-2" in only |
| 115 |
assert "/usr/share/common-licenses/GPL-2" in later |
| 116 |
|
| 117 |
|
| 118 |
def test_reference_without_header_is_reference_only(): |
| 119 |
# BSL-1 is in the common-licenses map but has no generated header. |
| 120 |
body = LicenseTextResolver().body("BSL-1") |
| 121 |
assert body is not None |
| 122 |
assert "/usr/share/common-licenses/BSL-1.0" in body |
| 123 |
assert "This program is free software" not in body |
| 124 |
|
| 125 |
|
| 126 |
def test_header_body_lines_within_width(): |
| 127 |
body = LicenseTextResolver().body("GPL-2") |
| 128 |
assert body is not None |
| 129 |
assert all(len(line) <= WRAP_WIDTH for line in body.split("\n")) |
| 130 |
|
| 131 |
|
| 132 |
def test_with_exception_full(): |
| 133 |
body = LicenseTextResolver().body("GPL-2+ with AutoConf exception") |
| 134 |
assert body is not None |
| 135 |
# base body: the GPL-2 common-licenses reference + "version 2 or later" header |
| 136 |
assert "`/usr/share/common-licenses/GPL-2'" in body |
| 137 |
assert "or (at your option) any later version" in " ".join(body.split()) |
| 138 |
# appended verbatim exception text, blank-line separated from the base |
| 139 |
assert "As a special exception" in body |
| 140 |
assert "output of Autoconf" in body |
| 141 |
|
| 142 |
|
| 143 |
def test_with_exception_uncurated_keeps_base_and_notes_label(): |
| 144 |
body = LicenseTextResolver().body("GPL-2 with Nonesuch exception") |
| 145 |
assert body is not None |
| 146 |
assert "`/usr/share/common-licenses/GPL-2'" in body # base body kept |
| 147 |
assert "Nonesuch" in body # the missing exception is named, not dropped |
| 148 |
|
| 149 |
|
| 150 |
def test_with_exception_uncurated_base_only_still_emits_block(): |
| 151 |
# A base with no curated body (Beerware) plus an uncurated exception still |
| 152 |
# produces a non-empty stanza naming the exception. |
| 153 |
body = LicenseTextResolver().body("Beerware with Nonesuch exception") |
| 154 |
assert body is not None |
| 155 |
assert "Nonesuch" in body |
| 156 |
|
| 157 |
|
| 158 |
def test_embedded_exception_text_not_wrapped(): |
| 159 |
# The exception text is verbatim (like embedded licences), not reflowed. |
| 160 |
body = LicenseTextResolver().body("GPL-2 with AutoConf exception") |
| 161 |
assert body is not None |
| 162 |
assert any(len(line) > WRAP_WIDTH for line in body.split("\n")) |