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

fix(slice): fix bug in slice that produces new line for empty input slice #216

Merged
merged 4 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func Post(url string) *Pipe {

// Slice creates a pipe containing each element of s, one per line.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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
Loading