| 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 parsers.py - verifies return shapes and helper behaviour.""" |
| 18 |
|
| 19 |
import bz2 |
| 20 |
import gzip |
| 21 |
import lzma |
| 22 |
import re |
| 23 |
import shutil |
| 24 |
import subprocess |
| 25 |
import tempfile |
| 26 |
import unittest |
| 27 |
import warnings |
| 28 |
from pathlib import Path |
| 29 |
|
| 30 |
import xdg.Mime |
| 31 |
|
| 32 |
from decopy.common.core_types import CopyrightHolder, Options |
| 33 |
from decopy.fstree import parsers |
| 34 |
|
| 35 |
|
| 36 |
def _make_options(**kwargs) -> Options: |
| 37 |
defaults: dict = { |
| 38 |
"mode": "full", |
| 39 |
"copyright_file": "debian/copyright", |
| 40 |
"debug": False, |
| 41 |
"text": True, |
| 42 |
"group_by": "license", |
| 43 |
"split_on_license": False, |
| 44 |
"split_debian": False, |
| 45 |
"glob": False, |
| 46 |
"progress": False, |
| 47 |
"jobs": 1, |
| 48 |
"output": "", |
| 49 |
"root": ".", |
| 50 |
"files": [], |
| 51 |
"exclude_fullname_res": (), |
| 52 |
"exclude_special_re": re.compile(""), |
| 53 |
"exclude_file_re": re.compile(""), |
| 54 |
"exclude_directory_re": re.compile(""), |
| 55 |
} |
| 56 |
defaults.update(kwargs) |
| 57 |
return Options(**defaults) |
| 58 |
|
| 59 |
|
| 60 |
class TestParseFile(unittest.TestCase): |
| 61 |
"""parse_file returns (list[CopyrightHolder], ValuesView[str]).""" |
| 62 |
|
| 63 |
def test_returns_tuple_of_list_and_iterable_of_strings(self): |
| 64 |
options = _make_options(text=True) |
| 65 |
holders, licenses = parsers.parse_file("tests/testdata/one/bsd.txt", options) |
| 66 |
self.assertIsInstance(holders, list) |
| 67 |
for lic in licenses: |
| 68 |
self.assertIsInstance(lic, str) |
| 69 |
|
| 70 |
def test_holders_are_copyright_holder_instances(self): |
| 71 |
options = _make_options(text=True) |
| 72 |
# decopy source files carry copyright headers and are always available; |
| 73 |
# use the imported module's own file so this works both in-tree and |
| 74 |
# when running against the installed package (e.g. autopkgtest). |
| 75 |
assert parsers.__file__ is not None |
| 76 |
holders, _ = parsers.parse_file(parsers.__file__, options) |
| 77 |
self.assertGreater(len(holders), 0) |
| 78 |
for holder in holders: |
| 79 |
self.assertIsInstance(holder, CopyrightHolder) |
| 80 |
|
| 81 |
def test_empty_file_returns_empty_holders(self): |
| 82 |
options = _make_options(text=True) |
| 83 |
holders, _ = parsers.parse_file("tests/testdata/one/empty.txt", options) |
| 84 |
self.assertIsInstance(holders, list) |
| 85 |
self.assertEqual(holders, []) |
| 86 |
|
| 87 |
def test_binary_file_is_skipped(self): |
| 88 |
options = _make_options(text=False) |
| 89 |
with tempfile.NamedTemporaryFile(suffix=".o", delete=False) as f: |
| 90 |
f.write(b"\x7fELF\x00\x00Copyright 2024 Ghost <[email protected]>\x00") |
| 91 |
path = f.name |
| 92 |
try: |
| 93 |
holders, _ = parsers.parse_file(path, options) |
| 94 |
self.assertEqual(holders, []) |
| 95 |
finally: |
| 96 |
Path(path).unlink() |
| 97 |
|
| 98 |
|
| 99 |
class TestHasControlChars(unittest.TestCase): |
| 100 |
"""has_control_chars identifies binary-like byte sequences.""" |
| 101 |
|
| 102 |
def test_printable_bytes_returns_false(self): |
| 103 |
self.assertFalse(parsers.has_control_chars(b"hello world\n")) |
| 104 |
|
| 105 |
def test_tab_lf_cr_are_allowed(self): |
| 106 |
self.assertFalse(parsers.has_control_chars(b"\t\n\r")) |
| 107 |
|
| 108 |
def test_null_byte_returns_true(self): |
| 109 |
self.assertTrue(parsers.has_control_chars(b"hello\x00world")) |
| 110 |
|
| 111 |
def test_del_returns_true(self): |
| 112 |
self.assertTrue(parsers.has_control_chars(b"hello\x7fworld")) |
| 113 |
|
| 114 |
def test_other_control_char_returns_true(self): |
| 115 |
self.assertTrue(parsers.has_control_chars(b"hello\x01world")) |
| 116 |
|
| 117 |
|
| 118 |
_COPYRIGHT_BYTES = ( |
| 119 |
b"# Copyright (C) 2024 Test Author <[email protected]>\n" |
| 120 |
b"# SPDX-License-Identifier: MIT\n" |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
class TestParserDispatch(unittest.TestCase): |
| 125 |
"""lookup_mimetypes routes each compression MIME type to the correct parser.""" |
| 126 |
|
| 127 |
def _lookup(self, ext: str) -> parsers.ParserFunc: |
| 128 |
return parsers.lookup_mimetypes(xdg.Mime.get_type(f"dummy{ext}")) |
| 129 |
|
| 130 |
def test_gz_uses_zcat(self): |
| 131 |
self.assertIs(self._lookup(".gz"), parsers.cmd_parser_factory("zcat")) |
| 132 |
|
| 133 |
def test_bz2_uses_bzcat(self): |
| 134 |
self.assertIs(self._lookup(".bz2"), parsers.cmd_parser_factory("bzcat")) |
| 135 |
|
| 136 |
def test_xz_uses_xzcat(self): |
| 137 |
self.assertIs(self._lookup(".xz"), parsers.cmd_parser_factory("xzcat")) |
| 138 |
|
| 139 |
def test_lzma_uses_xzcat(self): |
| 140 |
self.assertIs(self._lookup(".lzma"), parsers.cmd_parser_factory("xzcat")) |
| 141 |
|
| 142 |
def test_zst_uses_zstdcat(self): |
| 143 |
self.assertIs(self._lookup(".zst"), parsers.cmd_parser_factory("zstdcat")) |
| 144 |
|
| 145 |
def test_lz_uses_lzip(self): |
| 146 |
self.assertIs( |
| 147 |
self._lookup(".lz"), parsers.cmd_parser_factory("lzip", "-d", "-c") |
| 148 |
) |
| 149 |
|
| 150 |
def test_bz3_uses_bzip3(self): |
| 151 |
self.assertIs(self._lookup(".bz3"), parsers.cmd_parser_factory("bzip3", "-d")) |
| 152 |
|
| 153 |
|
| 154 |
class TestCompressedFileParsing(unittest.TestCase): |
| 155 |
"""parse_file decompresses and extracts copyright info from compressed files.""" |
| 156 |
|
| 157 |
def _parse(self, suffix: str, data: bytes) -> tuple: |
| 158 |
options = _make_options(text=False) |
| 159 |
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as f: |
| 160 |
f.write(data) |
| 161 |
fname = f.name |
| 162 |
try: |
| 163 |
return parsers.parse_file(fname, options) |
| 164 |
finally: |
| 165 |
Path(fname).unlink() |
| 166 |
|
| 167 |
def _has_author(self, holders: list) -> bool: |
| 168 |
return any("Test Author" in str(h) for h in holders) |
| 169 |
|
| 170 |
def test_gz(self): |
| 171 |
holders, _ = self._parse(".gz", gzip.compress(_COPYRIGHT_BYTES)) |
| 172 |
self.assertTrue(self._has_author(holders)) |
| 173 |
|
| 174 |
def test_bz2(self): |
| 175 |
holders, _ = self._parse(".bz2", bz2.compress(_COPYRIGHT_BYTES)) |
| 176 |
self.assertTrue(self._has_author(holders)) |
| 177 |
|
| 178 |
def test_xz(self): |
| 179 |
data = lzma.compress(_COPYRIGHT_BYTES, format=lzma.FORMAT_XZ) |
| 180 |
holders, _ = self._parse(".xz", data) |
| 181 |
self.assertTrue(self._has_author(holders)) |
| 182 |
|
| 183 |
def test_lzma(self): |
| 184 |
data = lzma.compress(_COPYRIGHT_BYTES, format=lzma.FORMAT_ALONE) |
| 185 |
holders, _ = self._parse(".lzma", data) |
| 186 |
self.assertTrue(self._has_author(holders)) |
| 187 |
|
| 188 |
@unittest.skipUnless( |
| 189 |
shutil.which("zstd") and shutil.which("zstdcat"), "zstd not installed" |
| 190 |
) |
| 191 |
def test_zst(self): |
| 192 |
zstd = shutil.which("zstd") |
| 193 |
assert zstd is not None |
| 194 |
result = subprocess.run( # noqa: S603 |
| 195 |
[zstd, "--quiet", "--stdout"], |
| 196 |
input=_COPYRIGHT_BYTES, |
| 197 |
capture_output=True, |
| 198 |
check=True, |
| 199 |
) |
| 200 |
holders, _ = self._parse(".zst", result.stdout) |
| 201 |
self.assertTrue(self._has_author(holders)) |
| 202 |
|
| 203 |
@unittest.skipUnless(shutil.which("lzip"), "lzip not installed") |
| 204 |
def test_lz(self): |
| 205 |
lzip = shutil.which("lzip") |
| 206 |
assert lzip is not None |
| 207 |
result = subprocess.run( # noqa: S603 |
| 208 |
[lzip, "-c"], |
| 209 |
input=_COPYRIGHT_BYTES, |
| 210 |
capture_output=True, |
| 211 |
check=True, |
| 212 |
) |
| 213 |
holders, _ = self._parse(".lz", result.stdout) |
| 214 |
self.assertTrue(self._has_author(holders)) |
| 215 |
|
| 216 |
|
| 217 |
_PY_SNIPPET = b"# Copyright (C) 2024 Alice <[email protected]>\n" |
| 218 |
# Python 2 only: the "ur" string prefix was removed in Python 3, so tokenize |
| 219 |
# raises TokenError on it (Debian #855836). |
| 220 |
_PY2_SNIPPET = ( |
| 221 |
b"#!/usr/bin/env python\n" |
| 222 |
b"# Copyright (C) 2011 Legacy <[email protected]>\n" |
| 223 |
b'PATTERN = ur"\\w+"\n' |
| 224 |
b'print "hi"\n' |
| 225 |
) |
| 226 |
_HTML_SNIPPET = b"<!-- Copyright 2024 Bob <[email protected]> -->\n<html></html>" |
| 227 |
_PO_SNIPPET = ( |
| 228 |
b"# Copyright 2024 Carol <[email protected]>\n" |
| 229 |
b"# Translator: Dave\n" |
| 230 |
b'msgid "hello"\n' |
| 231 |
b'msgstr ""\n' |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
class TestDetectMimeByMagic(unittest.TestCase): |
| 236 |
"""detect_mime_by_magic identifies format from header bytes.""" |
| 237 |
|
| 238 |
def test_python_shebang(self): |
| 239 |
self.assertEqual( |
| 240 |
parsers.detect_mime_by_magic(b"#!/usr/bin/python3\n"), "text/x-python" |
| 241 |
) |
| 242 |
|
| 243 |
def test_env_python_shebang(self): |
| 244 |
self.assertEqual( |
| 245 |
parsers.detect_mime_by_magic(b"#!/usr/bin/env python3\n"), "text/x-python" |
| 246 |
) |
| 247 |
|
| 248 |
def test_perl_shebang(self): |
| 249 |
self.assertEqual( |
| 250 |
parsers.detect_mime_by_magic(b"#!/usr/bin/perl\n"), "text/x-perl" |
| 251 |
) |
| 252 |
|
| 253 |
def test_xml_declaration(self): |
| 254 |
self.assertEqual( |
| 255 |
parsers.detect_mime_by_magic(b"<?xml version='1.0'?>"), "text/xml" |
| 256 |
) |
| 257 |
|
| 258 |
def test_html_doctype(self): |
| 259 |
self.assertEqual(parsers.detect_mime_by_magic(b"<!DOCTYPE html>"), "text/html") |
| 260 |
|
| 261 |
def test_html_tag(self): |
| 262 |
self.assertEqual(parsers.detect_mime_by_magic(b"<html>"), "text/html") |
| 263 |
|
| 264 |
def test_html_tag_case_insensitive(self): |
| 265 |
self.assertEqual(parsers.detect_mime_by_magic(b"<HTML>"), "text/html") |
| 266 |
|
| 267 |
def test_leading_whitespace_before_html(self): |
| 268 |
self.assertEqual(parsers.detect_mime_by_magic(b" <html>"), "text/html") |
| 269 |
|
| 270 |
def test_non_python_shebang_returns_none(self): |
| 271 |
self.assertIsNone(parsers.detect_mime_by_magic(b"#!/bin/bash\necho hello")) |
| 272 |
|
| 273 |
def test_unknown_returns_none(self): |
| 274 |
self.assertIsNone(parsers.detect_mime_by_magic(b"Hello, world!")) |
| 275 |
|
| 276 |
def test_empty_returns_none(self): |
| 277 |
self.assertIsNone(parsers.detect_mime_by_magic(b"")) |
| 278 |
|
| 279 |
|
| 280 |
class TestContentParsers(unittest.TestCase): |
| 281 |
"""Unit tests for the _*_content_parser helpers.""" |
| 282 |
|
| 283 |
def test_generic_extracts_copyright(self): |
| 284 |
data = b"# Copyright 2024 Eve <[email protected]>\n" |
| 285 |
holders, _ = parsers.generic_content_parser("test.txt", data) |
| 286 |
self.assertTrue(any("Eve" in str(h) for h in holders)) |
| 287 |
|
| 288 |
def test_html_extracts_copyright_from_comment(self): |
| 289 |
holders, _ = parsers.html_content_parser("test.html", _HTML_SNIPPET) |
| 290 |
self.assertTrue(any("Bob" in str(h) for h in holders)) |
| 291 |
|
| 292 |
def test_html_does_not_crash_on_bad_entities(self): |
| 293 |
holders, _ = parsers.html_content_parser( |
| 294 |
"test.html", b"<!-- Copyright && -->\n" |
| 295 |
) |
| 296 |
self.assertIsInstance(holders, list) |
| 297 |
|
| 298 |
def test_po_extracts_copyright_from_comment(self): |
| 299 |
holders, _ = parsers.po_content_parser("test.po", _PO_SNIPPET) |
| 300 |
self.assertTrue(any("Carol" in str(h) for h in holders)) |
| 301 |
|
| 302 |
def test_po_ignores_msgid_lines(self): |
| 303 |
holders, _ = parsers.po_content_parser("test.po", _PO_SNIPPET) |
| 304 |
all_text = " ".join(str(h) for h in holders) |
| 305 |
self.assertNotIn("hello", all_text) |
| 306 |
|
| 307 |
def test_python_extracts_copyright_from_comment(self): |
| 308 |
holders, _ = parsers.python_content_parser("test.py", _PY_SNIPPET) |
| 309 |
self.assertTrue(any("Alice" in str(h) for h in holders)) |
| 310 |
|
| 311 |
def test_python_falls_back_on_invalid_syntax(self): |
| 312 |
holders, _ = parsers.python_content_parser("bad.py", b"\xff\xfe garbage ~~~") |
| 313 |
self.assertIsInstance(holders, list) |
| 314 |
|
| 315 |
def test_python_content_falls_back_on_python2_source(self): |
| 316 |
holders, _ = parsers.python_content_parser("legacy.py", _PY2_SNIPPET) |
| 317 |
self.assertTrue(any("Legacy" in str(h) for h in holders)) |
| 318 |
|
| 319 |
def test_python_invalid_escape_emits_no_syntaxwarning(self): |
| 320 |
# Foreign source (e.g. ruff fixtures) may contain invalid escape |
| 321 |
# sequences such as f"\{x}". Tokenizing it to extract comments must not |
| 322 |
# leak a SyntaxWarning to the user's stderr. |
| 323 |
data = b'# Copyright 2024 Alice <[email protected]>\nx = f"\\{foo}\\{bar:\\}"\n' |
| 324 |
with warnings.catch_warnings(record=True) as caught: |
| 325 |
warnings.simplefilter("always") |
| 326 |
parsers.python_content_parser("frag.py", data) |
| 327 |
leaked = [w for w in caught if issubclass(w.category, SyntaxWarning)] |
| 328 |
self.assertFalse(leaked, "tokenizing foreign source leaked a SyntaxWarning") |
| 329 |
|
| 330 |
|
| 331 |
class TestPython2Fallback(unittest.TestCase): |
| 332 |
"""Python 2 source must not lose its copyright (Debian #855836). |
| 333 |
|
| 334 |
The path- and bytes-side parsers have to agree: the same source on disk and |
| 335 |
inside an archive must yield the same holders. |
| 336 |
""" |
| 337 |
|
| 338 |
def _parse_path(self, data: bytes) -> tuple: |
| 339 |
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as f: |
| 340 |
f.write(data) |
| 341 |
fname = f.name |
| 342 |
try: |
| 343 |
return parsers.python_parser(fname) |
| 344 |
finally: |
| 345 |
Path(fname).unlink() |
| 346 |
|
| 347 |
def test_path_parser_falls_back_on_python2_source(self): |
| 348 |
holders, _ = self._parse_path(_PY2_SNIPPET) |
| 349 |
self.assertTrue(any("Legacy" in str(h) for h in holders)) |
| 350 |
|
| 351 |
def test_path_and_bytes_parsers_agree(self): |
| 352 |
path_holders, _ = self._parse_path(_PY2_SNIPPET) |
| 353 |
bytes_holders, _ = parsers.python_content_parser("legacy.py", _PY2_SNIPPET) |
| 354 |
self.assertEqual( |
| 355 |
[str(h) for h in path_holders], [str(h) for h in bytes_holders] |
| 356 |
) |
| 357 |
|
| 358 |
def test_path_parser_still_reads_python3_comments(self): |
| 359 |
holders, _ = self._parse_path(_PY_SNIPPET) |
| 360 |
self.assertTrue(any("Alice" in str(h) for h in holders)) |
| 361 |
|
| 362 |
|
| 363 |
class TestLookupContentParser(unittest.TestCase): |
| 364 |
"""lookup_content_parser routes MIME types to content parsers.""" |
| 365 |
|
| 366 |
def _lookup(self, ext: str) -> parsers.ContentParserFunc: |
| 367 |
return parsers.lookup_content_parser(xdg.Mime.get_type(f"dummy{ext}")) |
| 368 |
|
| 369 |
def test_python_uses_python_content_parser(self): |
| 370 |
self.assertIs(self._lookup(".py"), parsers.python_content_parser) |
| 371 |
|
| 372 |
def test_html_uses_html_content_parser(self): |
| 373 |
self.assertIs(self._lookup(".html"), parsers.html_content_parser) |
| 374 |
|
| 375 |
def test_po_uses_po_content_parser(self): |
| 376 |
self.assertIs(self._lookup(".po"), parsers.po_content_parser) |
| 377 |
|
| 378 |
def test_gz_uses_cmd_content_parser_factory_result(self): |
| 379 |
parser = self._lookup(".gz") |
| 380 |
self.assertIs(parser, parsers.cmd_content_parser_factory("zcat")) |
| 381 |
|
| 382 |
def test_tar_uses_generic_content_parser(self): |
| 383 |
self.assertIs(self._lookup(".tar"), parsers.generic_content_parser) |
| 384 |
|
| 385 |
def test_unknown_extension_falls_back_to_generic(self): |
| 386 |
self.assertIs(self._lookup(".xyzzy_unknown"), parsers.generic_content_parser) |
| 387 |
|
| 388 |
|
| 389 |
class TestUnifiedParserRegistry(unittest.TestCase): |
| 390 |
"""One registry of Parser objects; the path/bytes lookups are views over it.""" |
| 391 |
|
| 392 |
def test_registry_holds_parser_objects(self): |
| 393 |
self.assertTrue(parsers.KNOWN_PARSERS) |
| 394 |
self.assertTrue( |
| 395 |
all(isinstance(p, parsers.Parser) for p in parsers.KNOWN_PARSERS.values()) |
| 396 |
) |
| 397 |
|
| 398 |
def test_lookup_parser_pairs_both_entry_points(self): |
| 399 |
p = parsers.lookup_parser(xdg.Mime.get_type("dummy.gz")) |
| 400 |
self.assertIs(p.from_path, parsers.cmd_parser_factory("zcat")) |
| 401 |
self.assertIs(p.from_bytes, parsers.cmd_content_parser_factory("zcat")) |
| 402 |
|
| 403 |
def test_lookups_are_views_over_lookup_parser(self): |
| 404 |
xt = xdg.Mime.get_type("dummy.py") |
| 405 |
p = parsers.lookup_parser(xt) |
| 406 |
self.assertIs(parsers.lookup_mimetypes(xt), p.from_path) |
| 407 |
self.assertIs(parsers.lookup_content_parser(xt), p.from_bytes) |
| 408 |
|
| 409 |
def test_unknown_falls_back_to_generic_parser_object(self): |
| 410 |
p = parsers.lookup_parser(xdg.Mime.get_type("dummy.xyzzy_unknown")) |
| 411 |
self.assertIs(p.from_path, parsers.generic_parser) |
| 412 |
self.assertIs(p.from_bytes, parsers.generic_content_parser) |
| 413 |
|
| 414 |
|
| 415 |
class TestParseContentBytes(unittest.TestCase): |
| 416 |
"""parse_content_bytes integration: dispatch and result shape.""" |
| 417 |
|
| 418 |
def test_text_mode_uses_generic_regardless_of_extension(self): |
| 419 |
opts = _make_options(text=True) |
| 420 |
data = b"# Copyright 2024 Grace <[email protected]>\n" |
| 421 |
holders, _ = parsers.parse_content_bytes("script.py", data, opts) |
| 422 |
self.assertTrue(any("Grace" in str(h) for h in holders)) |
| 423 |
|
| 424 |
def test_python_extension_finds_copyright(self): |
| 425 |
opts = _make_options(text=False) |
| 426 |
data = b"# Copyright 2024 Henry <[email protected]>\n" |
| 427 |
holders, _ = parsers.parse_content_bytes("script.py", data, opts) |
| 428 |
self.assertTrue(any("Henry" in str(h) for h in holders)) |
| 429 |
|
| 430 |
def test_magic_detection_shebang_for_extensionless_file(self): |
| 431 |
opts = _make_options(text=False) |
| 432 |
data = b"#!/usr/bin/python3\n# Copyright 2024 Iris <[email protected]>\n" |
| 433 |
holders, _ = parsers.parse_content_bytes("Makefile", data, opts) |
| 434 |
self.assertTrue(any("Iris" in str(h) for h in holders)) |
| 435 |
|
| 436 |
def test_returns_list_and_iterable_for_empty_bytes(self): |
| 437 |
opts = _make_options(text=False) |
| 438 |
holders, licenses = parsers.parse_content_bytes("empty.txt", b"", opts) |
| 439 |
self.assertIsInstance(holders, list) |
| 440 |
for lic in licenses: |
| 441 |
self.assertIsInstance(lic, str) |
| 442 |
|
| 443 |
def test_binary_content_is_skipped(self): |
| 444 |
opts = _make_options(text=False) |
| 445 |
data = b"\x7fELF\x00\x00Copyright 2024 Ghost <[email protected]>\x00" |
| 446 |
holders, _ = parsers.parse_content_bytes("blob.o", data, opts) |
| 447 |
self.assertEqual(holders, []) |
| 448 |
|
| 449 |
def test_binary_skip_not_applied_in_text_mode(self): |
| 450 |
# --text forces text handling; honour it even for binary bytes. |
| 451 |
opts = _make_options(text=True) |
| 452 |
data = b"\x00 Copyright 2024 Ghost <[email protected]>\x00" |
| 453 |
holders, _ = parsers.parse_content_bytes("blob.o", data, opts) |
| 454 |
self.assertTrue(any("Ghost" in str(h) for h in holders)) |
| 455 |
|
| 456 |
def test_text_with_unknown_mime_still_scanned(self): |
| 457 |
opts = _make_options(text=False) |
| 458 |
data = b"Copyright 2024 Henry <[email protected]>\n" |
| 459 |
holders, _ = parsers.parse_content_bytes("notes.xyz", data, opts) |
| 460 |
self.assertTrue(any("Henry" in str(h) for h in holders)) |
| 461 |
|
| 462 |
def test_utf16_bom_text_not_skipped(self): |
| 463 |
# UTF-16 text legitimately contains NUL bytes; the BOM exempts it. |
| 464 |
opts = _make_options(text=False) |
| 465 |
data = "Copyright 2024 Ivy <[email protected]>".encode("utf-16") |
| 466 |
holders, _ = parsers.parse_content_bytes("blob.o", data, opts) |
| 467 |
self.assertTrue(any("Ivy" in str(h) for h in holders)) |
| 468 |
|
| 469 |
|
| 470 |
if __name__ == "__main__": |
| 471 |
unittest.main() |