diff --git a/CHANGELOG.md b/CHANGELOG.md index 5811752909..ab983e8299 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ All notable changes to `src-cli` are documented in this file. - Fix network timeout in `src users clean` occuring in instances with many users [#901](https://github.com/sourcegraph/src-cli/pull/901) - Aligned parsing of spec file parameter of `src batch repos` with other commands. [#919](https://github.com/sourcegraph/src-cli/pull/919) +- batches: Remove empty log outputs. [#923](https://github.com/sourcegraph/src-cli/pull/923) ### Removed diff --git a/internal/batches/ui/interval_writer.go b/internal/batches/ui/interval_writer.go index 1cc285446f..75fafbe931 100644 --- a/internal/batches/ui/interval_writer.go +++ b/internal/batches/ui/interval_writer.go @@ -116,15 +116,17 @@ type prefixedWriter struct { prefix string } -func (w *prefixedWriter) Write(p []byte) (int, error) { - var prefixedLines []byte - for _, line := range bytes.Split(p, []byte("\n")) { - prefixedLine := append([]byte(w.prefix), line...) - prefixedLine = append(prefixedLine, []byte("\n")...) +var newLineByteSlice = []byte("\n") - prefixedLines = append(prefixedLines, prefixedLine...) +// Write is only ever called with a single line. That line may or may not end with a newline character. +// It then writes the content as a single line to the inner writer, regardless of if the provided line +// had a newline or not. That is, because our encoding requires exactly one line per formatted line. +func (w *prefixedWriter) Write(p []byte) (int, error) { + prefixedLine := append([]byte(w.prefix), p...) + if !bytes.HasSuffix(prefixedLine, newLineByteSlice) { + prefixedLine = append(prefixedLine, newLineByteSlice...) } - w.writes <- prefixedLines + w.writes <- prefixedLine <-w.writesDone return len(p), nil } diff --git a/internal/batches/ui/interval_writer_test.go b/internal/batches/ui/interval_writer_test.go index 1a56eb4288..043aaf4cc7 100644 --- a/internal/batches/ui/interval_writer_test.go +++ b/internal/batches/ui/interval_writer_test.go @@ -50,6 +50,10 @@ func TestIntervalWriter(t *testing.T) { stderrWriter.Write([]byte("4")) stdoutWriter.Write([]byte("5")) stderrWriter.Write([]byte("5")) + stdoutWriter.Write([]byte(`Hello world: 1 +`)) + stderrWriter.Write([]byte(`Hello world: 1 +`)) select { case <-ch: @@ -65,10 +69,11 @@ func TestIntervalWriter(t *testing.T) { want := "stdout: 2\nstderr: 2\n" + "stdout: 3\nstderr: 3\n" + "stdout: 4\nstderr: 4\n" + - "stdout: 5\nstderr: 5\n" + "stdout: 5\nstderr: 5\n" + + "stdout: Hello world: 1\nstderr: Hello world: 1\n" if d != want { - t.Fatalf("wrong data in sink. want") + t.Fatalf("wrong data in sink. want=%q, have=%q", want, d) } case <-time.After(1 * time.Second): t.Fatalf("ch has NO data")