Skip to content

Commit dd8d444

Browse files
authored
Slice now produces empty pipe for empty slice (fixes #110) (#216)
1 parent 6fff62a commit dd8d444

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Diff for: script.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,12 @@ func Post(url string) *Pipe {
180180
return NewPipe().Post(url)
181181
}
182182

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

Diff for: script_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,18 @@ func TestSliceProducesElementsOfSpecifiedSliceOnePerLine(t *testing.T) {
13871387
}
13881388
}
13891389

1390+
func TestSliceGivenEmptySliceProducesEmptyPipe(t *testing.T) {
1391+
t.Parallel()
1392+
want := ""
1393+
got, err := script.Slice([]string{}).String()
1394+
if err != nil {
1395+
t.Fatal(err)
1396+
}
1397+
if want != got {
1398+
t.Fatalf("want %q, got %q", want, got)
1399+
}
1400+
}
1401+
13901402
func TestStdoutReturnsErrorGivenReadErrorOnPipe(t *testing.T) {
13911403
t.Parallel()
13921404
brokenReader := iotest.ErrReader(errors.New("oh no"))

0 commit comments

Comments
 (0)