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

batches: skip empty log output #923

Merged
merged 8 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions internal/batches/ui/interval_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
BolajiOlajide marked this conversation as resolved.
Show resolved Hide resolved
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
}
9 changes: 7 additions & 2 deletions internal/batches/ui/interval_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand Down