Skip to content

Commit

Permalink
Add golangci-lint configuration and fix linting issues (#97)
Browse files Browse the repository at this point in the history
Add golangci-lint using official golangci action and fix linting issues
  • Loading branch information
renatosaksanni authored Jul 8, 2024
1 parent 7f34de5 commit 9b4caac
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
token: ${{secrets.CODECOV_TOKEN}}
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.txt
37 changes: 37 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions build_request_form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion raw_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"reflect"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9b4caac

Please sign in to comment.