mirror of https://github.com/gin-gonic/gin.git
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:
parent
90e7073d56
commit
ef687e0db2
7
mode.go
7
mode.go
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue