1
- package script
1
+ package script_test
2
2
3
3
import (
4
4
"bytes"
7
7
"regexp"
8
8
"strings"
9
9
"testing"
10
+
11
+ "github.com/bitfield/script"
10
12
)
11
13
12
14
func TestBasename (t * testing.T ) {
@@ -26,7 +28,7 @@ func TestBasename(t *testing.T) {
26
28
{"C:/Program Files/" , "Program Files\n " },
27
29
}
28
30
for _ , tc := range testCases {
29
- got , err := Echo (tc .testFileName ).Basename ().String ()
31
+ got , err := script . Echo (tc .testFileName ).Basename ().String ()
30
32
if err != nil {
31
33
t .Error (err )
32
34
}
@@ -42,7 +44,7 @@ func TestColumn(t *testing.T) {
42
44
if err != nil {
43
45
t .Fatal (err )
44
46
}
45
- got , err := File ("testdata/column.input.txt" ).Column (3 ).Bytes ()
47
+ got , err := script . File ("testdata/column.input.txt" ).Column (3 ).Bytes ()
46
48
if err != nil {
47
49
t .Error (err )
48
50
}
@@ -57,7 +59,7 @@ func TestConcat(t *testing.T) {
57
59
if err != nil {
58
60
t .Fatal (err )
59
61
}
60
- got , err := Echo ("testdata/test.txt\n testdata/doesntexist.txt\n testdata/hello.txt" ).Concat ().Bytes ()
62
+ got , err := script . Echo ("testdata/test.txt\n testdata/doesntexist.txt\n testdata/hello.txt" ).Concat ().Bytes ()
61
63
if err != nil {
62
64
t .Fatal (err )
63
65
}
@@ -97,7 +99,7 @@ func TestDirname(t *testing.T) {
97
99
// {"C:\\Program Files\\PHP\\", "C:\\Program Files\n"},
98
100
}
99
101
for _ , tc := range testCases {
100
- got , err := Echo (tc .testFileName ).Dirname ().String ()
102
+ got , err := script . Echo (tc .testFileName ).Dirname ().String ()
101
103
if err != nil {
102
104
t .Error (err )
103
105
}
@@ -109,7 +111,7 @@ func TestDirname(t *testing.T) {
109
111
110
112
func TestEachLine (t * testing.T ) {
111
113
t .Parallel ()
112
- p := Echo ("Hello\n Goodbye" )
114
+ p := script . Echo ("Hello\n Goodbye" )
113
115
q := p .EachLine (func (line string , out * strings.Builder ) {
114
116
out .WriteString (line + " world\n " )
115
117
})
@@ -126,7 +128,7 @@ func TestEachLine(t *testing.T) {
126
128
func TestExecFilter (t * testing.T ) {
127
129
t .Parallel ()
128
130
want := "hello world"
129
- p := File ("testdata/hello.txt" )
131
+ p := script . File ("testdata/hello.txt" )
130
132
q := p .Exec ("cat" )
131
133
got , err := q .String ()
132
134
if err != nil {
@@ -140,7 +142,7 @@ func TestExecFilter(t *testing.T) {
140
142
if err == nil {
141
143
t .Error ("input not closed after reading" )
142
144
}
143
- p = Echo ("hello world" )
145
+ p = script . Echo ("hello world" )
144
146
p .SetError (errors .New ("oh no" ))
145
147
// This should be a no-op because the pipe has error status.
146
148
out , _ := p .Exec ("cat" ).String ()
@@ -184,7 +186,7 @@ func TestExecForEach(t *testing.T) {
184
186
}
185
187
for _ , tc := range tcs {
186
188
t .Run (tc .Command , func (t * testing.T ) {
187
- p := Slice (tc .Input ).ExecForEach (tc .Command )
189
+ p := script . Slice (tc .Input ).ExecForEach (tc .Command )
188
190
if tc .ErrExpected != (p .Error () != nil ) {
189
191
t .Fatalf ("unexpected error value: %v" , p .Error ())
190
192
}
@@ -206,7 +208,7 @@ func TestFirst(t *testing.T) {
206
208
if err != nil {
207
209
t .Fatal (err )
208
210
}
209
- input := File ("testdata/first.input.txt" )
211
+ input := script . File ("testdata/first.input.txt" )
210
212
got , err := input .First (10 ).Bytes ()
211
213
if err != nil {
212
214
t .Error (err )
@@ -218,7 +220,7 @@ func TestFirst(t *testing.T) {
218
220
if err == nil {
219
221
t .Error ("input not closed after reading" )
220
222
}
221
- input = File ("testdata/first.input.txt" )
223
+ input = script . File ("testdata/first.input.txt" )
222
224
gotZero , err := input .First (0 ).CountLines ()
223
225
if err != nil {
224
226
t .Fatal (err )
@@ -230,11 +232,11 @@ func TestFirst(t *testing.T) {
230
232
if err == nil {
231
233
t .Error ("input not closed after reading" )
232
234
}
233
- want , err = File ("testdata/first.input.txt" ).Bytes ()
235
+ want , err = script . File ("testdata/first.input.txt" ).Bytes ()
234
236
if err != nil {
235
237
t .Fatal (err )
236
238
}
237
- got , err = File ("testdata/first.input.txt" ).First (100 ).Bytes ()
239
+ got , err = script . File ("testdata/first.input.txt" ).First (100 ).Bytes ()
238
240
if err != nil {
239
241
t .Fatal (err )
240
242
}
@@ -249,7 +251,7 @@ func TestFreq(t *testing.T) {
249
251
if err != nil {
250
252
t .Fatal (err )
251
253
}
252
- got , err := File ("testdata/freq.input.txt" ).Freq ().Bytes ()
254
+ got , err := script . File ("testdata/freq.input.txt" ).Freq ().Bytes ()
253
255
if err != nil {
254
256
t .Error (err )
255
257
}
@@ -262,7 +264,7 @@ func TestJoin(t *testing.T) {
262
264
t .Parallel ()
263
265
input := "hello\n from\n the\n join\n test\n "
264
266
want := "hello from the join test\n "
265
- got , err := Echo (input ).Join ().String ()
267
+ got , err := script . Echo (input ).Join ().String ()
266
268
if err != nil {
267
269
t .Error (err )
268
270
}
@@ -271,7 +273,7 @@ func TestJoin(t *testing.T) {
271
273
}
272
274
input = "hello\n world"
273
275
want = "hello world"
274
- got , err = Echo (input ).Join ().String ()
276
+ got , err = script . Echo (input ).Join ().String ()
275
277
if err != nil {
276
278
t .Error (err )
277
279
}
@@ -286,7 +288,7 @@ func TestLast(t *testing.T) {
286
288
if err != nil {
287
289
t .Fatal (err )
288
290
}
289
- input := File ("testdata/first.input.txt" )
291
+ input := script . File ("testdata/first.input.txt" )
290
292
got , err := input .Last (10 ).Bytes ()
291
293
if err != nil {
292
294
t .Error (err )
@@ -298,7 +300,7 @@ func TestLast(t *testing.T) {
298
300
if err == nil {
299
301
t .Error ("input not closed after reading" )
300
302
}
301
- input = File ("testdata/first.input.txt" )
303
+ input = script . File ("testdata/first.input.txt" )
302
304
gotZero , err := input .Last (0 ).CountLines ()
303
305
if err != nil {
304
306
t .Fatal (err )
@@ -310,11 +312,11 @@ func TestLast(t *testing.T) {
310
312
if err == nil {
311
313
t .Error ("input not closed after reading" )
312
314
}
313
- want , err = File ("testdata/first.input.txt" ).Bytes ()
315
+ want , err = script . File ("testdata/first.input.txt" ).Bytes ()
314
316
if err != nil {
315
317
t .Fatal (err )
316
318
}
317
- got , err = File ("testdata/first.input.txt" ).Last (100 ).Bytes ()
319
+ got , err = script . File ("testdata/first.input.txt" ).Last (100 ).Bytes ()
318
320
if err != nil {
319
321
t .Fatal (err )
320
322
}
@@ -336,7 +338,7 @@ func TestMatch(t *testing.T) {
336
338
{"testdata/empty.txt" , "line" , 0 },
337
339
}
338
340
for _ , tc := range testCases {
339
- got , err := File (tc .testFileName ).Match (tc .match ).CountLines ()
341
+ got , err := script . File (tc .testFileName ).Match (tc .match ).CountLines ()
340
342
if err != nil {
341
343
t .Error (err )
342
344
}
@@ -359,7 +361,7 @@ func TestMatchRegexp(t *testing.T) {
359
361
{"testdata/empty.txt" , `bogus$` , 0 },
360
362
}
361
363
for _ , tc := range testCases {
362
- got , err := File (tc .testFileName ).MatchRegexp (regexp .MustCompile (tc .match )).CountLines ()
364
+ got , err := script . File (tc .testFileName ).MatchRegexp (regexp .MustCompile (tc .match )).CountLines ()
363
365
if err != nil {
364
366
t .Error (err )
365
367
}
@@ -383,7 +385,7 @@ func TestReplace(t *testing.T) {
383
385
{"testdata/hello.txt" , "hello" , "Ж9" , "Ж9 world\n " },
384
386
}
385
387
for _ , tc := range testCases {
386
- got , err := File (tc .testFileName ).Replace (tc .search , tc .replace ).String ()
388
+ got , err := script . File (tc .testFileName ).Replace (tc .search , tc .replace ).String ()
387
389
if err != nil {
388
390
t .Error (err )
389
391
}
@@ -407,7 +409,7 @@ func TestReplaceRegexp(t *testing.T) {
407
409
{"testdata/hello.txt" , "hello{1}" , "Ж9" , "Ж9 world\n " },
408
410
}
409
411
for _ , tc := range testCases {
410
- got , err := File (tc .testFileName ).ReplaceRegexp (regexp .MustCompile (tc .regexp ), tc .replace ).String ()
412
+ got , err := script . File (tc .testFileName ).ReplaceRegexp (regexp .MustCompile (tc .regexp ), tc .replace ).String ()
411
413
if err != nil {
412
414
t .Error (err )
413
415
}
@@ -430,7 +432,7 @@ func TestReject(t *testing.T) {
430
432
{"testdata/empty.txt" , "line" , 0 },
431
433
}
432
434
for _ , tc := range testCases {
433
- got , err := File (tc .testFileName ).Reject (tc .reject ).CountLines ()
435
+ got , err := script . File (tc .testFileName ).Reject (tc .reject ).CountLines ()
434
436
if err != nil {
435
437
t .Error (err )
436
438
}
@@ -453,7 +455,7 @@ func TestRejectRegexp(t *testing.T) {
453
455
{"testdata/empty.txt" , "wontmatch" , 0 },
454
456
}
455
457
for _ , tc := range testCases {
456
- got , err := File (tc .testFileName ).RejectRegexp (regexp .MustCompile (tc .reject )).CountLines ()
458
+ got , err := script . File (tc .testFileName ).RejectRegexp (regexp .MustCompile (tc .reject )).CountLines ()
457
459
if err != nil {
458
460
t .Error (err )
459
461
}
@@ -475,12 +477,12 @@ func TestSHA256Sums(t *testing.T) {
475
477
{"testdata/multiple_files" , "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n " },
476
478
}
477
479
for _ , tc := range testCases {
478
- got , err := ListFiles (tc .testFileName ).SHA256Sums ().String ()
480
+ got , err := script . ListFiles (tc .testFileName ).SHA256Sums ().String ()
479
481
if err != nil {
480
482
t .Error (err )
481
483
}
482
484
if got != tc .want {
483
485
t .Errorf ("%q: want %q, got %q" , tc .testFileName , tc .want , got )
484
486
}
485
487
}
486
- }
488
+ }
0 commit comments