From 4d1a9910ec34d4c164bde8b663e44b5fd63b32a4 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sun, 18 Aug 2024 09:37:04 -0400 Subject: [PATCH 1/8] Add WaitError Method --- script.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/script.go b/script.go index c471f74..4bf147c 100644 --- a/script.go +++ b/script.go @@ -858,6 +858,14 @@ func (p *Pipe) Wait() { } } +// WaitError reads the pipe to completion and returns any error present on +// the pipe, or nil otherwise. This is mostly useful for waiting until +// concurrent filters have completed (see [Pipe.Filter]). +func (p *Pipe) WaitError() error { + p.Wait() + return p.Error() +} + // WithError sets the error err on the pipe. func (p *Pipe) WithError(err error) *Pipe { p.SetError(err) From 4c1396a03035c2c32bb4d9828c60d6b4b0a96643 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sun, 18 Aug 2024 09:37:20 -0400 Subject: [PATCH 2/8] Add Tests For New Method --- script_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/script_test.go b/script_test.go index b19b915..557b34a 100644 --- a/script_test.go +++ b/script_test.go @@ -1850,6 +1850,22 @@ func TestReadReturnsErrorGivenReadErrorOnPipe(t *testing.T) { } } +func TestWaitErrorForErrorOnPipe(t *testing.T) { + t.Parallel() + p := script.Echo("a\nb\nc\n").ExecForEach("{{invalid template syntax}}") + if p.WaitError() == nil { + t.Error("want error, got nil") + } +} + +func TestWaitErrorForNoErrorOnPipe(t *testing.T) { + t.Parallel() + p := script.Echo("a\nb\nc\n").ExecForEach("echo \"{{.}}\"") + if err := p.WaitError(); err != nil { + t.Fatal(err) + } +} + func ExampleArgs() { script.Args().Stdout() // prints command-line arguments From 57d9373d457677921f780085f843615f989bd3a8 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sun, 18 Aug 2024 09:39:17 -0400 Subject: [PATCH 3/8] Update Godoc For Error --- script.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/script.go b/script.go index 4bf147c..dde101c 100644 --- a/script.go +++ b/script.go @@ -348,6 +348,9 @@ func (p *Pipe) Echo(s string) *Pipe { } // Error returns any error present on the pipe, or nil otherwise. +// Error is not a sink and does not wait until the pipe reaches +// completion. To wait for completion before returning the error, +// see [Pipe.WaitError]. func (p *Pipe) Error() error { if p.mu == nil { // uninitialised pipe return nil From 8a276dbc6bc6b47434738bf178eb367a2d080c53 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sun, 18 Aug 2024 09:42:10 -0400 Subject: [PATCH 4/8] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 22a50ee..3b0ef31 100644 --- a/README.md +++ b/README.md @@ -331,6 +331,7 @@ Sinks are methods that return some data from a pipe, ending the pipeline and ext | [`Stdout`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Stdout) | standard output | bytes written, error | | [`String`](https://pkg.go.dev/github.com/bitfield/script#Pipe.String) | | data as `string`, error | | [`Wait`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Wait) | | none | +| [`WaitError`](https://pkg.go.dev/github.com/bitfield/script#Pipe.WaitError) | | error | | [`WriteFile`](https://pkg.go.dev/github.com/bitfield/script#Pipe.WriteFile) | specified file, truncating if it exists | bytes written, error | # What's new From 5354ed5d9dad558535d180fea7f00885a0117fbc Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 24 Aug 2024 09:25:20 -0400 Subject: [PATCH 5/8] Add Error To Wait Method Instead of New Method --- README.md | 3 +-- script.go | 16 +++++----------- script_test.go | 8 ++++---- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3b0ef31..9130c75 100644 --- a/README.md +++ b/README.md @@ -330,8 +330,7 @@ Sinks are methods that return some data from a pipe, ending the pipeline and ext | [`Slice`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Slice) | | data as `[]string`, error | | [`Stdout`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Stdout) | standard output | bytes written, error | | [`String`](https://pkg.go.dev/github.com/bitfield/script#Pipe.String) | | data as `string`, error | -| [`Wait`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Wait) | | none | -| [`WaitError`](https://pkg.go.dev/github.com/bitfield/script#Pipe.WaitError) | | error | +| [`Wait`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Wait) | | error | | [`WriteFile`](https://pkg.go.dev/github.com/bitfield/script#Pipe.WriteFile) | specified file, truncating if it exists | bytes written, error | # What's new diff --git a/script.go b/script.go index dde101c..6ca2bfb 100644 --- a/script.go +++ b/script.go @@ -350,7 +350,7 @@ func (p *Pipe) Echo(s string) *Pipe { // Error returns any error present on the pipe, or nil otherwise. // Error is not a sink and does not wait until the pipe reaches // completion. To wait for completion before returning the error, -// see [Pipe.WaitError]. +// see [Pipe.Wait]. func (p *Pipe) Error() error { if p.mu == nil { // uninitialised pipe return nil @@ -851,21 +851,15 @@ func (p *Pipe) Tee(writers ...io.Writer) *Pipe { return p.WithReader(io.TeeReader(p.Reader, teeWriter)) } -// Wait reads the pipe to completion and discards the result. This is mostly -// useful for waiting until concurrent filters have completed (see -// [Pipe.Filter]). -func (p *Pipe) Wait() { +// Wait reads the pipe to completion and returns any error present on +// the pipe, or nil otherwise. This is mostly useful for waiting until +// concurrent filters have completed (see [Pipe.Filter]). +func (p *Pipe) Wait() error { _, err := io.Copy(io.Discard, p) if err != nil { p.SetError(err) } -} -// WaitError reads the pipe to completion and returns any error present on -// the pipe, or nil otherwise. This is mostly useful for waiting until -// concurrent filters have completed (see [Pipe.Filter]). -func (p *Pipe) WaitError() error { - p.Wait() return p.Error() } diff --git a/script_test.go b/script_test.go index 557b34a..99c57ef 100644 --- a/script_test.go +++ b/script_test.go @@ -1850,18 +1850,18 @@ func TestReadReturnsErrorGivenReadErrorOnPipe(t *testing.T) { } } -func TestWaitErrorForErrorOnPipe(t *testing.T) { +func TestWaitForErrorOnPipe(t *testing.T) { t.Parallel() p := script.Echo("a\nb\nc\n").ExecForEach("{{invalid template syntax}}") - if p.WaitError() == nil { + if p.Wait() == nil { t.Error("want error, got nil") } } -func TestWaitErrorForNoErrorOnPipe(t *testing.T) { +func TestWaitForNoErrorOnPipe(t *testing.T) { t.Parallel() p := script.Echo("a\nb\nc\n").ExecForEach("echo \"{{.}}\"") - if err := p.WaitError(); err != nil { + if err := p.Wait(); err != nil { t.Fatal(err) } } From aff6d5727c7124072c5a0a4464ccf67f80462260 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Mon, 26 Aug 2024 21:17:51 -0400 Subject: [PATCH 6/8] Remove New Line --- script.go | 1 - 1 file changed, 1 deletion(-) diff --git a/script.go b/script.go index 6ca2bfb..c1d1219 100644 --- a/script.go +++ b/script.go @@ -859,7 +859,6 @@ func (p *Pipe) Wait() error { if err != nil { p.SetError(err) } - return p.Error() } From bcb2532e144e1c5c453c1382c3e7bfb7778b624f Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Mon, 26 Aug 2024 21:21:16 -0400 Subject: [PATCH 7/8] Fix Missing Bracket --- script_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/script_test.go b/script_test.go index 7c62626..f5e9445 100644 --- a/script_test.go +++ b/script_test.go @@ -1864,6 +1864,7 @@ func TestWaitForNoErrorOnPipe(t *testing.T) { if err := p.Wait(); err != nil { t.Fatal(err) } +} var base64Cases = []struct { name string From e9794bc711f5dbd35f99e8dd77f790b3c825f96d Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 27 Aug 2024 19:36:30 -0400 Subject: [PATCH 8/8] Rename Tests To Conform To gotestdox --- script_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script_test.go b/script_test.go index f5e9445..296869a 100644 --- a/script_test.go +++ b/script_test.go @@ -1850,7 +1850,7 @@ func TestReadReturnsErrorGivenReadErrorOnPipe(t *testing.T) { } } -func TestWaitForErrorOnPipe(t *testing.T) { +func TestWait_ReturnsErrorPresentOnPipe(t *testing.T) { t.Parallel() p := script.Echo("a\nb\nc\n").ExecForEach("{{invalid template syntax}}") if p.Wait() == nil { @@ -1858,7 +1858,7 @@ func TestWaitForErrorOnPipe(t *testing.T) { } } -func TestWaitForNoErrorOnPipe(t *testing.T) { +func TestWait_DoesNotReturnErrorForValidExecution(t *testing.T) { t.Parallel() p := script.Echo("a\nb\nc\n").ExecForEach("echo \"{{.}}\"") if err := p.Wait(); err != nil {