github.com/xgoffin/jenkins-library@v1.154.0/cmd/githubSetCommitStatus_test.go (about) 1 package cmd 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/SAP/jenkins-library/pkg/telemetry" 9 10 "github.com/google/go-github/v32/github" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 type ghSetCommitRepoService struct { 15 serviceError error 16 owner string 17 ref string 18 repo string 19 status *github.RepoStatus 20 } 21 22 func (g *ghSetCommitRepoService) CreateStatus(ctx context.Context, owner, repo, ref string, status *github.RepoStatus) (*github.RepoStatus, *github.Response, error) { 23 g.owner = owner 24 g.repo = repo 25 g.ref = ref 26 g.status = status 27 28 return nil, nil, g.serviceError 29 } 30 31 func TestRunGithubSetCommitStatus(t *testing.T) { 32 ctx := context.Background() 33 telemetryData := telemetry.CustomData{} 34 35 t.Run("success case", func(t *testing.T) { 36 config := githubSetCommitStatusOptions{CommitID: "testSha", Context: "test /context", Description: "testDescription", Owner: "testOrg", Repository: "testRepo", Status: "success", TargetURL: "https://test.url"} 37 ghRepo := ghSetCommitRepoService{} 38 err := runGithubSetCommitStatus(ctx, &config, &telemetryData, &ghRepo) 39 expectedStatus := github.RepoStatus{Context: &config.Context, Description: &config.Description, State: &config.Status, TargetURL: &config.TargetURL} 40 assert.NoError(t, err) 41 assert.Equal(t, config.CommitID, ghRepo.ref) 42 assert.Equal(t, config.Owner, ghRepo.owner) 43 assert.Equal(t, config.Repository, ghRepo.repo) 44 assert.Equal(t, &expectedStatus, ghRepo.status) 45 }) 46 47 t.Run("error calling GitHub", func(t *testing.T) { 48 config := githubSetCommitStatusOptions{CommitID: "testSha", Owner: "testOrg", Repository: "testRepo", Status: "pending"} 49 ghRepo := ghSetCommitRepoService{serviceError: fmt.Errorf("gh test error")} 50 err := runGithubSetCommitStatus(ctx, &config, &telemetryData, &ghRepo) 51 assert.EqualError(t, err, "failed to set status 'pending' on commitId 'testSha': gh test error") 52 }) 53 }