Skip to content

Commit

Permalink
tests: add stdout and stderr streaming testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
fho committed Jan 11, 2024
1 parent 0d8a676 commit 8d4399a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/exec/command_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exec

import (
"bytes"
"context"
"fmt"
"testing"
Expand Down Expand Up @@ -71,3 +72,15 @@ func TestExecDoesNotFailIfLongLinesAreStreamed(t *testing.T) {
ExpectSuccess().Run(context.Background())
require.NoError(t, err)
}

func TestStderrStream(t *testing.T) {
ctx := context.Background()
buf := bytes.Buffer{}

_, err := Command("bash", "-c", "echo 'stderrHello' >&2").
LogPrefix("").
LogFn(func(f string, a ...any) { fmt.Fprintf(&buf, f, a...) }).ExpectSuccess().Run(ctx)
require.NoError(t, err)
t.Log(buf.String())
assert.Contains(t, buf.String(), "stderrHello\n")
}
15 changes: 15 additions & 0 deletions internal/exec/command_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exec

import (
"bytes"
"context"
"fmt"
"runtime"
Expand Down Expand Up @@ -58,3 +59,17 @@ func TestExpectSuccess(t *testing.T) {
require.Error(t, err)
assert.Nil(t, res)
}

func TestOutputStream(t *testing.T) {
ctx := context.Background()
const echoStr = "hello\nline2"

buf := bytes.Buffer{}

_, err := Command("bash", "-c",
fmt.Sprintf("echo '%s'", echoStr)).LogPrefix("").
LogFn(func(f string, a ...any) { fmt.Fprintf(&buf, f, a...) }).ExpectSuccess().Run(ctx)
require.NoError(t, err)
require.Contains(t, buf.String(), echoStr)

}

0 comments on commit 8d4399a

Please sign in to comment.