-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
138 lines (128 loc) · 2.82 KB
/
main_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
package main
import (
"flag"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type parseFlagsCorrectTest struct {
args []string
conf config
}
func TestParseFlagsCorrect(t *testing.T) {
tests := []parseFlagsCorrectTest{
{
[]string{"-gf", "geofeed.csv"},
config{
gf: "geofeed.csv",
db: "/usr/local/share/GeoIP/GeoIP2-City.mmdb",
},
},
{
[]string{"-gf", "geofeed.csv", "-db", "file.mmdb"},
config{
gf: "geofeed.csv",
db: "file.mmdb",
},
},
{
[]string{"-db", "file.mmdb", "-gf", "geofeed.csv"},
config{
gf: "geofeed.csv",
db: "file.mmdb",
},
},
{
[]string{"--lax", "-db", "file.mmdb", "-gf", "geofeed.csv"},
config{
gf: "geofeed.csv",
db: "file.mmdb",
laxMode: true,
},
},
{
[]string{"-db", "file.mmdb", "-lax=true", "-gf", "geofeed.csv"},
config{
gf: "geofeed.csv",
db: "file.mmdb",
laxMode: true,
},
},
{
[]string{"-db", "file.mmdb", "-gf", "geofeed.csv", "--lax=false"},
config{
gf: "geofeed.csv",
db: "file.mmdb",
laxMode: false,
},
},
}
for _, test := range tests {
t.Run(strings.Join(test.args, " "), func(t *testing.T) {
conf, output, err := parseFlags("program", test.args)
require.NoError(t, err, "parseFlags ran without error")
assert.Empty(t, output, "parseFlags ran without output")
assert.Equal(t, test.conf, *conf, "parseFlags produced expected config")
})
}
}
func TestParseFlagsUsage(t *testing.T) {
usageArgs := []string{"-help", "-h", "--help"}
for _, arg := range usageArgs {
t.Run(arg, func(t *testing.T) {
conf, output, err := parseFlags("program", []string{arg})
assert.Equal(t, flag.ErrHelp, err)
assert.Nil(t, conf, "there should be no config set")
assert.Contains(t, output, "Usage of", "output contains usage info")
})
}
}
type parseFlagsErrorTest struct {
args []string
output string
errmsg string
}
func TestParseFlagsError(t *testing.T) {
tests := []parseFlagsErrorTest{
{
[]string{},
"Path to local geofeed file",
"-gf is required",
},
{
[]string{"-db", ""},
"Path to local geofeed file",
"-gf is required and -db can not be an empty string",
},
{
[]string{"-db", "file.mdb"},
"Path to local geofeed file",
"-gf is required",
},
{
[]string{"-gf", "geofeed.csv", "-db", ""},
"Path to local geofeed file",
"-db is required",
},
}
for _, test := range tests {
t.Run(
strings.Join(test.args, " "), func(t *testing.T) {
_, output, err := parseFlags("program", test.args)
assert.Contains(
t,
output,
test.output,
"output contains usage info: '%s'", test.output,
)
assert.EqualError(
t,
err,
test.errmsg,
"got expected error message: '%s'", test.errmsg,
)
},
)
}
}