diff --git a/envutil/info_test.go b/envutil/info_test.go index 20ebcd768..ca9243f08 100644 --- a/envutil/info_test.go +++ b/envutil/info_test.go @@ -51,10 +51,11 @@ func TestIsConsole(t *testing.T) { func TestIsSupportColor(t *testing.T) { is := assert.New(t) + // clear all OS env + testutil.ClearOSEnv() + defer testutil.RevertOSEnv() + // IsSupport256Color - oldVal := os.Getenv("TERM") - _ = os.Unsetenv("TERM") - // is.False(envutil.IsSupportColor()) is.False(envutil.IsSupport256Color()) // ConEmuANSI @@ -85,10 +86,4 @@ func TestIsSupportColor(t *testing.T) { is.NoErr(os.Setenv("TERM", "xterm-vt220")) is.True(envutil.IsSupportColor()) - // revert - if oldVal != "" { - is.NoErr(os.Setenv("TERM", oldVal)) - } else { - is.NoErr(os.Unsetenv("TERM")) - } } diff --git a/stdutil/convert_test.go b/stdutil/convert_test.go index 89eb7ae3b..9d2247477 100644 --- a/stdutil/convert_test.go +++ b/stdutil/convert_test.go @@ -15,7 +15,7 @@ func TestMustString(t *testing.T) { assert.PanicsErrMsg(t, func() { stdutil.MustString([]string{"a", "b"}) - }, "convert data type is failure") + }, comdef.ErrConvType.Error()) } func TestToString(t *testing.T) { diff --git a/testutil/envmock.go b/testutil/envmock.go index ecff8ce24..4604b2dfb 100644 --- a/testutil/envmock.go +++ b/testutil/envmock.go @@ -77,13 +77,32 @@ func MockOsEnv(mp map[string]string, fn func()) { MockCleanOsEnv(mp, fn) } +// backup os ENV +var envBak = os.Environ() + +// ClearOSEnv info. +// +// Usage: +// +// testutil.ClearOSEnv() +// defer testutil.RevertOSEnv() +// // do something ... +func ClearOSEnv() { os.Clearenv() } + +// RevertOSEnv info +func RevertOSEnv() { + os.Clearenv() + for _, str := range envBak { + nodes := strings.SplitN(str, "=", 2) + _ = os.Setenv(nodes[0], nodes[1]) + } +} + // MockCleanOsEnv by env map data. // // will clear all old ENV data, use given data map. // will recover old ENV after fn run. func MockCleanOsEnv(mp map[string]string, fn func()) { - envBak := os.Environ() - os.Clearenv() for key, val := range mp { _ = os.Setenv(key, val)