Created
July 15, 2016 10:16
-
-
Save kavirajk/eb137586ecff9ef32c0e2c665a1dc936 to your computer and use it in GitHub Desktop.
Test GetEnv function
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
package envron | |
import ( | |
"os" | |
"os/exec" | |
"testing" | |
) | |
func TestMustEnv(t *testing.T) { | |
// If env variable "TEST" is not set then fatal it | |
var value string | |
key := "TEST" | |
if value = os.Getenv(key); value != "" { | |
os.Unsetenv(key) | |
defer func() { os.Setenv(key, value) }() | |
} | |
if os.Getenv("TEST_MUST_ENV") == "1" { | |
MustEnv(key) | |
return | |
} | |
cmd := exec.Command(os.Args[0], "-test.run=TestMustEnv") | |
cmd.Env = append(os.Environ(), "TEST_MUST_ENV=1") | |
err := cmd.Run() | |
if e, ok := err.(*exec.ExitError); ok && !e.Success() { | |
return | |
} | |
t.Errorf("MustEnv() failed to fatal if env %q is not present", key) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment