Skip to content

Commit

Permalink
Fix stderr writing and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoog committed Jul 2, 2020
1 parent 36abb3c commit dedbab5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
30 changes: 30 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,33 @@ func testExecerFail(ctx context.Context, t *testing.T, execer Execer) {
assert.True(t, "exit code is nonzero", code.Code != 0)
assert.Error(t, "wait for process to error", err)
}

func TestStderrVsStdout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()

var (
stdout bytes.Buffer
stderr bytes.Buffer
)

ws, server := mockConn(ctx, t)
defer server.Close()

execer := RemoteExecer(ws)
process, err := execer.Start(ctx, Command{
Command: "sh",
Args: []string{"-c", "echo stdout-message; echo 1>&2 stderr-message"},
Stdin: false,
})
assert.Success(t, "start command", err)

go io.Copy(&stdout, process.Stdout())
go io.Copy(&stderr, process.Stderr())

err = process.Wait()
assert.Success(t, "wait for process to complete", err)

assert.Equal(t, "stdout", "stdout-message", strings.TrimSpace(stdout.String()))
assert.Equal(t, "stderr", "stderr-message", strings.TrimSpace(stderr.String()))
}
8 changes: 3 additions & 5 deletions localexec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,18 @@ func TestStdoutVsStderr(t *testing.T) {
)
process, err := execer.Start(ctx, Command{
Command: "sh",
Args: []string{"-c", "echo stdout-message; echo 1>&2 stderr-message"},
Stdin: false,
TTY: false,
Args: []string{"-c", "echo stdout-message; echo 1>&2 stderr-message"},
Stdin: false,
TTY: false,
})
assert.Success(t, "start command", err)

go io.Copy(&stdout, process.Stdout())
go io.Copy(&stderr, process.Stderr())

time.Sleep(time.Second)
err = process.Wait()
assert.Success(t, "wait for process to complete", err)

assert.Equal(t, "stdout", "stdout-message", strings.TrimSpace(stdout.String()))
assert.Equal(t, "stderr", "stderr-message", strings.TrimSpace(stderr.String()))
}

2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Serve(ctx context.Context, c *websocket.Conn, execer Execer) error {
return copyWithHeader(process.Stdout(), wsNetConn, proto.Header{Type: proto.TypeStdout})
})
outputgroup.Go(func() error {
return copyWithHeader(process.Stderr(), wsNetConn, proto.Header{Type: proto.TypeStdout})
return copyWithHeader(process.Stderr(), wsNetConn, proto.Header{Type: proto.TypeStderr})
})

go func() {
Expand Down

0 comments on commit dedbab5

Please sign in to comment.