Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Created July 15, 2016 10:16
Show Gist options
  • Save kavirajk/eb137586ecff9ef32c0e2c665a1dc936 to your computer and use it in GitHub Desktop.
Save kavirajk/eb137586ecff9ef32c0e2c665a1dc936 to your computer and use it in GitHub Desktop.
Test GetEnv function
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