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 ac26d47..644e1c8 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 {