gunk-dev/the-valley

pushed_change_test.go

source68d12192bbd3main's published tip history
4.9 KiB
1package cmd
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "the-valley/flow/internal/event"
13 "the-valley/flow/internal/run"
14)
15
16// TestRecordPushedChange runs recordPushedChange against real git: a clone
17// of a bare origin whose branch launch-b holds a change pushed before the
18// run started.
19func TestRecordPushedChange(t *testing.T) {
20 launchChange := strings.Repeat("qz", 16)
21 newChange := strings.Repeat("vp", 16)
22
23 setup := func(t *testing.T) (dir, launchHead string, gitc func(...string) string) {
24 root := t.TempDir()
25 origin, dir := filepath.Join(root, "origin.git"), filepath.Join(root, "clone")
26 gitc = func(args ...string) string {
27 t.Helper()
28 cmd := exec.Command("git", args...)
29 cmd.Dir = dir
30 cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t")
31 out, err := cmd.CombinedOutput()
32 if err != nil {
33 t.Fatalf("git %v: %v\n%s", args, err, out)
34 }
35 return strings.TrimSpace(string(out))
36 }
37 if out, err := exec.Command("git", "init", "-q", "--bare", "-b", "main", origin).CombinedOutput(); err != nil {
38 t.Fatalf("%v: %s", err, out)
39 }
40 if out, err := exec.Command("git", "clone", "-q", origin, dir).CombinedOutput(); err != nil {
41 t.Fatalf("%v: %s", err, out)
42 }
43 gitc("checkout", "-q", "-b", "main")
44 gitc("commit", "-q", "--allow-empty", "-m", "base")
45 gitc("push", "-q", "origin", "main")
46 gitc("checkout", "-q", "-b", "launch-b")
47 commit(gitc, "keys", launchChange)
48 gitc("push", "-q", "-u", "origin", "launch-b")
49 return dir, gitc("rev-parse", "HEAD"), gitc
50 }
51 launched := func(dir, launchHead string) *run.State {
52 return &run.State{ID: "20261022-1036-d8deeb25", Branch: "launch-b", ChangeID: launchChange, Worktree: dir,
53 HeadSHA: launchHead, TookOver: []string{"20261021-2343-2a82bcc5"}, Unready: &run.Unready{Repo: "widget", Handle: "qvzz"}}
54 }
55 recorded := func(t *testing.T, baseDir string) []map[string]interface{} {
56 evts, err := event.NewLog(baseDir).Read()
57 if err != nil && !os.IsNotExist(err) {
58 t.Fatal(err)
59 }
60 var out []map[string]interface{}
61 for _, e := range evts {
62 if e.Type == event.AgentChangeRecorded {
63 out = append(out, e.Data)
64 }
65 }
66 return out
67 }
68
69 t.Run("pushed nothing new", func(t *testing.T) {
70 dir, launchHead, _ := setup(t)
71 baseDir := t.TempDir()
72 st := launched(dir, launchHead)
73 recordPushedChange(context.Background(), baseDir, st, launchHead, "main")
74 if st.ChangeID != launchChange || st.Branch != "launch-b" || len(st.TookOver) != 1 || st.Unready.ChangeID != "" {
75 t.Errorf("launch's values should stay: %+v", st)
76 }
77 if st.PushedBranch != nil {
78 t.Errorf("the run pushed nothing, but recorded %s", *st.PushedBranch)
79 }
80 if evts := recorded(t, baseDir); len(evts) != 0 {
81 t.Errorf("events = %v, want none", evts)
82 }
83 })
84
85 t.Run("pushed two changes, ending on the new one", func(t *testing.T) {
86 dir, launchHead, gitc := setup(t)
87 baseDir := t.TempDir()
88 // The agent pushes more of the launch change, then a new change of
89 // its own from main, where it ends.
90 commit(gitc, "more keys", launchChange)
91 gitc("push", "-q", "origin", "launch-b")
92 launchTip := gitc("rev-parse", "HEAD")
93 gitc("switch", "-q", "-c", "registry", "origin/main")
94 commit(gitc, "registry", newChange)
95 gitc("push", "-q", "-u", "origin", "HEAD")
96 final := gitc("rev-parse", "HEAD")
97
98 st := launched(dir, launchHead)
99 st.HeadSHA = final
100 recordPushedChange(context.Background(), baseDir, st, launchHead, "main")
101 if st.ChangeID != newChange || st.Branch != "registry" || deref(st.PushedBranch) != "registry" || deref(st.PushedSHA) != final {
102 t.Errorf("recorded change %s on %s, pushed %s@%s; want %s on registry at %s",
103 st.ChangeID, st.Branch, deref(st.PushedBranch), deref(st.PushedSHA), newChange, final)
104 }
105 if st.PushedSubject != "registry" {
106 t.Errorf("pushed subject = %q", st.PushedSubject)
107 }
108 // Launch's takeovers were of its change; its draft stays that change's.
109 if len(st.TookOver) != 0 || st.Unready.ChangeID != launchChange || st.UnreadyHandle() != "" {
110 t.Errorf("took over %v, unready %+v", st.TookOver, st.Unready)
111 }
112 evts := recorded(t, baseDir)
113 if len(evts) != 1 {
114 t.Fatalf("agent:change-recorded events = %v, want one", evts)
115 }
116 want := fmt.Sprint([]interface{}{
117 map[string]interface{}{"change_id": newChange, "branch": "registry", "head": final},
118 map[string]interface{}{"change_id": launchChange, "branch": "launch-b", "head": launchTip},
119 })
120 if got := fmt.Sprint(evts[0]["pushed"]); got != want {
121 t.Errorf("pushed = %s, want %s", got, want)
122 }
123 if evts[0]["launch_change_id"] != launchChange || evts[0]["launch_branch"] != "launch-b" || evts[0]["change_id"] != newChange {
124 t.Errorf("event data = %v", evts[0])
125 }
126 })
127}
128
129// commit makes an empty commit with subject and a Change-Id trailer.
130func commit(gitc func(...string) string, subject, changeID string) {
131 gitc("commit", "-q", "--allow-empty", "-m", subject+"\n\nChange-Id: "+changeID)
132}