From 1e1e4fc867b1729f600cf0e3ce096b815503e969 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 12 Jun 2017 21:50:42 -0500 Subject: [PATCH] Add Makefile to check the following thing (#947) * Add Makefile to check the following thing. * vet check * fmt check * embedmd check * misspell check Signed-off-by: Bo-Yi Wu * remove unused variable. Signed-off-by: Bo-Yi Wu --- .travis.yml | 11 ++++---- Makefile | 61 +++++++++++++++++++++++++++++++++++++++++ binding/form_mapping.go | 2 +- context.go | 2 +- context_test.go | 7 ++--- errors.go | 2 +- gin_integration_test.go | 2 +- 7 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 Makefile diff --git a/.travis.yml b/.travis.yml index 644a178a..6532a334 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,13 +10,14 @@ git: depth: 3 install: - - go get -v github.com/kardianos/govendor - - govendor sync - - go get -u github.com/campoy/embedmd + - make install script: - - embedmd -d README.md - - go test -v -covermode=count -coverprofile=coverage.out + - make vet + - make fmt-check + - make embedmd + - make misspell-check + - make test after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..9ba475a4 --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +GOFMT ?= gofmt "-s" +PACKAGES ?= $(shell go list ./... | grep -v /vendor/) +GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*") + +all: build + +install: deps + govendor sync + +.PHONY: test +test: + go test -v -covermode=count -coverprofile=coverage.out + +.PHONY: fmt +fmt: + $(GOFMT) -w $(GOFILES) + +.PHONY: fmt-check +fmt-check: + # get all go files and run go fmt on them + @diff=$$($(GOFMT) -d $(GOFILES)); \ + if [ -n "$$diff" ]; then \ + echo "Please run 'make fmt' and commit the result:"; \ + echo "$${diff}"; \ + exit 1; \ + fi; + +vet: + go vet $(PACKAGES) + +deps: + @hash govendor > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/kardianos/govendor; \ + fi + @hash embedmd > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/campoy/embedmd; \ + fi + +embedmd: + embedmd -d *.md + +.PHONY: lint +lint: + @hash golint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/golang/lint/golint; \ + fi + for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done; + +.PHONY: misspell-check +misspell-check: + @hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/client9/misspell/cmd/misspell; \ + fi + misspell -error $(GOFILES) + +.PHONY: misspell +misspell: + @hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/client9/misspell/cmd/misspell; \ + fi + misspell -w $(GOFILES) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 1af8165e..34f12678 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -152,7 +152,7 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val if timeFormat == "" { return errors.New("Blank time format") } - + if val == "" { value.Set(reflect.ValueOf(time.Time{})) return nil diff --git a/context.go b/context.go index 971675d8..c5ff7937 100644 --- a/context.go +++ b/context.go @@ -87,7 +87,7 @@ func (c *Context) HandlerName() string { // Handler returns the main handler. func (c *Context) Handler() HandlerFunc { - return c.handlers.Last() + return c.handlers.Last() } /************************************/ diff --git a/context_test.go b/context_test.go index 66a55cd2..e8cea05d 100644 --- a/context_test.go +++ b/context_test.go @@ -283,13 +283,12 @@ var handlerTest HandlerFunc = func(c *Context) { } func TestContextHandler(t *testing.T) { - c, _ := CreateTestContext(httptest.NewRecorder()) - c.handlers = HandlersChain{func(c *Context) {}, handlerTest} + c, _ := CreateTestContext(httptest.NewRecorder()) + c.handlers = HandlersChain{func(c *Context) {}, handlerTest} - assert.Equal(t, reflect.ValueOf(handlerTest).Pointer(), reflect.ValueOf(c.Handler()).Pointer()) + assert.Equal(t, reflect.ValueOf(handlerTest).Pointer(), reflect.ValueOf(c.Handler()).Pointer()) } - func TestContextQuery(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("GET", "http://example.com/?foo=bar&page=10&id=", nil) diff --git a/errors.go b/errors.go index 694b428a..896af6fc 100644 --- a/errors.go +++ b/errors.go @@ -80,7 +80,7 @@ func (msg *Error) IsType(flags ErrorType) bool { return (msg.Type & flags) > 0 } -// Returns a readonly copy filterd the byte. +// Returns a readonly copy filtered the byte. // ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic func (a errorMsgs) ByType(typ ErrorType) errorMsgs { if len(a) == 0 { diff --git a/gin_integration_test.go b/gin_integration_test.go index 0a6351d7..f45dd6c1 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -20,8 +20,8 @@ import ( func testRequest(t *testing.T, url string) { resp, err := http.Get(url) - defer resp.Body.Close() assert.NoError(t, err) + defer resp.Body.Close() body, ioerr := ioutil.ReadAll(resp.Body) assert.NoError(t, ioerr)