gunk-dev/the-valley

e2e_test.go

source6a7605f70901main's published tip history
1.9 KiB
1//go:build e2e
2
3// Package e2e contains end-to-end tests that drive the real klaus binary
4// against a real (but fully isolated) tmux server and real git repositories.
5//
6// These tests are guarded by the `e2e` build tag so the default
7// `go test ./...` is unaffected. Run them with:
8//
9// go test -tags e2e ./e2e/...
10//
11// See docs/development.md for the design and how to add new scenarios.
12package e2e
13
14import (
15 "fmt"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "testing"
20)
21
22// klausBinary is the path to the klaus binary built once for the whole test
23// binary by TestMain. Every Harness shells out to this exact binary.
24var klausBinary string
25
26// TestMain builds the klaus binary a single time before running the e2e
27// suite, then removes it afterwards. Building once keeps the suite fast even
28// though each test spins up its own isolated environment.
29func TestMain(m *testing.M) {
30 code, err := runMain(m)
31 if err != nil {
32 fmt.Fprintln(os.Stderr, "e2e setup:", err)
33 os.Exit(1)
34 }
35 os.Exit(code)
36}
37
38func runMain(m *testing.M) (int, error) {
39 // Ensure tmux and git exist — the whole suite is meaningless without them.
40 if _, err := exec.LookPath("tmux"); err != nil {
41 return 0, fmt.Errorf("tmux not found in PATH: %w", err)
42 }
43 if _, err := exec.LookPath("git"); err != nil {
44 return 0, fmt.Errorf("git not found in PATH: %w", err)
45 }
46
47 wd, err := os.Getwd()
48 if err != nil {
49 return 0, err
50 }
51 // The e2e package lives directly under the module root.
52 repoRoot := filepath.Dir(wd)
53
54 binDir, err := os.MkdirTemp("", "klaus-e2e-bin-")
55 if err != nil {
56 return 0, err
57 }
58 defer os.RemoveAll(binDir)
59
60 klausBinary = filepath.Join(binDir, "klaus")
61 build := exec.Command("go", "build", "-o", klausBinary, "./cmd/klaus/")
62 build.Dir = repoRoot
63 build.Stdout = os.Stderr
64 build.Stderr = os.Stderr
65 if err := build.Run(); err != nil {
66 return 0, fmt.Errorf("building klaus binary: %w", err)
67 }
68
69 return m.Run(), nil
70}