Skip to content
Myr

test(debug): improve the test coverage of debug.go to 100% (#4404)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by OHZEKI Naoki · committed by GitHub · parent 2a794cd · Browse files · 2 files+26 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+26 added−12 removed

  • other:+26 added−12 removed

2 files+26 added−12 removed

modified:debug_test.go+23 added-11 removed
10 unmodified lines
11 "io"12 "log"13 "net/http"14 "os"15 "runtime"15 "strings"16 "sync"17 "testing"1819 "github.com/stretchr/testify/assert"20 "github.com/stretchr/testify/require"21)2224// TODO25// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {26// func debugPrint(format string, values ...any) {2723func TestIsDebugging(t *testing.T) {24 SetMode(DebugMode)25 assert.True(t, IsDebugging())26 SetMode(ReleaseMode)
15 unmodified lines
42 })43 assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)44}4546func 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}5758func TestDebugPrintError(t *testing.T) {59 re := captureOutput(t, func() {60 SetMode(DebugMode)61 debugPrintError(nil)
48 unmodified lines
110 SetMode(DebugMode)111 debugPrintWARNINGDefault()112 SetMode(TestMode)113 })107 m, e := getMinVer(runtime.Version())108 if e == nil && m < ginSupportMinGoVer {109 assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.24+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)110 } else {111 assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)114 assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)112 }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 })124 assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.24+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)125}126127func TestDebugPrintWARNINGNew(t *testing.T) {128 re := captureOutput(t, func() {129 SetMode(DebugMode)130 debugPrintWARNINGNew()
50 unmodified lines
modified:debug.go+3 added-1 removed
13 unmodified lines
14)1516const ginSupportMinGoVer = 241718var runtimeVersion = runtime.Version()1920// IsDebugging returns true if the framework is running in debug mode.21// Use SetMode(gin.ReleaseMode) to disable debug mode.22func IsDebugging() bool {23 return atomic.LoadInt32(&ginMode) == debugCode
54 unmodified lines
78 return strconv.ParseUint(v[first+1:last], 10, 64)79}8081func debugPrintWARNINGDefault() {80 if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {82 if v, e := getMinVer(runtimeVersion); e == nil && v < ginSupportMinGoVer {83 debugPrint(`[WARNING] Now Gin requires Go 1.24+.8485`)86 }
28 unmodified lines