github.com/jaylevin/jenkins-library@v1.230.4/cmd/githubCommentIssue_test.go (about) 1 package cmd 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/google/go-github/v32/github" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 type ghIssueCommentMock struct { 14 issueComment *github.IssueComment 15 issueID int64 16 issueError error 17 owner string 18 repo string 19 number int 20 } 21 22 func (g *ghIssueCommentMock) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) { 23 g.issueComment = comment 24 g.owner = owner 25 g.repo = repo 26 g.number = number 27 28 issueComment := github.IssueComment{ID: &g.issueID, Body: comment.Body} 29 30 ghRes := github.Response{Response: &http.Response{Status: "200"}} 31 if g.issueError != nil { 32 ghRes.Status = "401" 33 } 34 35 return &issueComment, &ghRes, g.issueError 36 } 37 38 func TestRunGithubCommentIssue(t *testing.T) { 39 ctx := context.Background() 40 t.Parallel() 41 42 t.Run("Success", func(t *testing.T) { 43 // init 44 ghIssueCommentService := ghIssueCommentMock{ 45 issueID: 1, 46 } 47 config := githubCommentIssueOptions{ 48 Owner: "TEST", 49 Repository: "test", 50 Body: "This is my test body", 51 Number: 1, 52 } 53 54 // test 55 err := runGithubCommentIssue(ctx, &config, nil, &ghIssueCommentService) 56 57 // assert 58 assert.NoError(t, err) 59 assert.Equal(t, config.Owner, ghIssueCommentService.owner) 60 assert.Equal(t, config.Repository, ghIssueCommentService.repo) 61 assert.Equal(t, config.Body, ghIssueCommentService.issueComment.GetBody()) 62 assert.Equal(t, config.Number, ghIssueCommentService.number) 63 }) 64 65 t.Run("Error", func(t *testing.T) { 66 // init 67 ghIssueCommentService := ghIssueCommentMock{ 68 issueError: fmt.Errorf("error creating comment"), 69 } 70 config := githubCommentIssueOptions{ 71 Number: 1, 72 } 73 74 // test 75 err := runGithubCommentIssue(ctx, &config, nil, &ghIssueCommentService) 76 77 // assert 78 assert.EqualError(t, err, "Error occurred when creating comment on issue 1: error creating comment") 79 }) 80 }