Skip to content

Commit

Permalink
chore(lint): add golang lint config
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jan 2, 2022
1 parent 0b63c38 commit 7756450
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
43 changes: 43 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
linters:
enable-all: false
disable-all: true
fast: false
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- exportloopref
- exhaustive
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- gofumpt

run:
timeout: 3m
30 changes: 16 additions & 14 deletions static_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package static

import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -13,8 +14,9 @@ import (
"github.com/stretchr/testify/assert"
)

// nolint:unparam
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
req, _ := http.NewRequestWithContext(context.Background(), method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
Expand All @@ -36,33 +38,33 @@ func TestEmptyDirectory(t *testing.T) {
router := gin.New()
router.Use(ServeRoot("/", dir))
router.GET("/", func(c *gin.Context) {
c.String(200, "index")
c.String(http.StatusOK, "index")
})
router.GET("/a", func(c *gin.Context) {
c.String(200, "a")
c.String(http.StatusOK, "a")
})
router.GET("/"+filename, func(c *gin.Context) {
c.String(200, "this is not printed")
c.String(http.StatusOK, "this is not printed")
})
w := performRequest(router, "GET", "/")
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Code, http.StatusOK)
assert.Equal(t, w.Body.String(), "index")

w = performRequest(router, "GET", "/"+filename)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Code, http.StatusOK)
assert.Equal(t, w.Body.String(), "Gin Web Framework")

w = performRequest(router, "GET", "/"+filename+"a")
assert.Equal(t, w.Code, 404)

w = performRequest(router, "GET", "/a")
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Code, http.StatusOK)
assert.Equal(t, w.Body.String(), "a")

router2 := gin.New()
router2.Use(ServeRoot("/static", dir))
router2.GET("/"+filename, func(c *gin.Context) {
c.String(200, "this is printed")
c.String(http.StatusOK, "this is printed")
})

w = performRequest(router2, "GET", "/")
Expand All @@ -71,18 +73,18 @@ func TestEmptyDirectory(t *testing.T) {
w = performRequest(router2, "GET", "/static")
assert.Equal(t, w.Code, 404)
router2.GET("/static", func(c *gin.Context) {
c.String(200, "index")
c.String(http.StatusOK, "index")
})

w = performRequest(router2, "GET", "/static")
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Code, http.StatusOK)

w = performRequest(router2, "GET", "/"+filename)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Code, http.StatusOK)
assert.Equal(t, w.Body.String(), "this is printed")

w = performRequest(router2, "GET", "/static/"+filename)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Code, http.StatusOK)
assert.Equal(t, w.Body.String(), "Gin Web Framework")
}

Expand All @@ -106,7 +108,7 @@ func TestIndex(t *testing.T) {
assert.Equal(t, w.Code, 301)

w = performRequest(router, "GET", "/")
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Code, http.StatusOK)
assert.Equal(t, w.Body.String(), "index")
}

Expand All @@ -126,7 +128,7 @@ func TestListIndex(t *testing.T) {
router.Use(Serve("/", LocalFile(dir, true)))

w := performRequest(router, "GET", "/"+filename)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Code, http.StatusOK)
assert.Equal(t, w.Body.String(), "Gin Web Framework")

w = performRequest(router, "GET", "/")
Expand Down

0 comments on commit 7756450

Please sign in to comment.