forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithubCommentIssue_test.go
80 lines (66 loc) · 1.93 KB
/
githubCommentIssue_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
package cmd
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/google/go-github/v32/github"
"github.com/stretchr/testify/assert"
)
type ghIssueCommentMock struct {
issueComment *github.IssueComment
issueID int64
issueError error
owner string
repo string
number int
}
func (g *ghIssueCommentMock) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
g.issueComment = comment
g.owner = owner
g.repo = repo
g.number = number
issueComment := github.IssueComment{ID: &g.issueID, Body: comment.Body}
ghRes := github.Response{Response: &http.Response{Status: "200"}}
if g.issueError != nil {
ghRes.Status = "401"
}
return &issueComment, &ghRes, g.issueError
}
func TestRunGithubCommentIssue(t *testing.T) {
ctx := context.Background()
t.Parallel()
t.Run("Success", func(t *testing.T) {
// init
ghIssueCommentService := ghIssueCommentMock{
issueID: 1,
}
config := githubCommentIssueOptions{
Owner: "TEST",
Repository: "test",
Body: "This is my test body",
Number: 1,
}
// test
err := runGithubCommentIssue(ctx, &config, nil, &ghIssueCommentService)
// assert
assert.NoError(t, err)
assert.Equal(t, config.Owner, ghIssueCommentService.owner)
assert.Equal(t, config.Repository, ghIssueCommentService.repo)
assert.Equal(t, config.Body, ghIssueCommentService.issueComment.GetBody())
assert.Equal(t, config.Number, ghIssueCommentService.number)
})
t.Run("Error", func(t *testing.T) {
// init
ghIssueCommentService := ghIssueCommentMock{
issueError: fmt.Errorf("error creating comment"),
}
config := githubCommentIssueOptions{
Number: 1,
}
// test
err := runGithubCommentIssue(ctx, &config, nil, &ghIssueCommentService)
// assert
assert.EqualError(t, err, "Error occurred when creating comment on issue 1: error creating comment")
})
}