diff --git a/tests/test.py b/tests/test.py index a9d7a20..4b8e2a9 100644 --- a/tests/test.py +++ b/tests/test.py @@ -2,15 +2,22 @@ from tempfile import TemporaryDirectory import os +import sys import subprocess from pathlib import Path import shutil import unittest +from typing import List TEST_ROOT = Path(__file__).resolve().parent +def run(cmd: List[str], **kwargs) -> subprocess.CompletedProcess: + print("$ " + " ".join(cmd)) + return subprocess.run(cmd, **kwargs) + + class IntegrationTest(unittest.TestCase): def setUp(self) -> None: self.env = os.environ.copy() @@ -24,12 +31,32 @@ class IntegrationTest(unittest.TestCase): f.write(f"use nix") def test_direnv(self) -> None: - subprocess.run( - ["direnv", "allow"], cwd=str(self.testenv), env=self.env, check=True + run(["direnv", "allow"], cwd=str(self.testenv), env=self.env, check=True) + + run(["nix-collect-garbage"], check=True) + + out1 = run( + ["direnv", "exec", str(self.testenv), "hello"], + env=self.env, + stderr=subprocess.PIPE, + text=True, ) - subprocess.run( - ["direnv", "exec", str(self.testenv), "hello"], env=self.env, check=True + sys.stderr.write(out1.stderr) + self.assertIn("renewed cache and derivation link", out1.stderr) + self.assertEqual(out1.returncode, 0) + + run(["nix-collect-garbage"], check=True) + run(["sh", "-c", "realpath /nix/var/nix/gcroots/per-user/$USER/*"], check=True) + + out2 = run( + ["direnv", "exec", str(self.testenv), "hello"], + env=self.env, + stderr=subprocess.PIPE, + text=True, ) + sys.stderr.write(out2.stderr) + self.assertIn("using cached derivation", out2.stderr) + self.assertEqual(out2.returncode, 0) def tearDown(self) -> None: self.dir.cleanup()