1
2
3
4
5
6
7
8
9
10
11
12
13
14 package config
15
16 import (
17 "io"
18 "testing"
19
20 "gopkg.in/alecthomas/kingpin.v2"
21 )
22
23 var (
24 url *string
25 id *string
26 )
27
28 func newApp() *kingpin.Application {
29 url = new(string)
30 id = new(string)
31
32 app := kingpin.New("app", "")
33 app.UsageWriter(io.Discard)
34 app.ErrorWriter(io.Discard)
35 app.Terminate(nil)
36
37 app.Flag("url", "").StringVar(url)
38
39 silence := app.Command("silence", "")
40 silenceDel := silence.Command("del", "")
41 silenceDel.Flag("id", "").StringVar(id)
42
43 return app
44 }
45
46 func TestNewConfigResolver(t *testing.T) {
47 for i, tc := range []struct {
48 files []string
49 err bool
50 }{
51 {[]string{}, false},
52 {[]string{"testdata/amtool.good1.yml", "testdata/amtool.good2.yml"}, false},
53 {[]string{"testdata/amtool.good1.yml", "testdata/not_existing.yml"}, false},
54 {[]string{"testdata/amtool.good1.yml", "testdata/amtool.bad.yml"}, true},
55 } {
56 _, err := NewResolver(tc.files, nil)
57 if tc.err != (err != nil) {
58 if tc.err {
59 t.Fatalf("%d: expected error but got none", i)
60 } else {
61 t.Fatalf("%d: expected no error but got %v", i, err)
62 }
63 }
64 }
65 }
66
67 type expectFn func()
68
69 func TestConfigResolverBind(t *testing.T) {
70 expectURL := func(expected string) expectFn {
71 return func() {
72 if *url != expected {
73 t.Fatalf("expected url flag %q but got %q", expected, *url)
74 }
75 }
76 }
77 expectID := func(expected string) expectFn {
78 return func() {
79 if *id != expected {
80 t.Fatalf("expected ID flag %q but got %q", expected, *id)
81 }
82 }
83 }
84
85 for i, tc := range []struct {
86 files []string
87 legacyFlags map[string]string
88 args []string
89
90 err bool
91 expCmd string
92 expFns []expectFn
93 }{
94 {
95 []string{"testdata/amtool.good1.yml", "testdata/amtool.good2.yml"},
96 nil,
97 []string{},
98
99 true,
100 "",
101 []expectFn{expectURL("url1")},
102 },
103 {
104 []string{"testdata/amtool.good2.yml"},
105 nil,
106 []string{},
107
108 true,
109 "",
110 []expectFn{expectURL("url2")},
111 },
112 {
113 []string{"testdata/amtool.good1.yml", "testdata/amtool.good2.yml"},
114 nil,
115 []string{"--url", "url3"},
116
117 true,
118 "",
119 []expectFn{expectURL("url3")},
120 },
121 {
122 []string{"testdata/amtool.good1.yml", "testdata/amtool.good2.yml"},
123 map[string]string{"old-id": "id"},
124 []string{"silence", "del"},
125
126 false,
127 "silence del",
128 []expectFn{
129 expectURL("url1"),
130 expectID("id1"),
131 },
132 },
133 {
134 []string{"testdata/amtool.good2.yml"},
135 map[string]string{"old-id": "id"},
136 []string{"silence", "del"},
137
138 false,
139 "silence del",
140 []expectFn{
141 expectURL("url2"),
142 expectID("id2"),
143 },
144 },
145 {
146 []string{"testdata/amtool.good2.yml"},
147 map[string]string{"old-id": "id"},
148 []string{"silence", "del", "--id", "id3"},
149
150 false,
151 "silence del",
152 []expectFn{
153 expectURL("url2"),
154 expectID("id3"),
155 },
156 },
157 } {
158 r, err := NewResolver(tc.files, tc.legacyFlags)
159 if err != nil {
160 t.Fatalf("%d: expected no error but got: %v", i, err)
161 }
162
163 app := newApp()
164 err = r.Bind(app, tc.args)
165 if err != nil {
166 t.Fatalf("%d: expected Bind() to return no error but got: %v", i, err)
167 }
168
169 cmd, err := app.Parse(tc.args)
170 if tc.err != (err != nil) {
171 if tc.err {
172 t.Fatalf("%d: expected Parse() to return an error but got none", i)
173 } else {
174 t.Fatalf("%d: expected Parse() to return no error but got: %v", i, err)
175 }
176 }
177 if cmd != tc.expCmd {
178 t.Fatalf("%d: expected command %q but got %q", i, tc.expCmd, cmd)
179 }
180 for _, fn := range tc.expFns {
181 fn()
182 }
183 }
184 }
185
View as plain text