From 5c4d3eeffd3ddc2d024ee0b6d0807a91c78d5df8 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Mon, 2 Jan 2023 03:21:15 +0200 Subject: [PATCH] allow setting context with existing In case of running command from web interface, it will raise exception because context has been copied with subprocesses --- src/ahriman/core/__init__.py | 9 ++++++--- src/ahriman/core/repository/repository.py | 10 +++++----- tests/ahriman/core/repository/test_repository.py | 10 +++++----- tests/ahriman/core/test_context_init.py | 11 +++++++++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/ahriman/core/__init__.py b/src/ahriman/core/__init__.py index a75f51b5..3bcc143a 100644 --- a/src/ahriman/core/__init__.py +++ b/src/ahriman/core/__init__.py @@ -58,23 +58,26 @@ class _Context: raise ValueError(f"Value {value} is not an instance of {key.return_type}") return value - def set(self, key: ContextKey[T], value: T) -> None: + def set(self, key: ContextKey[T], value: T, strict: bool = True) -> None: """ set value for the specified key Args: key(ContextKey[T]): context key name value(T): context value associated with the specified key + strict(bool, optional): check if key already exists (Default value = True) Raises: KeyError: in case if the specified context variable already exists ValueError: in case if type of value is not an instance of specified return type """ - if key.key in self._content: + has_key = key.key in self._content + if strict and has_key: raise KeyError(key.key) if not isinstance(value, key.return_type): raise ValueError(f"Value {value} is not an instance of {key.return_type}") - self._content[key.key] = value + if not has_key: + self._content[key.key] = value def __iter__(self) -> Iterator[str]: """ diff --git a/src/ahriman/core/repository/repository.py b/src/ahriman/core/repository/repository.py index acd8d9a0..e1e39abd 100644 --- a/src/ahriman/core/repository/repository.py +++ b/src/ahriman/core/repository/repository.py @@ -83,12 +83,12 @@ class Repository(Executor, UpdateHandler): """ ctx = context.get() - ctx.set(ContextKey("database", SQLite), self.database) - ctx.set(ContextKey("configuration", Configuration), self.configuration) - ctx.set(ContextKey("pacman", Pacman), self.pacman) - ctx.set(ContextKey("sign", GPG), self.sign) + ctx.set(ContextKey("database", SQLite), self.database, strict=False) + ctx.set(ContextKey("configuration", Configuration), self.configuration, strict=False) + ctx.set(ContextKey("pacman", Pacman), self.pacman, strict=False) + ctx.set(ContextKey("sign", GPG), self.sign, strict=False) - ctx.set(ContextKey("repository", type(self)), self) + ctx.set(ContextKey("repository", type(self)), self, strict=False) context.set(ctx) diff --git a/tests/ahriman/core/repository/test_repository.py b/tests/ahriman/core/repository/test_repository.py index ee05c55a..6051d5fd 100644 --- a/tests/ahriman/core/repository/test_repository.py +++ b/tests/ahriman/core/repository/test_repository.py @@ -32,11 +32,11 @@ def test_set_context(configuration: Configuration, database: SQLite, mocker: Moc instance = Repository.load("x86_64", configuration, database, report=False, unsafe=False) set_mock.assert_has_calls([ - MockCall(ContextKey("database", SQLite), instance.database), - MockCall(ContextKey("configuration", Configuration), instance.configuration), - MockCall(ContextKey("pacman", Pacman), instance.pacman), - MockCall(ContextKey("sign", GPG), instance.sign), - MockCall(ContextKey("repository", Repository), instance), + MockCall(ContextKey("database", SQLite), instance.database, strict=False), + MockCall(ContextKey("configuration", Configuration), instance.configuration, strict=False), + MockCall(ContextKey("pacman", Pacman), instance.pacman, strict=False), + MockCall(ContextKey("sign", GPG), instance.sign, strict=False), + MockCall(ContextKey("repository", Repository), instance, strict=False), ]) diff --git a/tests/ahriman/core/test_context_init.py b/tests/ahriman/core/test_context_init.py index ba4425f2..ede41f24 100644 --- a/tests/ahriman/core/test_context_init.py +++ b/tests/ahriman/core/test_context_init.py @@ -57,6 +57,17 @@ def test_set_value_exception() -> None: ctx.set(ContextKey("key", str), 42) +def test_set_value_exists() -> None: + """ + must skip key set in case if key already exists and strict check is disabled + """ + key, value = ContextKey("key", int), 42 + ctx = _Context() + ctx.set(key, value) + + ctx.set(key, value, strict=False) + + def test_contains() -> None: """ must correctly check if element is in list