always resolve symlinks for TemporaryDirectory

On macOS mktemp returns a temporary directory in a symlink.
Nix has a bug where it won't accept path:// located in a symlink.
This avoid this issue by always resolving symlinks as returned by
TemporaryDirectory.
This commit is contained in:
Jörg Thalheim
2025-03-19 16:31:36 +01:00
parent 7fef29d7aa
commit 93cbe62765
12 changed files with 55 additions and 55 deletions

View File

@@ -209,12 +209,13 @@ class ClanFlake:
@pytest.fixture(scope="session")
def minimal_flake_template() -> Iterator[ClanFlake]:
with (
tempfile.TemporaryDirectory(prefix="flake-") as home,
tempfile.TemporaryDirectory(prefix="flake-") as _home,
pytest.MonkeyPatch.context() as mp,
):
mp.setenv("HOME", home)
home = Path(_home).resolve()
mp.setenv("HOME", str(home))
flake = ClanFlake(
temporary_home=Path(home),
temporary_home=home,
flake_template=clan_templates(TemplateType.CLAN) / "minimal",
)
flake.init_from_template()

View File

@@ -11,12 +11,13 @@ log = logging.getLogger(__name__)
@pytest.fixture
def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
with tempfile.TemporaryDirectory(prefix="pytest-home-") as dirpath:
with tempfile.TemporaryDirectory(prefix="pytest-home-") as _dirpath:
dirpath = Path(_dirpath).resolve()
xdg_runtime_dir = os.getenv("XDG_RUNTIME_DIR")
monkeypatch.setenv("HOME", str(dirpath))
monkeypatch.setenv("XDG_CONFIG_HOME", str(Path(dirpath) / ".config"))
monkeypatch.setenv("XDG_CONFIG_HOME", str(dirpath / ".config"))
runtime_dir = Path(dirpath) / "xdg-runtime-dir"
runtime_dir = dirpath / "xdg-runtime-dir"
runtime_dir.mkdir()
runtime_dir.chmod(0o700)
@@ -34,10 +35,10 @@ def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir))
monkeypatch.chdir(str(dirpath))
yield Path(dirpath)
yield dirpath
@pytest.fixture
def temp_dir() -> Iterator[Path]:
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath:
yield Path(dirpath)
with tempfile.TemporaryDirectory(prefix="pytest-") as _dirpath:
yield Path(_dirpath).resolve()

View File

@@ -1,5 +1,4 @@
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from clan_cli.clan_uri import ClanURI
@@ -36,14 +35,13 @@ def test_firefox_strip_uri() -> None:
assert uri.get_url() == "git+https://git.clan.lol/clan/democlan.git"
def test_local_uri() -> None:
with TemporaryDirectory(prefix="clan_test") as tempdir:
flake_nix = Path(tempdir) / "flake.nix"
flake_nix.write_text("outputs = _: {}")
def test_local_uri(temp_dir: Path) -> None:
flake_nix = temp_dir / "flake.nix"
flake_nix.write_text("outputs = _: {}")
# Create a ClanURI object from a local URI
uri = ClanURI.from_str(f"clan://file://{tempdir}")
assert uri.flake.path == Path(tempdir)
# Create a ClanURI object from a local URI
uri = ClanURI.from_str(f"clan://file://{temp_dir}")
assert uri.flake.path == temp_dir
def test_is_remote() -> None:
@@ -79,25 +77,23 @@ def test_from_str_remote() -> None:
assert uri.flake.identifier == "https://example.com"
def test_from_str_local() -> None:
with TemporaryDirectory(prefix="clan_test") as tempdir:
flake_nix = Path(tempdir) / "flake.nix"
flake_nix.write_text("outputs = _: {}")
def test_from_str_local(temp_dir: Path) -> None:
flake_nix = temp_dir / "flake.nix"
flake_nix.write_text("outputs = _: {}")
uri = ClanURI.from_str(url=tempdir, machine_name="myVM")
assert uri.get_url().endswith(tempdir)
assert uri.machine_name == "myVM"
assert uri.flake.is_local
assert str(uri.flake).endswith(tempdir) # type: ignore
uri = ClanURI.from_str(url=str(temp_dir), machine_name="myVM")
assert uri.get_url().endswith(str(temp_dir))
assert uri.machine_name == "myVM"
assert uri.flake.is_local
assert str(uri.flake).endswith(str(temp_dir))
def test_from_str_local_no_machine() -> None:
with TemporaryDirectory(prefix="clan_test") as tempdir:
flake_nix = Path(tempdir) / "flake.nix"
flake_nix.write_text("outputs = _: {}")
def test_from_str_local_no_machine(temp_dir: Path) -> None:
flake_nix = temp_dir / "flake.nix"
flake_nix.write_text("outputs = _: {}")
uri = ClanURI.from_str(tempdir)
assert uri.get_url().endswith(tempdir)
assert uri.machine_name == "defaultVM"
assert uri.flake.is_local
assert str(uri.flake).endswith(tempdir) # type: ignore
uri = ClanURI.from_str(str(temp_dir))
assert uri.get_url().endswith(str(temp_dir))
assert uri.machine_name == "defaultVM"
assert uri.flake.is_local
assert str(uri.flake).endswith(str(temp_dir))