| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | // TestAgyLaunchNeedsBash: agy runs every command through bash, so an agy |
| 16 | // worker on a PATH without bash could edit files and nothing else. Launch on |
| 17 | // such a PATH fails up front, naming bash and the PATH it searched, and |
| 18 | // leaves no run or worktree behind; on the harness's PATH, which has bash, |
| 19 | // the same launch starts the worker. |
| 20 | func TestAgyLaunchNeedsBash(t *testing.T) { |
| 21 | t.Parallel() |
| 22 | h := NewHarness(t) |
| 23 | h.WriteStub("agy", h.claudeStubScript()) |
| 24 | |
| 25 | // klaus, the agy stub and git, and no bash. |
| 26 | gitBin, err := exec.LookPath("git") |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | noBash := filepath.Join(h.E2EDir, "nobash") |
| 31 | if err := os.Mkdir(noBash, 0o755); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if err := os.Symlink(gitBin, filepath.Join(noBash, "git")); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | path := h.BinDir + ":" + noBash |
| 38 | |
| 39 | res := h.runKlausOnPath(path, "launch", "--backend", "agy", "small task") |
| 40 | if res.ExitCode == 0 { |
| 41 | t.Fatalf("launch on a PATH without bash succeeded:\n%s%s", res.Stdout, res.Stderr) |
| 42 | } |
| 43 | for _, want := range []string{"the agy worker can't run: bash isn't on the PATH it gets", `searched PATH="` + path + `"`} { |
| 44 | if !strings.Contains(res.Stderr, want) { |
| 45 | t.Errorf("stderr missing %q:\n%s", want, res.Stderr) |
| 46 | } |
| 47 | } |
| 48 | if strings.Contains(res.Stdout, "worktree:") || strings.Contains(res.Stderr, "cleaning up worktree") { |
| 49 | t.Errorf("the check came after the worktree was created:\n%s%s", res.Stdout, res.Stderr) |
| 50 | } |
| 51 | if ids := h.RunIDs(); len(ids) != 0 { |
| 52 | t.Fatalf("a launch that can't run recorded runs: %v", ids) |
| 53 | } |
| 54 | |
| 55 | res = h.RunKlaus("launch", "--backend", "agy", "small task") |
| 56 | if res.ExitCode != 0 { |
| 57 | t.Fatalf("launch with bash on PATH: %s%s", res.Stdout, res.Stderr) |
| 58 | } |
| 59 | h.WaitForClaudeStart(30 * time.Second) |
| 60 | releaseBackendWorkers(t, h) |
| 61 | } |
| 62 | |
| 63 | // runKlausOnPath is RunKlaus with PATH replaced by path. |
| 64 | func (h *Harness) runKlausOnPath(path string, args ...string) RunResult { |
| 65 | h.t.Helper() |
| 66 | cmd := exec.Command(klausBinary, args...) |
| 67 | cmd.Dir = h.RepoDir |
| 68 | for _, kv := range h.env() { |
| 69 | if strings.HasPrefix(kv, "PATH=") { |
| 70 | kv = "PATH=" + path |
| 71 | } |
| 72 | cmd.Env = append(cmd.Env, kv) |
| 73 | } |
| 74 | var stdout, stderr bytes.Buffer |
| 75 | cmd.Stdout, cmd.Stderr = &stdout, &stderr |
| 76 | err := cmd.Run() |
| 77 | res := RunResult{Stdout: stdout.String(), Stderr: stderr.String()} |
| 78 | if ee, ok := err.(*exec.ExitError); ok { |
| 79 | res.ExitCode = ee.ExitCode() |
| 80 | } else if err != nil { |
| 81 | h.t.Fatalf("running klaus %v: %v", args, err) |
| 82 | } |
| 83 | return res |
| 84 | } |