forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add a ttools helper for easier local testing
Previously a local test required to setup the local development environment and passing a bunch of environment variables prior to running `go test ./...`. Now we allow for environment variables to be passed, but also default back to variables we used across tests and pipelines as default, making testing a little more comfy. test: add additional tests fix: use os.LookupEnv instead docs: add comment
- Loading branch information
Showing
4 changed files
with
115 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ttools | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// GetEnvDefault looks up an environment variable, and return a default value | ||
// when it is not present | ||
func GetEnvDefault(key, dvalue string) string { | ||
v, ok := os.LookupEnv(key) | ||
if !ok { | ||
return dvalue | ||
} | ||
|
||
return v | ||
} |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ttools | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
const ( | ||
ENV_TEST_VARIABLE = "test" | ||
ENV_TEST_EMPTY_VARIABLE = "" | ||
) | ||
|
||
func setEnv() { | ||
os.Setenv("ENV_TEST_VARIABLE", ENV_TEST_VARIABLE) | ||
os.Setenv("ENV_TEST_EMPTY_VARIABLE", ENV_TEST_EMPTY_VARIABLE) | ||
} | ||
|
||
func unsetEnv() { | ||
os.Unsetenv("ENV_TEST_VARIABLE") | ||
os.Unsetenv("ENV_TEST_EMPTY_VARIABLE") | ||
} | ||
|
||
func TestGetEnvDefault(t *testing.T) { | ||
type tcase struct { | ||
key string | ||
dvalue string | ||
expected string | ||
} | ||
|
||
setEnv() | ||
defer unsetEnv() | ||
|
||
fn := func(tc tcase) func(*testing.T) { | ||
return func(t *testing.T) { | ||
|
||
v := GetEnvDefault(tc.key, tc.dvalue) | ||
|
||
if v != tc.expected { | ||
t.Errorf("\n\nexpected: %s \ngot: %s \n\n", tc.expected, v) | ||
} | ||
} | ||
} | ||
|
||
tests := map[string]tcase{ | ||
"should use default value": { | ||
key: "ENV_THAT_DOESNT_EXIST", | ||
dvalue: "DEFAULT_VALUE", | ||
expected: "DEFAULT_VALUE", | ||
}, | ||
"should get variable from environment": { | ||
key: "ENV_TEST_VARIABLE", | ||
dvalue: "DEFAULT_VALUE", | ||
expected: ENV_TEST_VARIABLE, | ||
}, | ||
"should use default when key is empty string": { | ||
key: "", | ||
dvalue: "DEFAULT_VALUE", | ||
expected: "DEFAULT_VALUE", | ||
}, | ||
"should get variable from empty string environment": { | ||
key: "ENV_TEST_EMPTY_VARIABLE", | ||
dvalue: "DEFAULT_VALUE", | ||
expected: ENV_TEST_EMPTY_VARIABLE, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, fn(tc)) | ||
} | ||
} |
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