helper/stub: output to stdout
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
f8502c3ece
commit
5c82f1ed3e
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -55,8 +56,8 @@ func testHelper(t *testing.T, createHelper func(ctx context.Context, setOutput f
|
|||||||
|
|
||||||
t.Run("start helper with status channel and wait", func(t *testing.T) {
|
t.Run("start helper with status channel and wait", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
stdout, stderr := new(strings.Builder), new(strings.Builder)
|
stdout := new(strings.Builder)
|
||||||
h := createHelper(ctx, func(stdoutP, stderrP *io.Writer) { *stdoutP, *stderrP = stdout, stderr }, true)
|
h := createHelper(ctx, func(stdoutP, stderrP *io.Writer) { *stdoutP, *stderrP = stdout, os.Stderr }, true)
|
||||||
|
|
||||||
t.Run("wait not yet started helper", func(t *testing.T) {
|
t.Run("wait not yet started helper", func(t *testing.T) {
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -88,8 +89,8 @@ func testHelper(t *testing.T, createHelper func(ctx context.Context, setOutput f
|
|||||||
|
|
||||||
t.Log("waiting on helper")
|
t.Log("waiting on helper")
|
||||||
if err := h.Wait(); !errors.Is(err, context.Canceled) {
|
if err := h.Wait(); !errors.Is(err, context.Canceled) {
|
||||||
t.Errorf("Wait() err = %v stderr = %s",
|
t.Errorf("Wait: error = %v",
|
||||||
err, stderr)
|
err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("wait already finalised helper", func(t *testing.T) {
|
t.Run("wait already finalised helper", func(t *testing.T) {
|
||||||
@ -101,8 +102,8 @@ func testHelper(t *testing.T, createHelper func(ctx context.Context, setOutput f
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if got := stderr.String(); got != wantPayload {
|
if got := trimStdout(stdout); got != wantPayload {
|
||||||
t.Errorf("Start: stderr = %v, want %v",
|
t.Errorf("Start: stdout = %q, want %q",
|
||||||
got, wantPayload)
|
got, wantPayload)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -110,23 +111,27 @@ func testHelper(t *testing.T, createHelper func(ctx context.Context, setOutput f
|
|||||||
t.Run("start helper and wait", func(t *testing.T) {
|
t.Run("start helper and wait", func(t *testing.T) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
stdout, stderr := new(strings.Builder), new(strings.Builder)
|
stdout := new(strings.Builder)
|
||||||
h := createHelper(ctx, func(stdoutP, stderrP *io.Writer) { *stdoutP, *stderrP = stdout, stderr }, false)
|
h := createHelper(ctx, func(stdoutP, stderrP *io.Writer) { *stdoutP, *stderrP = stdout, os.Stderr }, false)
|
||||||
|
|
||||||
if err := h.Start(); err != nil {
|
if err := h.Start(); err != nil {
|
||||||
t.Errorf("Start() error = %v",
|
t.Errorf("Start: error = %v",
|
||||||
err)
|
err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.Wait(); err != nil {
|
if err := h.Wait(); err != nil {
|
||||||
t.Errorf("Wait() err = %v stdout = %s stderr = %s",
|
t.Errorf("Wait: error = %v stdout = %q",
|
||||||
err, stdout, stderr)
|
err, stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
if got := stderr.String(); got != wantPayload {
|
if got := trimStdout(stdout); got != wantPayload {
|
||||||
t.Errorf("Start() stderr = %v, want %v",
|
t.Errorf("Start: stdout = %q, want %q",
|
||||||
got, wantPayload)
|
got, wantPayload)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func trimStdout(stdout fmt.Stringer) string {
|
||||||
|
return strings.TrimPrefix(stdout.String(), "=== RUN TestHelperInit\n")
|
||||||
|
}
|
||||||
|
@ -63,7 +63,7 @@ func flagRestoreFiles(offset int, ap, sp string) (argsFile, statFile *os.File) {
|
|||||||
func genericStub(argsFile, statFile *os.File) {
|
func genericStub(argsFile, statFile *os.File) {
|
||||||
if argsFile != nil {
|
if argsFile != nil {
|
||||||
// this output is checked by parent
|
// this output is checked by parent
|
||||||
if _, err := io.Copy(os.Stderr, argsFile); err != nil {
|
if _, err := io.Copy(os.Stdout, argsFile); err != nil {
|
||||||
panic("cannot read args: " + err.Error())
|
panic("cannot read args: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user