-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun_test.go
48 lines (40 loc) · 1.15 KB
/
run_test.go
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
38
39
40
41
42
43
44
45
46
47
48
package goext
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRunWithEnv(t *testing.T) {
assert := assert.New(t)
os.Setenv("PRE_VAR_1", "baz")
os.Setenv("PRE_VAR_2", "hello")
assertEnvEquals(assert, "PRE_VAR_1", "baz")
assertEnvEquals(assert, "PRE_VAR_2", "hello")
assertEnvIsUnset(assert, "VAR_1")
assertEnvIsUnset(assert, "VAR_2")
err := RunWithOptions(func() error {
assertEnvEquals(assert, "PRE_VAR_1", "baz")
assertEnvEquals(assert, "PRE_VAR_2", "world")
assertEnvEquals(assert, "VAR_1", "foo")
assertEnvEquals(assert, "VAR_2", "bar")
return nil
}, RunOptionWithEnvs(map[string]string{
"VAR_1": "foo",
"VAR_2": "bar",
"PRE_VAR_2": "world",
}))
assert.NoError(err)
assertEnvEquals(assert, "PRE_VAR_1", "baz")
assertEnvEquals(assert, "PRE_VAR_2", "hello")
assertEnvIsUnset(assert, "VAR_1")
assertEnvIsUnset(assert, "VAR_2")
}
func assertEnvEquals(assert *assert.Assertions, name string, expected string) {
actual, ok := os.LookupEnv(name)
assert.True(ok)
assert.Equal(expected, actual)
}
func assertEnvIsUnset(assert *assert.Assertions, name string) {
_, ok := os.LookupEnv(name)
assert.False(ok)
}