diff --git a/test/integration/activity_test.go b/test/integration/activity_test.go index 39743c0b9c9..afa5a4184aa 100644 --- a/test/integration/activity_test.go +++ b/test/integration/activity_test.go @@ -29,9 +29,7 @@ func TestActivity_Starring(t *testing.T) { } // the rest of the tests requires auth - if !checkAuth("TestActivity_Starring") { - return - } + skipIfMissingAuth(t) // first, check if already starred the target repository star, _, err := client.Activity.IsStarred(t.Context(), owner, repo) @@ -119,9 +117,7 @@ func TestActivity_Watching(t *testing.T) { } // the rest of the tests requires auth - if !checkAuth("TestActivity_Watching") { - return - } + skipIfMissingAuth(t) // first, check if already watching the target repository sub, _, err := client.Activity.GetRepositorySubscription(t.Context(), owner, repo) diff --git a/test/integration/audit_log_test.go b/test/integration/audit_log_test.go index 2fa99cc26ba..3ac53e26220 100644 --- a/test/integration/audit_log_test.go +++ b/test/integration/audit_log_test.go @@ -15,6 +15,8 @@ import ( // Note: Org must be part of an enterprise. // Test requires auth - set env var GITHUB_AUTH_TOKEN. func TestOrganizationAuditLog(t *testing.T) { + skipIfMissingAuth(t) + org := "example_org" entries, _, err := client.Organizations.GetAuditLog(t.Context(), org, nil) if err != nil { diff --git a/test/integration/github_test.go b/test/integration/github_test.go index 58000cc9ed2..9b1bc61f59b 100644 --- a/test/integration/github_test.go +++ b/test/integration/github_test.go @@ -12,35 +12,27 @@ import ( "math/rand" "net/http" "os" + "sync" "testing" "github.com/google/go-github/v75/github" ) -var ( - client *github.Client - - // auth indicates whether tests are being run with an OAuth token. - // Tests can use this flag to skip certain tests when run without auth. - auth bool -) - -func init() { +// client is a github.Client with the default http.Client. It is authorized if auth is true. +// auth indicates whether tests are being run with an OAuth token +// that is defined in the GITHUB_AUTH_TOKEN environment variable. +var client, auth = sync.OnceValues(func() (*github.Client, bool) { token := os.Getenv("GITHUB_AUTH_TOKEN") if token == "" { - fmt.Print("!!! No OAuth token. Some tests won't run. !!!\n\n") - client = github.NewClient(nil) - } else { - client = github.NewClient(nil).WithAuthToken(token) - auth = true + return github.NewClient(nil), false } -} + return github.NewClient(nil).WithAuthToken(token), true +})() -func checkAuth(name string) bool { +func skipIfMissingAuth(t *testing.T) { if !auth { - fmt.Printf("No auth - skipping portions of %v\n", name) + t.Skipf("No OAuth token - skipping portions of %v\n", t.Name()) } - return auth } func createRandomTestRepository(t *testing.T, owner string, autoinit bool) *github.Repository { diff --git a/test/integration/misc_test.go b/test/integration/misc_test.go index 623c7c8aaf6..a6937d63944 100644 --- a/test/integration/misc_test.go +++ b/test/integration/misc_test.go @@ -41,8 +41,8 @@ func TestAPIMeta(t *testing.T) { t.Error("Get returned no git addresses") } - if !*meta.VerifiablePasswordAuthentication { - t.Error("APIMeta VerifiablePasswordAuthentication is false") + if *meta.VerifiablePasswordAuthentication { + t.Error("APIMeta VerifiablePasswordAuthentication is true") } } diff --git a/test/integration/repos_test.go b/test/integration/repos_test.go index b06ea20acb3..21dec66ed54 100644 --- a/test/integration/repos_test.go +++ b/test/integration/repos_test.go @@ -17,9 +17,7 @@ import ( ) func TestRepositories_CRUD(t *testing.T) { - if !checkAuth("TestRepositories_CRUD") { - return - } + skipIfMissingAuth(t) repo := createRandomTestRepository(t, "", true) @@ -75,9 +73,7 @@ func TestRepositories_BranchesTags(t *testing.T) { } func TestRepositories_EditBranches(t *testing.T) { - if !checkAuth("TestRepositories_EditBranches") { - return - } + skipIfMissingAuth(t) repo := createRandomTestRepository(t, "", true) @@ -148,9 +144,7 @@ func TestRepositories_EditBranches(t *testing.T) { } func TestRepositories_ListByAuthenticatedUser(t *testing.T) { - if !checkAuth("TestRepositories_ListByAuthenticatedUser") { - return - } + skipIfMissingAuth(t) _, _, err := client.Repositories.ListByAuthenticatedUser(t.Context(), nil) if err != nil { @@ -177,9 +171,7 @@ func TestRepositories_ListByUser(t *testing.T) { } func TestRepositories_DownloadReleaseAsset(t *testing.T) { - if !checkAuth("TestRepositories_DownloadReleaseAsset") { - return - } + skipIfMissingAuth(t) rc, _, err := client.Repositories.DownloadReleaseAsset(t.Context(), "andersjanmyr", "goose", 484892, http.DefaultClient) if err != nil { @@ -193,9 +185,7 @@ func TestRepositories_DownloadReleaseAsset(t *testing.T) { } func TestRepositories_Autolinks(t *testing.T) { - if !checkAuth("TestRepositories_Autolinks") { - return - } + skipIfMissingAuth(t) repo := createRandomTestRepository(t, "", true) diff --git a/test/integration/users_test.go b/test/integration/users_test.go index dd196aea928..0f46cd39d6f 100644 --- a/test/integration/users_test.go +++ b/test/integration/users_test.go @@ -46,9 +46,7 @@ func TestUsers_Get(t *testing.T) { } func TestUsers_Update(t *testing.T) { - if !checkAuth("TestUsers_Get") { - return - } + skipIfMissingAuth(t) u, _, err := client.Users.Get(t.Context(), "") if err != nil { @@ -93,9 +91,7 @@ func TestUsers_Update(t *testing.T) { } func TestUsers_Emails(t *testing.T) { - if !checkAuth("TestUsers_Emails") { - return - } + skipIfMissingAuth(t) emails, _, err := client.Users.ListEmails(t.Context(), nil) if err != nil { @@ -169,9 +165,7 @@ func TestUsers_Keys(t *testing.T) { } // the rest of the tests requires auth - if !checkAuth("TestUsers_Keys") { - return - } + skipIfMissingAuth(t) // TODO: make this integration test work for any authenticated user. keys, _, err = client.Users.ListKeys(t.Context(), "", nil)