Skip to content

Commit

Permalink
Slice now produces empty pipe for empty slice (fixes #110) (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahadzaryab1 authored Oct 20, 2024
1 parent 6fff62a commit dd8d444
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion script.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@ func Post(url string) *Pipe {
return NewPipe().Post(url)
}

// Slice creates a pipe containing each element of s, one per line.
// Slice creates a pipe containing each element of s, one per line. If s is
// empty or nil, then the pipe is empty.
func Slice(s []string) *Pipe {
if len(s) == 0 {
return NewPipe()
}
return Echo(strings.Join(s, "\n") + "\n")
}

Expand Down
12 changes: 12 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,18 @@ func TestSliceProducesElementsOfSpecifiedSliceOnePerLine(t *testing.T) {
}
}

func TestSliceGivenEmptySliceProducesEmptyPipe(t *testing.T) {
t.Parallel()
want := ""
got, err := script.Slice([]string{}).String()
if err != nil {
t.Fatal(err)
}
if want != got {
t.Fatalf("want %q, got %q", want, got)
}
}

func TestStdoutReturnsErrorGivenReadErrorOnPipe(t *testing.T) {
t.Parallel()
brokenReader := iotest.ErrReader(errors.New("oh no"))
Expand Down

0 comments on commit dd8d444

Please sign in to comment.