Skip to content
Myr

debug_test.go

This file

Path

debug_test.go

166 lines · 4.9 KiB · Go

Last changed by

chore: support min go version 1.18 (#3511)thinkeroubefore adoption1e1f0b1

History of this file

Take it

The walk stopped after 200 commits: lines older than a550c56 point at it.

chore: update go.mod and remove space from copyright (#3158)thinkeroubefore adoption4b68a5f

line 1why, one step back
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 2–14why, one step back
2// Use of this source code is governed by a MIT style
3// license that can be found in the LICENSE file.45package gin67import (8 "errors"9 "fmt"10 "html/template"11 "io"12 "log"13 "os"14 "runtime"

Replace bytes.Buffer with strings.Builder where appropriate (#3347)hopehookbefore adoptionb2d4185

line 15why, one step back
15 "strings"

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 16–23why, one step back
16 "sync"
17 "testing"1819 "github.com/stretchr/testify/assert"20)2122// TODO23// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {

chore: support min go version 1.18 (#3511)thinkeroubefore adoption1e1f0b1

line 24why, one step back
24// func debugPrint(format string, values ...any) {

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 25–105why, one step back
25
26func TestIsDebugging(t *testing.T) {27 SetMode(DebugMode)28 assert.True(t, IsDebugging())29 SetMode(ReleaseMode)30 assert.False(t, IsDebugging())31 SetMode(TestMode)32 assert.False(t, IsDebugging())33}3435func TestDebugPrint(t *testing.T) {36 re := captureOutput(t, func() {37 SetMode(DebugMode)38 SetMode(ReleaseMode)39 debugPrint("DEBUG this!")40 SetMode(TestMode)41 debugPrint("DEBUG this!")42 SetMode(DebugMode)43 debugPrint("these are %d %s", 2, "error messages")44 SetMode(TestMode)45 })46 assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)47}4849func TestDebugPrintError(t *testing.T) {50 re := captureOutput(t, func() {51 SetMode(DebugMode)52 debugPrintError(nil)53 debugPrintError(errors.New("this is an error"))54 SetMode(TestMode)55 })56 assert.Equal(t, "[GIN-debug] [ERROR] this is an error\n", re)57}5859func TestDebugPrintRoutes(t *testing.T) {60 re := captureOutput(t, func() {61 SetMode(DebugMode)62 debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})63 SetMode(TestMode)64 })65 assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re)66}6768func TestDebugPrintRouteFunc(t *testing.T) {69 DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {70 fmt.Fprintf(DefaultWriter, "[GIN-debug] %-6s %-40s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)71 }72 re := captureOutput(t, func() {73 SetMode(DebugMode)74 debugPrintRoute("GET", "/path/to/route/:param1/:param2", HandlersChain{func(c *Context) {}, handlerNameTest})75 SetMode(TestMode)76 })77 assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param1/:param2 --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re)78}7980func TestDebugPrintLoadTemplate(t *testing.T) {81 re := captureOutput(t, func() {82 SetMode(DebugMode)83 templ := template.Must(template.New("").Delims("{[{", "}]}").ParseGlob("./testdata/template/hello.tmpl"))84 debugPrintLoadTemplate(templ)85 SetMode(TestMode)86 })87 assert.Regexp(t, `^\[GIN-debug\] Loaded HTML Templates \(2\): \n(\t- \n|\t- hello\.tmpl\n){2}\n`, re)88}8990func TestDebugPrintWARNINGSetHTMLTemplate(t *testing.T) {91 re := captureOutput(t, func() {92 SetMode(DebugMode)93 debugPrintWARNINGSetHTMLTemplate()94 SetMode(TestMode)95 })96 assert.Equal(t, "[GIN-debug] [WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called\nat initialization. ie. before any route is registered or the router is listening in a socket:\n\n\trouter := gin.Default()\n\trouter.SetHTMLTemplate(template) // << good place\n\n", re)97}9899func TestDebugPrintWARNINGDefault(t *testing.T) {100 re := captureOutput(t, func() {101 SetMode(DebugMode)102 debugPrintWARNINGDefault()103 SetMode(TestMode)104 })105 m, e := getMinVer(runtime.Version())

Fix the value of ginSupportMinGoVer constant by semantic (#3221)mstmdevbefore adoption088cdd7

line 106why, one step back
106 if e == nil && m < ginSupportMinGoVer {

chore: support min go version 1.18 (#3511)thinkeroubefore adoption1e1f0b1

line 107why, one step back
107 assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.18+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 108–140why, one step back
108 } else {
109 assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)110 }111}112113func TestDebugPrintWARNINGNew(t *testing.T) {114 re := captureOutput(t, func() {115 SetMode(DebugMode)116 debugPrintWARNINGNew()117 SetMode(TestMode)118 })119 assert.Equal(t, "[GIN-debug] [WARNING] Running in \"debug\" mode. Switch to \"release\" mode in production.\n - using env:\texport GIN_MODE=release\n - using code:\tgin.SetMode(gin.ReleaseMode)\n\n", re)120}121122func captureOutput(t *testing.T, f func()) string {123 reader, writer, err := os.Pipe()124 if err != nil {125 panic(err)126 }127 defaultWriter := DefaultWriter128 defaultErrorWriter := DefaultErrorWriter129 defer func() {130 DefaultWriter = defaultWriter131 DefaultErrorWriter = defaultErrorWriter132 log.SetOutput(os.Stderr)133 }()134 DefaultWriter = writer135 DefaultErrorWriter = writer136 log.SetOutput(writer)137 out := make(chan string)138 wg := new(sync.WaitGroup)139 wg.Add(1)140 go func() {

Replace bytes.Buffer with strings.Builder where appropriate (#3347)hopehookbefore adoptionb2d4185

line 141why, one step back
141 var buf strings.Builder

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 142–166why, one step back
142 wg.Done()
143 _, err := io.Copy(&buf, reader)144 assert.NoError(t, err)145 out <- buf.String()146 }()147 wg.Wait()148 f()149 writer.Close()150 return <-out151}152153func TestGetMinVer(t *testing.T) {154 var m uint64155 var e error156 _, e = getMinVer("go1")157 assert.NotNil(t, e)158 m, e = getMinVer("go1.1")159 assert.Equal(t, uint64(1), m)160 assert.Nil(t, e)161 m, e = getMinVer("go1.1.1")162 assert.Nil(t, e)163 assert.Equal(t, uint64(1), m)164 _, e = getMinVer("go1.1.1.1")165 assert.NotNil(t, e)166}