cedfc22e45
Resolve the run workspace's environment with workenv (direnv, parent .envrc, mise; same account rule as Claude chats) at run start and resume, and apply it to Pi stage processes, a per-environment Pipelines-owned OpenCode stage service (keyed by a hash of the change, reused across runs, idle ones stopped after PIPELINES_OPENCODE_STAGE_IDLE_TIMEOUT, revived per registration), delivery-gate checks and merges, plain-stage opencode CLI calls and file-change capture git reads. Record the directory, .envrc files, sources and variable names (never values) on the run detail; a blocked .envrc becomes a run warning.
72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
// Package workenvtest provides a fake direnv for tests of code that applies a
|
|
// directory's environment. It never touches the operator's real direnv state.
|
|
package workenvtest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// fakeDirenv behaves like `direnv export json` for the nearest .envrc from
|
|
// $PWD up: an .envrc next to an .envrc.allowed marker exports .envrc.json; any
|
|
// other .envrc is reported blocked, with direnv's wording.
|
|
const fakeDirenv = `#!/bin/sh
|
|
[ "$1 $2" = "export json" ] || exit 2
|
|
d="$PWD"
|
|
while :; do
|
|
if [ -f "$d/.envrc" ]; then
|
|
if [ ! -f "$d/.envrc.allowed" ]; then
|
|
echo "direnv: error $d/.envrc is blocked. Run ` + "`direnv allow`" + ` to approve its content" >&2
|
|
exit 1
|
|
fi
|
|
cat "$d/.envrc.json"
|
|
exit 0
|
|
fi
|
|
[ "$d" = "/" ] && exit 0
|
|
d=$(dirname "$d")
|
|
done
|
|
`
|
|
|
|
// InstallDirenv writes the fake direnv into a new directory, puts it first on
|
|
// the test's PATH and returns that directory.
|
|
func InstallDirenv(t testing.TB) string {
|
|
t.Helper()
|
|
bin := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(bin, "direnv"), []byte(fakeDirenv), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
return bin
|
|
}
|
|
|
|
// WriteEnvrc creates dir/.envrc exporting vars (a nil value unsets), allowed or
|
|
// blocked.
|
|
func WriteEnvrc(t testing.TB, dir string, allowed bool, vars map[string]*string) {
|
|
t.Helper()
|
|
file := filepath.Join(dir, ".envrc")
|
|
if err := os.WriteFile(file, []byte("# fake\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := map[string]*string{"DIRENV_FILE": &file}
|
|
for key, value := range vars {
|
|
out[key] = value
|
|
}
|
|
data, _ := json.Marshal(out)
|
|
if err := os.WriteFile(filepath.Join(dir, ".envrc.json"), data, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
marker := filepath.Join(dir, ".envrc.allowed")
|
|
if allowed {
|
|
if err := os.WriteFile(marker, nil, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else {
|
|
_ = os.Remove(marker)
|
|
}
|
|
}
|
|
|
|
// Str returns a pointer to value, for WriteEnvrc.
|
|
func Str(value string) *string { return &value }
|