-
Notifications
You must be signed in to change notification settings - Fork 25
/
config_test.go
138 lines (127 loc) · 2.65 KB
/
config_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
// Copyright 2018 The go-bindata Authors. All rights reserved.
// Use of this source code is governed by a CC0 1.0 Universal (CC0 1.0)
// Public Domain Dedication license that can be found in the LICENSE file.
package bindata
import (
"os"
"path/filepath"
"testing"
)
func TestValidateInput(t *testing.T) {
tests := []struct {
desc string
cfg *Config
exp []InputConfig
expErr string
}{{
desc: `With empty list`,
cfg: &Config{},
expErr: ErrNoInput.Error(),
}, {
desc: `With empty path`,
cfg: &Config{
Input: []InputConfig{{
Path: "",
}},
},
exp: []InputConfig{{
Path: ".",
}},
}, {
desc: `With directory not exist`,
cfg: &Config{
Input: []InputConfig{{
Path: "./notexist",
}},
},
expErr: `failed to stat input path 'notexist': lstat notexist: no such file or directory`,
}, {
desc: `With file as input`,
cfg: &Config{
Input: []InputConfig{{
Path: "./README.md",
}},
},
exp: []InputConfig{{
Path: "README.md",
}},
}, {
desc: `With duplicate inputs`,
cfg: &Config{
Input: []InputConfig{{
Path: "./testdata/in/test.asset",
}, {
Path: "./testdata/in/test.asset",
}},
},
exp: []InputConfig{{
Path: "testdata/in/test.asset",
}},
}}
for _, test := range tests {
t.Log(test.desc)
err := test.cfg.validateInput()
if err != nil {
assert(t, test.expErr, err.Error(), true)
continue
}
assert(t, test.exp, test.cfg.Input, true)
}
}
func TestValidateOutput(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cases := []struct {
desc string
cfg *Config
expErr []string
expOutput string
}{{
desc: `With empty`,
cfg: &Config{
cwd: cwd,
},
expOutput: filepath.Join(cwd, DefOutputName),
}, {
desc: `With unwriteable directory`,
cfg: &Config{
Output: "/root/.ssh/template.go",
},
expErr: []string{
`create output directory: mkdir /root: read-only file system`,
`create output directory: mkdir /root/.ssh/: permission denied`,
},
}, {
desc: `With unwriteable file`,
cfg: &Config{
Output: "/template.go",
},
expErr: []string{
`open /template.go: permission denied`,
`open /template.go: read-only file system`,
},
}, {
desc: `With output as directory`,
cfg: &Config{
Output: "/tmp/",
},
expOutput: filepath.Join("/tmp", DefOutputName),
}}
test:
for _, c := range cases {
t.Log(c.desc)
err := c.cfg.validateOutput()
if err != nil {
for _, expErr := range c.expErr {
if expErr == err.Error() {
continue test
}
}
t.Fatalf("expecting one of error %q, got %q",
c.expErr, err.Error())
}
assert(t, c.expOutput, c.cfg.Output, true)
}
}