Skip to content

Commit f9720f1

Browse files
committed
fix tests
1 parent 9755a02 commit f9720f1

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build:
88
go build -o bin/actions ./cmd
99

1010
test:
11-
echo
11+
go test ./...
1212

1313
# Run this like: SOURCE_BRANCH= make build/docker so that you can build the `latest` from a commit that is not tagged yet
1414
build/docker: SOURCE_BRANCH ?= v1.2.3

cmd/checks/checks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func main() {
12-
cmd := exec.New()
12+
cmd := checks.New()
1313

1414
fs := flag.CommandLine
1515
fs.Init("checks", flag.ExitOnError)

pkg/pullvet/pullvet.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type Command struct {
3030
anyMilestone bool
3131
requireAny bool
3232
requireAll bool
33+
34+
getPullRequestBody func(string, string, int) (string, error)
3335
}
3436

3537
func normalizeNewlines(str string) string {
@@ -38,6 +40,7 @@ func normalizeNewlines(str string) string {
3840

3941
func New() *Command {
4042
return &Command{
43+
getPullRequestBody: GetPullRequestBody,
4144
}
4245
}
4346

@@ -112,25 +115,11 @@ func (c *Command) HandlePullRequest(owner, repo string, pullRequest *github.Pull
112115
var body string
113116

114117
if owner != "" {
115-
client, err := actions.CreateInstallationTokenClient(os.Getenv("GITHUB_TOKEN"), "", "")
116-
if err != nil {
117-
return err
118-
}
119-
120-
pr, _, err := client.PullRequests.Get(context.Background(), owner, repo, pullRequest.GetNumber())
118+
var err error
119+
body, err = c.getPullRequestBody(owner, repo, pullRequest.GetNumber())
121120
if err != nil {
122121
return err
123122
}
124-
125-
buf := bytes.Buffer{}
126-
enc := json.NewEncoder(&buf)
127-
enc.SetIndent("", " ")
128-
if err := enc.Encode(pr); err != nil {
129-
return err
130-
}
131-
log.Printf("Pull request:\n%s", buf.String())
132-
133-
body = pr.GetBody()
134123
} else {
135124
body = pullRequest.GetBody()
136125
}
@@ -168,6 +157,28 @@ func (c *Command) HandlePullRequest(owner, repo string, pullRequest *github.Pull
168157
return nil
169158
}
170159

160+
func GetPullRequestBody(owner, repo string, prNumber int) (string, error) {
161+
client, err := actions.CreateInstallationTokenClient(os.Getenv("GITHUB_TOKEN"), "", "")
162+
if err != nil {
163+
return "", err
164+
}
165+
166+
pr, _, err := client.PullRequests.Get(context.Background(), owner, repo, prNumber)
167+
if err != nil {
168+
return "", err
169+
}
170+
171+
buf := bytes.Buffer{}
172+
enc := json.NewEncoder(&buf)
173+
enc.SetIndent("", " ")
174+
if err := enc.Encode(pr); err != nil {
175+
return "", err
176+
}
177+
log.Printf("Pull request:\n%s", buf.String())
178+
179+
return pr.GetBody(), nil
180+
}
181+
171182
func formatFailures(failures []string) string {
172183
var lines []string
173184
for _, f := range failures {

pkg/pullvet/pullvet_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,48 +88,54 @@ func TestNoteRegex(t *testing.T) {
8888
}
8989

9090
func TestRun(t *testing.T) {
91+
stubPRBody := func(body string) func(owner, repo string, num int) (string, error) {
92+
return func(owner, repo string, num int) (string, error) {
93+
return body, nil
94+
}
95+
}
96+
9197
testcases := []struct {
9298
cmd *Command
9399
input *github.PullRequest
94100
expected string
95101
}{
96102
{
97-
cmd: &Command{requireAny: true, labels: []string{"v1"}},
103+
cmd: &Command{requireAny: true, labels: []string{"v1"}, noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
98104
input: &github.PullRequest{Labels: []*github.Label{&github.Label{Name: github.String("v1")}}},
99105
expected: "",
100106
},
101107
{
102-
cmd: &Command{requireAny: true, labels: []string{"v2"}},
108+
cmd: &Command{requireAny: true, labels: []string{"v2"}, noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
103109
input: &github.PullRequest{Labels: []*github.Label{&github.Label{Name: github.String("v1")}}},
104110
expected: "1 check(s) failed:\n* missing label: v2",
105111
},
106112
{
107-
cmd: &Command{requireAny: true, labels: []string{"v2", "v3"}},
113+
cmd: &Command{requireAny: true, labels: []string{"v2", "v3"}, noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
108114
input: &github.PullRequest{Labels: []*github.Label{&github.Label{Name: github.String("v1")}}},
109115
expected: "2 check(s) failed:\n* missing label: v2\n* missing label: v3",
110116
},
111117
{
112-
cmd: &Command{requireAny: true, milestone: "v1"},
118+
cmd: &Command{requireAny: true, milestone: "v1", noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
113119
input: &github.PullRequest{Milestone: &github.Milestone{Title: github.String("v1")}},
114120
expected: "",
115121
},
116122
{
117-
cmd: &Command{requireAny: true, anyMilestone: true},
123+
cmd: &Command{requireAny: true, anyMilestone: true, noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
118124
input: &github.PullRequest{Milestone: &github.Milestone{Title: github.String("v1")}},
119125
expected: "",
120126
},
121127
{
122-
cmd: &Command{requireAny: true, milestone: "v2"},
128+
cmd: &Command{requireAny: true, milestone: "v2", noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
123129
input: &github.PullRequest{Milestone: &github.Milestone{Title: github.String("v1")}},
124130
expected: "1 check(s) failed:\n* unexpected milestone: expected \"v2\", got \"v1\"",
125131
},
126132
{
127-
cmd: &Command{requireAny: true, anyMilestone: true},
133+
cmd: &Command{requireAny: true, anyMilestone: true, noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
128134
input: &github.PullRequest{},
129135
expected: "1 check(s) failed:\n* missing milestone",
130136
},
131137
{
132-
cmd: &Command{requireAny: true, labels: []string{"v2", "v3"}},
138+
cmd: &Command{requireAny: true, labels: []string{"v2", "v3"}, noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
133139
input: &github.PullRequest{
134140
Labels: []*github.Label{
135141
&github.Label{Name: github.String("v2")},
@@ -139,7 +145,7 @@ func TestRun(t *testing.T) {
139145
expected: "",
140146
},
141147
{
142-
cmd: &Command{requireAll: true, labels: []string{"v2", "v3"}},
148+
cmd: &Command{requireAll: true, labels: []string{"v2", "v3"}, noteRegex: defaultNoteRegex, getPullRequestBody: stubPRBody("")},
143149
input: &github.PullRequest{
144150
Labels: []*github.Label{
145151
&github.Label{Name: github.String("v2")},
@@ -153,7 +159,7 @@ func TestRun(t *testing.T) {
153159
for i := range testcases {
154160
tc := testcases[i]
155161

156-
err := tc.cmd.HandlePullRequest("", "", tc.input)
162+
err := tc.cmd.HandlePullRequest("myuser", "myrepo", tc.input)
157163

158164
if tc.expected != "" && !strings.Contains(err.Error(), tc.expected) {
159165
t.Errorf("unexpected error: expected=%q, got=%q", tc.expected, err)

0 commit comments

Comments
 (0)