forked from bitfield/script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsources_test.go
More file actions
307 lines (292 loc) · 7.99 KB
/
sources_test.go
File metadata and controls
307 lines (292 loc) · 7.99 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package script
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestArgs(t *testing.T) {
t.Parallel()
// dummy test to prove coverage
Args()
// now the real test
cmd := exec.Command(os.Args[0], "hello", "world")
cmd.Env = append(os.Environ(), "SCRIPT_TEST=args")
got, err := cmd.Output()
if err != nil {
t.Error(err)
}
want := "hello\nworld\n"
if string(got) != want {
t.Errorf("want %q, got %q", want, string(got))
}
}
func TestEcho(t *testing.T) {
t.Parallel()
want := "Hello, world."
p := Echo(want)
got, err := p.String()
if err != nil {
t.Error(err)
}
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
func TestExec(t *testing.T) {
t.Parallel()
tcs := []struct {
Command string
ErrExpected bool
WantErrContain string
WantOutputContain string
}{
{
Command: "doesntexist",
ErrExpected: true,
WantErrContain: "file not found",
WantOutputContain: "",
},
{
Command: "go",
ErrExpected: true,
WantErrContain: "exit status 2",
WantOutputContain: "Usage",
},
{
Command: "go help",
ErrExpected: false,
WantErrContain: "",
WantOutputContain: "Usage",
},
{
Command: "sh -c 'echo hello'",
ErrExpected: false,
WantErrContain: "",
WantOutputContain: "hello\n",
},
{
Command: "sh -c 'echo oh no",
ErrExpected: true,
WantErrContain: "",
WantOutputContain: "",
},
{
Command: "sh -c 'sh -c \"echo inception\"'",
ErrExpected: false,
WantErrContain: "",
WantOutputContain: "inception\n",
},
}
for _, tc := range tcs {
t.Run(tc.Command, func(t *testing.T) {
p := Exec(tc.Command)
if tc.ErrExpected != (p.Error() != nil) {
t.Fatalf("unexpected error value: %v", p.Error())
}
if p.Error() != nil && !strings.Contains(p.Error().Error(), tc.WantErrContain) {
t.Fatalf("want error string %q to contain %q", p.Error().Error(), tc.WantErrContain)
}
p.SetError(nil) // else p.String() would be a no-op
output, err := p.String()
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if !strings.Contains(output, tc.WantOutputContain) {
t.Fatalf("want output %q to contain %q", output, tc.WantOutputContain)
}
})
}
}
func TestFile(t *testing.T) {
t.Parallel()
wantRaw, _ := ioutil.ReadFile("testdata/test.txt") // ignoring error
want := string(wantRaw)
p := File("testdata/test.txt")
gotRaw, err := ioutil.ReadAll(p.Reader)
if err != nil {
t.Error(err)
}
got := string(gotRaw)
if got != want {
t.Errorf("want %q, got %q", want, got)
}
q := File("doesntexist")
if q.Error() == nil {
t.Error("want error status on opening non-existent file, but got nil")
}
}
func TestFindFiles(t *testing.T) {
tcs := []struct {
Path string
ErrExpected bool
wantErrContain string
Want string
}{
{
Path: "testdata/multiple_files",
ErrExpected: false,
Want: "testdata/multiple_files/1.txt\ntestdata/multiple_files/2.txt\ntestdata/multiple_files/3.tar.zip\n",
},
{
Path: "testdata/multiple_files_with_subdirectory",
ErrExpected: false,
Want: "testdata/multiple_files_with_subdirectory/1.txt\ntestdata/multiple_files_with_subdirectory/2.txt\ntestdata/multiple_files_with_subdirectory/3.tar.zip\ntestdata/multiple_files_with_subdirectory/dir/.hidden\ntestdata/multiple_files_with_subdirectory/dir/1.txt\ntestdata/multiple_files_with_subdirectory/dir/2.txt\n",
},
{
Path: "noneexistentpath",
ErrExpected: true,
Want: "",
},
}
for _, tc := range tcs {
t.Run(tc.Path, func(t *testing.T) {
p := FindFiles(tc.Path)
if tc.ErrExpected != (p.Error() != nil) {
t.Fatalf("unexpected error value: %v", p.Error())
}
p.SetError(nil) // else p.String() would be a no-op
got, err := p.String()
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if !cmp.Equal(tc.Want, got) {
t.Fatalf("want %q, got %q", tc.Want, got)
}
})
}
}
func TestFindFilesGlob(t *testing.T) {
tcs := []struct {
Path string
ErrExpected bool
wantErrContain string
Want string
}{
{
Path: "testdata/multiple_files_*",
ErrExpected: false,
Want: "testdata/multiple_files_with_subdirectory/1.txt\ntestdata/multiple_files_with_subdirectory/2.txt\ntestdata/multiple_files_with_subdirectory/3.tar.zip\ntestdata/multiple_files_with_subdirectory/dir/.hidden\ntestdata/multiple_files_with_subdirectory/dir/1.txt\ntestdata/multiple_files_with_subdirectory/dir/2.txt\n",
},
{
Path: "testdata/*",
ErrExpected: false,
Want: "testdata/multiple_files/1.txt\ntestdata/multiple_files/2.txt\ntestdata/multiple_files/3.tar.zip\ntestdata/multiple_files_with_subdirectory/1.txt\ntestdata/multiple_files_with_subdirectory/2.txt\ntestdata/multiple_files_with_subdirectory/3.tar.zip\ntestdata/multiple_files_with_subdirectory/dir/.hidden\ntestdata/multiple_files_with_subdirectory/dir/1.txt\ntestdata/multiple_files_with_subdirectory/dir/2.txt\n",
},
{
Path: "testdata/*/di*",
ErrExpected: false,
Want: "testdata/multiple_files_with_subdirectory/dir/.hidden\ntestdata/multiple_files_with_subdirectory/dir/1.txt\ntestdata/multiple_files_with_subdirectory/dir/2.txt\n",
},
{
Path: "testdata/m*s",
ErrExpected: false,
Want: "testdata/multiple_files/1.txt\ntestdata/multiple_files/2.txt\ntestdata/multiple_files/3.tar.zip\n",
},
{
Path: "testdata/*/di*/.hidden",
ErrExpected: false,
Want: "\n",
},
}
for _, tc := range tcs {
t.Run(tc.Path, func(t *testing.T) {
p := FindFiles(tc.Path)
if tc.ErrExpected != (p.Error() != nil) {
t.Fatalf("unexpected error value: %v", p.Error())
}
p.SetError(nil) // else p.String() would be a no-op
got, err := p.String()
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if !cmp.Equal(tc.Want, got) {
t.Fatalf("want %q, got %q", tc.Want, got)
}
})
}
}
func TestIfExists(t *testing.T) {
t.Parallel()
p := IfExists("testdata/doesntexist")
if p.Error() == nil {
t.Errorf("want error from IfExists on non-existent file, but got nil")
}
p = IfExists("testdata/empty.txt")
if p.Error() != nil {
t.Errorf("want no error from IfExists on existing file, but got %v", p.Error())
}
}
func TestListFilesMultipleFiles(t *testing.T) {
t.Parallel()
dir := "testdata/multiple_files"
want := fmt.Sprintf("%s/1.txt\n%s/2.txt\n%s/3.tar.zip\n", dir, dir, dir)
got, err := ListFiles(dir).String()
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("Want %q, got %q", want, got)
}
}
func TestListFilesNonexistent(t *testing.T) {
t.Parallel()
p := ListFiles("nonexistentpath")
if p.Error() == nil {
t.Error("want error status on listing non-existent path, but got nil")
}
}
func TestListFilesSingle(t *testing.T) {
t.Parallel()
got, err := ListFiles("testdata/multiple_files/1.txt").String()
if err != nil {
t.Fatal(err)
}
want := "testdata/multiple_files/1.txt"
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func TestListFilesGlob(t *testing.T) {
t.Parallel()
dir := "testdata/multiple_files"
want := fmt.Sprintf("%s/1.txt\n%s/2.txt\n", dir, dir)
got, err := ListFiles("testdata/multi?le_files/*.txt").String()
if err != nil {
t.Fatal(err)
}
if want != got {
t.Errorf("Want %q, got %q", want, got)
}
}
func TestSlice(t *testing.T) {
t.Parallel()
want := "1\n2\n3\n"
got, err := Slice([]string{"1", "2", "3"}).String()
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func TestStdin(t *testing.T) {
t.Parallel()
// dummy test to prove coverage
Stdin()
// now the real test
want := "hello world"
cmd := exec.Command(os.Args[0])
cmd.Env = append(os.Environ(), "SCRIPT_TEST=stdin")
cmd.Stdin = Echo(want).Reader
got, err := cmd.Output()
if err != nil {
t.Error(err)
}
if string(got) != want {
t.Errorf("want %q, got %q", want, string(got))
}
}