-
Notifications
You must be signed in to change notification settings - Fork 188
/
args_test.go
183 lines (154 loc) · 3.81 KB
/
args_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// +build !integration
package main
import (
"fmt"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/suite"
)
type ArgsTestSuite struct {
suite.Suite
args args
}
func (s *ArgsTestSuite) SetupTest() {
httpListenAndServe = func(addr string, handler http.Handler) error {
return nil
}
}
// NewArgs
func (s ArgsTestSuite) Test_NewArgs_ReturnsNewStruct() {
a := newArgs()
s.IsType(args{}, a)
}
// Parse > Server
func (s ArgsTestSuite) Test_Parse_ParsesServerLongArgs() {
os.Args = []string{"myProgram", "server"}
data := []struct {
expected string
key string
value *string
}{
{"ipFromArgs", "ip", &serverImpl.IP},
{"portFromArgs", "port", &serverImpl.Port},
}
for _, d := range data {
os.Args = append(os.Args, fmt.Sprintf("--%s", d.key), d.expected)
}
args{}.parse()
for _, d := range data {
s.Equal(d.expected, *d.value)
}
s.Len(serverImpl.ListenerAddresses, 1)
}
func (s ArgsTestSuite) Test_Parse_ParsesServerShortArgs() {
os.Args = []string{"myProgram", "server"}
data := []struct {
expected string
key string
value *string
}{
{"ipFromArgs", "i", &serverImpl.IP},
{"portFromArgs", "p", &serverImpl.Port},
}
for _, d := range data {
os.Args = append(os.Args, fmt.Sprintf("-%s", d.key), d.expected)
}
args{}.parse()
for _, d := range data {
s.Equal(d.expected, *d.value)
}
s.Len(serverImpl.ListenerAddresses, 1)
}
func (s ArgsTestSuite) Test_Parse_ServerHasDefaultValues() {
os.Args = []string{"myProgram", "server"}
data := []struct {
expected string
value *string
}{
{"0.0.0.0", &serverImpl.IP},
{"8080", &serverImpl.Port},
}
args{}.parse()
for _, d := range data {
s.Equal(d.expected, *d.value)
}
s.Len(serverImpl.ListenerAddresses, 1)
}
func (s ArgsTestSuite) Test_Parse_ServerDefaultsToEnvVars() {
os.Args = []string{"myProgram", "server"}
data := []struct {
expected string
key string
value *string
}{
{"ipFromEnv", "IP", &serverImpl.IP},
{"portFromEnv", "PORT", &serverImpl.Port},
}
for _, d := range data {
os.Setenv(d.key, d.expected)
}
defer func() {
for _, d := range data {
os.Unsetenv(d.key)
}
}()
args{}.parse()
for _, d := range data {
s.Equal(d.expected, *d.value)
}
s.Len(serverImpl.ListenerAddresses, 1)
}
func (s ArgsTestSuite) Test_Parse_ParsesListnerAddressShortArgs() {
dataTable := []struct {
value []string
expected []string
}{
{[]string{"-l", "dfsl1", "-l", "dfsl2"}, []string{"dfsl1", "dfsl2"}},
{[]string{"-l", "dfsl1"}, []string{"dfsl1"}},
}
rootArgs := []string{"myProgram", "server"}
for _, data := range dataTable {
os.Args = append(rootArgs, data.value...)
args{}.parse()
s.Require().Equal(data.expected, serverImpl.ListenerAddresses)
}
}
func (s ArgsTestSuite) Test_Parse_ParsesListnerAddressLongArgs() {
dataTable := []struct {
value []string
expected []string
}{
{[]string{"--listener-address", "dfsl1", "--listener-address", "dfsl2"}, []string{"dfsl1", "dfsl2"}},
{[]string{"--listener-address", "dfsl1"}, []string{"dfsl1"}},
}
rootArgs := []string{"myProgram", "server"}
for _, data := range dataTable {
os.Args = append(rootArgs, data.value...)
args{}.parse()
s.Require().Equal(data.expected, serverImpl.ListenerAddresses)
}
}
func (s ArgsTestSuite) Test_Parse_ParsesListnerAddressEnvVars() {
os.Args = []string{"myProgram", "server"}
dataTable := []struct {
value string
expected []string
}{
{"dfsl1,dfsl2", []string{"dfsl1", "dfsl2"}},
{"dfsl1", []string{"dfsl1"}},
}
defer func() {
os.Unsetenv("LISTENER_ADDRESS")
}()
for _, data := range dataTable {
os.Setenv("LISTENER_ADDRESS", data.value)
args{}.parse()
s.Require().Equal(data.expected, serverImpl.ListenerAddresses)
}
}
// Suite
func TestArgsUnitTestSuite(t *testing.T) {
logPrintf = func(format string, v ...interface{}) {}
suite.Run(t, new(ArgsTestSuite))
}