Skip to content

Commit 2b7f0a1

Browse files
mhaggerznull
authored andcommitted
pipeline_test.go: remove unnecessary tmpdirs and simplify test setup
Ported from version-2 branch commits: - 95dc2e8 pipeline_test.go: get rid of a bunch of unnecessary tmpdirs - 5fdc22a TestPipelineStdinThatIsNeverClosed(): create stdin more simply - c2c9802 pipeline_test.go: use WithStdoutCloser() to close stdout pipes Tests that don't run external commands (or whose commands don't need a specific working directory) don't need t.TempDir().
1 parent a654916 commit 2b7f0a1

1 file changed

Lines changed: 22 additions & 79 deletions

File tree

pipe/pipeline_test.go

Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ func TestMain(m *testing.M) {
2929
func TestPipelineFirstStageFailsToStart(t *testing.T) {
3030
t.Parallel()
3131
ctx := context.Background()
32-
33-
dir := t.TempDir()
34-
3532
startErr := errors.New("foo")
3633

37-
p := pipe.New(pipe.WithDir(dir))
34+
p := pipe.New()
3835
p.Add(
3936
ErrorStartingStage{startErr},
4037
ErrorStartingStage{errors.New("this error should never happen")},
@@ -45,12 +42,9 @@ func TestPipelineFirstStageFailsToStart(t *testing.T) {
4542
func TestPipelineSecondStageFailsToStart(t *testing.T) {
4643
t.Parallel()
4744
ctx := context.Background()
48-
49-
dir := t.TempDir()
50-
5145
startErr := errors.New("foo")
5246

53-
p := pipe.New(pipe.WithDir(dir))
47+
p := pipe.New()
5448
p.Add(
5549
seqFunction(20000),
5650
ErrorStartingStage{startErr},
@@ -61,10 +55,7 @@ func TestPipelineSecondStageFailsToStart(t *testing.T) {
6155
func TestPipelineSingleCommandOutput(t *testing.T) {
6256
t.Parallel()
6357
ctx := context.Background()
64-
65-
dir := t.TempDir()
66-
67-
p := pipe.New(pipe.WithDir(dir))
58+
p := pipe.New()
6859
p.Add(pipe.Command("echo", "hello world"))
6960
out, err := p.Output(ctx)
7061
if assert.NoError(t, err) {
@@ -75,12 +66,9 @@ func TestPipelineSingleCommandOutput(t *testing.T) {
7566
func TestPipelineSingleCommandWithStdout(t *testing.T) {
7667
t.Parallel()
7768
ctx := context.Background()
78-
79-
dir := t.TempDir()
80-
8169
stdout := &bytes.Buffer{}
8270

83-
p := pipe.New(pipe.WithDir(dir), pipe.WithStdout(stdout))
71+
p := pipe.New(pipe.WithStdout(stdout))
8472
p.Add(pipe.Command("echo", "hello world"))
8573
if assert.NoError(t, p.Run(ctx)) {
8674
assert.Equal(t, "hello world\n", stdout.String())
@@ -158,10 +146,7 @@ func TestPipelineStdinThatIsNeverClosed(t *testing.T) {
158146
func TestNontrivialPipeline(t *testing.T) {
159147
t.Parallel()
160148
ctx := context.Background()
161-
162-
dir := t.TempDir()
163-
164-
p := pipe.New(pipe.WithDir(dir))
149+
p := pipe.New()
165150
p.Add(
166151
pipe.Command("echo", "hello world"),
167152
pipe.Command("sed", "s/hello/goodbye/"),
@@ -210,9 +195,6 @@ func TestPipelineReadFromSlowly2(t *testing.T) {
210195
t.Parallel()
211196
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
212197
defer cancel()
213-
214-
dir := t.TempDir()
215-
216198
r, w := io.Pipe()
217199

218200
var buf []byte
@@ -236,7 +218,7 @@ func TestPipelineReadFromSlowly2(t *testing.T) {
236218
}
237219
}()
238220

239-
p := pipe.New(pipe.WithDir(dir), pipe.WithStdout(w))
221+
p := pipe.New(pipe.WithStdout(w))
240222
p.Add(pipe.Command("seq", "100"))
241223
assert.NoError(t, p.Run(ctx))
242224

@@ -252,10 +234,7 @@ func TestPipelineReadFromSlowly2(t *testing.T) {
252234
func TestPipelineTwoCommandsPiping(t *testing.T) {
253235
t.Parallel()
254236
ctx := context.Background()
255-
256-
dir := t.TempDir()
257-
258-
p := pipe.New(pipe.WithDir(dir))
237+
p := pipe.New()
259238
p.Add(pipe.Command("echo", "hello world"))
260239
assert.Panics(t, func() { p.Add(pipe.Command("")) })
261240
out, err := p.Output(ctx)
@@ -282,10 +261,7 @@ func TestPipelineDir(t *testing.T) {
282261
func TestPipelineExit(t *testing.T) {
283262
t.Parallel()
284263
ctx := context.Background()
285-
286-
dir := t.TempDir()
287-
288-
p := pipe.New(pipe.WithDir(dir))
264+
p := pipe.New()
289265
p.Add(
290266
pipe.Command("false"),
291267
pipe.Command("true"),
@@ -316,11 +292,10 @@ func TestPipelineInterrupted(t *testing.T) {
316292
}
317293

318294
t.Parallel()
319-
dir := t.TempDir()
320295

321296
stdout := &bytes.Buffer{}
322297

323-
p := pipe.New(pipe.WithDir(dir), pipe.WithStdout(stdout))
298+
p := pipe.New(pipe.WithStdout(stdout))
324299
p.Add(pipe.Command("sleep", "10"))
325300

326301
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
@@ -339,11 +314,10 @@ func TestPipelineCanceled(t *testing.T) {
339314
}
340315

341316
t.Parallel()
342-
dir := t.TempDir()
343317

344318
stdout := &bytes.Buffer{}
345319

346-
p := pipe.New(pipe.WithDir(dir), pipe.WithStdout(stdout))
320+
p := pipe.New(pipe.WithStdout(stdout))
347321
p.Add(pipe.Command("sleep", "10"))
348322

349323
ctx, cancel := context.WithCancel(context.Background())
@@ -367,9 +341,8 @@ func TestLittleEPIPE(t *testing.T) {
367341
}
368342

369343
t.Parallel()
370-
dir := t.TempDir()
371344

372-
p := pipe.New(pipe.WithDir(dir))
345+
p := pipe.New()
373346
p.Add(
374347
pipe.Command("sh", "-c", "sleep 1; echo foo"),
375348
pipe.Command("true"),
@@ -391,9 +364,8 @@ func TestBigEPIPE(t *testing.T) {
391364
}
392365

393366
t.Parallel()
394-
dir := t.TempDir()
395367

396-
p := pipe.New(pipe.WithDir(dir))
368+
p := pipe.New()
397369
p.Add(
398370
pipe.Command("seq", "100000"),
399371
pipe.Command("true"),
@@ -415,9 +387,8 @@ func TestIgnoredSIGPIPE(t *testing.T) {
415387
}
416388

417389
t.Parallel()
418-
dir := t.TempDir()
419390

420-
p := pipe.New(pipe.WithDir(dir))
391+
p := pipe.New()
421392
p.Add(
422393
pipe.IgnoreError(pipe.Command("seq", "100000"), pipe.IsSIGPIPE),
423394
pipe.Command("echo", "foo"),
@@ -433,11 +404,8 @@ func TestIgnoredSIGPIPE(t *testing.T) {
433404
func TestFunction(t *testing.T) {
434405
t.Parallel()
435406
ctx := context.Background()
436-
437-
dir := t.TempDir()
438-
439407
t.Run("successful function", func(t *testing.T) {
440-
p := pipe.New(pipe.WithDir(dir))
408+
p := pipe.New()
441409
p.Add(
442410
pipe.Print("hello world"),
443411
pipe.Function(
@@ -463,7 +431,6 @@ func TestFunction(t *testing.T) {
463431

464432
t.Run("panic with handler", func(t *testing.T) {
465433
p := pipe.New(
466-
pipe.WithDir(dir),
467434
pipe.WithStagePanicHandler(func(p any) error {
468435
err := fmt.Errorf("panic handled: %v", p)
469436
return err
@@ -488,10 +455,7 @@ func TestFunction(t *testing.T) {
488455
func TestPipelineWithFunction(t *testing.T) {
489456
t.Parallel()
490457
ctx := context.Background()
491-
492-
dir := t.TempDir()
493-
494-
p := pipe.New(pipe.WithDir(dir))
458+
p := pipe.New()
495459
p.Add(
496460
pipe.Command("echo", "-n", "hello world"),
497461
pipe.Function(
@@ -552,10 +516,7 @@ func seqFunction(n int) pipe.Stage {
552516
func TestPipelineWithLinewiseFunction(t *testing.T) {
553517
t.Parallel()
554518
ctx := context.Background()
555-
556-
dir := t.TempDir()
557-
558-
p := pipe.New(pipe.WithDir(dir))
519+
p := pipe.New()
559520
// Print the numbers from 1 to 20 (generated from scratch):
560521
p.Add(
561522
seqFunction(20),
@@ -694,10 +655,7 @@ func TestScannerFinishEarly(t *testing.T) {
694655
func TestPrintln(t *testing.T) {
695656
t.Parallel()
696657
ctx := context.Background()
697-
698-
dir := t.TempDir()
699-
700-
p := pipe.New(pipe.WithDir(dir))
658+
p := pipe.New()
701659
p.Add(pipe.Println("Look Ma, no hands!"))
702660
out, err := p.Output(ctx)
703661
if assert.NoError(t, err) {
@@ -708,10 +666,7 @@ func TestPrintln(t *testing.T) {
708666
func TestPrintf(t *testing.T) {
709667
t.Parallel()
710668
ctx := context.Background()
711-
712-
dir := t.TempDir()
713-
714-
p := pipe.New(pipe.WithDir(dir))
669+
p := pipe.New()
715670
p.Add(pipe.Printf("Strangely recursive: %T", p))
716671
out, err := p.Output(ctx)
717672
if assert.NoError(t, err) {
@@ -903,11 +858,8 @@ func TestErrors(t *testing.T) {
903858

904859
func BenchmarkSingleProgram(b *testing.B) {
905860
ctx := context.Background()
906-
907-
dir := b.TempDir()
908-
909861
for i := 0; i < b.N; i++ {
910-
p := pipe.New(pipe.WithDir(dir))
862+
p := pipe.New()
911863
p.Add(
912864
pipe.Command("true"),
913865
)
@@ -917,11 +869,8 @@ func BenchmarkSingleProgram(b *testing.B) {
917869

918870
func BenchmarkTenPrograms(b *testing.B) {
919871
ctx := context.Background()
920-
921-
dir := b.TempDir()
922-
923872
for i := 0; i < b.N; i++ {
924-
p := pipe.New(pipe.WithDir(dir))
873+
p := pipe.New()
925874
p.Add(
926875
pipe.Command("echo", "hello world"),
927876
pipe.Command("cat"),
@@ -943,16 +892,13 @@ func BenchmarkTenPrograms(b *testing.B) {
943892

944893
func BenchmarkTenFunctions(b *testing.B) {
945894
ctx := context.Background()
946-
947-
dir := b.TempDir()
948-
949895
cp := func(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error {
950896
_, err := io.Copy(stdout, stdin)
951897
return err
952898
}
953899

954900
for i := 0; i < b.N; i++ {
955-
p := pipe.New(pipe.WithDir(dir))
901+
p := pipe.New()
956902
p.Add(
957903
pipe.Println("hello world"),
958904
pipe.Function("copy1", cp),
@@ -974,16 +920,13 @@ func BenchmarkTenFunctions(b *testing.B) {
974920

975921
func BenchmarkTenMixedStages(b *testing.B) {
976922
ctx := context.Background()
977-
978-
dir := b.TempDir()
979-
980923
cp := func(_ context.Context, _ pipe.Env, stdin io.Reader, stdout io.Writer) error {
981924
_, err := io.Copy(stdout, stdin)
982925
return err
983926
}
984927

985928
for i := 0; i < b.N; i++ {
986-
p := pipe.New(pipe.WithDir(dir))
929+
p := pipe.New()
987930
p.Add(
988931
pipe.Command("echo", "hello world"),
989932
pipe.Function("copy1", cp),

0 commit comments

Comments
 (0)