| 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 license literal prefilter. |
| 18 |
|
| 19 |
The critical property is soundness: candidate_indices(text) must be a SUPERSET of |
| 20 |
the rules that actually match, so a prefiltered find_licenses() never misses a |
| 21 |
detection. (The full pipeline equivalence is covered separately when the prefilter |
| 22 |
is wired into find_licenses.) |
| 23 |
""" |
| 24 |
|
| 25 |
from __future__ import annotations |
| 26 |
|
| 27 |
import json |
| 28 |
import unittest |
| 29 |
from pathlib import Path |
| 30 |
from unittest.mock import patch |
| 31 |
|
| 32 |
from decopy.licensing import license_prefilter |
| 33 |
from decopy.licensing.licenses import clean_comments, find_licenses |
| 34 |
from decopy.licensing.res import COMPILED_RES, LICENSES_RES |
| 35 |
|
| 36 |
_LICENSES_DIR = Path(__file__).parent / "testdata" / "licenses" |
| 37 |
|
| 38 |
|
| 39 |
def _rule_index(name: str) -> int: |
| 40 |
return next(i for i, r in enumerate(LICENSES_RES) if r.license == name) |
| 41 |
|
| 42 |
|
| 43 |
class TestLicensePrefilter(unittest.TestCase): |
| 44 |
def test_available_in_this_env(self): |
| 45 |
# pyahocorasick is a declared optional dep; the test environment has it. |
| 46 |
self.assertTrue(license_prefilter.prefilter_available()) |
| 47 |
self.assertIsInstance(license_prefilter.candidate_indices("x"), frozenset) |
| 48 |
|
| 49 |
def test_candidates_superset_of_actual_matches(self): |
| 50 |
# Soundness: every rule that matches the (cleaned) text must be a |
| 51 |
# candidate. This is what guarantees prefiltering loses no detection. |
| 52 |
files = sorted(_LICENSES_DIR.glob("*.json"))[:40] |
| 53 |
checked = 0 |
| 54 |
for path in files: |
| 55 |
data = json.loads(path.read_text(encoding="utf-8")) |
| 56 |
for key in ("licenseText", "standardLicenseHeader"): |
| 57 |
text = data.get(key) |
| 58 |
if not text: |
| 59 |
continue |
| 60 |
cleaned = clean_comments(text) |
| 61 |
candidates = license_prefilter.candidate_indices(cleaned) |
| 62 |
assert candidates is not None |
| 63 |
matched = { |
| 64 |
i for i, pat in enumerate(COMPILED_RES) if pat.search(cleaned) |
| 65 |
} |
| 66 |
self.assertEqual( |
| 67 |
matched - candidates, |
| 68 |
set(), |
| 69 |
f"{path.name}/{key}: matched rules missing from candidates", |
| 70 |
) |
| 71 |
checked += 1 |
| 72 |
self.assertGreater(checked, 0) |
| 73 |
|
| 74 |
def test_find_licenses_prefiltered_equals_unfiltered(self): |
| 75 |
# The gate: prefiltered find_licenses must equal find_licenses running |
| 76 |
# every rule (prefilter disabled via the None fallback), over a diverse |
| 77 |
# corpus. Patching candidate_indices to None also exercises the fallback. |
| 78 |
sample = sorted(_LICENSES_DIR.glob("*.json"))[::7] |
| 79 |
compared = 0 |
| 80 |
for path in sample: |
| 81 |
data = json.loads(path.read_text(encoding="utf-8")) |
| 82 |
for key in ("licenseText", "standardLicenseHeader", "licenseExceptionText"): |
| 83 |
text = data.get(key) |
| 84 |
if not text: |
| 85 |
continue |
| 86 |
cleaned = clean_comments(text) |
| 87 |
prefiltered = list(find_licenses(cleaned)) |
| 88 |
with patch( |
| 89 |
"decopy.licensing.licenses.candidate_indices", return_value=None |
| 90 |
): |
| 91 |
unfiltered = list(find_licenses(cleaned)) |
| 92 |
self.assertEqual(prefiltered, unfiltered, f"{path.name}/{key}") |
| 93 |
compared += 1 |
| 94 |
self.assertGreater(compared, 50) |
| 95 |
|
| 96 |
def test_known_literal_selects_its_rule(self): |
| 97 |
cpl = _rule_index("CPL") |
| 98 |
candidates = license_prefilter.candidate_indices( |
| 99 |
"Licensed under the Common Public License Version 1.0", |
| 100 |
) |
| 101 |
assert candidates is not None |
| 102 |
self.assertIn(cpl, candidates) |
| 103 |
|
| 104 |
def test_expensive_rule_skipped_for_plain_text(self): |
| 105 |
# An expensive family rule must not be a candidate for non-license prose. |
| 106 |
font = _rule_index("FontLicense") |
| 107 |
candidates = license_prefilter.candidate_indices( |
| 108 |
"the quick brown fox jumps over the lazy dog", |
| 109 |
) |
| 110 |
assert candidates is not None |
| 111 |
self.assertNotIn(font, candidates) |
| 112 |
|
| 113 |
def test_plain_text_yields_few_candidates(self): |
| 114 |
# Sanity: non-license text should select almost nothing (only any |
| 115 |
# always-run rules), proving the prefilter actually prunes. |
| 116 |
candidates = license_prefilter.candidate_indices("hello world\n") |
| 117 |
assert candidates is not None |
| 118 |
self.assertLess(len(candidates), 10) |
| 119 |
|
| 120 |
|
| 121 |
if __name__ == "__main__": |
| 122 |
unittest.main() |