forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithubCreatePullRequest_test.go
120 lines (98 loc) · 4.16 KB
/
githubCreatePullRequest_test.go
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
110
111
112
113
114
115
116
117
118
119
120
package cmd
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/google/go-github/v32/github"
"github.com/stretchr/testify/assert"
)
type ghPRMock struct {
pullrequest *github.NewPullRequest
prError error
owner string
repo string
}
func (g *ghPRMock) Create(ctx context.Context, owner string, repo string, pull *github.NewPullRequest) (*github.PullRequest, *github.Response, error) {
g.pullrequest = pull
g.owner = owner
g.repo = repo
prNumber := 1
head := github.PullRequestBranch{Ref: pull.Head}
base := github.PullRequestBranch{Ref: pull.Base}
pr := github.PullRequest{Number: &prNumber, Title: pull.Title, Head: &head, Base: &base, Body: pull.Body}
ghRes := github.Response{Response: &http.Response{Status: "200"}}
if g.prError != nil {
ghRes.Status = "401"
}
return &pr, &ghRes, g.prError
}
type ghIssueMock struct {
issueRequest *github.IssueRequest
issueError error
owner string
repo string
number int
}
func (g *ghIssueMock) Edit(ctx context.Context, owner string, repo string, number int, issue *github.IssueRequest) (*github.Issue, *github.Response, error) {
g.issueRequest = issue
g.owner = owner
g.repo = repo
g.number = number
labels := []*github.Label{}
for _, l := range *issue.Labels {
labels = append(labels, &github.Label{Name: &l})
}
assignees := []*github.User{}
for _, a := range *issue.Assignees {
assignees = append(assignees, &github.User{Login: &a})
}
updatedIssue := github.Issue{Number: &number, Labels: labels, Assignees: assignees}
ghRes := github.Response{Response: &http.Response{Status: "200"}}
if g.issueError != nil {
ghRes.Status = "401"
}
return &updatedIssue, &ghRes, g.issueError
}
func TestRunGithubCreatePullRequest(t *testing.T) {
ctx := context.Background()
myGithubPROptions := githubCreatePullRequestOptions{
Owner: "TEST",
Repository: "test",
Title: "Test Title",
Body: "This is the test body.",
Head: "head/test",
Base: "base/test",
Labels: []string{"Test1", "Test2"},
Assignees: []string{"User1", "User2"},
}
t.Run("Success", func(t *testing.T) {
ghPRService := ghPRMock{}
ghIssueService := ghIssueMock{}
err := runGithubCreatePullRequest(ctx, &myGithubPROptions, &ghPRService, &ghIssueService)
assert.NoError(t, err, "Error occurred but none expected.")
assert.Equal(t, myGithubPROptions.Owner, ghPRService.owner, "Owner not passed correctly")
assert.Equal(t, myGithubPROptions.Repository, ghPRService.repo, "Repository not passed correctly")
assert.Equal(t, myGithubPROptions.Title, ghPRService.pullrequest.GetTitle(), "Title not passed correctly")
assert.Equal(t, myGithubPROptions.Body, ghPRService.pullrequest.GetBody(), "Body not passed correctly")
assert.Equal(t, myGithubPROptions.Head, ghPRService.pullrequest.GetHead(), "Head not passed correctly")
assert.Equal(t, myGithubPROptions.Base, ghPRService.pullrequest.GetBase(), "Base not passed correctly")
assert.Equal(t, myGithubPROptions.Owner, ghIssueService.owner, "Owner not passed correctly")
assert.Equal(t, myGithubPROptions.Repository, ghIssueService.repo, "Repository not passed correctly")
assert.Equal(t, myGithubPROptions.Labels, ghIssueService.issueRequest.GetLabels(), "Labels not passed correctly")
assert.Equal(t, myGithubPROptions.Assignees, ghIssueService.issueRequest.GetAssignees(), "Assignees not passed correctly")
assert.Equal(t, 1, ghIssueService.number, "PR number not passed correctly")
})
t.Run("Create error", func(t *testing.T) {
ghPRService := ghPRMock{prError: fmt.Errorf("Authentication failed")}
ghIssueService := ghIssueMock{}
err := runGithubCreatePullRequest(ctx, &myGithubPROptions, &ghPRService, &ghIssueService)
assert.EqualError(t, err, "Error occurred when creating pull request: Authentication failed", "Wrong error returned")
})
t.Run("Edit error", func(t *testing.T) {
ghPRService := ghPRMock{}
ghIssueService := ghIssueMock{issueError: fmt.Errorf("Authentication failed")}
err := runGithubCreatePullRequest(ctx, &myGithubPROptions, &ghPRService, &ghIssueService)
assert.EqualError(t, err, "Error occurred when editing pull request: Authentication failed", "Wrong error returned")
})
}