generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_test.go
76 lines (59 loc) · 1.45 KB
/
env_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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package osenv
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGet(t *testing.T) {
key := "GET"
assert.Equal(t, "", Get(key))
for _, fallback := range []string{"", "FIZZ", "BUZZ"} {
assert.Equal(t, fallback, Get(key, fallback))
}
value := "BAR"
os.Setenv(key, value)
assert.Equal(t, value, Get(key))
assert.Equal(t, value, Get(key, "FALLBACK"))
os.Setenv("EMPTY", "")
assert.Equal(t, "", Get("EMPTY", "NOT EMPTY"))
}
func TestMustGet(t *testing.T) {
key := "MUST_GET"
value := "MUST_GET VALUE"
os.Setenv(key, value)
assert.Equal(t, value, MustGet(key))
assert.Panics(t, func() {
MustGet("NIL")
})
}
func TestSetNX(t *testing.T) {
key := "SEX_NX"
value := "SET_NX VALUE"
assert.Equal(t, "", os.Getenv(key))
assert.Nil(t, SetNX(key, value))
assert.Equal(t, value, os.Getenv(key))
assert.Nil(t, SetNX(key, "new value"))
assert.Equal(t, value, os.Getenv(key))
}
func TestGetInt(t *testing.T) {
key := "GET_INT"
_, err := GetInt(key)
assert.NotNil(t, err)
fallback := 1
v, _ := GetInt(key, fallback)
assert.Equal(t, fallback, v)
os.Setenv(key, "2")
v, _ = GetInt(key)
assert.Equal(t, 2, v)
}
func TestMustGetInt(t *testing.T) {
key := "MUST_GET_INT"
assert.Panics(t, func() {
MustGetInt(key)
})
os.Setenv(key, "3")
assert.Equal(t, 3, MustGetInt(key))
}