"""run() jobs get the .envrc direnv would load for the kernel's folder, and only when direnv allows it, as in the person's shell. A blocked file does not load and the cell says so without telling the model to allow it; once allowed, the next job starts in its environment (PATH_add included, so its tools are found by name); an edit blocks it again; leaving the folder unloads it; a denied file stays quiet. Runs the real direnv, with its allow list in the test's own XDG directories. """ import json import os import shutil import subprocess import unittest from harness import Albedo, Provider, exclusive, python, text ENVRC = "export ALBEDO_DIRENV_PROBE=from-envrc\nPATH_add bin\n" PROBE = '#!/bin/sh\necho "probe:${ALBEDO_DIRENV_PROBE-unset}"\n' @exclusive @unittest.skipUnless(shutil.which("direnv"), "needs direnv") class DirenvTests(unittest.TestCase): def setUp(self): self.cells = [] def script(request): if request["messages"][-1].get("role") == "user" and self.cells: return python(self.cells.pop(0)) return text("ok") def prepare(app): # direnv's allow list and config live here, never the developer's for name in ("XDG_DATA_HOME", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"): app.env[name] = str(app.root / name.lower()) self.provider = Provider(script) self.addCleanup(self.provider.close) self.app = Albedo(self.provider, prepare=prepare) self.app.__enter__() self.addCleanup(self.app.__exit__, None, None, None) def direnv(self, *args): subprocess.run( ["direnv", *args], env=self.app.env, check=True, capture_output=True ) def cell(self, session, code): self.cells.append(code) self.app.prompt(session, "run it").close() self.app.idle(session) results = [ json.loads(part["value"]) for entry in self.app.history(session)["items"] if entry["kind"] == "tool_result" and entry["tool"]["name"] == "python" for part in entry["content"] if part["kind"] == "json" and part["field"] == "result" ] self.assertEqual(results[-1]["status"], "ok", results[-1]) return results[-1]["output"].strip() def job(self, session, program): return self.cell( session, f"job = run({program!r})\nawait job\nprint(job.tail().strip())" ) def test_jobs_load_the_envrc_direnv_allows(self): workspace = self.app.root / "project" (workspace / "bin").mkdir(parents=True) envrc = workspace / ".envrc" envrc.write_text(ENVRC) probe = workspace / "bin" / "probe" probe.write_text(PROBE) probe.chmod(0o755) session = self.app.session(workspace) output = self.job(session, str(probe)) self.assertIn("is not allowed, so jobs start without it", output) self.assertNotIn("direnv allow", output) self.assertTrue(output.endswith("probe:unset"), output) self.direnv("allow", str(envrc)) output = self.job(session, "probe") self.assertIn("direnv: loading", output) self.assertTrue(output.endswith("probe:from-envrc"), output) # unchanged, the environment stays and direnv has nothing to say self.assertEqual(self.job(session, "probe"), "probe:from-envrc") # direnv stores watched mtimes at one-second resolution. Ensure this # edit is observable rather than accidentally reusing the prior tick. watched_mtime = envrc.stat().st_mtime envrc.write_text(ENVRC + "export SECOND=1\n") os.utime(envrc, (watched_mtime + 2, watched_mtime + 2)) output = self.job(session, str(probe)) self.assertIn("is not allowed", output) self.assertTrue(output.endswith("probe:unset"), output) self.direnv("allow", str(envrc)) self.assertTrue(self.job(session, "probe").endswith("probe:from-envrc")) elsewhere = self.app.root / "elsewhere" elsewhere.mkdir() output = self.cell( session, f"os.chdir({str(elsewhere)!r})\njob = run({str(probe)!r})\nawait job\nprint(job.tail().strip())", ) self.assertEqual(output, "direnv: unloading\nprobe:unset") self.direnv("deny", str(envrc)) output = self.cell( session, f"os.chdir({str(workspace)!r})\njob = run({str(probe)!r})\nawait job\nprint(job.tail().strip())", ) self.assertEqual(output, "probe:unset")