Skip to content

Commit

Permalink
Make silent debug info on tests (gin-gonic#1765)
Browse files Browse the repository at this point in the history
* make silent log on tests

* fix coverage: check end-of-line at the end of debug msg
  • Loading branch information
vkd authored and thinkerou committed Feb 18, 2019
1 parent a768f06 commit 31bbb10
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDebugPrint(t *testing.T) {
SetMode(TestMode)
debugPrint("DEBUG this!")
SetMode(DebugMode)
debugPrint("these are %d %s\n", 2, "error messages")
debugPrint("these are %d %s", 2, "error messages")
SetMode(TestMode)
})
assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)
Expand Down
4 changes: 3 additions & 1 deletion deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func TestBindWith(t *testing.T) {
Foo string `form:"foo"`
Bar string `form:"bar"`
}
assert.NoError(t, c.BindWith(&obj, binding.Form))
captureOutput(t, func() {
assert.NoError(t, c.BindWith(&obj, binding.Form))
})
assert.Equal(t, "foo", obj.Bar)
assert.Equal(t, "bar", obj.Foo)
assert.Equal(t, 0, w.Body.Len())
Expand Down
29 changes: 17 additions & 12 deletions gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ func formatAsDate(t time.Time) string {

func setupHTMLFiles(t *testing.T, mode string, tls bool, loadMethod func(*Engine)) *httptest.Server {
SetMode(mode)
router := New()
router.Delims("{[{", "}]}")
router.SetFuncMap(template.FuncMap{
"formatAsDate": formatAsDate,
})
loadMethod(router)
router.GET("/test", func(c *Context) {
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
})
router.GET("/raw", func(c *Context) {
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
defer SetMode(TestMode)

var router *Engine
captureOutput(t, func() {
router = New()
router.Delims("{[{", "}]}")
router.SetFuncMap(template.FuncMap{
"formatAsDate": formatAsDate,
})
loadMethod(router)
router.GET("/test", func(c *Context) {
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
})
router.GET("/raw", func(c *Context) {
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
})
})
})

Expand Down
10 changes: 5 additions & 5 deletions githubapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ var githubAPI = []route{

func TestShouldBindUri(t *testing.T) {
DefaultWriter = os.Stdout
router := Default()
router := New()

type Person struct {
Name string `uri:"name" binding:"required"`
Expand All @@ -309,7 +309,7 @@ func TestShouldBindUri(t *testing.T) {

func TestBindUri(t *testing.T) {
DefaultWriter = os.Stdout
router := Default()
router := New()

type Person struct {
Name string `uri:"name" binding:"required"`
Expand All @@ -331,7 +331,7 @@ func TestBindUri(t *testing.T) {

func TestBindUriError(t *testing.T) {
DefaultWriter = os.Stdout
router := Default()
router := New()

type Member struct {
Number string `uri:"num" binding:"required,uuid"`
Expand Down Expand Up @@ -361,7 +361,7 @@ func githubConfigRouter(router *Engine) {

func TestGithubAPI(t *testing.T) {
DefaultWriter = os.Stdout
router := Default()
router := New()
githubConfigRouter(router)

for _, route := range githubAPI {
Expand Down Expand Up @@ -436,7 +436,7 @@ func BenchmarkParallelGithub(b *testing.B) {

func BenchmarkParallelGithubDefault(b *testing.B) {
DefaultWriter = os.Stdout
router := Default()
router := New()
githubConfigRouter(router)

req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil)
Expand Down
1 change: 1 addition & 0 deletions recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestPanicInHandler(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Contains(t, buffer.String(), "GET /recovery")

SetMode(TestMode)
}

// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
Expand Down

0 comments on commit 31bbb10

Please sign in to comment.