package server import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" "git.a-wan.net/alan/pipelines/internal/workenv" ) // TestLiveDirenvReachesStageShell is opt-in: point PIPELINES_LIVE_DIRENV_DIR at // a scratch directory whose allowed .envrc exports SCRATCH_VAR. It uses the // installed direnv; a stage shell in a subdirectory and a delivery check must // both see SCRATCH_VAR. func TestLiveDirenvReachesStageShell(t *testing.T) { scratch := os.Getenv("PIPELINES_LIVE_DIRENV_DIR") if scratch == "" { t.Skip("set PIPELINES_LIVE_DIRENV_DIR to a scratch directory with an allowed .envrc exporting SCRATCH_VAR") } workspace := filepath.Join(scratch, "sub", "worktree") if err := os.MkdirAll(workspace, 0o755); err != nil { t.Fatal(err) } ctx, r, id := concurrentStore(t) r.dirEnv = workenv.New() var stageOutput string r.execute = func(ctx context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) { cmd := exec.CommandContext(ctx, "sh", "-c", "env | grep SCRATCH_VAR") cmd.Dir = workspace cmd.Env = workenv.CommandEnv(ctx, workspace) out, err := cmd.Output() stageOutput = string(out) return stage.Result{Outcome: "success"}, err } run, err := r.start(protocol.Message{PipelineID: id, Version: 1, RequestKey: "live-direnv", Workspace: workspace, Input: map[string]any{"prompt": "task"}}) if err != nil { t.Fatal(err) } r.wg.Wait() detail, err := r.store.GetRun(ctx, run.ID) if err != nil { t.Fatal(err) } t.Logf("stage shell: %s", strings.TrimSpace(stageOutput)) t.Logf("recorded: files=%v sources=%v set=%v warnings=%v", detail.Environment.Files, detail.Environment.Sources, detail.Environment.Set, detail.Environment.Warnings) if !strings.Contains(stageOutput, "SCRATCH_VAR=") || detail.Status != "succeeded" { t.Fatalf("stage shell did not see SCRATCH_VAR (status %s, warnings %v)", detail.Status, detail.Warnings) } env := workenv.New().Environment(context.Background(), workspace, os.Environ()) out := filepath.Join(t.TempDir(), "check.txt") if err := runDeliveryCheck(workenv.WithEnvironment(context.Background(), env), t.TempDir(), "env | grep SCRATCH_VAR > "+out); err != nil { t.Fatal(err) } data, _ := os.ReadFile(out) t.Logf("delivery check: %s", strings.TrimSpace(string(data))) }