From d64b1676296b9d255fadc1ccad25848a23f8b0fc Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Wed, 22 Apr 2026 23:59:16 +0200 Subject: [PATCH] test: set locale for test_install_packages_error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test `test_install_packages_error` will fail on systems where the locale is not set to C (english): ``` $ python3 -m pytest tests/integration/test_ui_gtk.py tests/system/test_packaging_apt_dpkg.py::test_install_packages_error [...] > exc_info.match("E:Type 'bogus' is not known .* sources could not be read.$") E AssertionError: Regex pattern did not match. E Expected regex: "E:Type 'bogus' is not known .* sources could not be read.$" E Actual message: 'E:Typ »bogus« in Zeile 1 der Quellliste /tmp/tmp4erzi0jq/cache/Foonux 22.04/apt/etc/apt/sources.list ist unbekannt., E:Die Liste der Quellen konnte nicht gelesen werden.' ``` Set the locale explicitly for this test. --- tests/helper.py | 9 +++++++++ tests/system/test_packaging_apt_dpkg.py | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/helper.py b/tests/helper.py index d294eaef8..315264e34 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -4,6 +4,7 @@ import functools import importlib.machinery import importlib.util +import locale import os import pathlib import shutil @@ -103,6 +104,14 @@ def read_shebang(command: str) -> str | None: return first_line.decode().split(" ", 1)[0][2:] +@contextlib.contextmanager +def restore_locale(category: int) -> Generator[None]: + """Restore locale after leaving this context manager.""" + orig_locale = locale.getlocale(category) + yield + locale.setlocale(category, orig_locale) + + @contextlib.contextmanager def restore_os_environ() -> Generator[None]: """Restore os.environ after leaving this context manager.""" diff --git a/tests/system/test_packaging_apt_dpkg.py b/tests/system/test_packaging_apt_dpkg.py index fee721ffa..45ba0565a 100644 --- a/tests/system/test_packaging_apt_dpkg.py +++ b/tests/system/test_packaging_apt_dpkg.py @@ -4,6 +4,7 @@ import glob import gzip +import locale import os import pathlib import shutil @@ -17,7 +18,7 @@ import pytest from apport.packaging_impl.apt_dpkg import _parse_deb822_sources, impl -from tests.helper import has_internet +from tests.helper import has_internet, restore_locale from tests.paths import get_test_data_directory if shutil.which("dpkg") is None: @@ -348,10 +349,12 @@ def test_install_packages_system( @pytest.mark.skipif(not has_internet(), reason="online test") +@restore_locale(locale.LC_MESSAGES) def test_install_packages_error( configdir: str, cachedir: str, rootdir: str, apt_style: AptStyle ) -> None: """install_packages() with errors""" + locale.setlocale(locale.LC_MESSAGES, "C.UTF-8") # sources.list with invalid format release = _setup_foonux_config(configdir, apt_style) with open(