Repository navigation
Expand file tree
/
Copy pathstart_pr_test.go
More file actions
109 lines (90 loc) · 3.5 KB
/
Copy pathstart_pr_test.go
File metadata and controls
109 lines (90 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package dispatch_test
import (
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// startPRRemote gives the repo an origin whose refs/pull/88/head holds a commit that no branch
// holds, as GitHub keeps a fork's PR. It returns that commit.
func startPRRemote(t *testing.T, d *Dispatch) string {
t.Helper()
remote := filepath.Join(filepath.Dir(d.Repo), "remote.git")
hintGit(t, filepath.Dir(d.Repo), "init", "-q", "--bare", remote)
hintGit(t, d.Repo, "remote", "add", "origin", remote)
hintGit(t, d.Repo, "push", "-q", "origin", "main")
hintGit(t, d.Repo, "switch", "-q", "-c", "fork-work")
hintGit(t, d.Repo, "-c", "user.name=test", "-c", "user.email=test@example.com", "commit", "-q", "--allow-empty", "-m", "fork work")
head := revParse(t, d.Repo, "HEAD")
hintGit(t, d.Repo, "switch", "-q", "main")
hintGit(t, d.Repo, "branch", "-q", "-D", "fork-work")
hintGit(t, d.Repo, "push", "-q", "origin", head+":refs/heads/tmp")
hintGit(t, remote, "update-ref", "refs/pull/88/head", head)
hintGit(t, remote, "update-ref", "-d", "refs/heads/tmp")
return head
}
func startPRDispatch(t *testing.T) *Dispatch {
t.Helper()
d := newDispatch(t, dispatchOptions{Env: launchEnv, Launcher: builtinLauncher, Config: localOn})
d.StartBeforeTheEvents()
return d
}
func TestTickStartsALocalTicketsBranchAtItsStartPRHead(t *testing.T) {
t.Parallel()
d := startPRDispatch(t)
tree := d.Trees.Pooled()
head := startPRRemote(t, d)
d.fileLocal("In Progress", "--start-pr", "88")
d.Tick()
assert.Equal(t, "loc-1-work", branchIn(t, tree))
assert.Equal(t, head, revParse(t, tree, "HEAD"))
assert.Empty(t, dispatchKeys(t, d.Repo))
out, _ := exec.Command("git", "-C", d.Repo, "config", "--get", "branch.loc-1-work.remote").Output()
assert.Empty(t, strings.TrimSpace(string(out)))
}
func TestTickNeverTreatsAStartPRAsTheTicketsOwnPR(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Config: localOn, RepoSettings: autoMergeSetting})
d.StartBeforeTheEvents()
d.fileLocal("In Review", "--start-pr", "88")
d.Workers.Running("LOC-1", "idle")
// An approved PR on another branch, as a start PR is. It would merge if dispatch took it for LOC-1's.
d.GitHub.PullRequest("LOC-2", approvedPR("a1"))
d.Tick()
assert.Equal(t, []string{"loc-1-work"}, d.prReads())
assert.Empty(t, d.GitHub.Merged())
assert.Empty(t, d.receiptsOfKind("LOC-1", "nudge"))
assert.Empty(t, d.receiptsOfKind("LOC-1", "merge"))
}
func TestTickStartsALocalTicketWithoutStartPRAtTheBase(t *testing.T) {
t.Parallel()
d := startPRDispatch(t)
tree := d.Trees.Pooled()
startPRRemote(t, d)
d.fileLocal("In Progress")
d.Tick()
assert.Equal(t, revParse(t, d.Repo, "main"), revParse(t, tree, "HEAD"))
}
func TestTickReturnsTheLeaseWhenTheStartPRIsMissing(t *testing.T) {
t.Parallel()
d := startPRDispatch(t)
d.Trees.Pooled()
startPRRemote(t, d)
d.fileLocal("In Progress", "--start-pr", "99")
// A failed launch makes Tick return an error, which is what this test reads.
res := d.Run("tick")
require.ErrorContains(t, res.Err, "LOC-1 launch failed: switch_failed: git switch to loc-1-work from PR 99 failed")
assert.NotEmpty(t, d.Trees.Returned())
}
func TestTickKeepsAnExistingBranchWhenTheTicketHasAStartPR(t *testing.T) {
t.Parallel()
d := startPRDispatch(t)
tree := d.Trees.Pooled()
startPRRemote(t, d)
hintGit(t, d.Repo, "branch", "loc-1-work", "main")
d.fileLocal("In Progress", "--start-pr", "88")
d.Tick()
assert.Equal(t, revParse(t, d.Repo, "main"), revParse(t, tree, "HEAD"))
}