debug_test.go
This file
Path
debug_test.go
180 lines · 5.3 KiB · Go
Last changed by
ci: update Go version support to 1.25+ across CI and docs (#4550)Bo-Yi Wubefore adoptionae3f524
History of this fileTake it
The walk stopped after 200 commits: lines older than cf32d2d point at it.
chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 1–45why, one step back1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "errors"9 "fmt"10 "html/template"11 "io"12 "log"13 "net/http"14 "os"15 "strings"16 "sync"17 "testing"1819 "github.com/stretchr/testify/assert"20 "github.com/stretchr/testify/require"21)2223func TestIsDebugging(t *testing.T) {24 SetMode(DebugMode)25 assert.True(t, IsDebugging())26 SetMode(ReleaseMode)27 assert.False(t, IsDebugging())28 SetMode(TestMode)29 assert.False(t, IsDebugging())30}3132func TestDebugPrint(t *testing.T) {33 re := captureOutput(t, func() {34 SetMode(DebugMode)35 SetMode(ReleaseMode)36 debugPrint("DEBUG this!")37 SetMode(TestMode)38 debugPrint("DEBUG this!")39 SetMode(DebugMode)40 debugPrint("these are %d %s", 2, "error messages")41 SetMode(TestMode)42 })43 assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)44}45test(debug): improve the test coverage of debug.go to 100% (#4404)OHZEKI Naokibefore adoption19b877f
lines 46–57why, one step back46func TestDebugPrintFunc(t *testing.T) {47 DebugPrintFunc = func(format string, values ...any) {48 fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)49 }50 re := captureOutput(t, func() {51 SetMode(DebugMode)52 debugPrint("debug print func test: %d", 123)53 SetMode(TestMode)54 })55 assert.Regexp(t, `^\[GIN-debug\] debug print func test: 123`, re)56}57chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 58–113why, one step back58func TestDebugPrintError(t *testing.T) {59 re := captureOutput(t, func() {60 SetMode(DebugMode)61 debugPrintError(nil)62 debugPrintError(errors.New("this is an error"))63 SetMode(TestMode)64 })65 assert.Equal(t, "[GIN-debug] [ERROR] this is an error\n", re)66}6768func TestDebugPrintRoutes(t *testing.T) {69 re := captureOutput(t, func() {70 SetMode(DebugMode)71 debugPrintRoute(http.MethodGet, "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})72 SetMode(TestMode)73 })74 assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re)75}7677func TestDebugPrintRouteFunc(t *testing.T) {78 DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {79 fmt.Fprintf(DefaultWriter, "[GIN-debug] %-6s %-40s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)80 }81 re := captureOutput(t, func() {82 SetMode(DebugMode)83 debugPrintRoute(http.MethodGet, "/path/to/route/:param1/:param2", HandlersChain{func(c *Context) {}, handlerNameTest})84 SetMode(TestMode)85 })86 assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param1/:param2 --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re)87}8889func TestDebugPrintLoadTemplate(t *testing.T) {90 re := captureOutput(t, func() {91 SetMode(DebugMode)92 templ := template.Must(template.New("").Delims("{[{", "}]}").ParseGlob("./testdata/template/hello.tmpl"))93 debugPrintLoadTemplate(templ)94 SetMode(TestMode)95 })96 assert.Regexp(t, `^\[GIN-debug\] Loaded HTML Templates \(2\): \n(\t- \n|\t- hello\.tmpl\n){2}\n`, re)97}9899func TestDebugPrintWARNINGSetHTMLTemplate(t *testing.T) {100 re := captureOutput(t, func() {101 SetMode(DebugMode)102 debugPrintWARNINGSetHTMLTemplate()103 SetMode(TestMode)104 })105 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)106}107108func TestDebugPrintWARNINGDefault(t *testing.T) {109 re := captureOutput(t, func() {110 SetMode(DebugMode)111 debugPrintWARNINGDefault()112 SetMode(TestMode)113 })test(debug): improve the test coverage of debug.go to 100% (#4404)OHZEKI Naokibefore adoption19b877f
lines 114–123why, one step back114 assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)115}116117func TestDebugPrintWARNINGDefaultWithUnsupportedVersion(t *testing.T) {118 runtimeVersion = "go1.23.12"119 re := captureOutput(t, func() {120 SetMode(DebugMode)121 debugPrintWARNINGDefault()122 SetMode(TestMode)123 })ci: update Go version support to 1.25+ across CI and docs (#4550)Bo-Yi Wubefore adoptionae3f524
line 124why, one step back124 assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.25+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 125–180why, one step back125}126127func TestDebugPrintWARNINGNew(t *testing.T) {128 re := captureOutput(t, func() {129 SetMode(DebugMode)130 debugPrintWARNINGNew()131 SetMode(TestMode)132 })133 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)134}135136func captureOutput(t *testing.T, f func()) string {137 reader, writer, err := os.Pipe()138 if err != nil {139 panic(err)140 }141 defaultWriter := DefaultWriter142 defaultErrorWriter := DefaultErrorWriter143 defer func() {144 DefaultWriter = defaultWriter145 DefaultErrorWriter = defaultErrorWriter146 log.SetOutput(os.Stderr)147 }()148 DefaultWriter = writer149 DefaultErrorWriter = writer150 log.SetOutput(writer)151 out := make(chan string)152 wg := new(sync.WaitGroup)153 wg.Add(1)154 go func() {155 var buf strings.Builder156 wg.Done()157 _, err := io.Copy(&buf, reader)158 assert.NoError(t, err)159 out <- buf.String()160 }()161 wg.Wait()162 f()163 writer.Close()164 return <-out165}166167func TestGetMinVer(t *testing.T) {168 var m uint64169 var e error170 _, e = getMinVer("go1")171 require.Error(t, e)172 m, e = getMinVer("go1.1")173 assert.Equal(t, uint64(1), m)174 require.NoError(t, e)175 m, e = getMinVer("go1.1.1")176 require.NoError(t, e)177 assert.Equal(t, uint64(1), m)178 _, e = getMinVer("go1.1.1.1")179 require.Error(t, e)180}