[RFC PATCH 09/13] tests: replace NamedTemporaryFile with tmp_path in patatt fixture

Adrian Neftali Sanchez <[email protected]>
Newsgroups org.kernel.linux.tools
Message-ID <[email protected]>
Use pytest's built-in tmp_path fixture to store the ephemeral ed25519
private key instead of tempfile.NamedTemporaryFile.  This removes the
manual os.unlink teardown step (pytest removes the directory
automatically, even when a test raises) and also avoids the Windows
restriction that a NamedTemporaryFile with delete=True cannot be opened
by a second process while still open.

Signed-off-by: Adrian Neftali Sanchez <[email protected]>
---
 src/tests/test_patatt.py | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/src/tests/test_patatt.py b/src/tests/test_patatt.py
index de05277..3ed4711 100644
--- a/src/tests/test_patatt.py
+++ b/src/tests/test_patatt.py
@@ -6,6 +6,7 @@ Uses ephemeral ed25519 keys so no external key material is needed.
 import base64
 import email.message
 import os
+import pathlib
 import tempfile
 from collections.abc import Generator
 from typing import Tuple, Union
@@ -18,21 +19,29 @@ import patatt
 
 
 @pytest.fixture()
-def ed25519_keypair() -> Generator[Tuple[str, str, str, str], None, None]:
-    """Generate an ephemeral ed25519 keypair written to temp files.
-
-    Returns (privkey_path, verify_key_b64, identity, selector).
-    The private key file is written so patatt can find it via
-    signingkey = ed25519:/path/to/key.
+def ed25519_keypair(
+    tmp_path: pathlib.Path,
+    monkeypatch: pytest.MonkeyPatch,
+) -> Generator[Tuple[str, str, str, str], None, None]:
+    """Generate an ephemeral ed25519 keypair stored in patatt's data directory.
+
+    Returns (key_name, verify_key_b64, identity, selector).  The private key
+    is written to ``<tmp_path>/patatt/private/<key_name>.key``.  XDG_DATA_HOME
+    is pointed at ``tmp_path`` for the duration of the test so patatt resolves
+    the key by name without any OS-specific absolute-path handling.
+    patatt.KEYCACHE is cleared to prevent key material from a previous test
+    leaking into this one (all tests share the identity "[email protected]").
     """
     sk = SigningKey.generate()
     sk_b64 = base64.b64encode(sk.encode()).decode()
     vk_b64 = base64.b64encode(sk.verify_key.encode()).decode()
-    with tempfile.NamedTemporaryFile(mode='w', suffix='.key', delete=False) as fh:
-        fh.write(sk_b64)
-        privkey_path = fh.name
-    yield privkey_path, vk_b64, '[email protected]', 'default'
-    os.unlink(privkey_path)
+    key_name = 'test-key'
+    private_dir = tmp_path / 'patatt' / 'private'
+    private_dir.mkdir(parents=True, exist_ok=True)
+    (private_dir / f'{key_name}.key').write_text(sk_b64)
+    monkeypatch.setenv('XDG_DATA_HOME', str(tmp_path))
+    monkeypatch.setattr(patatt, 'KEYCACHE', {})
+    yield key_name, vk_b64, '[email protected]', 'default'
 
 
 @pytest.fixture()
-- 
2.45.0.windows.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.