Skip to content

Commit 31bbb10

Browse files
vkdthinkerou
authored andcommitted
Make silent debug info on tests (gin-gonic#1765)
* make silent log on tests * fix coverage: check end-of-line at the end of debug msg
1 parent a768f06 commit 31bbb10

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

debug_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestDebugPrint(t *testing.T) {
3939
SetMode(TestMode)
4040
debugPrint("DEBUG this!")
4141
SetMode(DebugMode)
42-
debugPrint("these are %d %s\n", 2, "error messages")
42+
debugPrint("these are %d %s", 2, "error messages")
4343
SetMode(TestMode)
4444
})
4545
assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)

deprecated_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ func TestBindWith(t *testing.T) {
2424
Foo string `form:"foo"`
2525
Bar string `form:"bar"`
2626
}
27-
assert.NoError(t, c.BindWith(&obj, binding.Form))
27+
captureOutput(t, func() {
28+
assert.NoError(t, c.BindWith(&obj, binding.Form))
29+
})
2830
assert.Equal(t, "foo", obj.Bar)
2931
assert.Equal(t, "bar", obj.Foo)
3032
assert.Equal(t, 0, w.Body.Len())

gin_test.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,23 @@ func formatAsDate(t time.Time) string {
2727

2828
func setupHTMLFiles(t *testing.T, mode string, tls bool, loadMethod func(*Engine)) *httptest.Server {
2929
SetMode(mode)
30-
router := New()
31-
router.Delims("{[{", "}]}")
32-
router.SetFuncMap(template.FuncMap{
33-
"formatAsDate": formatAsDate,
34-
})
35-
loadMethod(router)
36-
router.GET("/test", func(c *Context) {
37-
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
38-
})
39-
router.GET("/raw", func(c *Context) {
40-
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
41-
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
30+
defer SetMode(TestMode)
31+
32+
var router *Engine
33+
captureOutput(t, func() {
34+
router = New()
35+
router.Delims("{[{", "}]}")
36+
router.SetFuncMap(template.FuncMap{
37+
"formatAsDate": formatAsDate,
38+
})
39+
loadMethod(router)
40+
router.GET("/test", func(c *Context) {
41+
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
42+
})
43+
router.GET("/raw", func(c *Context) {
44+
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
45+
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
46+
})
4247
})
4348
})
4449

githubapi_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ var githubAPI = []route{
287287

288288
func TestShouldBindUri(t *testing.T) {
289289
DefaultWriter = os.Stdout
290-
router := Default()
290+
router := New()
291291

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

310310
func TestBindUri(t *testing.T) {
311311
DefaultWriter = os.Stdout
312-
router := Default()
312+
router := New()
313313

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

332332
func TestBindUriError(t *testing.T) {
333333
DefaultWriter = os.Stdout
334-
router := Default()
334+
router := New()
335335

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

362362
func TestGithubAPI(t *testing.T) {
363363
DefaultWriter = os.Stdout
364-
router := Default()
364+
router := New()
365365
githubConfigRouter(router)
366366

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

437437
func BenchmarkParallelGithubDefault(b *testing.B) {
438438
DefaultWriter = os.Stdout
439-
router := Default()
439+
router := New()
440440
githubConfigRouter(router)
441441

442442
req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil)

recovery_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestPanicInHandler(t *testing.T) {
4343
assert.Equal(t, http.StatusInternalServerError, w.Code)
4444
assert.Contains(t, buffer.String(), "GET /recovery")
4545

46+
SetMode(TestMode)
4647
}
4748

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

0 commit comments

Comments
 (0)