Signal failed assumptions instead of passing tests in the "vault" module
Marco Ricci

Marco Ricci commited on 2025-01-26 23:42:36
Zeige 1 geänderte Dateien mit 8 Einfügungen und 2 Löschungen.


One hypothesis-enabled test function of the "vault" module tests that
the constructor accepts a given set of settings.  While the generator
attempts to yield only satisfiable settings, there are certain settings
that cannot derive a passphrase for one service name, but can derive one
for other names.  In these cases, the specific generated characters and
occurrence limitations together disallow all otherwise eligible
candidates for the next character.

Tests with such configurations should actually be skipped, but
hypothesis-based tests need different skip handling than normal
pytest-based tests.  Previously we would "fake" skipping by silently
returning early, which suggests that the test actually *passed*.  Now,
we use `hypothesis.assume` to signal to hypothesis to generate
a different datum instead.
... ...
@@ -221,8 +221,14 @@ class TestVault:
221 221
         try:
222 222
             password = Vault(phrase=phrase, **config).generate(service)
223 223
         except ValueError as exc:
224
-            if 'no allowed characters left' in exc.args:
225
-                return
224
+            # The service configuration strategy attempts to only
225
+            # generate satisfiable configurations.  It is possible,
226
+            # though rare, that this fails, and that unsatisfiability is
227
+            # only recognized when actually deriving a passphrase.  In
228
+            # that case, reject the generated configuration.
229
+            hypothesis.assume('no allowed characters left' not in exc.args)
230
+            # Otherwise it's a genuine bug in the test case or the
231
+            # implementation, and should be raised.
226 232
             raise  # pragma: no cover
227 233
         n = len(password)
228 234
         assert n == config['length'], 'Password has wrong length.'
229 235