Files
alan cedfc22e45 Pipeline runs use the workspace's directory environment
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.
2026-11-04 18:08:42 +00:00

116 lines
4.5 KiB
Go

package workenv_test
import (
"context"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/workenv"
"git.a-wan.net/alan/pipelines/internal/workenv/workenvtest"
)
func lookup(env []string, key string) (string, bool) {
for i := len(env) - 1; i >= 0; i-- {
if k, v, ok := strings.Cut(env[i], "="); ok && k == key {
return v, true
}
}
return "", false
}
func TestEnvironmentSubdirectoryInheritsAndDropsCredentials(t *testing.T) {
workenvtest.InstallDirenv(t)
project := t.TempDir()
workspace := filepath.Join(project, "repos", "worktrees", "item")
if err := os.MkdirAll(workspace, 0o755); err != nil {
t.Fatal(err)
}
workenvtest.WriteEnvrc(t, project, true, map[string]*string{
"CLAUDE_CONFIG_DIR": workenvtest.Str(filepath.Join(project, ".claude-profile")),
"CLOUDSDK_CONFIG": workenvtest.Str(filepath.Join(project, ".gcloud-profile")),
"GONE": nil,
})
base := []string{"PATH=" + os.Getenv("PATH"), "HOME=" + t.TempDir(), "ANTHROPIC_API_KEY=server-key", "CLAUDE_CODE_OAUTH_TOKEN=server-token", "GONE=1", "KEEP=1"}
r := &workenv.Resolver{Timeout: 10 * time.Second}
got := r.Environment(context.Background(), workspace, base)
if !got.Applies() || got.Key == "" || len(got.Warnings) != 0 {
t.Fatalf("key %q warnings %v", got.Key, got.Warnings)
}
if v, _ := lookup(got.Env, "CLAUDE_CONFIG_DIR"); v != filepath.Join(project, ".claude-profile") {
t.Fatalf("parent .envrc not applied to the subdirectory: %q", v)
}
for _, key := range []string{"ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN", "GONE"} {
if _, ok := lookup(got.Env, key); ok {
t.Fatalf("%s kept", key)
}
}
if v, _ := lookup(got.Env, "KEEP"); v != "1" {
t.Fatal("unrelated variable lost")
}
if !slices.Equal(got.Set, []string{"CLAUDE_CONFIG_DIR", "CLOUDSDK_CONFIG"}) || !slices.Equal(got.Unset, []string{"GONE"}) ||
!slices.Equal(got.Dropped, []string{"ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"}) {
t.Fatalf("names: set %v unset %v dropped %v", got.Set, got.Unset, got.Dropped)
}
if !slices.Equal(got.Files, []string{filepath.Join(project, ".envrc")}) {
t.Fatalf("files: %v", got.Files)
}
if v, _ := lookup(got.Command(workspace), "PWD"); v != workspace {
t.Fatalf("PWD = %q", v)
}
// The same environment from another subdirectory has the same key; a
// different value has another.
other := filepath.Join(project, "repos")
if again := r.Environment(context.Background(), other, base); again.Key != got.Key {
t.Fatal("same environment, different key")
}
second := t.TempDir()
workenvtest.WriteEnvrc(t, second, true, map[string]*string{"CLAUDE_CONFIG_DIR": workenvtest.Str("/elsewhere")})
if r.Environment(context.Background(), second, base).Key == got.Key {
t.Fatal("different environments share a key")
}
}
func TestEnvironmentWithoutAccountKeepsCredentialsAndNoneAppliesWithoutChanges(t *testing.T) {
workenvtest.InstallDirenv(t)
dir := t.TempDir()
workenvtest.WriteEnvrc(t, dir, true, map[string]*string{"SCRATCH_VAR": workenvtest.Str("1")})
base := []string{"PATH=" + os.Getenv("PATH"), "HOME=" + t.TempDir(), "ANTHROPIC_API_KEY=server-key"}
r := &workenv.Resolver{Timeout: 10 * time.Second}
got := r.Environment(context.Background(), dir, base)
if v, _ := lookup(got.Env, "ANTHROPIC_API_KEY"); v != "server-key" || len(got.Dropped) != 0 {
t.Fatal("credentials dropped without a directory account")
}
none := r.Environment(context.Background(), t.TempDir(), base)
if none.Applies() || none.Key != "" || none.Command("/x") != nil {
t.Fatalf("key %q", none.Key)
}
}
func TestEnvironmentBlockedEnvrcWarns(t *testing.T) {
workenvtest.InstallDirenv(t)
project := t.TempDir()
workspace := filepath.Join(project, "sub")
os.Mkdir(workspace, 0o755)
workenvtest.WriteEnvrc(t, project, false, map[string]*string{"CLAUDE_CONFIG_DIR": workenvtest.Str("/p")})
got := (&workenv.Resolver{Timeout: 10 * time.Second}).Environment(context.Background(), workspace, []string{"PATH=" + os.Getenv("PATH"), "HOME=" + t.TempDir()})
if got.Applies() || len(got.Warnings) != 1 || !strings.Contains(got.Warnings[0], "not allowed") || !strings.Contains(got.Warnings[0], filepath.Join(project, ".envrc")) {
t.Fatalf("key %q warnings %v", got.Key, got.Warnings)
}
}
func TestEnvironmentContext(t *testing.T) {
if workenv.CommandEnv(context.Background(), "/x") != nil {
t.Fatal("no environment should inherit")
}
ctx := workenv.WithEnvironment(context.Background(), workenv.Environment{Env: []string{"A=1"}, Key: "k"})
env := workenv.CommandEnv(ctx, "/x")
if !slices.Equal(env, []string{"A=1", "PWD=/x"}) {
t.Fatal(env)
}
}