Skip to content
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
26 changes: 26 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"encoding/binary"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -778,6 +779,31 @@ func (t *T) Log(args ...any) {
}
}

// Output returns a Writer that writes to the same test output stream as T.Log.
// The output is indented like T.Log lines, but Output does not
// add source locations or newlines. The output is internally line
// buffered, and a call to T.Log or the end of the test will implicitly
// flush the buffer, followed by a newline. After a test function and all its
// parents return, neither Output nor the Write method may be called.
//
// Only available on Go >= 1.25
func (t *T) Output() io.Writer {
t.Helper()

if t.rawLog != nil {
return t.rawLog.Writer()
} else if t.tbLog {
if tout, ok := t.tb.(interface{ Output() io.Writer }); ok {
return tout.Output()
} else {
t.Fatal("[rapid] Output requires Go 1.25 or newer")
return nil
}
} else {
return io.Discard
}
}

// Skipf is equivalent to [T.Logf] followed by [T.SkipNow].
func (t *T) Skipf(format string, args ...any) {
if t.tbLog {
Expand Down
15 changes: 15 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package rapid

import (
"bytes"
"context"
"errors"
"reflect"
Expand Down Expand Up @@ -251,6 +252,20 @@ func TestCheckCleanupContextCreatedInCleanup(t *testing.T) {
})
}

func TestOutputRawLog(t *testing.T) {
t.Parallel()

msg := []byte("Hello World")

out := captureTestOutput(t, func(t *T) {
t.Output().Write(msg)
}, nil)

if !bytes.Contains(out, msg) {
t.Errorf("expected output to contain %q, got: %q", msg, out)
}
}

// ignoreErrorsTB is a TB that ignores all errors posted to it.
type ignoreErrorsTB struct{ TB }

Expand Down
Loading