File tree Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Original file line number Diff line number Diff 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.
184185func Slice (s []string ) * Pipe {
186+ if len (s ) == 0 {
187+ return NewPipe ()
188+ }
185189 return Echo (strings .Join (s , "\n " ) + "\n " )
186190}
187191
Original file line number Diff line number Diff 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+
13901402func TestStdoutReturnsErrorGivenReadErrorOnPipe (t * testing.T ) {
13911403 t .Parallel ()
13921404 brokenReader := iotest .ErrReader (errors .New ("oh no" ))
You can’t perform that action at this time.
0 commit comments