From 8d262109f418fbfce2bc25c68b6731567ae2f015 Mon Sep 17 00:00:00 2001 From: kd Date: Thu, 16 Jul 2020 11:45:55 -0400 Subject: Initial commit --- src/app_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/app_test.go (limited to 'src/app_test.go') 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) +} -- cgit v1.2.3