Skip to content

Commit 1c356ea

Browse files
committed
test(debug): improve tests
1 parent 2a794cd commit 1c356ea

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

debug.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515

1616
const ginSupportMinGoVer = 24
1717

18+
var runtimeVersion = runtime.Version()
19+
1820
// IsDebugging returns true if the framework is running in debug mode.
1921
// Use SetMode(gin.ReleaseMode) to disable debug mode.
2022
func IsDebugging() bool {
@@ -77,7 +79,7 @@ func getMinVer(v string) (uint64, error) {
7779
}
7880

7981
func debugPrintWARNINGDefault() {
80-
if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {
82+
if v, e := getMinVer(runtimeVersion); e == nil && v < ginSupportMinGoVer {
8183
debugPrint(`[WARNING] Now Gin requires Go 1.24+.
8284
8385
`)

debug_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"log"
1313
"net/http"
1414
"os"
15-
"runtime"
1615
"strings"
1716
"sync"
1817
"testing"
@@ -21,10 +20,6 @@ import (
2120
"github.com/stretchr/testify/require"
2221
)
2322

24-
// TODO
25-
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
26-
// func debugPrint(format string, values ...any) {
27-
2823
func TestIsDebugging(t *testing.T) {
2924
SetMode(DebugMode)
3025
assert.True(t, IsDebugging())
@@ -48,6 +43,18 @@ func TestDebugPrint(t *testing.T) {
4843
assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)
4944
}
5045

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+
5158
func TestDebugPrintError(t *testing.T) {
5259
re := captureOutput(t, func() {
5360
SetMode(DebugMode)
@@ -104,12 +111,17 @@ func TestDebugPrintWARNINGDefault(t *testing.T) {
104111
debugPrintWARNINGDefault()
105112
SetMode(TestMode)
106113
})
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)
112-
}
114+
assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
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)
113125
}
114126

115127
func TestDebugPrintWARNINGNew(t *testing.T) {

0 commit comments

Comments
 (0)