add temp_dir fixture

This commit is contained in:
Jörg Thalheim
2024-10-01 20:44:18 +02:00
parent 4826582547
commit 27b1aeb827
2 changed files with 19 additions and 17 deletions

View File

@@ -11,7 +11,7 @@ log = logging.getLogger(__name__)
@pytest.fixture @pytest.fixture
def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]: def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath: with tempfile.TemporaryDirectory(prefix="pytest-home-") as dirpath:
xdg_runtime_dir = os.getenv("XDG_RUNTIME_DIR") xdg_runtime_dir = os.getenv("XDG_RUNTIME_DIR")
monkeypatch.setenv("HOME", str(dirpath)) monkeypatch.setenv("HOME", str(dirpath))
monkeypatch.setenv("XDG_CONFIG_HOME", str(Path(dirpath) / ".config")) monkeypatch.setenv("XDG_CONFIG_HOME", str(Path(dirpath) / ".config"))
@@ -34,5 +34,10 @@ def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir)) monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir))
monkeypatch.chdir(str(dirpath)) monkeypatch.chdir(str(dirpath))
log.debug("Temp HOME directory: %s", str(dirpath)) yield Path(dirpath)
@pytest.fixture
def temp_dir() -> Iterator[Path]:
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath:
yield Path(dirpath) yield Path(dirpath)

View File

@@ -3,7 +3,6 @@ import subprocess
from dataclasses import dataclass from dataclasses import dataclass
from io import StringIO from io import StringIO
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory
import pytest import pytest
from age_keys import SopsSetup from age_keys import SopsSetup
@@ -24,7 +23,7 @@ from root import CLAN_CORE
from stdout import CaptureOutput from stdout import CaptureOutput
def test_dependencies_as_files() -> None: def test_dependencies_as_files(temp_dir: Path) -> None:
from clan_cli.vars.generate import dependencies_as_dir from clan_cli.vars.generate import dependencies_as_dir
decrypted_dependencies = { decrypted_dependencies = {
@@ -37,19 +36,17 @@ def test_dependencies_as_files() -> None:
"var_2b": b"var_2b", "var_2b": b"var_2b",
}, },
} }
with TemporaryDirectory() as tmpdir: dependencies_as_dir(decrypted_dependencies, temp_dir)
dep_tmpdir = Path(tmpdir) assert temp_dir.is_dir()
dependencies_as_dir(decrypted_dependencies, dep_tmpdir) assert (temp_dir / "gen_1" / "var_1a").read_bytes() == b"var_1a"
assert dep_tmpdir.is_dir() assert (temp_dir / "gen_1" / "var_1b").read_bytes() == b"var_1b"
assert (dep_tmpdir / "gen_1" / "var_1a").read_bytes() == b"var_1a" assert (temp_dir / "gen_2" / "var_2a").read_bytes() == b"var_2a"
assert (dep_tmpdir / "gen_1" / "var_1b").read_bytes() == b"var_1b" assert (temp_dir / "gen_2" / "var_2b").read_bytes() == b"var_2b"
assert (dep_tmpdir / "gen_2" / "var_2a").read_bytes() == b"var_2a" # ensure the files are not world readable
assert (dep_tmpdir / "gen_2" / "var_2b").read_bytes() == b"var_2b" assert (temp_dir / "gen_1" / "var_1a").stat().st_mode & 0o777 == 0o600
# ensure the files are not world readable assert (temp_dir / "gen_1" / "var_1b").stat().st_mode & 0o777 == 0o600
assert (dep_tmpdir / "gen_1" / "var_1a").stat().st_mode & 0o777 == 0o600 assert (temp_dir / "gen_2" / "var_2a").stat().st_mode & 0o777 == 0o600
assert (dep_tmpdir / "gen_1" / "var_1b").stat().st_mode & 0o777 == 0o600 assert (temp_dir / "gen_2" / "var_2b").stat().st_mode & 0o777 == 0o600
assert (dep_tmpdir / "gen_2" / "var_2a").stat().st_mode & 0o777 == 0o600
assert (dep_tmpdir / "gen_2" / "var_2b").stat().st_mode & 0o777 == 0o600
def test_required_generators() -> None: def test_required_generators() -> None: