-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathenv_test.go
More file actions
71 lines (53 loc) · 1.73 KB
/
env_test.go
File metadata and controls
71 lines (53 loc) · 1.73 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company
// SPDX-License-Identifier: Apache-2.0
// This needs to be in a separate package to allow importing go-bits/assert
// without causing an import loop.
package osext_test
import (
"os"
"testing"
"github.com/sapcc/go-bits/assert"
"github.com/sapcc/go-bits/osext"
)
const KEY = "GOBITS_OSENV_FOO"
const VAL = "this is an example value"
const DEFAULT = "some default value"
func TestGetenv(t *testing.T) {
// test with string value
t.Setenv(KEY, VAL)
str, err := osext.NeedGetenv(KEY)
assert.Equal(t, str, VAL)
assert.ErrEqual(t, err, nil)
str = osext.GetenvOrDefault(KEY, DEFAULT)
assert.Equal(t, str, VAL)
ok := osext.GetenvBool(KEY)
assert.Equal(t, ok, false) // not a valid boolean literal -> false
// test with empty value
t.Setenv(KEY, "")
_, err = osext.NeedGetenv(KEY)
assert.ErrEqual(t, err, osext.MissingEnvError{Key: KEY})
str = osext.GetenvOrDefault(KEY, DEFAULT)
assert.Equal(t, str, DEFAULT)
ok = osext.GetenvBool(KEY)
assert.Equal(t, ok, false)
// test with null value
os.Unsetenv(KEY)
_, err = osext.NeedGetenv(KEY)
assert.ErrEqual(t, err, osext.MissingEnvError{Key: KEY})
str = osext.GetenvOrDefault(KEY, DEFAULT)
assert.Equal(t, str, DEFAULT)
ok = osext.GetenvBool(KEY)
assert.Equal(t, ok, false)
// test GetenvBool with explicitly true-ish values
for _, value := range []string{"t", "True", "1"} {
t.Logf("testing GetenvBool for %q", value)
t.Setenv(KEY, value)
assert.Equal(t, osext.GetenvBool(KEY), true)
}
// test GetenvBool with explicitly false-ish values
for _, value := range []string{"f", "False", "0"} {
t.Logf("testing GetenvBool for %q", value)
t.Setenv(KEY, value)
assert.Equal(t, osext.GetenvBool(KEY), false)
}
}