Skip to content

Commit

Permalink
add GitHub integration test (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladopajic authored Nov 11, 2024
1 parent 0cfd077 commit 99c4bea
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
5 changes: 1 addition & 4 deletions .github/.testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ profile: cover.short.profile,cover.long.profile
local-prefix: "github.com/vladopajic/go-test-coverage/v2"
threshold:
file: 100
total: 95
override:
- threshold: 50
path: badgestorer/github.go$
total: 100
exclude:
paths:
- main\.go$
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
go-version-file: go.mod

- name: test
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed for GitHub badge storer integration test
run: make test

- name: check test coverage
Expand Down
4 changes: 2 additions & 2 deletions pkg/testcoverage/badgestorer/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ func (s *githubStorer) Store(data []byte) (bool, error) {
return updateBadge(nil)
}

if err != nil {
if err != nil { // coverage-ignore
return false, fmt.Errorf("get badge content: %w", err)
}

content, err := fc.GetContent()
if err != nil {
if err != nil { // coverage-ignore
return false, fmt.Errorf("decode badge content: %w", err)
}

Expand Down
78 changes: 78 additions & 0 deletions pkg/testcoverage/badgestorer/github_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package badgestorer_test

import (
"context"
"os"
"testing"

"github.com/google/go-github/v56/github"
"github.com/stretchr/testify/assert"

. "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/badgestorer"
)

const envGitToken = "GITHUB_TOKEN" //nolint:gosec // false-positive

func Test_Github_Error(t *testing.T) {
t.Parallel()

Expand All @@ -27,3 +32,76 @@ func Test_Github_Error(t *testing.T) {
assert.Error(t, err)
assert.False(t, updated)
}

func Test_Github(t *testing.T) {
t.Parallel()

if testing.Short() {
return
}

if getEnv(envGitToken) == "" {
t.Skipf("%v env variable not set", envGitToken)
return
}

data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
cfg := Git{
Token: getEnv(envGitToken),
Owner: "vladopajic",
Repository: "go-test-coverage",
Branch: "badges-integration-test",
FileName: "badge.svg",
}
s := NewGithub(cfg)

// put badge
updated, err := s.Store(data)
assert.NoError(t, err)
assert.True(t, updated)

// put badge again - no change
updated, err = s.Store(data)
assert.NoError(t, err)
assert.False(t, updated)

// put badge again - expect change
updated, err = s.Store(append(data, byte(1)))
assert.NoError(t, err)
assert.True(t, updated)

deleteFile(t, cfg)
}

func getEnv(key string) string {
value, _ := os.LookupEnv(key)
return value
}

func deleteFile(t *testing.T, cfg Git) {
t.Helper()

client := github.NewClient(nil).WithAuthToken(cfg.Token)

fc, _, _, err := client.Repositories.GetContents(
context.Background(),
cfg.Owner,
cfg.Repository,
cfg.FileName,
&github.RepositoryContentGetOptions{Ref: cfg.Branch},
)
assert.NoError(t, err)

_, _, err = client.Repositories.DeleteFile(
context.Background(),
cfg.Owner,
cfg.Repository,
cfg.FileName,
&github.RepositoryContentFileOptions{
Message: github.String("delete testing badge " + cfg.FileName),
Branch: &cfg.Branch,
SHA: fc.SHA,
},
)
assert.NoError(t, err)
}

0 comments on commit 99c4bea

Please sign in to comment.