Skip to content

Commit 7ad252b

Browse files
committed
Make tests external
1 parent 9a33f38 commit 7ad252b

6 files changed

+94
-86
lines changed

filters_test.go

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package script
1+
package script_test
22

33
import (
44
"bytes"
@@ -7,6 +7,8 @@ import (
77
"regexp"
88
"strings"
99
"testing"
10+
11+
"github.com/bitfield/script"
1012
)
1113

1214
func TestBasename(t *testing.T) {
@@ -26,7 +28,7 @@ func TestBasename(t *testing.T) {
2628
{"C:/Program Files/", "Program Files\n"},
2729
}
2830
for _, tc := range testCases {
29-
got, err := Echo(tc.testFileName).Basename().String()
31+
got, err := script.Echo(tc.testFileName).Basename().String()
3032
if err != nil {
3133
t.Error(err)
3234
}
@@ -42,7 +44,7 @@ func TestColumn(t *testing.T) {
4244
if err != nil {
4345
t.Fatal(err)
4446
}
45-
got, err := File("testdata/column.input.txt").Column(3).Bytes()
47+
got, err := script.File("testdata/column.input.txt").Column(3).Bytes()
4648
if err != nil {
4749
t.Error(err)
4850
}
@@ -57,7 +59,7 @@ func TestConcat(t *testing.T) {
5759
if err != nil {
5860
t.Fatal(err)
5961
}
60-
got, err := Echo("testdata/test.txt\ntestdata/doesntexist.txt\ntestdata/hello.txt").Concat().Bytes()
62+
got, err := script.Echo("testdata/test.txt\ntestdata/doesntexist.txt\ntestdata/hello.txt").Concat().Bytes()
6163
if err != nil {
6264
t.Fatal(err)
6365
}
@@ -97,7 +99,7 @@ func TestDirname(t *testing.T) {
9799
// {"C:\\Program Files\\PHP\\", "C:\\Program Files\n"},
98100
}
99101
for _, tc := range testCases {
100-
got, err := Echo(tc.testFileName).Dirname().String()
102+
got, err := script.Echo(tc.testFileName).Dirname().String()
101103
if err != nil {
102104
t.Error(err)
103105
}
@@ -109,7 +111,7 @@ func TestDirname(t *testing.T) {
109111

110112
func TestEachLine(t *testing.T) {
111113
t.Parallel()
112-
p := Echo("Hello\nGoodbye")
114+
p := script.Echo("Hello\nGoodbye")
113115
q := p.EachLine(func(line string, out *strings.Builder) {
114116
out.WriteString(line + " world\n")
115117
})
@@ -126,7 +128,7 @@ func TestEachLine(t *testing.T) {
126128
func TestExecFilter(t *testing.T) {
127129
t.Parallel()
128130
want := "hello world"
129-
p := File("testdata/hello.txt")
131+
p := script.File("testdata/hello.txt")
130132
q := p.Exec("cat")
131133
got, err := q.String()
132134
if err != nil {
@@ -140,7 +142,7 @@ func TestExecFilter(t *testing.T) {
140142
if err == nil {
141143
t.Error("input not closed after reading")
142144
}
143-
p = Echo("hello world")
145+
p = script.Echo("hello world")
144146
p.SetError(errors.New("oh no"))
145147
// This should be a no-op because the pipe has error status.
146148
out, _ := p.Exec("cat").String()
@@ -184,7 +186,7 @@ func TestExecForEach(t *testing.T) {
184186
}
185187
for _, tc := range tcs {
186188
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)
188190
if tc.ErrExpected != (p.Error() != nil) {
189191
t.Fatalf("unexpected error value: %v", p.Error())
190192
}
@@ -206,7 +208,7 @@ func TestFirst(t *testing.T) {
206208
if err != nil {
207209
t.Fatal(err)
208210
}
209-
input := File("testdata/first.input.txt")
211+
input := script.File("testdata/first.input.txt")
210212
got, err := input.First(10).Bytes()
211213
if err != nil {
212214
t.Error(err)
@@ -218,7 +220,7 @@ func TestFirst(t *testing.T) {
218220
if err == nil {
219221
t.Error("input not closed after reading")
220222
}
221-
input = File("testdata/first.input.txt")
223+
input = script.File("testdata/first.input.txt")
222224
gotZero, err := input.First(0).CountLines()
223225
if err != nil {
224226
t.Fatal(err)
@@ -230,11 +232,11 @@ func TestFirst(t *testing.T) {
230232
if err == nil {
231233
t.Error("input not closed after reading")
232234
}
233-
want, err = File("testdata/first.input.txt").Bytes()
235+
want, err = script.File("testdata/first.input.txt").Bytes()
234236
if err != nil {
235237
t.Fatal(err)
236238
}
237-
got, err = File("testdata/first.input.txt").First(100).Bytes()
239+
got, err = script.File("testdata/first.input.txt").First(100).Bytes()
238240
if err != nil {
239241
t.Fatal(err)
240242
}
@@ -249,7 +251,7 @@ func TestFreq(t *testing.T) {
249251
if err != nil {
250252
t.Fatal(err)
251253
}
252-
got, err := File("testdata/freq.input.txt").Freq().Bytes()
254+
got, err := script.File("testdata/freq.input.txt").Freq().Bytes()
253255
if err != nil {
254256
t.Error(err)
255257
}
@@ -262,7 +264,7 @@ func TestJoin(t *testing.T) {
262264
t.Parallel()
263265
input := "hello\nfrom\nthe\njoin\ntest\n"
264266
want := "hello from the join test\n"
265-
got, err := Echo(input).Join().String()
267+
got, err := script.Echo(input).Join().String()
266268
if err != nil {
267269
t.Error(err)
268270
}
@@ -271,7 +273,7 @@ func TestJoin(t *testing.T) {
271273
}
272274
input = "hello\nworld"
273275
want = "hello world"
274-
got, err = Echo(input).Join().String()
276+
got, err = script.Echo(input).Join().String()
275277
if err != nil {
276278
t.Error(err)
277279
}
@@ -286,7 +288,7 @@ func TestLast(t *testing.T) {
286288
if err != nil {
287289
t.Fatal(err)
288290
}
289-
input := File("testdata/first.input.txt")
291+
input := script.File("testdata/first.input.txt")
290292
got, err := input.Last(10).Bytes()
291293
if err != nil {
292294
t.Error(err)
@@ -298,7 +300,7 @@ func TestLast(t *testing.T) {
298300
if err == nil {
299301
t.Error("input not closed after reading")
300302
}
301-
input = File("testdata/first.input.txt")
303+
input = script.File("testdata/first.input.txt")
302304
gotZero, err := input.Last(0).CountLines()
303305
if err != nil {
304306
t.Fatal(err)
@@ -310,11 +312,11 @@ func TestLast(t *testing.T) {
310312
if err == nil {
311313
t.Error("input not closed after reading")
312314
}
313-
want, err = File("testdata/first.input.txt").Bytes()
315+
want, err = script.File("testdata/first.input.txt").Bytes()
314316
if err != nil {
315317
t.Fatal(err)
316318
}
317-
got, err = File("testdata/first.input.txt").Last(100).Bytes()
319+
got, err = script.File("testdata/first.input.txt").Last(100).Bytes()
318320
if err != nil {
319321
t.Fatal(err)
320322
}
@@ -336,7 +338,7 @@ func TestMatch(t *testing.T) {
336338
{"testdata/empty.txt", "line", 0},
337339
}
338340
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()
340342
if err != nil {
341343
t.Error(err)
342344
}
@@ -359,7 +361,7 @@ func TestMatchRegexp(t *testing.T) {
359361
{"testdata/empty.txt", `bogus$`, 0},
360362
}
361363
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()
363365
if err != nil {
364366
t.Error(err)
365367
}
@@ -383,7 +385,7 @@ func TestReplace(t *testing.T) {
383385
{"testdata/hello.txt", "hello", "Ж9", "Ж9 world\n"},
384386
}
385387
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()
387389
if err != nil {
388390
t.Error(err)
389391
}
@@ -407,7 +409,7 @@ func TestReplaceRegexp(t *testing.T) {
407409
{"testdata/hello.txt", "hello{1}", "Ж9", "Ж9 world\n"},
408410
}
409411
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()
411413
if err != nil {
412414
t.Error(err)
413415
}
@@ -430,7 +432,7 @@ func TestReject(t *testing.T) {
430432
{"testdata/empty.txt", "line", 0},
431433
}
432434
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()
434436
if err != nil {
435437
t.Error(err)
436438
}
@@ -453,7 +455,7 @@ func TestRejectRegexp(t *testing.T) {
453455
{"testdata/empty.txt", "wontmatch", 0},
454456
}
455457
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()
457459
if err != nil {
458460
t.Error(err)
459461
}
@@ -475,12 +477,12 @@ func TestSHA256Sums(t *testing.T) {
475477
{"testdata/multiple_files", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"},
476478
}
477479
for _, tc := range testCases {
478-
got, err := ListFiles(tc.testFileName).SHA256Sums().String()
480+
got, err := script.ListFiles(tc.testFileName).SHA256Sums().String()
479481
if err != nil {
480482
t.Error(err)
481483
}
482484
if got != tc.want {
483485
t.Errorf("%q: want %q, got %q", tc.testFileName, tc.want, got)
484486
}
485487
}
486-
}
488+
}

main_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
package script
1+
package script_test
22

33
import (
44
"os"
55
"testing"
6+
7+
"github.com/bitfield/script"
68
)
79

810
func TestMain(m *testing.M) {
911
switch os.Getenv("SCRIPT_TEST") {
1012
case "args":
1113
// Print out command-line arguments
12-
Args().Stdout()
14+
script.Args().Stdout()
1315
case "stdin":
1416
// Echo input to output
15-
Stdin().Stdout()
17+
script.Stdin().Stdout()
1618
default:
1719
os.Exit(m.Run())
1820
}

pipes_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package script
1+
package script_test
22

33
import (
44
"bytes"
@@ -9,12 +9,14 @@ import (
99
"regexp"
1010
"strings"
1111
"testing"
12+
13+
"github.com/bitfield/script"
1214
)
1315

1416
func TestWithReader(t *testing.T) {
1517
t.Parallel()
1618
want := "Hello, world."
17-
p := NewPipe().WithReader(strings.NewReader(want))
19+
p := script.NewPipe().WithReader(strings.NewReader(want))
1820
got, err := p.String()
1921
if err != nil {
2022
t.Error(err)
@@ -28,7 +30,7 @@ func TestWithStdout(t *testing.T) {
2830
t.Parallel()
2931
buf := &bytes.Buffer{}
3032
want := "Hello, world."
31-
_, err := Echo(want).WithStdout(buf).Stdout()
33+
_, err := script.Echo(want).WithStdout(buf).Stdout()
3234
if err != nil {
3335
t.Fatal(err)
3436
}
@@ -40,7 +42,7 @@ func TestWithStdout(t *testing.T) {
4042

4143
func TestError(t *testing.T) {
4244
t.Parallel()
43-
p := File("testdata/nonexistent.txt")
45+
p := script.File("testdata/nonexistent.txt")
4446
if p.Error() == nil {
4547
t.Error("want error status reading nonexistent file, but got nil")
4648
}
@@ -80,22 +82,22 @@ func TestExitStatus(t *testing.T) {
8082
{"exit status 1 followed by junk", 0},
8183
}
8284
for _, tc := range tcs {
83-
p := NewPipe()
85+
p := script.NewPipe()
8486
p.SetError(fmt.Errorf(tc.input))
8587
got := p.ExitStatus()
8688
if got != tc.want {
8789
t.Errorf("input %q: want %d, got %d", tc.input, tc.want, got)
8890
}
8991
}
90-
got := NewPipe().ExitStatus()
92+
got := script.NewPipe().ExitStatus()
9193
if got != 0 {
9294
t.Errorf("want 0, got %d", got)
9395
}
9496
}
9597

9698
// doMethodsOnPipe calls every kind of method on the supplied pipe and
9799
// tries to trigger a panic.
98-
func doMethodsOnPipe(t *testing.T, p *Pipe, kind string) {
100+
func doMethodsOnPipe(t *testing.T, p *script.Pipe, kind string) {
99101
var action string
100102
defer func() {
101103
if r := recover(); r != nil {
@@ -154,8 +156,6 @@ func doMethodsOnPipe(t *testing.T, p *Pipe, kind string) {
154156
p.Slice()
155157
action = "Stdout()"
156158
p.Stdout()
157-
q := &Pipe{}
158-
q.Stdout()
159159
action = "String()"
160160
p.String()
161161
action = "WithError()"
@@ -173,17 +173,17 @@ func TestNilPipes(t *testing.T) {
173173

174174
func TestZeroPipes(t *testing.T) {
175175
t.Parallel()
176-
doMethodsOnPipe(t, &Pipe{}, "zero")
176+
doMethodsOnPipe(t, &script.Pipe{}, "zero")
177177
}
178178

179179
func TestNewPipes(t *testing.T) {
180180
t.Parallel()
181-
doMethodsOnPipe(t, NewPipe(), "new")
181+
doMethodsOnPipe(t, script.NewPipe(), "new")
182182
}
183183

184184
func TestPipeIsReader(t *testing.T) {
185185
t.Parallel()
186-
var p io.Reader = NewPipe()
186+
var p io.Reader = script.NewPipe()
187187
_, err := ioutil.ReadAll(p)
188188
if err != nil {
189189
t.Error(err)

read_auto_closer_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package script
1+
package script_test
22

33
import (
44
"bytes"
55
"io/ioutil"
66
"os"
77
"testing"
8+
9+
"github.com/bitfield/script"
810
)
911

1012
func TestReadAutoCloser(t *testing.T) {
@@ -22,7 +24,7 @@ func TestReadAutoCloser(t *testing.T) {
2224
if err != nil {
2325
t.Fatal(err)
2426
}
25-
acr := NewReadAutoCloser(input)
27+
acr := script.NewReadAutoCloser(input)
2628
got, err := ioutil.ReadAll(acr)
2729
if err != nil {
2830
t.Error(err)

0 commit comments

Comments
 (0)