From 9b4caace93b8e784833cefb2127d9cc80f385c50 Mon Sep 17 00:00:00 2001 From: Renato Saksanni <147796347+renatosaksanni@users.noreply.github.com> Date: Mon, 8 Jul 2024 21:57:08 +0700 Subject: [PATCH] Add golangci-lint configuration and fix linting issues (#97) Add golangci-lint using official golangci action and fix linting issues --- .github/workflows/main.yml | 14 ++++++++++---- .golangci.yml | 37 +++++++++++++++++++++++++++++++++++++ bot_test.go | 6 +++++- build_request_form_test.go | 6 ++++-- raw_request.go | 7 ++++++- 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 .golangci.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9770d77..890e334 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,14 +16,20 @@ jobs: go-version: 1.18 - name: Checkout code - uses: actions/checkout@master + uses: actions/checkout@v2 - - name: run tests + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.49.0 + args: --config .golangci.yml + + - name: Run tests run: | - go test -coverprofile=coverage.txt -covermode=atomic ./... + go test -coverprofile=coverage.txt -covermode=atomic ./... - name: Publish coverage uses: codecov/codecov-action@v1.0.6 with: - token: ${{secrets.CODECOV_TOKEN}} + token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.txt diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..5a47bf5 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,37 @@ +linters: + enable: + - govet + - errcheck + - staticcheck + - unused + - gofmt + - goimports + - gocritic + +linters-settings: + govet: + check-shadowing: true + gocritic: + check: + - range-loop-ref + - ifElseChain + - typeSwitchVar + - wrapperFunc + +run: + timeout: 5m + tests: true + skip-dirs: + - vendor + +issues: + max-issues-per-linter: 0 + max-same-issues: 0 + exclude-use-default: false + include: + - EXC0001 + +exclude-rules: + - path: _test\.go + linters: + - errcheck diff --git a/bot_test.go b/bot_test.go index 86ff03a..0142292 100644 --- a/bot_test.go +++ b/bot_test.go @@ -46,7 +46,11 @@ func (s *serverMock) handler(rw http.ResponseWriter, req *http.Request) { if errReadBody != nil { panic(errReadBody) } - defer req.Body.Close() + defer func() { + if err := req.Body.Close(); err != nil { + panic(err) + } + }() hook, okHook := s.hooks[req.URL.String()] if okHook { diff --git a/build_request_form_test.go b/build_request_form_test.go index 2f0803d..8b5764b 100644 --- a/build_request_form_test.go +++ b/build_request_form_test.go @@ -11,7 +11,7 @@ import ( func assertFormData(t *testing.T, data, expect string) { if !strings.Contains(expect, "\r\n") { - expect = strings.Replace(expect, "\n", "\r\n", -1) + expect = strings.ReplaceAll(expect, "\n", "\r\n") } if data != expect { @@ -60,7 +60,9 @@ func Test_buildRequestForm(t *testing.T) { t.Error(errBuild) return } - form.Close() + if err := form.Close(); err != nil { + t.Errorf("failed to close form: %v", err) + } expect := `--XXX Content-Disposition: form-data; name="string" diff --git a/raw_request.go b/raw_request.go index 6f30d60..edb401c 100644 --- a/raw_request.go +++ b/raw_request.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "log" "mime/multipart" "net/http" "reflect" @@ -67,7 +68,11 @@ func (b *Bot) rawRequest(ctx context.Context, method string, params any, dest an if errDo != nil { return fmt.Errorf("error do request for method %s, %w", method, errDo) } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + log.Printf("failed to close response body: %v", err) + } + }() body, errReadBody := io.ReadAll(resp.Body) if errReadBody != nil {