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
Provenance
- Commit
19b877f- Tree
850aa2c- Author
OHZEKI Naoki <0h23k1.n40k1@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
2a794cd
2 files changed+26 added−12 removed
modified:debug_test.go+23 added-11 removed
10 unmodified lines11 "io"12 "log"13 "net/http"14 "os"15- "runtime"15 "strings"16 "sync"17 "testing"18 19 "github.com/stretchr/testify/assert"20 "github.com/stretchr/testify/require"21 )22 24-// TODO25-// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {26-// func debugPrint(format string, values ...any) {27-23 func TestIsDebugging(t *testing.T) {24 SetMode(DebugMode)25 assert.True(t, IsDebugging())26 SetMode(ReleaseMode)15 unmodified lines42 })43 assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)44 }45 46+func 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+}57+58 func TestDebugPrintError(t *testing.T) {59 re := captureOutput(t, func() {60 SetMode(DebugMode)61 debugPrintError(nil)48 unmodified lines110 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 }116 117+func 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+}126+127 func TestDebugPrintWARNINGNew(t *testing.T) {128 re := captureOutput(t, func() {129 SetMode(DebugMode)130 debugPrintWARNINGNew()50 unmodified linesmodified:debug.go+3 added-1 removed
13 unmodified lines14 )15 16 const ginSupportMinGoVer = 2417 18+var runtimeVersion = runtime.Version()19+20 // IsDebugging returns true if the framework is running in debug mode.21 // Use SetMode(gin.ReleaseMode) to disable debug mode.22 func IsDebugging() bool {23 return atomic.LoadInt32(&ginMode) == debugCode54 unmodified lines78 return strconv.ParseUint(v[first+1:last], 10, 64)79 }80 81 func 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+.84 85 `)86 }28 unmodified lines