Skip to content

Commit cfbcf22

Browse files
committed
revert test change + docs
1 parent a0c92da commit cfbcf22

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

docs/github_action.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Here is an example of how to post comments with the coverage report to your pull
4848
- name: check test coverage
4949
id: coverage
5050
uses: vladopajic/go-test-coverage@v2
51+
continue-on-error: true # Should fail after coverage comment is posted
5152
with:
5253
config: ./.github/.testcoverage.yml
5354
@@ -84,4 +85,9 @@ Here is an example of how to post comments with the coverage report to your pull
8485
${{ fromJSON(steps.coverage.outputs.report) }}
8586
```
8687
edit-mode: replace
88+
89+
- name: "finally check coverage"
90+
if: steps.coverage.outcome == 'failure'
91+
shell: bash
92+
run: echo "coverage check failed" && exit 1
8793
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package badgestorer_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"runtime"
8+
"testing"
9+
"time"
10+
11+
"github.com/google/go-github/v56/github"
12+
"github.com/stretchr/testify/assert"
13+
14+
. "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/badgestorer"
15+
)
16+
17+
const envGitToken = "GITHUB_TOKEN" //nolint:gosec // false-positive
18+
19+
func Test_Github_Error(t *testing.T) {
20+
t.Parallel()
21+
22+
if testing.Short() {
23+
return
24+
}
25+
26+
data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
27+
cfg := Git{
28+
Token: `🔑`,
29+
Owner: "owner",
30+
Repository: "repo",
31+
}
32+
s := NewGithub(cfg)
33+
34+
updated, err := s.Store(data)
35+
assert.Error(t, err)
36+
assert.False(t, updated)
37+
}
38+
39+
func Test_Github(t *testing.T) {
40+
t.Parallel()
41+
42+
if testing.Short() {
43+
return
44+
}
45+
46+
if getEnv(envGitToken) == "" {
47+
t.Skipf("%v env variable not set", envGitToken)
48+
return
49+
}
50+
51+
data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
52+
cfg := Git{
53+
Token: getEnv(envGitToken),
54+
Owner: "vladopajic",
55+
Repository: "go-test-coverage",
56+
Branch: "badges-integration-test",
57+
// badge name must be unique because two tests running from different platforms
58+
// in CI can cause race condition if badge has the same name
59+
FileName: fmt.Sprintf("badge_%s_%d.svg", runtime.GOOS, time.Now().UnixNano()),
60+
}
61+
s := NewGithub(cfg)
62+
63+
// put badge
64+
updated, err := s.Store(data)
65+
assert.NoError(t, err)
66+
assert.True(t, updated)
67+
68+
// put badge again - no change
69+
updated, err = s.Store(data)
70+
assert.NoError(t, err)
71+
assert.False(t, updated)
72+
73+
// put badge again - expect change
74+
updated, err = s.Store(append(data, byte(1)))
75+
assert.NoError(t, err)
76+
assert.True(t, updated)
77+
78+
deleteFile(t, cfg)
79+
}
80+
81+
func getEnv(key string) string {
82+
value, _ := os.LookupEnv(key)
83+
return value
84+
}
85+
86+
func deleteFile(t *testing.T, cfg Git) {
87+
t.Helper()
88+
89+
client := github.NewClient(nil).WithAuthToken(cfg.Token)
90+
91+
fc, _, _, err := client.Repositories.GetContents(
92+
context.Background(),
93+
cfg.Owner,
94+
cfg.Repository,
95+
cfg.FileName,
96+
&github.RepositoryContentGetOptions{Ref: cfg.Branch},
97+
)
98+
assert.NoError(t, err)
99+
100+
_, _, err = client.Repositories.DeleteFile(
101+
context.Background(),
102+
cfg.Owner,
103+
cfg.Repository,
104+
cfg.FileName,
105+
&github.RepositoryContentFileOptions{
106+
Message: github.String("delete testing badge " + cfg.FileName),
107+
Branch: &cfg.Branch,
108+
SHA: fc.SHA,
109+
},
110+
)
111+
assert.NoError(t, err)
112+
}

0 commit comments

Comments
 (0)