From 2391edfbe1efe32d2a14dae3404f830b79397e11 Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Mon, 23 Oct 2023 15:40:01 -0700 Subject: [PATCH] :seedling: add style linters: mirror, tenv, usestdlibvars (#3586) * fix tenv linter and bug with t.Parallel Signed-off-by: Spencer Schrock * fix usestdlibvars linter Signed-off-by: Spencer Schrock * fix mirror linter Signed-off-by: Spencer Schrock --------- Signed-off-by: Spencer Schrock --- .golangci.yml | 3 +++ checks/webhook_test.go | 7 ++----- clients/cii_http_client.go | 2 +- clients/gitlabrepo/branches.go | 5 +++-- clients/gitlabrepo/branches_test.go | 4 ++-- clients/gitlabrepo/issues.go | 3 ++- cmd/internal/nuget/client_test.go | 6 +++--- cmd/package_managers_test.go | 6 +++--- 8 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index f046e3f7..3cfc8760 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -51,16 +51,19 @@ linters: - ineffassign - lll - makezero + - mirror - misspell - nakedret - nestif - predeclared - staticcheck - stylecheck + - tenv - thelper - typecheck - unconvert - unused + - usestdlibvars - whitespace - wrapcheck presets: diff --git a/checks/webhook_test.go b/checks/webhook_test.go index 205ff6af..05c35f8e 100644 --- a/checks/webhook_test.go +++ b/checks/webhook_test.go @@ -16,7 +16,6 @@ package checks import ( "context" - "os" "testing" "github.com/golang/mock/gomock" @@ -27,8 +26,8 @@ import ( scut "github.com/ossf/scorecard/v4/utests" ) +//nolint:tparallel,paralleltest // since t.Setenv is used func TestWebhooks(t *testing.T) { - t.Parallel() tests := []struct { expected checker.CheckResult uri string @@ -96,9 +95,7 @@ func TestWebhooks(t *testing.T) { for _, tt := range tests { tt := tt // Re-initializing variable so it is not changed while executing the closure below t.Run(tt.name, func(t *testing.T) { - t.Parallel() - - os.Setenv("SCORECARD_EXPERIMENTAL", "true") + t.Setenv("SCORECARD_EXPERIMENTAL", "true") ctrl := gomock.NewController(t) mockRepo := mockrepo.NewMockRepoClient(ctrl) diff --git a/clients/cii_http_client.go b/clients/cii_http_client.go index 4de75327..0e3de359 100644 --- a/clients/cii_http_client.go +++ b/clients/cii_http_client.go @@ -50,7 +50,7 @@ func (transport *expBackoffTransport) RoundTrip(req *http.Request) (*http.Respon func (client *httpClientCIIBestPractices) GetBadgeLevel(ctx context.Context, uri string) (BadgeLevel, error) { repoURI := fmt.Sprintf("https://%s", uri) url := fmt.Sprintf("https://www.bestpractices.dev/projects.json?url=%s", repoURI) - req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return Unknown, fmt.Errorf("error during http.NewRequestWithContext: %w", err) } diff --git a/clients/gitlabrepo/branches.go b/clients/gitlabrepo/branches.go index d3a2d7a0..33f43112 100644 --- a/clients/gitlabrepo/branches.go +++ b/clients/gitlabrepo/branches.go @@ -16,6 +16,7 @@ package gitlabrepo import ( "fmt" + "net/http" "strings" "sync" @@ -84,10 +85,10 @@ func (handler *branchesHandler) setup() error { if branch.Protected { protectedBranch, resp, err := handler.getProtectedBranch( handler.repourl.projectID, branch.Name) - if err != nil && resp.StatusCode != 403 { + if err != nil && resp.StatusCode != http.StatusForbidden { handler.errSetup = fmt.Errorf("request for protected branch failed with error %w", err) return - } else if resp.StatusCode == 403 { + } else if resp.StatusCode == http.StatusForbidden { handler.errSetup = fmt.Errorf("incorrect permissions to fully check branch protection %w", err) return } diff --git a/clients/gitlabrepo/branches_test.go b/clients/gitlabrepo/branches_test.go index 5fbf9c73..447bbf9b 100644 --- a/clients/gitlabrepo/branches_test.go +++ b/clients/gitlabrepo/branches_test.go @@ -52,7 +52,7 @@ func TestGetBranches(t *testing.T) { }, returnStatus: &gitlab.Response{ Response: &http.Response{ - StatusCode: 200, + StatusCode: http.StatusOK, }, }, branchReturn: &gitlab.ProtectedBranch{}, @@ -69,7 +69,7 @@ func TestGetBranches(t *testing.T) { }, returnStatus: &gitlab.Response{ Response: &http.Response{ - StatusCode: 200, + StatusCode: http.StatusOK, }, }, }, diff --git a/clients/gitlabrepo/issues.go b/clients/gitlabrepo/issues.go index 83844f26..5447c7de 100644 --- a/clients/gitlabrepo/issues.go +++ b/clients/gitlabrepo/issues.go @@ -16,6 +16,7 @@ package gitlabrepo import ( "fmt" + "net/http" "sync" "github.com/xanzy/go-gitlab" @@ -53,7 +54,7 @@ func (handler *issuesHandler) setup() error { if err != nil && resp.StatusCode != 401 { handler.errSetup = fmt.Errorf("unable to find access tokens associated with the project id: %w", err) return - } else if resp.StatusCode == 401 { + } else if resp.StatusCode == http.StatusUnauthorized { handler.errSetup = fmt.Errorf("insufficient permissions to check issue author associations %w", err) return } diff --git a/cmd/internal/nuget/client_test.go b/cmd/internal/nuget/client_test.go index bb5f8c2c..af48233c 100644 --- a/cmd/internal/nuget/client_test.go +++ b/cmd/internal/nuget/client_test.go @@ -608,7 +608,7 @@ func testResult(wantErr bool, responseFileName string) (*http.Response, error) { } if wantErr && responseFileName == "text" { return &http.Response{ - StatusCode: 200, + StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString("text")), }, nil } @@ -617,7 +617,7 @@ func testResult(wantErr bool, responseFileName string) (*http.Response, error) { return nil, fmt.Errorf("%w", err) } return &http.Response{ - StatusCode: 200, - Body: io.NopCloser(bytes.NewBufferString(string(content))), + StatusCode: http.StatusOK, + Body: io.NopCloser(bytes.NewBuffer(content)), }, nil } diff --git a/cmd/package_managers_test.go b/cmd/package_managers_test.go index edb64630..f2c84825 100644 --- a/cmd/package_managers_test.go +++ b/cmd/package_managers_test.go @@ -146,7 +146,7 @@ func Test_fetchGitRepositoryFromNPM(t *testing.T) { } return &http.Response{ - StatusCode: 200, + StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString(tt.args.result)), }, nil }).AnyTimes() @@ -456,7 +456,7 @@ func Test_fetchGitRepositoryFromPYPI(t *testing.T) { } return &http.Response{ - StatusCode: 200, + StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString(tt.args.result)), }, nil }).AnyTimes() @@ -725,7 +725,7 @@ func Test_fetchGitRepositoryFromRubyGems(t *testing.T) { } return &http.Response{ - StatusCode: 200, + StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString(tt.args.result)), }, nil }).AnyTimes()