diff options
| author | kd <kdam0@localhost.localdomain> | 2020-07-16 15:45:55 +0000 |
|---|---|---|
| committer | kd <kdam0@localhost.localdomain> | 2020-07-16 15:45:55 +0000 |
| commit | 8d262109f418fbfce2bc25c68b6731567ae2f015 (patch) | |
| tree | 2216a700e065c5127eae8dea350906564f34aa9d /src/app_test.go | |
Initial commit
Diffstat (limited to 'src/app_test.go')
| -rw-r--r-- | src/app_test.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/app_test.go b/src/app_test.go new file mode 100644 index 0000000..ee2aa3d --- /dev/null +++ b/src/app_test.go @@ -0,0 +1,37 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "os" + "os/exec" + "testing" +) + +func TestGetEnvUndefined(t *testing.T) { + key := "TEST_VAR" + expectedErrorString := "exit status 1" + + // Run the crashing code when FLAG is set + if os.Getenv("GO_CRASHER") == "1" { + getEnv(key) + return + } + + // Run the test in a subprocess - this is a hack! + cmd := exec.Command(os.Args[0], "-test.run=TestGetEnvUndefined") + cmd.Env = append(os.Environ(), "GO_CRASHER=1") + err := cmd.Run() + + // Cast the error as *exec.ExitError and compare the result + e, ok := err.(*exec.ExitError) + assert.True(t, ok) // check if its an error + assert.Equal(t, expectedErrorString, e.Error()) // check if it exited +} + +func TestGetEnvDefined(t *testing.T) { + key := "TEST_VAR" + val := "1" + os.Setenv(key, val) + actual := getEnv(key) + assert.Equal(t, val, actual) +} |
