diff --git a/.travis.yml b/.travis.yml index b39eb17..9352cec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: go sudo: required go: - - "1.11" - "1.12" - master diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f06b593 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "go.formatTool": "goimports" +} \ No newline at end of file diff --git a/go.mod b/go.mod index 74f16c0..165b0f1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,9 @@ module github.com/vicanso/cod-recover +go 1.12 + require ( - github.com/vicanso/cod v0.0.7 + github.com/stretchr/testify v1.3.0 + github.com/vicanso/cod v0.1.1 github.com/vicanso/hes v0.1.4 ) diff --git a/go.sum b/go.sum index a0c2e2b..6f6a78b 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/vicanso/cod v0.0.7 h1:CHyIL7zkNOhEv0JosDDyRHCUALovry1P+dLISaANj1w= github.com/vicanso/cod v0.0.7/go.mod h1:tHGTs/JDTcp/OE4VaCutVMpYL69aR1rKUggsHviTuFc= +github.com/vicanso/cod v0.1.1 h1:l4dbZSGhGRVnrxLbArC/60GXNxDo0O/RlY5z4iHSa7I= +github.com/vicanso/cod v0.1.1/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw= github.com/vicanso/hes v0.1.4 h1:n8kG8krvNJF4Sj1PvZOEUzdUsmDSbCcGr8C1nYnoP+o= github.com/vicanso/hes v0.1.4/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo= github.com/vicanso/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM= diff --git a/recover_test.go b/recover_test.go index 29605e2..3605ca6 100644 --- a/recover_test.go +++ b/recover_test.go @@ -1,14 +1,18 @@ package recover import ( + "fmt" "net/http" "net/http/httptest" + "os" "testing" + "github.com/stretchr/testify/assert" "github.com/vicanso/cod" ) func TestRecover(t *testing.T) { + assert := assert.New(t) var ctx *cod.Context d := cod.New() d.Use(New()) @@ -34,15 +38,28 @@ func TestRecover(t *testing.T) { }) d.ServeHTTP(resp, req) - if resp.Code != http.StatusInternalServerError || - resp.Body.String() != "category=cod-recover, message=abc" || - !ctx.Committed || - !catchError { - t.Fatalf("recover fail") - } + assert.Equal(resp.Code, http.StatusInternalServerError) + assert.Equal(resp.Body.String(), "category=cod-recover, message=abc") + assert.True(ctx.Committed) + assert.True(catchError) for _, key := range keys { - if resp.Header().Get(key) != "" { - t.Fatalf("reset response header fail") + assert.Empty(ctx.GetHeader(key), "header should be reseted") + } +} + +// https://stackoverflow.com/questions/50120427/fail-unit-tests-if-coverage-is-below-certain-percentage +func TestMain(m *testing.M) { + // call flag.Parse() here if TestMain uses flags + rc := m.Run() + + // rc 0 means we've passed, + // and CoverMode will be non empty if run with -cover + if rc == 0 && testing.CoverMode() != "" { + c := testing.Coverage() + if c < 0.9 { + fmt.Println("Tests passed but coverage failed at", c) + rc = -1 } } + os.Exit(rc) }