Skip to content

Commit cdd1661

Browse files
committed
Added alt_tmp_path pytest fixture
1 parent d971596 commit cdd1661

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#
2+
# MIT License
3+
#
4+
# Copyright (c) 2024, Mattias Aabmets
5+
#
6+
# The contents of this file are subject to the terms and conditions defined in the License.
7+
# You may not use, modify, or distribute this file except in compliance with the License.
8+
#
9+
# SPDX-License-Identifier: MIT
10+
#
11+
12+
import re
13+
import string
14+
import pytest
15+
import secrets
16+
import tempfile
17+
from pathlib import Path
18+
19+
20+
@pytest.fixture(name="alt_tmp_path", scope="function")
21+
def fixture_alt_tmp_path(tmp_path) -> Path:
22+
base_path = Path(tempfile.gettempdir())
23+
24+
match = re.search(r"/pytest-(\d+)/", tmp_path.as_posix())
25+
pytest_dir = "qc_pytest" + ('_' + match.group(1) if match else '')
26+
27+
charset = string.ascii_letters + string.digits
28+
test_dir = ''.join([secrets.choice(charset) for _ in range(20)])
29+
30+
test_path = base_path / pytest_dir / test_dir
31+
if test_path.exists():
32+
raise RuntimeError(f"Cannot reuse existing temp test path: {test_path}")
33+
34+
test_path.mkdir(parents=True, exist_ok=True)
35+
return test_path

tests/test_cli/conftest.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
#
1111

1212
import os
13-
import shutil
1413
import pytest
15-
import secrets
1614
import typing as t
1715
from pathlib import Path
1816
from dataclasses import dataclass
@@ -45,17 +43,15 @@ class CLIMessages:
4543

4644

4745
@pytest.fixture(name="cfp_setup", scope="function")
48-
def fixture_cfp_setup(tmp_path: Path) -> Callable[..., t.ContextManager[CryptoFilePaths]]:
46+
def fixture_cfp_setup(alt_tmp_path) -> Callable[..., t.ContextManager[CryptoFilePaths]]:
4947
@contextmanager
5048
def closure(algorithm: str) -> t.Generator[CryptoFilePaths, t.Any, None]:
51-
sub_path = tmp_path / secrets.token_hex(16)
52-
sub_path.mkdir(parents=True, exist_ok=True)
5349
cfp_dict = dict(
54-
public_key_fp=sub_path / f"{algorithm}-pubkey.qc",
55-
secret_key_fp=sub_path / f"{algorithm}-seckey.qc",
56-
ciphertext_fp=sub_path / "ciphertext.kptn",
57-
plaintext_fp=sub_path / "plaintext.bin",
58-
signature_fp=sub_path / "signature.sig",
50+
public_key_fp=alt_tmp_path / f"{algorithm}-pubkey.qc",
51+
secret_key_fp=alt_tmp_path / f"{algorithm}-seckey.qc",
52+
ciphertext_fp=alt_tmp_path / "ciphertext.kptn",
53+
plaintext_fp=alt_tmp_path / "plaintext.bin",
54+
signature_fp=alt_tmp_path / "signature.sig",
5955
ptf_data=os.urandom(1024)
6056
)
6157
cfp = CryptoFilePaths(**{
@@ -65,7 +61,7 @@ def closure(algorithm: str) -> t.Generator[CryptoFilePaths, t.Any, None]:
6561
with open(cfp.plaintext_fp, "wb") as file:
6662
file.write(cfp.ptf_data)
6763
cwd = os.getcwd()
68-
os.chdir(sub_path)
64+
os.chdir(alt_tmp_path)
6965
yield cfp
7066
os.chdir(cwd)
7167
return closure

0 commit comments

Comments
 (0)