Skip to content

Commit c84c80a

Browse files
batches: skip empty log output (#923)
1 parent 90bd461 commit c84c80a

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ All notable changes to `src-cli` are documented in this file.
2424

2525
- Fix network timeout in `src users clean` occuring in instances with many users [#901](https://github.com/sourcegraph/src-cli/pull/901)
2626
- Aligned parsing of spec file parameter of `src batch repos` with other commands. [#919](https://github.com/sourcegraph/src-cli/pull/919)
27+
- batches: Remove empty log outputs. [#923](https://github.com/sourcegraph/src-cli/pull/923)
2728

2829
### Removed
2930

internal/batches/ui/interval_writer.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,17 @@ type prefixedWriter struct {
116116
prefix string
117117
}
118118

119-
func (w *prefixedWriter) Write(p []byte) (int, error) {
120-
var prefixedLines []byte
121-
for _, line := range bytes.Split(p, []byte("\n")) {
122-
prefixedLine := append([]byte(w.prefix), line...)
123-
prefixedLine = append(prefixedLine, []byte("\n")...)
119+
var newLineByteSlice = []byte("\n")
124120

125-
prefixedLines = append(prefixedLines, prefixedLine...)
121+
// Write is only ever called with a single line. That line may or may not end with a newline character.
122+
// It then writes the content as a single line to the inner writer, regardless of if the provided line
123+
// had a newline or not. That is, because our encoding requires exactly one line per formatted line.
124+
func (w *prefixedWriter) Write(p []byte) (int, error) {
125+
prefixedLine := append([]byte(w.prefix), p...)
126+
if !bytes.HasSuffix(prefixedLine, newLineByteSlice) {
127+
prefixedLine = append(prefixedLine, newLineByteSlice...)
126128
}
127-
w.writes <- prefixedLines
129+
w.writes <- prefixedLine
128130
<-w.writesDone
129131
return len(p), nil
130132
}

internal/batches/ui/interval_writer_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func TestIntervalWriter(t *testing.T) {
5050
stderrWriter.Write([]byte("4"))
5151
stdoutWriter.Write([]byte("5"))
5252
stderrWriter.Write([]byte("5"))
53+
stdoutWriter.Write([]byte(`Hello world: 1
54+
`))
55+
stderrWriter.Write([]byte(`Hello world: 1
56+
`))
5357

5458
select {
5559
case <-ch:
@@ -65,10 +69,11 @@ func TestIntervalWriter(t *testing.T) {
6569
want := "stdout: 2\nstderr: 2\n" +
6670
"stdout: 3\nstderr: 3\n" +
6771
"stdout: 4\nstderr: 4\n" +
68-
"stdout: 5\nstderr: 5\n"
72+
"stdout: 5\nstderr: 5\n" +
73+
"stdout: Hello world: 1\nstderr: Hello world: 1\n"
6974

7075
if d != want {
71-
t.Fatalf("wrong data in sink. want")
76+
t.Fatalf("wrong data in sink. want=%q, have=%q", want, d)
7277
}
7378
case <-time.After(1 * time.Second):
7479
t.Fatalf("ch has NO data")

0 commit comments

Comments
 (0)