|
@ -13,6 +13,7 @@ import ( |
|
|
"errors" |
|
|
"errors" |
|
|
"flag" |
|
|
"flag" |
|
|
"fmt" |
|
|
"fmt" |
|
|
|
|
|
"io" |
|
|
"io/fs" |
|
|
"io/fs" |
|
|
"os" |
|
|
"os" |
|
|
"strings" |
|
|
"strings" |
|
@ -49,7 +50,7 @@ func Init() { |
|
|
// common holds the elements common between T and B and
|
|
|
// common holds the elements common between T and B and
|
|
|
// captures common methods such as Errorf.
|
|
|
// captures common methods such as Errorf.
|
|
|
type common struct { |
|
|
type common struct { |
|
|
output bytes.Buffer |
|
|
output *logger |
|
|
indent string |
|
|
indent string |
|
|
ran bool // Test or benchmark (or one of its subtests) was executed.
|
|
|
ran bool // Test or benchmark (or one of its subtests) was executed.
|
|
|
failed bool // Test or benchmark has failed.
|
|
|
failed bool // Test or benchmark has failed.
|
|
@ -70,6 +71,27 @@ type common struct { |
|
|
tempDirSeq int32 |
|
|
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.
|
|
|
// Short reports whether the -test.short flag is set.
|
|
|
func Short() bool { |
|
|
func Short() bool { |
|
|
return flagShort |
|
|
return flagShort |
|
@ -95,8 +117,8 @@ func (c *common) flushToParent(testName, format string, args ...interface{}) { |
|
|
// Not quite sure how this works upstream.
|
|
|
// Not quite sure how this works upstream.
|
|
|
c.output.WriteTo(os.Stdout) |
|
|
c.output.WriteTo(os.Stdout) |
|
|
} else { |
|
|
} else { |
|
|
fmt.Fprintf(&c.parent.output, format, args...) |
|
|
fmt.Fprintf(c.parent.output, format, args...) |
|
|
c.output.WriteTo(&c.parent.output) |
|
|
c.output.WriteTo(c.parent.output) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -178,16 +200,10 @@ func (c *common) log(s string) { |
|
|
} |
|
|
} |
|
|
lines := strings.Split(s, "\n") |
|
|
lines := strings.Split(s, "\n") |
|
|
// First line.
|
|
|
// First line.
|
|
|
c.output.WriteString(c.indent) |
|
|
fmt.Fprintf(c.output, "%s %s\n", c.indent, lines[0]) |
|
|
c.output.WriteString(" ") // 4 spaces
|
|
|
|
|
|
c.output.WriteString(lines[0]) |
|
|
|
|
|
c.output.WriteByte('\n') |
|
|
|
|
|
// More lines.
|
|
|
// More lines.
|
|
|
for _, line := range lines[1:] { |
|
|
for _, line := range lines[1:] { |
|
|
c.output.WriteString(c.indent) |
|
|
fmt.Fprintf(c.output, "%s %s\n", c.indent, line) |
|
|
c.output.WriteString(" ") // 8 spaces
|
|
|
|
|
|
c.output.WriteString(line) |
|
|
|
|
|
c.output.WriteByte('\n') |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -409,6 +425,7 @@ func (t *T) Run(name string, f func(t *T)) bool { |
|
|
// Create a subtest.
|
|
|
// Create a subtest.
|
|
|
sub := T{ |
|
|
sub := T{ |
|
|
common: common{ |
|
|
common: common{ |
|
|
|
|
|
output: &logger{logToStdout: flagVerbose}, |
|
|
name: testName, |
|
|
name: testName, |
|
|
parent: &t.common, |
|
|
parent: &t.common, |
|
|
level: t.level + 1, |
|
|
level: t.level + 1, |
|
@ -419,7 +436,7 @@ func (t *T) Run(name string, f func(t *T)) bool { |
|
|
sub.indent = sub.indent + " " |
|
|
sub.indent = sub.indent + " " |
|
|
} |
|
|
} |
|
|
if flagVerbose { |
|
|
if flagVerbose { |
|
|
fmt.Fprintf(&t.output, "=== RUN %s\n", sub.name) |
|
|
fmt.Fprintf(t.output, "=== RUN %s\n", sub.name) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
tRunner(&sub, f) |
|
|
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")) |
|
|
ctx := newTestContext(newMatcher(matchString, flagRunRegexp, "-test.run")) |
|
|
t := &T{ |
|
|
t := &T{ |
|
|
|
|
|
common: common{ |
|
|
|
|
|
output: &logger{logToStdout: flagVerbose}, |
|
|
|
|
|
}, |
|
|
context: ctx, |
|
|
context: ctx, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|