Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: return error on pipe from Wait method after completion of pipe #211

Merged
merged 9 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,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 |
| [`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
Expand Down
12 changes: 8 additions & 4 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ func (p *Pipe) EncodeBase64() *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.Wait].
func (p *Pipe) Error() error {
if p.mu == nil { // uninitialised pipe
return nil
Expand Down Expand Up @@ -874,14 +877,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)
}
return p.Error()
}

// WithError sets the error err on the pipe.
Expand Down
16 changes: 16 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,22 @@ func TestReadReturnsErrorGivenReadErrorOnPipe(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 {
t.Error("want error, got nil")
}
}

func TestWait_DoesNotReturnErrorForValidExecution(t *testing.T) {
t.Parallel()
p := script.Echo("a\nb\nc\n").ExecForEach("echo \"{{.}}\"")
if err := p.Wait(); err != nil {
bitfield marked this conversation as resolved.
Show resolved Hide resolved
t.Fatal(err)
}
}

var base64Cases = []struct {
name string
decoded string
Expand Down
Loading