feat: automatically SetMode to TestMode when run go test. (#3139)

related issue: https://github.com/gin-gonic/gin/issues/3134
This commit is contained in:
micanzhang 2022-05-14 09:11:35 +08:00 committed by GitHub
parent 90e7073d56
commit ef687e0db2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -5,6 +5,7 @@
package gin
import (
"flag"
"io"
"os"
@ -54,7 +55,11 @@ func init() {
// SetMode sets gin mode according to input string.
func SetMode(value string) {
if value == "" {
value = DebugMode
if flag.Lookup("test.v") != nil {
value = TestMode
} else {
value = DebugMode
}
}
switch value {

View File

@ -5,6 +5,7 @@
package gin
import (
"flag"
"os"
"testing"
@ -21,9 +22,16 @@ func TestSetMode(t *testing.T) {
assert.Equal(t, TestMode, Mode())
os.Unsetenv(EnvGinMode)
SetMode("")
assert.Equal(t, testCode, ginMode)
assert.Equal(t, TestMode, Mode())
tmp := flag.CommandLine
flag.CommandLine = flag.NewFlagSet("", flag.ContinueOnError)
SetMode("")
assert.Equal(t, debugCode, ginMode)
assert.Equal(t, DebugMode, Mode())
flag.CommandLine = tmp
SetMode(DebugMode)
assert.Equal(t, debugCode, ginMode)