@@ -17,9 +17,11 @@ limitations under the License.
17
17
package config
18
18
19
19
import (
20
+ "fmt"
20
21
"io/ioutil"
21
22
"os"
22
23
"path/filepath"
24
+ "strings"
23
25
"testing"
24
26
25
27
. "github.com/onsi/gomega"
@@ -38,10 +40,10 @@ func Test_viperReader_Init(t *testing.T) {
38
40
g .Expect (err ).NotTo (HaveOccurred ())
39
41
defer os .RemoveAll (dir )
40
42
41
- configFile := filepath .Join (dir , ". clusterctl.yaml" )
43
+ configFile := filepath .Join (dir , "clusterctl.yaml" )
42
44
g .Expect (ioutil .WriteFile (configFile , []byte ("bar: bar" ), 0640 )).To (Succeed ())
43
45
44
- configFileBadContents := filepath .Join (dir , ". clusterctl-bad.yaml" )
46
+ configFileBadContents := filepath .Join (dir , "clusterctl-bad.yaml" )
45
47
g .Expect (ioutil .WriteFile (configFileBadContents , []byte ("bad-contents" ), 0640 )).To (Succeed ())
46
48
47
49
tests := []struct {
@@ -99,7 +101,7 @@ func Test_viperReader_Get(t *testing.T) {
99
101
100
102
os .Setenv ("FOO" , "foo" )
101
103
102
- configFile := filepath .Join (dir , ". clusterctl.yaml" )
104
+ configFile := filepath .Join (dir , "clusterctl.yaml" )
103
105
g .Expect (ioutil .WriteFile (configFile , []byte ("bar: bar" ), 0640 )).To (Succeed ())
104
106
105
107
type args struct {
@@ -140,7 +142,7 @@ func Test_viperReader_Get(t *testing.T) {
140
142
t .Run (tt .name , func (t * testing.T ) {
141
143
gs := NewWithT (t )
142
144
143
- v := & viperReader {}
145
+ v := newViperReader ( InjectConfigPaths ([] string { dir }))
144
146
145
147
gs .Expect (v .Init (configFile )).To (Succeed ())
146
148
@@ -156,6 +158,22 @@ func Test_viperReader_Get(t *testing.T) {
156
158
}
157
159
}
158
160
161
+ func Test_viperReader_GetWithoutDefaultConfig (t * testing.T ) {
162
+ g := NewWithT (t )
163
+ dir , err := ioutil .TempDir ("" , "clusterctl" )
164
+ g .Expect (err ).NotTo (HaveOccurred ())
165
+ defer os .RemoveAll (dir )
166
+
167
+ os .Setenv ("FOO_FOO" , "bar" )
168
+
169
+ v := newViperReader (InjectConfigPaths ([]string {dir }))
170
+ g .Expect (v .Init ("" )).To (Succeed ())
171
+
172
+ got , err := v .Get ("FOO_FOO" )
173
+ g .Expect (err ).NotTo (HaveOccurred ())
174
+ g .Expect (got ).To (Equal ("bar" ))
175
+ }
176
+
159
177
func Test_viperReader_Set (t * testing.T ) {
160
178
g := NewWithT (t )
161
179
@@ -165,7 +183,7 @@ func Test_viperReader_Set(t *testing.T) {
165
183
166
184
os .Setenv ("FOO" , "foo" )
167
185
168
- configFile := filepath .Join (dir , ". clusterctl.yaml" )
186
+ configFile := filepath .Join (dir , "clusterctl.yaml" )
169
187
170
188
g .Expect (ioutil .WriteFile (configFile , []byte ("bar: bar" ), 0640 )).To (Succeed ())
171
189
@@ -203,3 +221,48 @@ func Test_viperReader_Set(t *testing.T) {
203
221
})
204
222
}
205
223
}
224
+
225
+ func Test_viperReader_checkDefaultConfig (t * testing.T ) {
226
+ g := NewWithT (t )
227
+ dir , err := ioutil .TempDir ("" , "clusterctl" )
228
+ g .Expect (err ).NotTo (HaveOccurred ())
229
+ defer os .RemoveAll (dir )
230
+ dir = strings .TrimSuffix (dir , "/" )
231
+
232
+ configFile := filepath .Join (dir , "clusterctl.yaml" )
233
+ g .Expect (ioutil .WriteFile (configFile , []byte ("bar: bar" ), 0640 )).To (Succeed ())
234
+
235
+ type fields struct {
236
+ configPaths []string
237
+ }
238
+ tests := []struct {
239
+ name string
240
+ fields fields
241
+ want bool
242
+ }{
243
+ {
244
+ name : "tmp path without final /" ,
245
+ fields : fields {
246
+ configPaths : []string {dir },
247
+ },
248
+ want : true ,
249
+ },
250
+ {
251
+ name : "tmp path with final /" ,
252
+ fields : fields {
253
+ configPaths : []string {fmt .Sprintf ("%s/" , dir )},
254
+ },
255
+ want : true ,
256
+ },
257
+ }
258
+ for _ , tt := range tests {
259
+ t .Run (tt .name , func (t * testing.T ) {
260
+ gs := NewWithT (t )
261
+
262
+ v := & viperReader {
263
+ configPaths : tt .fields .configPaths ,
264
+ }
265
+ gs .Expect (v .checkDefaultConfig ()).To (Equal (tt .want ))
266
+ })
267
+ }
268
+ }
0 commit comments