-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): use
t.Setenv
(#13036)
Signed-off-by: Anton Gilgur <agilgur5@gmail.com>
- Loading branch information
Showing
14 changed files
with
40 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,46 @@ | ||
package env | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLookupEnvDurationOr(t *testing.T) { | ||
defer func() { _ = os.Unsetenv("FOO") }() | ||
assert.Equal(t, time.Second, LookupEnvDurationOr("", time.Second), "default value") | ||
_ = os.Setenv("FOO", "bar") | ||
t.Setenv("FOO", "bar") | ||
assert.Panics(t, func() { LookupEnvDurationOr("FOO", time.Second) }, "bad value") | ||
_ = os.Setenv("FOO", "1h") | ||
t.Setenv("FOO", "1h") | ||
assert.Equal(t, time.Hour, LookupEnvDurationOr("FOO", time.Second), "env var value") | ||
_ = os.Setenv("FOO", "") | ||
t.Setenv("FOO", "") | ||
assert.Equal(t, time.Second, LookupEnvDurationOr("FOO", time.Second), "empty var value; default value") | ||
} | ||
|
||
func TestLookupEnvIntOr(t *testing.T) { | ||
defer func() { _ = os.Unsetenv("FOO") }() | ||
assert.Equal(t, 1, LookupEnvIntOr("", 1), "default value") | ||
_ = os.Setenv("FOO", "not-int") | ||
t.Setenv("FOO", "not-int") | ||
assert.Panics(t, func() { LookupEnvIntOr("FOO", 1) }, "bad value") | ||
_ = os.Setenv("FOO", "2") | ||
t.Setenv("FOO", "2") | ||
assert.Equal(t, 2, LookupEnvIntOr("FOO", 1), "env var value") | ||
_ = os.Setenv("FOO", "") | ||
t.Setenv("FOO", "") | ||
assert.Equal(t, 1, LookupEnvIntOr("FOO", 1), "empty var value; default value") | ||
} | ||
|
||
func TestLookupEnvFloatOr(t *testing.T) { | ||
defer func() { _ = os.Unsetenv("FOO") }() | ||
assert.Equal(t, 1., LookupEnvFloatOr("", 1.), "default value") | ||
_ = os.Setenv("FOO", "not-float") | ||
t.Setenv("FOO", "not-float") | ||
assert.Panics(t, func() { LookupEnvFloatOr("FOO", 1.) }, "bad value") | ||
_ = os.Setenv("FOO", "2.0") | ||
t.Setenv("FOO", "2.0") | ||
assert.Equal(t, 2., LookupEnvFloatOr("FOO", 1.), "env var value") | ||
_ = os.Setenv("FOO", "") | ||
t.Setenv("FOO", "") | ||
assert.Equal(t, 1., LookupEnvFloatOr("FOO", 1.), "empty var value; default value") | ||
} | ||
|
||
func TestLookupEnvStringOr(t *testing.T) { | ||
defer func() { _ = os.Unsetenv("FOO") }() | ||
assert.Equal(t, "a", LookupEnvStringOr("", "a"), "default value") | ||
_ = os.Setenv("FOO", "b") | ||
t.Setenv("FOO", "b") | ||
assert.Equal(t, "b", LookupEnvStringOr("FOO", "a"), "env var value") | ||
_ = os.Setenv("FOO", "") | ||
t.Setenv("FOO", "") | ||
assert.Equal(t, "a", LookupEnvStringOr("FOO", "a"), "empty var value; default value") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.