Skip to content

Commit c4add1a

Browse files
committed
Send TAP output to any io.Writer
In some contexts a TAP test can't send output directly to stdout. Instead it should be sent to some other writer (HTTP stream, buffer, etc). Allow this usage without breaking existing users.
1 parent 8615712 commit c4add1a

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

util/tap/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TESTS = auto check known
1+
TESTS = auto check known writer
22
GOPATH = $(CURDIR)/gopath
33

44
.PHONY: $(TESTS)

util/tap/tap.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package tap // import "github.com/mndrix/tap-go"
2222

2323
import (
2424
"fmt"
25+
"io"
2526
"os"
2627
)
2728
import "testing/quick"
@@ -30,6 +31,9 @@ import "testing/quick"
3031
// output.
3132
type T struct {
3233
nextTestNumber int
34+
35+
// Writer indicates where TAP output should be sent. The default is os.Stdout.
36+
Writer io.Writer
3337
}
3438

3539
// New creates a new Tap value
@@ -39,8 +43,15 @@ func New() *T {
3943
}
4044
}
4145

46+
func (t *T) w() io.Writer {
47+
if t.Writer == nil {
48+
return os.Stdout
49+
}
50+
return t.Writer
51+
}
52+
4253
func (t *T) printf(format string, a ...interface{}) {
43-
fmt.Printf(format, a...)
54+
fmt.Fprintf(t.w(), format, a...)
4455
}
4556

4657
// Header displays a TAP header including version number and expected

util/tap/test/writer/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"os"
6+
7+
"github.com/mndrix/tap-go"
8+
)
9+
10+
func main() {
11+
buf := new(bytes.Buffer)
12+
t := tap.New()
13+
t.Writer = buf
14+
t.Header(2)
15+
t.Ok(true, "a test")
16+
t.Ok(buf.Len() > 0, "buffer has content")
17+
18+
buf.WriteTo(os.Stdout)
19+
}

0 commit comments

Comments
 (0)