Skip to content

Commit 0daf4b2

Browse files
authored
return pipe error from Wait (#211)
1 parent 10e8421 commit 0daf4b2

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ Sinks are methods that return some data from a pipe, ending the pipeline and ext
333333
| [`Slice`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Slice) | | data as `[]string`, error |
334334
| [`Stdout`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Stdout) | standard output | bytes written, error |
335335
| [`String`](https://pkg.go.dev/github.com/bitfield/script#Pipe.String) | | data as `string`, error |
336-
| [`Wait`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Wait) | | none |
336+
| [`Wait`](https://pkg.go.dev/github.com/bitfield/script#Pipe.Wait) | | error |
337337
| [`WriteFile`](https://pkg.go.dev/github.com/bitfield/script#Pipe.WriteFile) | specified file, truncating if it exists | bytes written, error |
338338

339339
# What's new

Diff for: script.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ func (p *Pipe) EncodeBase64() *Pipe {
374374
}
375375

376376
// Error returns any error present on the pipe, or nil otherwise.
377+
// Error is not a sink and does not wait until the pipe reaches
378+
// completion. To wait for completion before returning the error,
379+
// see [Pipe.Wait].
377380
func (p *Pipe) Error() error {
378381
if p.mu == nil { // uninitialised pipe
379382
return nil
@@ -874,14 +877,15 @@ func (p *Pipe) Tee(writers ...io.Writer) *Pipe {
874877
return p.WithReader(io.TeeReader(p.Reader, teeWriter))
875878
}
876879

877-
// Wait reads the pipe to completion and discards the result. This is mostly
878-
// useful for waiting until concurrent filters have completed (see
879-
// [Pipe.Filter]).
880-
func (p *Pipe) Wait() {
880+
// Wait reads the pipe to completion and returns any error present on
881+
// the pipe, or nil otherwise. This is mostly useful for waiting until
882+
// concurrent filters have completed (see [Pipe.Filter]).
883+
func (p *Pipe) Wait() error {
881884
_, err := io.Copy(io.Discard, p)
882885
if err != nil {
883886
p.SetError(err)
884887
}
888+
return p.Error()
885889
}
886890

887891
// WithError sets the error err on the pipe.

Diff for: script_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,22 @@ func TestReadReturnsErrorGivenReadErrorOnPipe(t *testing.T) {
18501850
}
18511851
}
18521852

1853+
func TestWait_ReturnsErrorPresentOnPipe(t *testing.T) {
1854+
t.Parallel()
1855+
p := script.Echo("a\nb\nc\n").ExecForEach("{{invalid template syntax}}")
1856+
if p.Wait() == nil {
1857+
t.Error("want error, got nil")
1858+
}
1859+
}
1860+
1861+
func TestWait_DoesNotReturnErrorForValidExecution(t *testing.T) {
1862+
t.Parallel()
1863+
p := script.Echo("a\nb\nc\n").ExecForEach("echo \"{{.}}\"")
1864+
if err := p.Wait(); err != nil {
1865+
t.Fatal(err)
1866+
}
1867+
}
1868+
18531869
var base64Cases = []struct {
18541870
name string
18551871
decoded string

0 commit comments

Comments
 (0)