package workenv import ( "bytes" "compress/zlib" "context" "encoding/base64" "os" "path/filepath" "strings" "testing" "time" ) // FakeDirenv is a stand-in for direnv: an .envrc next to an .envrc.allowed // marker exports the JSON in .envrc.json; any other .envrc is blocked. Each // call appends a line to $FAKE_TOOL_CALLS. const FakeDirenv = `#!/bin/sh echo direnv >> "$FAKE_TOOL_CALLS" [ "$1 $2" = "export json" ] || exit 2 [ -f .envrc ] || exit 0 if [ ! -f .envrc.allowed ]; then echo "direnv: error $PWD/.envrc is blocked. Run ` + "`direnv allow`" + ` to approve its content" >&2 exit 1 fi echo "direnv: loading $PWD/.envrc" >&2 cat .envrc.json ` const fakeMise = `#!/bin/sh echo mise >> "$FAKE_TOOL_CALLS" [ "$1 $2" = "env --json" ] || exit 2 if [ -f mise.toml ]; then if [ -f mise.untrusted ]; then echo "mise ERROR Config files in $PWD/mise.toml are not trusted." >&2; exit 1; fi cat mise.json exit 0 fi echo '{}' ` func install(t *testing.T, bin, name, script string) { t.Helper() if err := os.WriteFile(filepath.Join(bin, name), []byte(script), 0o755); err != nil { t.Fatal(err) } } func write(t *testing.T, path, content string) { t.Helper() if err := os.WriteFile(path, []byte(content), 0o644); err != nil { t.Fatal(err) } } func setup(t *testing.T) (*Resolver, string, []string, string) { t.Helper() bin := t.TempDir() calls := filepath.Join(t.TempDir(), "calls") base := []string{"PATH=" + bin + ":/usr/bin:/bin", "HOME=" + t.TempDir(), "FAKE_TOOL_CALLS=" + calls, "KEEP=1", "DROP=1"} return &Resolver{Timeout: 10 * time.Second}, bin, base, calls } func calls(t *testing.T, path string) int { data, err := os.ReadFile(path) if err != nil { return 0 } return strings.Count(string(data), "\n") } func TestNoToolsNoChanges(t *testing.T) { r, _, base, _ := setup(t) if got := r.Resolve(context.Background(), t.TempDir(), base); len(got.Changes) != 0 || len(got.Warnings) != 0 { t.Fatalf("%+v", got) } } func TestAllowedEnvrcIsAppliedAndCached(t *testing.T) { r, bin, base, callLog := setup(t) install(t, bin, "direnv", FakeDirenv) dir := t.TempDir() write(t, filepath.Join(dir, ".envrc"), "export CLAUDE_CONFIG_DIR=x\nunset DROP\n") write(t, filepath.Join(dir, ".envrc.allowed"), "") write(t, filepath.Join(dir, ".envrc.json"), `{"CLAUDE_CONFIG_DIR":"/profile","DROP":null,"DIRENV_DIR":"-`+dir+`"}`) got := r.Resolve(context.Background(), dir, base) if v, ok := got.Value("CLAUDE_CONFIG_DIR"); !ok || v != "/profile" || len(got.Warnings) != 0 { t.Fatalf("%+v", got) } if got.Touches("DIRENV_DIR") { t.Fatal("direnv bookkeeping leaked into the environment") } env := strings.Join(got.Apply(base), "\n") if !strings.Contains(env, "CLAUDE_CONFIG_DIR=/profile") || strings.Contains(env, "DROP=") || !strings.Contains(env, "KEEP=1") { t.Fatalf("merged env: %s", env) } r.Resolve(context.Background(), dir, base) if n := calls(t, callLog); n != 1 { t.Fatalf("unchanged directory ran direnv %d times", n) } // Editing the .envrc invalidates the cache. time.Sleep(10 * time.Millisecond) write(t, filepath.Join(dir, ".envrc"), "export CLAUDE_CONFIG_DIR=y\n") write(t, filepath.Join(dir, ".envrc.json"), `{"CLAUDE_CONFIG_DIR":"/other"}`) got = r.Resolve(context.Background(), dir, base) if v, _ := got.Value("CLAUDE_CONFIG_DIR"); v != "/other" || calls(t, callLog) != 2 { t.Fatalf("edited .envrc not re-read: %+v", got) } } func TestParentEnvrcAppliesToSubdirectory(t *testing.T) { r, bin, base, _ := setup(t) // A real direnv walks up; the fake only reads the cwd, so point a wrapper // at the parent to check the resolver runs in the chosen directory and // fingerprints parent files. parent := t.TempDir() child := filepath.Join(parent, "repo") if err := os.Mkdir(child, 0o755); err != nil { t.Fatal(err) } install(t, bin, "direnv", "#!/bin/sh\necho direnv >> \"$FAKE_TOOL_CALLS\"\n[ \"$PWD\" = \""+child+"\" ] || exit 3\ncat ../.envrc.json\n") write(t, filepath.Join(parent, ".envrc.json"), `{"A":"1"}`) got := r.Resolve(context.Background(), child, base) if v, _ := got.Value("A"); v != "1" { t.Fatalf("%+v", got) } write(t, filepath.Join(parent, ".envrc"), "x") write(t, filepath.Join(parent, ".envrc.json"), `{"A":"2"}`) if v, _ := r.Resolve(context.Background(), child, base).Value("A"); v != "2" { t.Fatal("a new parent .envrc did not invalidate the cache") } } func TestBlockedEnvrcWarnsAndIsNotApplied(t *testing.T) { r, bin, base, _ := setup(t) install(t, bin, "direnv", FakeDirenv) dir := t.TempDir() write(t, filepath.Join(dir, ".envrc"), "export CLAUDE_CONFIG_DIR=x\n") write(t, filepath.Join(dir, ".envrc.json"), `{"CLAUDE_CONFIG_DIR":"/profile"}`) got := r.Resolve(context.Background(), dir, base) if len(got.Changes) != 0 { t.Fatalf("blocked .envrc applied: %+v", got) } if len(got.Warnings) != 1 || !strings.Contains(got.Warnings[0], "not allowed") || !strings.Contains(got.Warnings[0], "direnv allow") { t.Fatalf("warnings: %+v", got.Warnings) } // Allowing it later (direnv's allow record or the file changes) is seen. write(t, filepath.Join(dir, ".envrc.allowed"), "") write(t, filepath.Join(dir, ".envrc"), "export CLAUDE_CONFIG_DIR=x \n") if v, _ := r.Resolve(context.Background(), dir, base).Value("CLAUDE_CONFIG_DIR"); v != "/profile" { t.Fatal("allowed .envrc not applied after change") } } func TestMiseEnvIsAppliedAndUntrustedWarns(t *testing.T) { r, bin, base, _ := setup(t) install(t, bin, "mise", fakeMise) dir := t.TempDir() write(t, filepath.Join(dir, "mise.toml"), "[env]\nGH_CONFIG_DIR='/gh'\n") write(t, filepath.Join(dir, "mise.json"), `{"GH_CONFIG_DIR":"/gh","KEEP":"1"}`) got := r.Resolve(context.Background(), dir, base) if v, _ := got.Value("GH_CONFIG_DIR"); v != "/gh" || got.Touches("KEEP") { t.Fatalf("%+v", got) } write(t, filepath.Join(dir, "mise.untrusted"), "") write(t, filepath.Join(dir, "mise.toml"), "[env]\nGH_CONFIG_DIR='/gh2'\n") got = r.Resolve(context.Background(), dir, base) if len(got.Changes) != 0 || len(got.Warnings) != 1 || !strings.Contains(got.Warnings[0], "mise trust") { t.Fatalf("%+v", got) } } func TestDirenvWinsOverMise(t *testing.T) { r, bin, base, _ := setup(t) install(t, bin, "mise", fakeMise) install(t, bin, "direnv", FakeDirenv) dir := t.TempDir() write(t, filepath.Join(dir, "mise.toml"), "") write(t, filepath.Join(dir, "mise.json"), `{"X":"mise","M":"1"}`) write(t, filepath.Join(dir, ".envrc"), "") write(t, filepath.Join(dir, ".envrc.allowed"), "") write(t, filepath.Join(dir, ".envrc.json"), `{"X":"direnv"}`) got := r.Resolve(context.Background(), dir, base) if x, _ := got.Value("X"); x != "direnv" { t.Fatalf("%+v", got) } if m, _ := got.Value("M"); m != "1" { t.Fatalf("%+v", got) } } func TestDisabled(t *testing.T) { r, bin, base, _ := setup(t) install(t, bin, "direnv", FakeDirenv) dir := t.TempDir() write(t, filepath.Join(dir, ".envrc"), "") if got := r.Resolve(context.Background(), dir, append(base, DisableEnvKey+"=off")); len(got.Changes)+len(got.Warnings) != 0 { t.Fatalf("%+v", got) } } func TestDecodeWatches(t *testing.T) { var buf bytes.Buffer w := zlib.NewWriter(&buf) _, _ = w.Write([]byte(`[{"path":"/a/.envrc","modtime":1,"exists":true}]`)) _ = w.Close() got := decodeWatches(base64.URLEncoding.EncodeToString(buf.Bytes())) if len(got) != 1 || got[0] != "/a/.envrc" { t.Fatalf("%v", got) } if decodeWatches("!!") != nil { t.Fatal("garbage decoded") } }