Repair the --version output test for derivepassphrase export vault
Marco Ricci

Marco Ricci commited on 2026-08-23 17:23:09
Zeige 1 geänderte Dateien mit 44 Einfügungen und 3 Löschungen.


The test was no longer triggering the fallback behavior for when the
"export" extra/the "cryptography" library was not available.  Partially,
this was because we now need to parametrize this test for both cases,
but forgot to do so.  Partially, this was *also* because querying the
installed version of "cryptography" needs to be separately mocked,
beyond just blocking the import.
... ...
@@ -13,7 +13,9 @@ from __future__ import annotations
13 13
 
14 14
 import contextlib
15 15
 import enum
16
+import importlib.metadata
16 17
 import re
18
+import sys
17 19
 import types
18 20
 from typing import TYPE_CHECKING
19 21
 
... ...
@@ -29,6 +31,8 @@ from tests.machinery import pytest as pytest_machinery
29 31
 if TYPE_CHECKING:
30 32
     from collections.abc import Generator
31 33
 
34
+    from typing_extensions import Literal
35
+
32 36
 
33 37
 class VersionOutputData(NamedTuple):
34 38
     derivation_schemes: dict[str, bool]
... ...
@@ -161,6 +165,11 @@ class Parametrize(types.SimpleNamespace):
161 165
         ],
162 166
         ids=["cmd"],
163 167
     )
168
+    HAS_EXPORT_EXTRA = pytest.mark.parametrize(
169
+        "export_extra",
170
+        [True, False],
171
+        ids=["has_export", "no_export"],
172
+    )
164 173
     ISATTY = pytest.mark.parametrize(
165 174
         "isatty",
166 175
         [False, True],
... ...
@@ -775,8 +784,11 @@ class TestVersionOutput:
775 784
         assert not version_data.features
776 785
         assert not version_data.extras
777 786
 
787
+    @Parametrize.HAS_EXPORT_EXTRA
778 788
     def test_export_vault_version_option_output(
779 789
         self,
790
+        monkeypatch: pytest.MonkeyPatch,
791
+        export_extra: bool,
780 792
     ) -> None:
781 793
         """The version output states supported features.
782 794
 
... ...
@@ -792,12 +804,41 @@ class TestVersionOutput:
792 804
         correct program version number.
793 805
 
794 806
         """
807
+        if not export_extra:
808
+            monkeypatch.delitem(
809
+                sys.modules,
810
+                "derivepassphrase.exporter.vault_native",
811
+                raising=False,
812
+            )
813
+            monkeypatch.delitem(
814
+                sys.modules,
815
+                "derivepassphrase.exporter.storeroom",
816
+                raising=False,
817
+            )
818
+            monkeypatch.delitem(
819
+                sys.modules, "derivepassphrase.exporter", raising=False
820
+            )
821
+            monkeypatch.setitem(sys.modules, "cryptography", None)
822
+            importlib_metadata_version = importlib.metadata.version
823
+
824
+            def no_cryptography_version(
825
+                distribution_name: str,
826
+            ) -> str:  # pragma: no cover [external]
827
+                if distribution_name == "cryptography":
828
+                    raise importlib.metadata.PackageNotFoundError(
829
+                        distribution_name
830
+                    )
831
+                return importlib_metadata_version(distribution_name)
832
+
833
+            monkeypatch.setattr(
834
+                importlib.metadata, "version", no_cryptography_version
835
+            )
836
+
837
+        from derivepassphrase.exporter import storeroom, vault_native  # noqa: I001,PLC0415
838
+
795 839
         version_data = self._test(["export", "vault"])
796 840
         actually_known_formats: dict[str, bool] = {}
797 841
         actually_enabled_extras: set[str] = set()
798
-        with contextlib.suppress(ModuleNotFoundError):
799
-            from derivepassphrase.exporter import storeroom, vault_native  # noqa: I001,PLC0415
800
-
801 842
         actually_known_formats.update({
802 843
             _types.ForeignConfigurationFormat.VAULT_STOREROOM: not storeroom.STUBBED,
803 844
             _types.ForeignConfigurationFormat.VAULT_V02: not vault_native.STUBBED,
804 845