From 40f1b2eccfc27764e49b20bf712d4f5fa321fcac Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sun, 22 Jun 2025 18:31:46 +0200 Subject: fix: tests during Nix build Fixes the following error when executing the tests during a Nix build: Traceback (most recent call last): File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions yield File "/build/inkstitch/lib/elements/element.py", line 650, in embroider stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups stitch_groups = get_stitch_plan_cache().get(cache_key) ^^^^^^^^^^^^^^^^^^^^^^^ File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache cache_dir = get_user_dir('cache') ^^^^^^^^^^^^^^^^^^^^^ File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir os.makedirs(path) File "", line 215, in makedirs File "", line 215, in makedirs File "", line 225, in makedirs PermissionError: [Errno 13] Permission denied: '/homeless-shelter' --- lib/utils/__init__.py | 1 - lib/utils/settings.py | 28 ++++++++++++++-------------- tests/conftest.py | 8 ++++++++ tests/test_clone.py | 11 ----------- 4 files changed, 22 insertions(+), 26 deletions(-) create mode 100644 tests/conftest.py diff --git a/lib/utils/__init__.py b/lib/utils/__init__.py index 60a0bd36..b24257be 100644 --- a/lib/utils/__init__.py +++ b/lib/utils/__init__.py @@ -3,7 +3,6 @@ # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. -from . import cache as cache_module # Slight hack to allow cache to be imported for monkeypatching from .cache import cache from .dotdict import DotDict from .geometry import * diff --git a/lib/utils/settings.py b/lib/utils/settings.py index 28c52290..501798ac 100644 --- a/lib/utils/settings.py +++ b/lib/utils/settings.py @@ -49,40 +49,40 @@ class GlobalSettings(MutableMapping): # and via imports also runs on generate-inx-files, which with the Nix # package manager is executed with a non-writable home directory. user_dir = get_user_dir(create=False) - self.__settings_file = os.path.join(user_dir, "settings.json") - self.__settings = {} + self._settings_file = os.path.join(user_dir, "settings.json") + self._settings = {} for name, value in DEFAULT_METADATA.items(): - self.__settings[f"default_{name}"] = value + self._settings[f"default_{name}"] = value - self.__settings.update(DEFAULT_SETTINGS) + self._settings.update(DEFAULT_SETTINGS) try: - with open(self.__settings_file, 'r') as settings_file: - self.__settings.update(json.load(settings_file)) + with open(self._settings_file, 'r') as settings_file: + self._settings.update(json.load(settings_file)) except (OSError, json.JSONDecodeError, ValueError): pass def __setitem__(self, item, value): - self.__settings[item] = value + self._settings[item] = value - with open(self.__settings_file, 'w') as settings_file: - json.dump(self.__settings, settings_file) + with open(self._settings_file, 'w') as settings_file: + json.dump(self._settings, settings_file) def __getitem__(self, item): - return self.__settings[item] + return self._settings[item] def __delitem__(self, item): - del self.__settings[item] + del self._settings[item] def __iter__(self): - return iter(self.__settings) + return iter(self._settings) def __len__(self): - return len(self.__settings) + return len(self._settings) def __json__(self): - return self.__settings + return self._settings global_settings = GlobalSettings() diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..c4e58897 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,8 @@ +import pytest + +import lib.utils.settings + +@pytest.fixture(scope="session", autouse=True) +def disable_cache(): + # We disable the cache because Nix executes our tests with a non-writable HOME directory. + lib.utils.settings.global_settings._settings['cache_size'] = 0 diff --git a/tests/test_clone.py b/tests/test_clone.py index 4b4cf101..ba6784e1 100644 --- a/tests/test_clone.py +++ b/tests/test_clone.py @@ -9,7 +9,6 @@ from inkex.tester.svg import svg from lib.commands import add_commands from lib.elements import Clone, EmbroideryElement, FillStitch from lib.svg.tags import INKSCAPE_LABEL, INKSTITCH_ATTRIBS, SVG_RECT_TAG -from lib.utils import cache_module from .utils import element_count @@ -22,16 +21,6 @@ def element_fill_angle(element: EmbroideryElement) -> Optional[float]: class CloneElementTest(TestCase): - # Monkey-patch the cahce to forcibly disable it: We may need to refactor this out for tests. - def setUp(self) -> None: - from pytest import MonkeyPatch - self.monkeypatch = MonkeyPatch() - self.monkeypatch.setattr(cache_module, "is_cache_disabled", lambda: True) - - def tearDown(self) -> None: - self.monkeypatch.undo() - return super().tearDown() - def assertAngleAlmostEqual(self, a, b) -> None: # Take the mod 180 of the returned angles, because e.g. -130deg and 50deg produce fills along the same angle. # We have to use a precision of 4 decimal digits because of the precision of the matrices as they are stored in the svg trees -- cgit v1.2.3