| 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 scripts/spdx_template_as_re.py's template-analysis helpers.""" |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import json |
| 22 |
import sys |
| 23 |
from pathlib import Path |
| 24 |
|
| 25 |
import pytest |
| 26 |
import regex as rex |
| 27 |
|
| 28 |
ROOT = Path(__file__).resolve().parent.parent |
| 29 |
sys.path.insert(0, str(ROOT / "scripts")) |
| 30 |
|
| 31 |
import spdx_template_as_re as tar # noqa: E402 |
| 32 |
|
| 33 |
from decopy.licensing import licenses # noqa: E402 |
| 34 |
|
| 35 |
LICENSES_DIR = ROOT / "tests" / "testdata" / "licenses" |
| 36 |
|
| 37 |
|
| 38 |
@pytest.mark.parametrize("license_id", ["MIT", "ISC", "HPND", "0BSD"]) |
| 39 |
def test_template_to_regex_matches_real_license_text(license_id: str) -> None: |
| 40 |
data = json.loads((LICENSES_DIR / f"{license_id}.json").read_text()) |
| 41 |
regex_str = tar.template_to_regex(data["standardLicenseTemplate"]) |
| 42 |
pattern = rex.compile(regex_str, rex.IGNORECASE | rex.DOTALL) |
| 43 |
assert pattern.search(licenses.clean_comments(data["licenseText"])) |
| 44 |
|
| 45 |
|
| 46 |
def test_template_literal_spans_splits_on_var_token() -> None: |
| 47 |
template = 'Alpha <<var;name="x";original="Y";match=".*">> beta.' |
| 48 |
assert tar.template_literal_spans(template) == ["Alpha", "beta."] |
| 49 |
|
| 50 |
|
| 51 |
def test_template_literal_spans_splits_on_paragraph_break() -> None: |
| 52 |
template = "Paragraph one is here.\n\nParagraph two follows." |
| 53 |
assert tar.template_literal_spans(template) == [ |
| 54 |
"Paragraph one is here.", |
| 55 |
"Paragraph two follows.", |
| 56 |
] |
| 57 |
|
| 58 |
|
| 59 |
def test_template_literal_spans_preserves_punctuation() -> None: |
| 60 |
# A stripped quote must not silently become whitespace: the live source |
| 61 |
# this is meant to search still has the quote character, not a space. |
| 62 |
template = 'files (the "Software"), to deal' |
| 63 |
assert tar.template_literal_spans(template) == ['files (the "Software"), to deal'] |
| 64 |
|
| 65 |
|
| 66 |
def test_template_literal_spans_collapses_wrapped_whitespace() -> None: |
| 67 |
template = "one\n two three" |
| 68 |
assert tar.template_literal_spans(template) == ["one two three"] |
| 69 |
|
| 70 |
|
| 71 |
def test_is_dubious_flags_placeholder_shapes() -> None: |
| 72 |
assert tar.is_dubious("copy of [NAME] here") |
| 73 |
assert tar.is_dubious("fill in the ____ blank") |
| 74 |
assert tar.is_dubious("insert the LIST of names") |
| 75 |
assert not tar.is_dubious("ordinary licence prose with no placeholders") |
| 76 |
|
| 77 |
|
| 78 |
def test_distinctive_window_returns_none_when_fully_contained_elsewhere() -> None: |
| 79 |
span = "this is a long enough literal phrase for testing purposes here today" |
| 80 |
other = span + " and then some more words after it" |
| 81 |
assert tar.distinctive_window([span], [other]) is None |
| 82 |
|
| 83 |
|
| 84 |
def test_distinctive_window_skips_dubious_but_finds_a_clean_window() -> None: |
| 85 |
span = ( |
| 86 |
"a prefix long enough to pass the minimum length before any dubious text " |
| 87 |
"then a dubious [PLACEHOLDER] marker right here and then a clean suffix " |
| 88 |
"that is also long enough on its own to qualify as a window by itself" |
| 89 |
) |
| 90 |
window = tar.distinctive_window([span], []) |
| 91 |
assert window is not None |
| 92 |
assert "[PLACEHOLDER]" not in window |
| 93 |
|
| 94 |
|
| 95 |
def test_distinctive_window_never_crosses_a_span_boundary() -> None: |
| 96 |
spans = ["short end of one paragraph here", "start of the next paragraph here"] |
| 97 |
window = tar.distinctive_window(spans, [], min_len=10, max_len=200) |
| 98 |
assert window is not None |
| 99 |
assert window in spans[0] or window in spans[1] |
| 100 |
|
| 101 |
|
| 102 |
def test_window_to_regexp_query_tolerates_rewrapped_whitespace() -> None: |
| 103 |
window = 'the "Software" (as defined)' |
| 104 |
query = tar.window_to_regexp_query(window) |
| 105 |
pattern = rex.compile(query) |
| 106 |
assert pattern.search('the "Software"\n (as defined)') |
| 107 |
assert pattern.search("the (Software) (as defined)") is None |