Skip to content

Commit

Permalink
feat: automatically SetMode to TestMode when run go test. (#3139)
Browse files Browse the repository at this point in the history
related issue: #3134
  • Loading branch information
micanzhang committed May 14, 2022
1 parent 90e7073 commit ef687e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mode.go
Expand Up @@ -5,6 +5,7 @@
package gin

import (
"flag"
"io"
"os"

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions mode_test.go
Expand Up @@ -5,6 +5,7 @@
package gin

import (
"flag"
"os"
"testing"

Expand All @@ -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)
Expand Down

0 comments on commit ef687e0

Please sign in to comment.