blob: ee2aa3dd1047e8d814e62d445fe9fc29959b4060 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)
}
|