Files
pipelines/internal/server/run_environment_live_test.go
T
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

62 lines
2.3 KiB
Go

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)))
}