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 5 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
18 changes: 10 additions & 8 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...)
}
w.writes <- prefixedLines
func (w *prefixedWriter) Write(p []byte) (int, error) {
BolajiOlajide marked this conversation as resolved.
Show resolved Hide resolved
var prefixedLine []byte
// We remove new lines appended to the log output so all of the stream can have
// only one new line separating them. This ensures the previous behavior for the log
// structure is preserved.
p = bytes.TrimRight(p, "\n")
prefixedLine = append([]byte(w.prefix), p...)
prefixedLine = append(prefixedLine, newLineByteSlice...)
w.writes <- prefixedLine
BolajiOlajide marked this conversation as resolved.
Show resolved Hide resolved
<-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