1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/githubSetCommitStatus_test.go
Oliver Nocon 328ee34369
GitHub steps: convenience updates (#2026)
* GitHub steps: convenience updates

* update generated files

* Update cmd/githubCheckBranchProtection.go

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* Update cmd/githubCheckBranchProtection.go

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* Update cmd/githubCheckBranchProtection.go

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2020-09-15 17:50:55 +02:00

54 lines
1.9 KiB
Go

package cmd
import (
"context"
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/google/go-github/v32/github"
"github.com/stretchr/testify/assert"
)
type ghSetCommitRepoService struct {
serviceError error
owner string
ref string
repo string
status *github.RepoStatus
}
func (g *ghSetCommitRepoService) CreateStatus(ctx context.Context, owner, repo, ref string, status *github.RepoStatus) (*github.RepoStatus, *github.Response, error) {
g.owner = owner
g.repo = repo
g.ref = ref
g.status = status
return nil, nil, g.serviceError
}
func TestRunGithubSetCommitStatus(t *testing.T) {
ctx := context.Background()
telemetryData := telemetry.CustomData{}
t.Run("success case", func(t *testing.T) {
config := githubSetCommitStatusOptions{CommitID: "testSha", Context: "test /context", Description: "testDescription", Owner: "testOrg", Repository: "testRepo", Status: "success", TargetURL: "https://test.url"}
ghRepo := ghSetCommitRepoService{}
err := runGithubSetCommitStatus(ctx, &config, &telemetryData, &ghRepo)
expectedStatus := github.RepoStatus{Context: &config.Context, Description: &config.Description, State: &config.Status, TargetURL: &config.TargetURL}
assert.NoError(t, err)
assert.Equal(t, config.CommitID, ghRepo.ref)
assert.Equal(t, config.Owner, ghRepo.owner)
assert.Equal(t, config.Repository, ghRepo.repo)
assert.Equal(t, &expectedStatus, ghRepo.status)
})
t.Run("error calling GitHub", func(t *testing.T) {
config := githubSetCommitStatusOptions{CommitID: "testSha", Owner: "testOrg", Repository: "testRepo", Status: "pending"}
ghRepo := ghSetCommitRepoService{serviceError: fmt.Errorf("gh test error")}
err := runGithubSetCommitStatus(ctx, &config, &telemetryData, &ghRepo)
assert.EqualError(t, err, "failed to set status 'pending' on commitId 'testSha': gh test error")
})
}