-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathflag_test.go
124 lines (100 loc) · 2.71 KB
/
flag_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseFlags(t *testing.T) {
t.Run("given empty args and env vars then flags are set to default values", func(t *testing.T) {
setInput(t, nil, nil)
flags, err := ParseFlags()
require.NoError(t, err)
assert.False(t, flags.Expiry)
assert.False(t, flags.Insecure)
assert.False(t, flags.Chains)
assert.False(t, flags.Pem)
assert.False(t, flags.PemOnly)
assert.False(t, flags.Version)
assert.Empty(t, flags.Args)
})
t.Run("given args are set and env vars empty then flags are set to provided args", func(t *testing.T) {
setInput(t, []string{"flag",
"-expiry=true",
"-insecure=true",
"-chains=true",
"-chains=true",
"-pem=true",
"-pem-only=true",
"-version=true",
}, nil)
flags, err := ParseFlags()
require.NoError(t, err)
assert.True(t, flags.Expiry)
assert.True(t, flags.Insecure)
assert.True(t, flags.Chains)
assert.True(t, flags.Pem)
assert.True(t, flags.PemOnly)
assert.True(t, flags.Version)
assert.Empty(t, flags.Args)
})
t.Run("given args are not set and env vars are set then flags are set to provided env vars", func(t *testing.T) {
setInput(t, []string{"flag"}, map[string]string{
"CERTINFO_EXPIRY": "true",
"CERTINFO_INSECURE": "true",
"CERTINFO_CHAINS": "true",
"CERTINFO_PEM": "true",
"CERTINFO_PEM_ONLY": "true",
"CERTINFO_VERSION": "true",
})
flags, err := ParseFlags()
require.NoError(t, err)
assert.True(t, flags.Expiry)
assert.True(t, flags.Insecure)
assert.True(t, flags.Chains)
assert.True(t, flags.Pem)
assert.True(t, flags.PemOnly)
assert.True(t, flags.Version)
assert.Empty(t, flags.Args)
})
t.Run("given args are set and env vars are set then flags are set to provided args", func(t *testing.T) {
setInput(t, []string{"flag",
"-insecure=true",
"-chains=true",
"-pem=false",
"-version=false",
}, map[string]string{
"CERTINFO_EXPIRY": "true",
"CERTINFO_CHAINS": "true",
"CERTINFO_PEM": "true",
"CERTINFO_PEM_ONLY": "true",
"CERTINFO_VERSION": "true",
})
flags, err := ParseFlags()
require.NoError(t, err)
assert.True(t, flags.Expiry)
assert.True(t, flags.Insecure)
assert.True(t, flags.Chains)
assert.False(t, flags.Pem)
assert.True(t, flags.PemOnly)
assert.False(t, flags.Version)
assert.Empty(t, flags.Args)
})
}
// --- helper functions ---
func setInput(t *testing.T, args []string, env map[string]string) {
osArgs := os.Args
if args == nil {
args = []string{"test"}
}
os.Args = args
for k, v := range env {
os.Setenv(k, v)
}
t.Cleanup(func() {
os.Args = osArgs
for k := range env {
os.Unsetenv(k)
}
})
}