From 2129790bda477ed5fb0305e184fb1b9147d5a506 Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 19 May 2025 15:29:08 +0200 Subject: [PATCH] clan_lib flake: test if cache gets invalidated with nix gc --- pkgs/clan-cli/clan_lib/flake/flake_test.py | 44 ++++++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/pkgs/clan-cli/clan_lib/flake/flake_test.py b/pkgs/clan-cli/clan_lib/flake/flake_test.py index bdeee2583..0c665b0c8 100644 --- a/pkgs/clan-cli/clan_lib/flake/flake_test.py +++ b/pkgs/clan-cli/clan_lib/flake/flake_test.py @@ -1,4 +1,8 @@ import logging +import subprocess +from pathlib import Path +from tempfile import TemporaryDirectory +from unittest.mock import patch import pytest from clan_cli.tests.fixtures_flakes import ClanFlake @@ -347,10 +351,6 @@ def test_conditional_all_selector(flake: ClanFlake) -> None: # Test that the caching works @pytest.mark.with_core def test_caching_works(flake: ClanFlake) -> None: - from unittest.mock import patch - - from clan_lib.flake import Flake - my_flake = Flake(str(flake.path)) with patch.object( @@ -363,6 +363,42 @@ def test_caching_works(flake: ClanFlake) -> None: assert tracked_build.call_count == 1 +@pytest.mark.with_core +def test_cache_gc(monkeypatch: pytest.MonkeyPatch) -> None: + with TemporaryDirectory() as tempdir_: + tempdir = Path(tempdir_) + + monkeypatch.setenv("NIX_STATE_DIR", str(tempdir / "var")) + monkeypatch.setenv("NIX_LOG_DIR", str(tempdir / "var" / "log")) + monkeypatch.setenv("NIX_STORE_DIR", str(tempdir / "store")) + monkeypatch.setenv("NIX_CACHE_HOME", str(tempdir / "cache")) + monkeypatch.setenv("HOME", str(tempdir / "home")) + monkeypatch.delenv("CLAN_TEST_STORE") + monkeypatch.setenv("NIX_BUILD_TOP", str(tempdir / "build")) + + test_file = tempdir / "flake" / "testfile" + test_file.parent.mkdir(parents=True, exist_ok=True) + test_file.write_text("test") + + test_flake = tempdir / "flake" / "flake.nix" + test_flake.write_text(""" + { + outputs = _: { + testfile = ./testfile; + }; + } + """) + + my_flake = Flake(str(tempdir / "flake")) + my_flake.select( + "testfile", nix_options=["--sandbox-build-dir", str(tempdir / "build")] + ) + assert my_flake._cache is not None # noqa: SLF001 + assert my_flake._cache.is_cached("testfile") # noqa: SLF001 + subprocess.run(["nix-collect-garbage"], check=True) + assert not my_flake._cache.is_cached("testfile") # noqa: SLF001 + + # This test fails because the CI sandbox does not have the required packages to run the generators # maybe @DavHau or @Qubasa can fix this at some point :) # @pytest.mark.with_core