-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment_test.go
97 lines (79 loc) · 1.81 KB
/
environment_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package env_test
import (
"fmt"
"os"
"testing"
"github.com/matryer/is"
"github.com/taybart/env"
)
func TestDefault(t *testing.T) {
is := is.New(t)
// Define key
k := "TEST_DEFAULT"
v := "default_value"
env.Add([]string{fmt.Sprintf("%s=%s", k, v)})
is.True(env.Is(k, v))
}
// Test that optionals are set to zero value
func TestOptionalKey(t *testing.T) {
is := is.New(t)
k := "TEST_OPTIONAL_KEY"
// Add optional to env
env.Add([]string{
fmt.Sprintf("%s?", k),
})
// make sure its empty string
is.True(env.Get(k) == "")
// make sure its false
is.True(!env.Bool(k))
// make sure its zero
is.True(env.Int(k) == 0)
}
func TestGet(t *testing.T) {
is := is.New(t)
k := "TestGet"
// set var
os.Setenv(k, "cool variable")
// Should return true since TESTING_ENV is set to true
is.True(env.Get(k) == "cool variable")
}
// TestHas : if value is set env.Has returns true
func TestHas(t *testing.T) {
is := is.New(t)
// Define key
key := "TEST_HAS"
// set env
os.Setenv(key, "this is defined now")
// set
is.True(env.Has(key))
}
func TestBool(t *testing.T) {
is := is.New(t)
// Define key
k := "TEST_BOOL"
// Set env
os.Setenv(k, "true")
// Should return true since TESTING_ENV is set to true
is.True(env.Bool(k))
}
func TestIs(t *testing.T) {
is := is.New(t)
os.Setenv("TEST_IS", "testing")
// Set
is.True(env.Is("TEST_IS", "testing"))
}
// Test json interface marshaling
func TestInterface(t *testing.T) {
is := is.New(t)
// Define key
k := "TEST_INTERFACE"
// expected by call to be set, particular value doesn't matter as long as the type is correct
os.Setenv(k, `{"key": "val", "other": "sudo su"}`)
// test struct
var returned map[string]string
err := env.JSON(k, &returned)
is.NoErr(err)
// Should get the correct value
expected := "val"
is.True(returned["key"] == expected)
}