-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_test.go
176 lines (151 loc) · 3.65 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
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
package main
import (
"os"
"testing"
)
func TestDefaultFlags(t *testing.T) {
rollback := setInput(nil, nil)
defer rollback()
flags, err := ParseFlags()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if flags.Port != 8080 {
t.Errorf("port: want 8080, got %d", flags.Port)
}
if flags.ProfilePort != 0 {
t.Errorf("profile port: want 0, got %d", flags.Port)
}
if flags.MaxLinks != 50 {
t.Errorf("max links: want 50, got %d", flags.MaxLinks)
}
if flags.MinLinks != 10 {
t.Errorf("host: want 10, got %d", flags.MinLinks)
}
if flags.MaxLinkDepth != 10 {
t.Errorf("host: want 10, got %d", flags.MaxLinkDepth)
}
if flags.MinLinkDepth != 1 {
t.Errorf("host: want 1, got %d", flags.MinLinkDepth)
}
}
func TestFlags(t *testing.T) {
rollback := setInput([]string{"flag",
"--port", "443",
"--max-links", "80",
"--min-links", "8",
"--max-link-depth", "8",
"--min-link-depth", "2",
}, nil)
defer func() { rollback() }()
flags, err := ParseFlags()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if flags.Port != 443 {
t.Errorf("port: want 443, got %d", flags.Port)
}
if flags.MaxLinks != 80 {
t.Errorf("max links: want 80, got %d", flags.MaxLinks)
}
if flags.MinLinks != 8 {
t.Errorf("host: want 8, got %d", flags.MinLinks)
}
if flags.MaxLinkDepth != 8 {
t.Errorf("host: want 8, got %d", flags.MaxLinkDepth)
}
if flags.MinLinkDepth != 2 {
t.Errorf("host: want 2, got %d", flags.MinLinkDepth)
}
}
func TestFlagsFromEnvVar(t *testing.T) {
rollback := setInput([]string{"flag"}, map[string]string{
"BH_PORT": "80",
"BH_MAX_LINKS": "7",
"BH_MIN_LINKS": "4",
"BH_MAX_LINK_DEPTH": "11",
"BH_MIN_LINK_DEPTH": "3",
})
defer func() { rollback() }()
flags, err := ParseFlags()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if flags.Port != 80 {
t.Errorf("port: want 80, got %d", flags.Port)
}
if flags.MaxLinks != 7 {
t.Errorf("max links: want 7, got %d", flags.MaxLinks)
}
if flags.MinLinks != 4 {
t.Errorf("host: want 4, got %d", flags.MinLinks)
}
if flags.MaxLinkDepth != 11 {
t.Errorf("host: want 11, got %d", flags.MaxLinkDepth)
}
if flags.MinLinkDepth != 3 {
t.Errorf("host: want 3, got %d", flags.MinLinkDepth)
}
}
func TestFlagsOverrideEnvVar(t *testing.T) {
rollback := setInput([]string{"flag",
"--port", "443",
"--max-links", "80",
"--min-links", "8",
"--max-link-depth", "8",
"--min-link-depth", "2",
}, map[string]string{
"BH_PORT": "80",
"BH_MAX_LINKS": "7",
"BH_MIN_LINKS": "4",
"BH_MAX_LINK_DEPTH": "11",
"BH_MIN_LINK_DEPTH": "3",
})
defer func() { rollback() }()
flags, err := ParseFlags()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if flags.Port != 443 {
t.Errorf("port: want 443, got %d", flags.Port)
}
if flags.MaxLinks != 80 {
t.Errorf("max links: want 80, got %d", flags.MaxLinks)
}
if flags.MinLinks != 8 {
t.Errorf("host: want 8, got %d", flags.MinLinks)
}
if flags.MaxLinkDepth != 8 {
t.Errorf("host: want 8, got %d", flags.MaxLinkDepth)
}
if flags.MinLinkDepth != 2 {
t.Errorf("host: want 2, got %d", flags.MinLinkDepth)
}
}
func TestFlagsValidate(t *testing.T) {
rollback := setInput([]string{"flag",
"--port", "70000",
}, nil)
defer func() { rollback() }()
if _, err := ParseFlags(); err == nil {
t.Error("expected error")
}
}
// --- helper functions ---
func setInput(args []string, env map[string]string) (rollback func()) {
osArgs := os.Args
rollback = func() {
os.Args = osArgs
for k := range env {
os.Unsetenv(k)
}
}
if args == nil {
args = []string{"test"}
}
os.Args = args
for k, v := range env {
os.Setenv(k, v)
}
return rollback
}