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