Fix some testing edge cases, formatting hiccups and missing debugging aids
Marco Ricci

Marco Ricci commited on 2025-12-25 11:34:27
Zeige 4 geänderte Dateien mit 26 Einfügungen und 11 Löschungen.


Fix some testing edge cases:

- When re-registering existing SSH agent socket provider names, we now
  explicitly ensure that the set of (automatically determined) names is
  actually non-empty.

- Using mangled `SSH_AUTH_SOCK` environment variables for test purposes
  requires `SSH_AUTH_SOCK` to actually be set.  We supply a default name
  that is also invalid as a (non-directory) filename.

Fix some unclear debugging aids:

- Mangling `SSH_AUTH_SOCK` is (anecdotally) better at detecting faulty
  test setups, relative to the "unset `SSH_AUTH_SOCK`" entry (because
  failures in the latter tend to be silent).  So, reorder the
  parametrizations to prioritize the former one over the latter one.

- The SSH agent socket provider system was printing distribution names
  without checking for `None` distributions.

- The `spawn_ssh_agent` fixture was printing exception messages
  directly, without the exception name, or the concrete parametrization.
  We now special-case `KeyError` instances (but not subclass instances).

Fix other phrasings and formattings:

- The skip message for SSH agents excluded via the `PERMITTED_AGENTS`
  environment variable was not a sentence, and just speaking of
  "agents", not "SSH agents".
... ...
@@ -761,7 +761,7 @@ class SocketProvider:
761 761
             dist = (
762 762
                 entry_point.dist.name  # type: ignore[union-attr]
763 763
                 if getattr(entry_point, "dist", None) is not None
764
-                else None
764
+                else "<unknown>"
765 765
             )
766 766
             origin = origins.get(key, "derivepassphrase")
767 767
             if not callable(provider_entry.provider):
... ...
@@ -533,7 +533,8 @@ for key, handler in spawn_handlers.items():
533 533
     marks = [
534 534
         pytest.mark.skipif(
535 535
             not is_agent_permitted(handler[2]),
536
-            reason="agent excluded via PERMITTED_AGENTS environment variable",
536
+            reason="SSH agent excluded via PERMITTED_AGENTS "
537
+            "environment variable.",
537 538
         ),
538 539
     ]
539 540
     if key in {"pageant", "ssh-agent", "(system)"}:
... ...
@@ -657,8 +658,15 @@ def spawn_ssh_agent(
657 658
             monkeypatch.delenv("SSH_AUTH_SOCK", raising=False)
658 659
         try:
659 660
             yield from spawn_named_agent(*request.param)
660
-        except (KeyError, OSError, CannotSpawnError) as exc:
661
-            pytest.skip(exc.args[0])
661
+        except KeyError as exc:
662
+            pytest.skip(
663
+                f"The environment variable {exc.args[0]!r} was not found"
664
+                if exc.__class__ is KeyError
665
+                else exc.args[0]
666
+            )
667
+        except (OSError, CannotSpawnError) as exc:
668
+            name = request.param.agent_name or request.param.key
669
+            pytest.skip(f"Cannot spawn {name}: {exc}")
662 670
         return
663 671
 
664 672
 
... ...
@@ -235,7 +235,10 @@ class SocketAddressAction(str, enum.Enum):
235 235
             pass
236 236
         elif self == self.MANGLE_SSH_AUTH_SOCK:
237 237
             monkeypatch.setenv(
238
-                "SSH_AUTH_SOCK", os.environ["SSH_AUTH_SOCK"] + "~"
238
+                "SSH_AUTH_SOCK",
239
+                os.environ["SSH_AUTH_SOCK"] + "~"
240
+                if "SSH_AUTH_SOCK" in os.environ
241
+                else "/",
239 242
             )
240 243
         elif self == self.UNSET_SSH_AUTH_SOCK:
241 244
             monkeypatch.delenv("SSH_AUTH_SOCK", raising=False)
... ...
@@ -477,21 +480,21 @@ class Parametrize(types.SimpleNamespace):
477 480
             ),
478 481
             pytest.param(
479 482
                 None,
480
-                SocketAddressAction.UNSET_SSH_AUTH_SOCK,
483
+                SocketAddressAction.MANGLE_SSH_AUTH_SOCK,
481 484
                 None,
482 485
                 SignAction.FAIL,
483
-                "Cannot find any running SSH agent",
486
+                "Cannot connect to the SSH agent",
484 487
                 [],
485
-                id="agent-address-missing",
488
+                id="agent-address-mangled",
486 489
             ),
487 490
             pytest.param(
488 491
                 None,
489
-                SocketAddressAction.MANGLE_SSH_AUTH_SOCK,
492
+                SocketAddressAction.UNSET_SSH_AUTH_SOCK,
490 493
                 None,
491 494
                 SignAction.FAIL,
492
-                "Cannot connect to the SSH agent",
495
+                "Cannot find any running SSH agent",
493 496
                 [],
494
-                id="agent-address-mangled",
497
+                id="agent-address-missing",
495 498
             ),
496 499
             pytest.param(
497 500
                 None,
... ...
@@ -889,6 +889,10 @@ class TestSSHAgentSocketProviderRegistry:
889 889
             monkeypatch.setattr(
890 890
                 socketprovider.SocketProvider, "registry", new_registry
891 891
             )
892
+            assert names, (
893
+                f"Existing SSH agent socket provider entry "
894
+                f"{existing!r} is not existing?!"
895
+            )
892 896
             assert not all(
893 897
                 map(socketprovider.SocketProvider.registry.__contains__, names)
894 898
             )
895 899