Merge pull request 'pytests: use /tmp on macos to avoid unix socket issues' (#3073) from nixpkgs-update into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3073
This commit is contained in:
@@ -16,6 +16,7 @@ from clan_cli.locked_open import locked_open
|
|||||||
from clan_cli.nix import nix_test_store
|
from clan_cli.nix import nix_test_store
|
||||||
from fixture_error import FixtureError
|
from fixture_error import FixtureError
|
||||||
from root import CLAN_CORE
|
from root import CLAN_CORE
|
||||||
|
from temporary_dir import TEMPDIR
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -209,13 +210,13 @@ class ClanFlake:
|
|||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def minimal_flake_template() -> Iterator[ClanFlake]:
|
def minimal_flake_template() -> Iterator[ClanFlake]:
|
||||||
with (
|
with (
|
||||||
tempfile.TemporaryDirectory(prefix="flake-") as _home,
|
tempfile.TemporaryDirectory(prefix="minimal-flake-", dir=TEMPDIR) as _dirpath,
|
||||||
pytest.MonkeyPatch.context() as mp,
|
pytest.MonkeyPatch.context() as mp,
|
||||||
):
|
):
|
||||||
home = Path(_home).resolve()
|
temporary_home = Path(_dirpath).resolve()
|
||||||
mp.setenv("HOME", str(home))
|
mp.setenv("HOME", str(temporary_home))
|
||||||
flake = ClanFlake(
|
flake = ClanFlake(
|
||||||
temporary_home=home,
|
temporary_home=temporary_home,
|
||||||
flake_template=clan_templates(TemplateType.CLAN) / "minimal",
|
flake_template=clan_templates(TemplateType.CLAN) / "minimal",
|
||||||
)
|
)
|
||||||
flake.init_from_template()
|
flake.init_from_template()
|
||||||
|
|||||||
@@ -3,42 +3,46 @@ import os
|
|||||||
import tempfile
|
import tempfile
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from sys import platform
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
TEMPDIR = None
|
||||||
|
# macOS' default temporary directory is too long for unix sockets
|
||||||
|
# This can break applications such as gpg-agent
|
||||||
|
if platform == "darwin":
|
||||||
|
TEMPDIR = Path("/tmp")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
|
def temporary_home(temp_dir: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
||||||
with tempfile.TemporaryDirectory(prefix="pytest-home-") as _dirpath:
|
xdg_runtime_dir = os.getenv("XDG_RUNTIME_DIR")
|
||||||
dirpath = Path(_dirpath).resolve()
|
monkeypatch.setenv("HOME", str(temp_dir))
|
||||||
xdg_runtime_dir = os.getenv("XDG_RUNTIME_DIR")
|
monkeypatch.setenv("XDG_CONFIG_HOME", str(temp_dir / ".config"))
|
||||||
monkeypatch.setenv("HOME", str(dirpath))
|
|
||||||
monkeypatch.setenv("XDG_CONFIG_HOME", str(dirpath / ".config"))
|
|
||||||
|
|
||||||
runtime_dir = dirpath / "xdg-runtime-dir"
|
runtime_dir = temp_dir / "xdg-runtime-dir"
|
||||||
runtime_dir.mkdir()
|
runtime_dir.mkdir()
|
||||||
runtime_dir.chmod(0o700)
|
runtime_dir.chmod(0o700)
|
||||||
|
|
||||||
gpgdir = runtime_dir / "gpgagent"
|
gpgdir = runtime_dir / "gpgagent"
|
||||||
gpgdir.mkdir()
|
gpgdir.mkdir()
|
||||||
gpgdir.chmod(0o700)
|
gpgdir.chmod(0o700)
|
||||||
monkeypatch.setenv("GPG_AGENT_INFO", str(gpgdir))
|
monkeypatch.setenv("GPG_AGENT_INFO", str(gpgdir))
|
||||||
|
|
||||||
# Iterate over all environment variables
|
# Iterate over all environment variables
|
||||||
for key, value in os.environ.items():
|
for key, value in os.environ.items():
|
||||||
if xdg_runtime_dir and value.startswith(xdg_runtime_dir):
|
if xdg_runtime_dir and value.startswith(xdg_runtime_dir):
|
||||||
monkeypatch.setenv(
|
monkeypatch.setenv(key, value.replace(xdg_runtime_dir, str(runtime_dir)))
|
||||||
key, value.replace(xdg_runtime_dir, str(runtime_dir))
|
|
||||||
)
|
|
||||||
|
|
||||||
monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir))
|
monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir))
|
||||||
monkeypatch.chdir(str(dirpath))
|
monkeypatch.chdir(str(temp_dir))
|
||||||
yield dirpath
|
return temp_dir
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def temp_dir() -> Iterator[Path]:
|
def temp_dir() -> Iterator[Path]:
|
||||||
with tempfile.TemporaryDirectory(prefix="pytest-") as _dirpath:
|
with tempfile.TemporaryDirectory(prefix="pytest-", dir=TEMPDIR) as _dirpath:
|
||||||
yield Path(_dirpath).resolve()
|
yield Path(_dirpath).resolve()
|
||||||
|
|||||||
Reference in New Issue
Block a user