diff --git a/script.go b/script.go index d3a643d..791de28 100644 --- a/script.go +++ b/script.go @@ -288,6 +288,9 @@ func ListFiles(path string) *Pipe { // Slice creates a pipe containing each element of the supplied slice of // strings, one per line. func Slice(s []string) *Pipe { + if len(s) == 0 { + return NewPipe() + } return Echo(strings.Join(s, "\n") + "\n") } @@ -545,9 +548,10 @@ func (p *Pipe) First(n int) *Pipe { // easier to read: // // 10 apple -// 4 banana -// 2 orange -// 1 kumquat +// +// 4 banana +// 2 orange +// 1 kumquat func (p *Pipe) Freq() *Pipe { freq := map[string]int{} type frequency struct { diff --git a/script_test.go b/script_test.go index a7e9430..5f24e6f 100644 --- a/script_test.go +++ b/script_test.go @@ -1126,6 +1126,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 !cmp.Equal(want, got) { + t.Error(cmp.Diff(want, got)) + } +} + func TestStdinReadsFromProgramStandardInput(t *testing.T) { t.Parallel() // dummy test to prove coverage