From e042b546ed64fc4971dcc6d5bea33f3f261a88f2 Mon Sep 17 00:00:00 2001 From: "Eugene R." Date: Sat, 28 Sep 2024 17:49:35 +0300 Subject: [PATCH] ci: update golangci-lint configuration (#143) --- .github/workflows/golangci-lint.yml | 20 +++++++-- .golangci.yml | 11 ++++- job/job_test.go | 1 + logger/logger_test.go | 66 +++++++++++++++-------------- 4 files changed, 62 insertions(+), 36 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index a01d685..d9e7a86 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -6,6 +6,9 @@ on: - master pull_request: +permissions: + contents: read + jobs: golangci: name: lint @@ -13,6 +16,17 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 - - - name: golangci-lint - uses: golangci/golangci-lint-action@v4 + + - name: Get go version from go.mod + run: | + echo "GO_VERSION=$(grep '^go ' go.mod | cut -d " " -f 2)" >> $GITHUB_ENV + + - name: Setup-go + uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.61.0 diff --git a/.golangci.yml b/.golangci.yml index 013396e..0a4e83f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,5 +1,5 @@ run: - timeout: 1m + timeout: 2m linters: disable-all: true @@ -8,7 +8,6 @@ linters: - errcheck - errname - errorlint - - exportloopref - funlen - gci - goconst @@ -16,24 +15,32 @@ linters: - gocyclo - gofmt - goimports + - gosec - gosimple - govet - ineffassign - lll - misspell + - nolintlint - prealloc - revive - staticcheck - stylecheck + - thelper + - tparallel - typecheck - unconvert - unparam - unused + - whitespace linters-settings: errcheck: exclude-functions: - (*log.Logger).Output + thelper: + test: + begin: false issues: exclude-rules: diff --git a/job/job_test.go b/job/job_test.go index 1570900..eb502d4 100644 --- a/job/job_test.go +++ b/job/job_test.go @@ -17,6 +17,7 @@ import ( "github.com/reugn/go-quartz/job" ) +//nolint:gosec func TestMultipleExecution(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() diff --git a/logger/logger_test.go b/logger/logger_test.go index 58b7e76..cb3b725 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -17,29 +17,29 @@ func TestSimpleLogger(t *testing.T) { logger.SetDefault(logger.NewSimpleLogger(stdLogger, logger.LevelInfo)) logger.Trace("Trace") - assertEmpty(&b, t) + assertEmpty(t, &b) logger.Tracef("Trace%s", "f") - assertEmpty(&b, t) + assertEmpty(t, &b) logger.Debug("Debug") - assertEmpty(&b, t) + assertEmpty(t, &b) logger.Debugf("Debug%s", "f") - assertEmpty(&b, t) + assertEmpty(t, &b) logger.Info("Info") - assertNotEmpty(&b, t) + assertNotEmpty(t, &b) logger.Infof("Info%s", "f") - assertNotEmpty(&b, t) + assertNotEmpty(t, &b) logger.Warn("Warn") - assertNotEmpty(&b, t) + assertNotEmpty(t, &b) logger.Warnf("Warn%s", "f") - assertNotEmpty(&b, t) + assertNotEmpty(t, &b) logger.Error("Error") - assertNotEmpty(&b, t) + assertNotEmpty(t, &b) logger.Errorf("Error%s", "f") - assertNotEmpty(&b, t) + assertNotEmpty(t, &b) } func TestLoggerOff(t *testing.T) { @@ -51,9 +51,9 @@ func TestLoggerOff(t *testing.T) { t.Fatal("logger.LevelError is enabled") } logger.Error("Error") - assertEmpty(&b, t) + assertEmpty(t, &b) logger.Errorf("Error%s", "f") - assertEmpty(&b, t) + assertEmpty(t, &b) } func TestLoggerRace(t *testing.T) { @@ -103,57 +103,61 @@ func TestLogFormat(t *testing.T) { empty := struct{}{} logr.Trace("Trace") - assertNotEmpty(&b, t) + assertNotEmpty(t, &b) logr.Tracef("Tracef: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logger.Tracef("Tracef: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logr.Debug("Debug") - assertNotEmpty(&b, t) + assertNotEmpty(t, &b) logr.Debugf("Debugf: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logger.Debugf("Debugf: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logr.Infof("Infof: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logger.Infof("Infof: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logr.Warnf("Warnf: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logger.Warnf("Warnf: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logr.Errorf("Errorf: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) logger.Errorf("Errorf: %s, %d, %v, %v", "a", 1, true, empty) - checkLogFormat(&b, t) + checkLogFormat(t, &b) } -func assertEmpty(r io.Reader, t *testing.T) { - logMsg := readAll(r, t) +func assertEmpty(t *testing.T, r io.Reader) { + t.Helper() + logMsg := readAll(t, r) if logMsg != "" { t.Fatalf("log msg is not empty: %s", logMsg) } } -func assertNotEmpty(r io.Reader, t *testing.T) { - logMsg := readAll(r, t) +func assertNotEmpty(t *testing.T, r io.Reader) { + t.Helper() + logMsg := readAll(t, r) if logMsg == "" { t.Fatal("log msg is empty") } } -func checkLogFormat(r io.Reader, t *testing.T) { - logMsg := readAll(r, t) +func checkLogFormat(t *testing.T, r io.Reader) { + t.Helper() + logMsg := readAll(t, r) if !strings.Contains(logMsg, "a, 1, true, {}") { t.Fatalf("invalid log format: %s", logMsg) } } -func readAll(r io.Reader, t *testing.T) string { +func readAll(t *testing.T, r io.Reader) string { + t.Helper() bytes, err := io.ReadAll(r) if err != nil { t.Fatal(err)