diff options
author | Damian Gryski <[email protected]> | 2023-03-30 21:35:18 -0700 |
---|---|---|
committer | Ron Evans <[email protected]> | 2023-03-31 09:07:13 +0200 |
commit | 9182664845affa6715c39d38c14c396ab8f907c7 (patch) | |
tree | ff911d674b2a952a15ff9e8a261fd4e0199a2168 /src/testing | |
parent | a2f95d6b87418cc4a75892a43a6a405674aab876 (diff) | |
download | tinygo-9182664845affa6715c39d38c14c396ab8f907c7.tar.gz tinygo-9182664845affa6715c39d38c14c396ab8f907c7.zip |
testing: make test output unbuffered when verbose
Fixes #3579
Diffstat (limited to 'src/testing')
-rw-r--r-- | src/testing/testing.go | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go index 76ff7e694..26cd53eba 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -13,6 +13,7 @@ import ( "errors" "flag" "fmt" + "io" "io/fs" "os" "strings" @@ -49,7 +50,7 @@ func Init() { // common holds the elements common between T and B and // captures common methods such as Errorf. type common struct { - output bytes.Buffer + output *logger indent string ran bool // Test or benchmark (or one of its subtests) was executed. failed bool // Test or benchmark has failed. @@ -70,6 +71,27 @@ type common struct { tempDirSeq int32 } +type logger struct { + logToStdout bool + b bytes.Buffer +} + +func (l *logger) Write(p []byte) (int, error) { + if l.logToStdout { + return os.Stdout.Write(p) + } + return l.b.Write(p) +} + +func (l *logger) WriteTo(w io.Writer) (int64, error) { + if l.logToStdout { + // We've already been logging to stdout; nothing to do. + return 0, nil + } + return l.b.WriteTo(w) + +} + // Short reports whether the -test.short flag is set. func Short() bool { return flagShort @@ -95,8 +117,8 @@ func (c *common) flushToParent(testName, format string, args ...interface{}) { // Not quite sure how this works upstream. c.output.WriteTo(os.Stdout) } else { - fmt.Fprintf(&c.parent.output, format, args...) - c.output.WriteTo(&c.parent.output) + fmt.Fprintf(c.parent.output, format, args...) + c.output.WriteTo(c.parent.output) } } @@ -178,16 +200,10 @@ func (c *common) log(s string) { } lines := strings.Split(s, "\n") // First line. - c.output.WriteString(c.indent) - c.output.WriteString(" ") // 4 spaces - c.output.WriteString(lines[0]) - c.output.WriteByte('\n') + fmt.Fprintf(c.output, "%s %s\n", c.indent, lines[0]) // More lines. for _, line := range lines[1:] { - c.output.WriteString(c.indent) - c.output.WriteString(" ") // 8 spaces - c.output.WriteString(line) - c.output.WriteByte('\n') + fmt.Fprintf(c.output, "%s %s\n", c.indent, line) } } @@ -409,6 +425,7 @@ func (t *T) Run(name string, f func(t *T)) bool { // Create a subtest. sub := T{ common: common{ + output: &logger{logToStdout: flagVerbose}, name: testName, parent: &t.common, level: t.level + 1, @@ -419,7 +436,7 @@ func (t *T) Run(name string, f func(t *T)) bool { sub.indent = sub.indent + " " } if flagVerbose { - fmt.Fprintf(&t.output, "=== RUN %s\n", sub.name) + fmt.Fprintf(t.output, "=== RUN %s\n", sub.name) } tRunner(&sub, f) @@ -484,6 +501,9 @@ func runTests(matchString func(pat, str string) (bool, error), tests []InternalT ctx := newTestContext(newMatcher(matchString, flagRunRegexp, "-test.run")) t := &T{ + common: common{ + output: &logger{logToStdout: flagVerbose}, + }, context: ctx, } |