gunk-dev/the-valley

merge_test.go

sourceb8e46aa6aaccmain's published tip history
5.1 KiB
1//go:build e2e
2
3package e2e
4
5import (
6 "fmt"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11)
12
13// mergeGHStub returns a gh stub that answers every query `klaus merge` makes
14// for one approved, CI-green, behind-but-clean PR. mergeOutcome scripts what
15// `gh pr merge` does: "ok" always succeeds, "stale-then-ok" refuses the first
16// attempt the way GitHub refuses a branch that must be up to date, then
17// succeeds.
18func mergeGHStub(h *Harness, mergeOutcome string) string {
19 return fmt.Sprintf(`#!/usr/bin/env bash
20dir=%q
21{ printf 'gh'; for a in "$@"; do printf ' %%q' "$a"; done; printf '\n'; } >> "$dir/gh.argv"
22case "$*" in
23 *"pr merge"*)
24 n=$(cat "$dir/merge.count" 2>/dev/null || echo 0)
25 echo $((n + 1)) > "$dir/merge.count"
26 if [ "%s" = stale-then-ok ] && [ "$n" = 0 ]; then
27 echo "failed to merge: Base branch policy prohibits the merge" >&2
28 exit 1
29 fi
30 exit 0 ;;
31 *update-branch*) exit 0 ;;
32 *"pr checks"*) echo "build pass 1s https://ci/1" ; exit 0 ;;
33 *"--json title"*) echo "Fix the widget" ; exit 0 ;;
34 *"--json mergeable"*) echo MERGEABLE ; exit 0 ;;
35 *"--json baseRefName"*) printf 'main\tfeature\tacme\n' ; exit 0 ;;
36 *"--json reviewDecision"*) echo APPROVED ; exit 0 ;;
37 *"--json headRefName"*) echo feature ; exit 0 ;;
38 *compare*) echo 2 ; exit 0 ;;
39 *"repo view"*) echo acme/widget ; exit 0 ;;
40esac
41exit 0
42`, h.E2EDir, mergeOutcome)
43}
44
45// mergeCount returns how many times the stubbed `gh pr merge` ran.
46func mergeCount(h *Harness) string {
47 b, err := os.ReadFile(filepath.Join(h.E2EDir, "merge.count"))
48 if err != nil {
49 return "0"
50 }
51 return strings.TrimSpace(string(b))
52}
53
54// A branch that is only behind main must be merged as-is. Rebasing it would
55// force-push a new head and, under a policy that dismisses reviews on new
56// commits, throw away the approval the merge depends on (issue #293).
57func TestMergeBehindBranchMergesWithoutRebasing(t *testing.T) {
58 t.Parallel()
59 h := NewHarness(t)
60 h.WriteStub("gh", mergeGHStub(h, "ok"))
61
62 res := h.RunKlausIn(h.E2EDir, "merge", "42", "--repo", "acme/widget", "--force")
63
64 if res.ExitCode != 0 {
65 t.Fatalf("merge exited %d\nstdout: %s\nstderr: %s", res.ExitCode, res.Stdout, res.Stderr)
66 }
67 if !strings.Contains(res.Stdout, "Merged PR #42") {
68 t.Errorf("expected the PR to merge, got: %s", res.Stdout)
69 }
70 if !strings.Contains(res.Stdout, "Behind: 2") {
71 t.Errorf("expected the behind count to be reported, got: %s", res.Stdout)
72 }
73 if strings.Contains(strings.ToLower(res.Stdout), "rebas") {
74 t.Errorf("a behind-but-clean branch must not be rebased, got: %s", res.Stdout)
75 }
76 if got := h.GHArgv(); strings.Contains(got, "update-branch") {
77 t.Errorf("update-branch is only for a refused merge, got: %s", got)
78 }
79 if got := mergeCount(h); got != "1" {
80 t.Errorf("gh pr merge ran %s times, want 1", got)
81 }
82}
83
84// When GitHub refuses the merge because the branch must be up to date, klaus
85// falls back to the server-side "Update branch" (a merge from main, which keeps
86// the head — and the approval — intact), waits for CI, and merges again.
87func TestMergeFallsBackToUpdateBranchOnStaleRefusal(t *testing.T) {
88 t.Parallel()
89 h := NewHarness(t)
90 h.WriteStub("gh", mergeGHStub(h, "stale-then-ok"))
91
92 res := h.RunKlausIn(h.E2EDir, "merge", "42", "--repo", "acme/widget", "--force")
93
94 if res.ExitCode != 0 {
95 t.Fatalf("merge exited %d\nstdout: %s\nstderr: %s", res.ExitCode, res.Stdout, res.Stderr)
96 }
97 if !strings.Contains(res.Stdout, "updating branch from main") {
98 t.Errorf("expected the update-branch fallback, got: %s", res.Stdout)
99 }
100 if !strings.Contains(res.Stdout, "Merged PR #42") {
101 t.Errorf("expected the PR to merge after the update, got: %s", res.Stdout)
102 }
103 if strings.Contains(strings.ToLower(res.Stdout), "rebas") {
104 t.Errorf("the fallback must not rebase, got: %s", res.Stdout)
105 }
106 argv := h.GHArgv()
107 if !strings.Contains(argv, "pr update-branch") {
108 t.Errorf("expected the server-side update-branch call, got: %s", argv)
109 }
110 if got := mergeCount(h); got != "2" {
111 t.Errorf("gh pr merge ran %s times, want 2 (refused, then retried)", got)
112 }
113}
114
115// merge must work outside a git checkout, resolving the target repo's config
116// (here require_approval=false) rather than the current directory's. Without
117// that resolution the approval default of true would skip the PR (issue #293).
118func TestMergeOutsideGitRepoUsesTargetRepoConfig(t *testing.T) {
119 t.Parallel()
120 h := NewHarness(t)
121 h.WriteStub("gh", mergeGHStub(h, "ok"))
122 h.AmendRepoConfig(map[string]any{"require_approval": false})
123 // Registers under the repo portion of the ref, i.e. the project name "widget".
124 h.RegisterProject("acme/widget", h.RepoDir)
125
126 res := h.RunKlausIn(h.E2EDir, "merge", "42", "--repo", "widget", "--yes")
127
128 if res.ExitCode != 0 {
129 t.Fatalf("merge exited %d\nstdout: %s\nstderr: %s", res.ExitCode, res.Stdout, res.Stderr)
130 }
131 if strings.Contains(res.Stdout, "not approved") {
132 t.Errorf("target repo config sets require_approval=false, got: %s", res.Stdout)
133 }
134 if !strings.Contains(res.Stdout, "Merged PR #42") {
135 t.Errorf("expected the PR to merge, got: %s", res.Stdout)
136 }
137 if strings.Contains(res.Stderr, "git repository root") {
138 t.Errorf("merge must not require a git checkout, got: %s", res.Stderr)
139 }
140}