Skip to content

Commit 9c77662

Browse files
committed
test: Simplify the function that skips integration tests
1 parent 233fe03 commit 9c77662

File tree

6 files changed

+24
-50
lines changed

6 files changed

+24
-50
lines changed

test/integration/activity_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ func TestActivity_Starring(t *testing.T) {
2929
}
3030

3131
// the rest of the tests requires auth
32-
if !checkAuth("TestActivity_Starring") {
33-
return
34-
}
32+
skipIfMissingAuth(t)
3533

3634
// first, check if already starred the target repository
3735
star, _, err := client.Activity.IsStarred(t.Context(), owner, repo)
@@ -119,9 +117,7 @@ func TestActivity_Watching(t *testing.T) {
119117
}
120118

121119
// the rest of the tests requires auth
122-
if !checkAuth("TestActivity_Watching") {
123-
return
124-
}
120+
skipIfMissingAuth(t)
125121

126122
// first, check if already watching the target repository
127123
sub, _, err := client.Activity.GetRepositorySubscription(t.Context(), owner, repo)

test/integration/audit_log_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
// Note: Org must be part of an enterprise.
1616
// Test requires auth - set env var GITHUB_AUTH_TOKEN.
1717
func TestOrganizationAuditLog(t *testing.T) {
18+
skipIfMissingAuth(t)
19+
1820
org := "example_org"
1921
entries, _, err := client.Organizations.GetAuditLog(t.Context(), org, nil)
2022
if err != nil {

test/integration/github_test.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,27 @@ import (
1212
"math/rand"
1313
"net/http"
1414
"os"
15+
"sync"
1516
"testing"
1617

1718
"github.com/google/go-github/v75/github"
1819
)
1920

20-
var (
21-
client *github.Client
22-
23-
// auth indicates whether tests are being run with an OAuth token.
24-
// Tests can use this flag to skip certain tests when run without auth.
25-
auth bool
26-
)
27-
28-
func init() {
21+
// client is a github.Client with the default http.Client. It is authorized if auth is true.
22+
// auth indicates whether tests are being run with an OAuth token
23+
// that is defined in the GITHUB_AUTH_TOKEN environment variable.
24+
var client, auth = sync.OnceValues(func() (*github.Client, bool) {
2925
token := os.Getenv("GITHUB_AUTH_TOKEN")
3026
if token == "" {
31-
fmt.Print("!!! No OAuth token. Some tests won't run. !!!\n\n")
32-
client = github.NewClient(nil)
33-
} else {
34-
client = github.NewClient(nil).WithAuthToken(token)
35-
auth = true
27+
return github.NewClient(nil), false
3628
}
37-
}
29+
return github.NewClient(nil).WithAuthToken(token), true
30+
})()
3831

39-
func checkAuth(name string) bool {
32+
func skipIfMissingAuth(t *testing.T) {
4033
if !auth {
41-
fmt.Printf("No auth - skipping portions of %v\n", name)
34+
t.Skipf("No OAuth token - skipping portions of %v\n", t.Name())
4235
}
43-
return auth
4436
}
4537

4638
func createRandomTestRepository(t *testing.T, owner string, autoinit bool) *github.Repository {

test/integration/misc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func TestAPIMeta(t *testing.T) {
4141
t.Error("Get returned no git addresses")
4242
}
4343

44-
if !*meta.VerifiablePasswordAuthentication {
45-
t.Error("APIMeta VerifiablePasswordAuthentication is false")
44+
if *meta.VerifiablePasswordAuthentication {
45+
t.Error("APIMeta VerifiablePasswordAuthentication is true")
4646
}
4747
}
4848

test/integration/repos_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import (
1717
)
1818

1919
func TestRepositories_CRUD(t *testing.T) {
20-
if !checkAuth("TestRepositories_CRUD") {
21-
return
22-
}
20+
skipIfMissingAuth(t)
2321

2422
repo := createRandomTestRepository(t, "", true)
2523

@@ -75,9 +73,7 @@ func TestRepositories_BranchesTags(t *testing.T) {
7573
}
7674

7775
func TestRepositories_EditBranches(t *testing.T) {
78-
if !checkAuth("TestRepositories_EditBranches") {
79-
return
80-
}
76+
skipIfMissingAuth(t)
8177

8278
repo := createRandomTestRepository(t, "", true)
8379

@@ -148,9 +144,7 @@ func TestRepositories_EditBranches(t *testing.T) {
148144
}
149145

150146
func TestRepositories_ListByAuthenticatedUser(t *testing.T) {
151-
if !checkAuth("TestRepositories_ListByAuthenticatedUser") {
152-
return
153-
}
147+
skipIfMissingAuth(t)
154148

155149
_, _, err := client.Repositories.ListByAuthenticatedUser(t.Context(), nil)
156150
if err != nil {
@@ -177,9 +171,7 @@ func TestRepositories_ListByUser(t *testing.T) {
177171
}
178172

179173
func TestRepositories_DownloadReleaseAsset(t *testing.T) {
180-
if !checkAuth("TestRepositories_DownloadReleaseAsset") {
181-
return
182-
}
174+
skipIfMissingAuth(t)
183175

184176
rc, _, err := client.Repositories.DownloadReleaseAsset(t.Context(), "andersjanmyr", "goose", 484892, http.DefaultClient)
185177
if err != nil {
@@ -193,9 +185,7 @@ func TestRepositories_DownloadReleaseAsset(t *testing.T) {
193185
}
194186

195187
func TestRepositories_Autolinks(t *testing.T) {
196-
if !checkAuth("TestRepositories_Autolinks") {
197-
return
198-
}
188+
skipIfMissingAuth(t)
199189

200190
repo := createRandomTestRepository(t, "", true)
201191

test/integration/users_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ func TestUsers_Get(t *testing.T) {
4646
}
4747

4848
func TestUsers_Update(t *testing.T) {
49-
if !checkAuth("TestUsers_Get") {
50-
return
51-
}
49+
skipIfMissingAuth(t)
5250

5351
u, _, err := client.Users.Get(t.Context(), "")
5452
if err != nil {
@@ -93,9 +91,7 @@ func TestUsers_Update(t *testing.T) {
9391
}
9492

9593
func TestUsers_Emails(t *testing.T) {
96-
if !checkAuth("TestUsers_Emails") {
97-
return
98-
}
94+
skipIfMissingAuth(t)
9995

10096
emails, _, err := client.Users.ListEmails(t.Context(), nil)
10197
if err != nil {
@@ -169,9 +165,7 @@ func TestUsers_Keys(t *testing.T) {
169165
}
170166

171167
// the rest of the tests requires auth
172-
if !checkAuth("TestUsers_Keys") {
173-
return
174-
}
168+
skipIfMissingAuth(t)
175169

176170
// TODO: make this integration test work for any authenticated user.
177171
keys, _, err = client.Users.ListKeys(t.Context(), "", nil)

0 commit comments

Comments
 (0)