diff --git a/tests/helper.py b/tests/helper.py index add6176ed..ed99dadc6 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -3,13 +3,14 @@ import contextlib import importlib.machinery import importlib.util +import locale import os import pathlib import shutil import subprocess import time import unittest.mock -from collections.abc import Callable, Generator, Iterator, Sequence, Set +from collections.abc import Callable, Generator, Iterable, Iterator, Sequence, Set from typing import Any from unittest.mock import MagicMock @@ -109,6 +110,17 @@ def _id(obj: Any) -> Any: return obj +@contextlib.contextmanager +def set_locale( + category: int, new_locale: Iterable[str | None] | None +) -> Generator[None]: + """Set locale and restore it after leaving this context manager.""" + orig_locale = locale.getlocale(category) + locale.setlocale(category, new_locale) + yield + locale.setlocale(category, orig_locale) + + def skip_if_command_is_missing(cmd: str) -> Callable: """Skip a test if the command is not found.""" if shutil.which(cmd) is None: diff --git a/tests/system/test_packaging_apt_dpkg.py b/tests/system/test_packaging_apt_dpkg.py index 19d25cb3b..4a62c609d 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,6 +18,7 @@ import pytest from apport.packaging_impl.apt_dpkg import _parse_deb822_sources, impl +from tests.helper import set_locale from tests.paths import get_test_data_directory if shutil.which("dpkg") is None: @@ -347,6 +349,7 @@ def test_install_packages_system( @pytest.mark.requires_internet +@set_locale(locale.LC_MESSAGES, "C.UTF-8") def test_install_packages_error( configdir: str, cachedir: str, rootdir: str, apt_style: AptStyle ) -> None: