Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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