e2e/gates_test.go

f327db6192d9a0877606a40385b24c0cda29fd2d
gitbay/e2e/gates_test.go history · blame · raw

99 lines · 4523 bytes

 1package e2e
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// mr show reports the gates before the merge fails, mr merge names every
11// unmet gate at once, and mr review says when a verdict is advisory
12// (#199).
13func TestMergeGatesVisible(t *testing.T) {
14	inst := startInstance(t)
15	aliceKey := inst.newKey(t, "alice")
16	bobKey := inst.newKey(t, "bob")
17	carolKey := inst.newKey(t, "carol")
18	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
19	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
20	inst.admin(t, "admin", "user", "create", "carol", "--key", carolKey+".pub")
21	for _, args := range [][]string{
22		{"repo", "create", "alice/svc"},
23		{"repo", "access", "grant", "alice/svc", "bob", "write"},
24		{"repo", "settings", "require-approvals", "alice/svc", "1"},
25		{"repo", "settings", "require-codeowners", "alice/svc", "on"},
26		{"repo", "settings", "require-resolved", "alice/svc", "on"},
27	} {
28		if _, errOut, code := inst.ssh(t, aliceKey, "", args...); code != 0 {
29			t.Fatalf("%v: %s", args, errOut)
30		}
31	}
32	env := inst.gitEnv(aliceKey)
33	work := t.TempDir()
34	mustGit(t, work, env, "clone", "-q", inst.sshURL("alice/svc"), "w")
35	dir := filepath.Join(work, "w")
36	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
37	os.WriteFile(filepath.Join(dir, "CODEOWNERS"), []byte("*.go @bob\n"), 0o644)
38	os.WriteFile(filepath.Join(dir, "svc.go"), []byte("package svc\n"), 0o644)
39	mustGit(t, dir, env, "add", ".")
40	mustGit(t, dir, env, "commit", "-q", "-m", "base")
41	mustGit(t, dir, env, "push", "-q", "origin", "main")
42	mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
43	os.WriteFile(filepath.Join(dir, "svc.go"), []byte("package svc\n\nvar V = 1\n"), 0o644)
44	mustGit(t, dir, env, "commit", "-q", "-am", "change")
45	mustGit(t, dir, env, "push", "-q", "origin", "feat")
46	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/svc",
47		"--source", "feat", "--target", "main", "--title", "'change'"); code != 0 {
48		t.Fatalf("mr create: %s", errOut)
49	}
50
51	// Before anyone reviews: two unmet gates, visible on mr show.
52	out, _, _ := inst.ssh(t, aliceKey, "", "mr", "show", "alice/svc", "1", "--json")
53	for _, want := range []string{
54		`"approvals_required":1`, `"codeowners_required":true`, `"resolved_required":true`,
55		`"owners_outstanding":[{"files":["svc.go"],"owners":["bob"]}]`, `"fast_forward":true`,
56		`requires 1 fresh approval(s)`, `CODEOWNERS approval missing for: svc.go (owned by bob)`,
57	} {
58		if !strings.Contains(out, want) {
59			t.Errorf("mr show gates missing %s:\n%s", want, out)
60		}
61	}
62	// The merge names both at once.
63	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/svc", "1"); code != 4 ||
64		!strings.Contains(errOut, "fresh approval") || !strings.Contains(errOut, "CODEOWNERS approval missing") {
65		t.Fatalf("merge refusal does not name every gate: %d %s", code, errOut)
66	}
67
68	// A reader's approval is advisory, and says so when made.
69	out, _, code := inst.ssh(t, carolKey, "", "mr", "review", "alice/svc", "1", "--approve", "--json")
70	if code != 0 || !strings.Contains(out, `"counts":false`) {
71		t.Fatalf("reader review: %d %s", code, out)
72	}
73	if out, _, _ := inst.ssh(t, carolKey, "", "mr", "review", "alice/svc", "1", "--comment"); !strings.Contains(out, "advisory") {
74		t.Fatalf("reader review does not say it is advisory: %s", out)
75	}
76	out, _, _ = inst.ssh(t, aliceKey, "", "mr", "show", "alice/svc", "1", "--json")
77	if !strings.Contains(out, `requires 1 fresh approval(s)`) {
78		t.Fatalf("advisory approval counted in the gates:\n%s", out)
79	}
80
81	// The owner's approval meets both; the block says so and the merge lands.
82	out, _, _ = inst.ssh(t, bobKey, "", "mr", "review", "alice/svc", "1", "--approve", "--json")
83	if !strings.Contains(out, `"counts":true`) {
84		t.Fatalf("writer review: %s", out)
85	}
86	out, _, _ = inst.ssh(t, aliceKey, "", "mr", "show", "alice/svc", "1", "--json")
87	if strings.Contains(out, `"unmet"`) || !strings.Contains(out, `"approvals":["bob"]`) {
88		t.Fatalf("gates after approval:\n%s", out)
89	}
90	if out, _, _ := inst.ssh(t, aliceKey, "", "mr", "show", "alice/svc", "1"); !strings.Contains(out, "gates: met; fast-forward possible") {
91		t.Fatalf("text gates line: %s", out)
92	}
93	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/svc", "1"); code != 0 {
94		t.Fatalf("merge: %s", errOut)
95	}
96	if out, _, _ := inst.ssh(t, aliceKey, "", "mr", "show", "alice/svc", "1", "--json"); strings.Contains(out, `"gates"`) {
97		t.Fatalf("gates reported on a merged request:\n%s", out)
98	}
99}