| 1 |
# Copyright (C) 2026 Siemens |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
|
| 8 |
|
| 9 |
def test_control_file_parsers_do_not_use_the_default_encoding(): |
| 10 |
code = """ |
| 11 |
from pathlib import Path |
| 12 |
import warnings |
| 13 |
|
| 14 |
from debsbom.apt import cache as apt_cache |
| 15 |
from debsbom.apt.cache import ExtendedStates, Repository |
| 16 |
from debsbom.apt.copyright import Copyright |
| 17 |
from debsbom.dpkg import package as dpkg_package |
| 18 |
from debsbom.dpkg.package import BinaryPackage |
| 19 |
|
| 20 |
apt_cache.HAS_PYTHON_APT = False |
| 21 |
dpkg_package.HAS_PYTHON_APT = False |
| 22 |
source_root = Path(apt_cache.__file__).resolve().parents[1] |
| 23 |
|
| 24 |
with warnings.catch_warnings(record=True) as caught: |
| 25 |
warnings.simplefilter("always", EncodingWarning) |
| 26 |
list(Copyright(Path("tests/data/utf8-copyright")).licenses()) |
| 27 |
list(BinaryPackage.parse_status_file(Path("tests/data/utf8-status"))) |
| 28 |
ExtendedStates.from_file("tests/root/apt-sources/var/lib/apt/extended_states") |
| 29 |
list(Repository.from_apt_cache("tests/root/utf8-apt-lists")) |
| 30 |
list(Repository._parse_sources("tests/data/utf8-Sources")) |
| 31 |
list(Repository._parse_packages("tests/data/utf8-Packages")) |
| 32 |
|
| 33 |
source_warnings = [ |
| 34 |
f"{warning.filename}:{warning.lineno}: {warning.message}" |
| 35 |
for warning in caught |
| 36 |
if warning.category is EncodingWarning |
| 37 |
and Path(warning.filename).resolve().is_relative_to(source_root) |
| 38 |
] |
| 39 |
if source_warnings: |
| 40 |
raise AssertionError("\\n".join(source_warnings)) |
| 41 |
""" |
| 42 |
|
| 43 |
result = subprocess.run( |
| 44 |
[sys.executable, "-X", "warn_default_encoding", "-c", code], |
| 45 |
capture_output=True, |
| 46 |
encoding="utf-8", |
| 47 |
) |
| 48 |
|
| 49 |
assert result.returncode == 0, result.stderr |