Skip to content

Commit

Permalink
refactor: update golangci-lint to v1.59 (#272)
Browse files Browse the repository at this point in the history
* ci: update `golangci-lint` to v1.56

* refactor: rename variable

* chore: disable `perfsprint` for now

* refactor: don't name unused parameters

* ci: update `golangci-lint` to v1.57

* ci: update `golangci-lint` to v1.58

* chore: replace `gomnd` with `mnd`

* refactor: set a more "secure" permission for test-generated config file

* refactor: use a static error

* ci: update `golangci-lint` to v1.59

* chore: remove deprecated linters
  • Loading branch information
G-Rath authored Oct 28, 2024
1 parent 00bca66 commit 4ec0a91
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
- name: Run golangci-lint
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1
with:
version: v1.55
version: v1.59
go-fmt:
permissions:
contents: read # to fetch code (actions/checkout)
Expand Down
8 changes: 2 additions & 6 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ linters:
disable:
- tagliatelle # we're parsing data from external sources
- varnamelen # maybe later
- exhaustivestruct # overkill
- exhaustruct # overkill
- forcetypeassert # too hard
- interfacer # deprecated
- golint # deprecated
- scopelint # deprecated
- maligned # deprecated
- lll # line length is hard
- godox # to-do comments are fine
- godot # comments are fine without full stops
- gomnd # not every number is magic
- mnd # not every number is magic
- wsl # disagree with, for now
- ireturn # disagree with, sort of
- nonamedreturns # they have their uses
- perfsprint # enable in dedicated PR
presets:
- bugs
- comment
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test-with-coverage:
lint: lint-with-golangci-lint lint-with-go-fmt

lint-with-golangci-lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2 run ./... --max-same-issues 0
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 run ./... --max-same-issues 0

lint-with-go-fmt:
gofmt -s -d */**.go
Expand Down
8 changes: 5 additions & 3 deletions internal/reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/g-rath/osv-detector/pkg/database"
)

var errOhNoes = fmt.Errorf("oh noes")

type TestResult struct {
Value string `json:"value"`
ErrorWhenMarshalling bool `json:"-"`
Expand All @@ -24,7 +26,7 @@ func (r TestResult) MarshalJSON() ([]byte, error) {
type rawTestResult TestResult

if r.ErrorWhenMarshalling {
return nil, fmt.Errorf("oh noes, an error")
return nil, errOhNoes
}

out, err := json.Marshal((rawTestResult)(r))
Expand Down Expand Up @@ -205,7 +207,7 @@ func TestReporter_PrintDatabaseLoadErr(t *testing.T) {
name: "",
args: args{
outputAsJSON: false,
err: fmt.Errorf("oh noes"),
err: errOhNoes,
},
wantedStdout: "",
wantedStderr: " failed: oh noes\n",
Expand All @@ -214,7 +216,7 @@ func TestReporter_PrintDatabaseLoadErr(t *testing.T) {
name: "",
args: args{
outputAsJSON: true,
err: fmt.Errorf("oh noes"),
err: errOhNoes,
},
wantedStdout: "",
wantedStderr: " failed: oh noes\n",
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ func TestRun_Ignores(t *testing.T) {
func setupConfigForUpdating(t *testing.T, path string, initial string, updated string) func() {
t.Helper()

err := os.WriteFile(path, []byte(initial), os.ModePerm)
err := os.WriteFile(path, []byte(initial), 0600)

if err != nil {
t.Fatalf("could not create test file: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/api-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ func (db APIDB) Check(pkgs []internal.PackageDetails) ([]Vulnerabilities, error)
return nil, err
}

for _, withIDS := range results {
vulns := make(Vulnerabilities, 0, len(withIDS))
for _, withIDs := range results {
vulns := make(Vulnerabilities, 0, len(withIDs))

for _, withID := range withIDS {
for _, withID := range withIDs {
vulns = append(vulns, OSV{ID: withID.ID})
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/database/api-check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func expectVulnerability(t *testing.T, vuln database.OSV, id string, summary str
func TestAPIDB_Check_NoPackages(t *testing.T) {
t.Parallel()

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ts := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
t.Errorf("an API request was made even though there are no packages to check")
}))
t.Cleanup(ts.Close)
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestAPIDB_Check_FetchSuccessful(t *testing.T) {
_, _ = w.Write(jsonData)
})

mux.HandleFunc("/vulns/GHSA-1234", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/vulns/GHSA-1234", func(w http.ResponseWriter, _ *http.Request) {
jsonData, err := json.Marshal(database.OSV{ID: "GHSA-1234", Summary: "my vulnerability"})

if err != nil {
Expand Down Expand Up @@ -330,12 +330,12 @@ func TestAPIDB_Check_FetchFails(t *testing.T) {
})

// this response is not a 200 OK
mux.HandleFunc("/vulns/GHSA-1234", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/vulns/GHSA-1234", func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "oh noes!", http.StatusForbidden)
})

// this response is not valid json
mux.HandleFunc("/vulns/GHSA-5678", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/vulns/GHSA-5678", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("<html></html>"))
})

Expand Down Expand Up @@ -386,7 +386,7 @@ func TestAPIDB_Check_FetchMixed(t *testing.T) {
_, _ = w.Write(jsonData)
})

mux.HandleFunc("/vulns/GHSA-1234", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/vulns/GHSA-1234", func(w http.ResponseWriter, _ *http.Request) {
jsonData, err := json.Marshal(database.OSV{ID: "GHSA-1234", Summary: "my vulnerability"})

if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions pkg/database/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func zipOSVs(t *testing.T, osvs map[string]database.OSV) []byte {
func TestNewZippedDB_Offline_WithoutCache(t *testing.T) {
t.Parallel()

ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(_ http.ResponseWriter, _ *http.Request) {
t.Errorf("a server request was made when running offline")
})

Expand All @@ -159,7 +159,7 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {
withDefaultAffected("GHSA-5"),
}

ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(_ http.ResponseWriter, _ *http.Request) {
t.Errorf("a server request was made when running offline")
})

Expand Down Expand Up @@ -192,7 +192,7 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {
func TestNewZippedDB_BadZip(t *testing.T) {
t.Parallel()

ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("this is not a zip"))
})

Expand Down Expand Up @@ -224,7 +224,7 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
withDefaultAffected("GHSA-5"),
}

ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{
"GHSA-1.json": withDefaultAffected("GHSA-1"),
"GHSA-2.json": withDefaultAffected("GHSA-2"),
Expand All @@ -246,7 +246,7 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
func TestNewZippedDB_Online_WithoutCache_NotFound(t *testing.T) {
t.Parallel()

ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{}))
})
Expand Down Expand Up @@ -362,7 +362,7 @@ func TestNewZippedDB_Online_WithBadCache(t *testing.T) {
withDefaultAffected("GHSA-3"),
}

ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{
"GHSA-1.json": withDefaultAffected("GHSA-1"),
"GHSA-2.json": withDefaultAffected("GHSA-2"),
Expand All @@ -386,7 +386,7 @@ func TestNewZippedDB_FileChecks(t *testing.T) {

osvs := []database.OSV{withDefaultAffected("GHSA-1234"), withDefaultAffected("GHSA-4321")}

ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{
"file.json": withDefaultAffected("GHSA-1234"),
// only files with .json suffix should be loaded
Expand All @@ -410,7 +410,7 @@ func TestNewZippedDB_WorkingDirectory(t *testing.T) {

osvs := []database.OSV{withDefaultAffected("GHSA-1234"), withDefaultAffected("GHSA-5678")}

ts := createZipServer(t, func(w http.ResponseWriter, r *http.Request) {
ts := createZipServer(t, func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(zipOSVs(t, map[string]database.OSV{
"reviewed/file.json": withDefaultAffected("GHSA-1234"),
"reviewed/nested/file.json": withDefaultAffected("GHSA-5678"),
Expand Down

0 comments on commit 4ec0a91

Please sign in to comment.