родитель
a2f95d6b87
коммит
9182664845
2 изменённых файлов: 43 добавлений и 19 удалений
18
main.go
18
main.go
|
@ -249,10 +249,16 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
|
||||||
flags = append(flags, "-test.count="+strconv.Itoa(testConfig.Count))
|
flags = append(flags, "-test.count="+strconv.Itoa(testConfig.Count))
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.Buffer{}
|
var buf bytes.Buffer
|
||||||
|
var output io.Writer = &buf
|
||||||
|
// Send the test output to stdout if -v or -bench
|
||||||
|
if testConfig.Verbose || testConfig.BenchRegexp != "" {
|
||||||
|
output = os.Stdout
|
||||||
|
}
|
||||||
|
|
||||||
passed := false
|
passed := false
|
||||||
var duration time.Duration
|
var duration time.Duration
|
||||||
result, err := buildAndRun(pkgName, config, &buf, flags, nil, 0, func(cmd *exec.Cmd, result builder.BuildResult) error {
|
result, err := buildAndRun(pkgName, config, output, flags, nil, 0, func(cmd *exec.Cmd, result builder.BuildResult) error {
|
||||||
if testConfig.CompileOnly || outpath != "" {
|
if testConfig.CompileOnly || outpath != "" {
|
||||||
// Write test binary to the specified file name.
|
// Write test binary to the specified file name.
|
||||||
if outpath == "" {
|
if outpath == "" {
|
||||||
|
@ -310,11 +316,9 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
|
||||||
duration = time.Since(start)
|
duration = time.Since(start)
|
||||||
passed = err == nil
|
passed = err == nil
|
||||||
|
|
||||||
// print the test output if
|
// if verbose or benchmarks, then output is already going to stdout
|
||||||
// 1) the tests passed and in verbose mode
|
// However, if we failed and weren't printing to stdout, print the output we accumulated.
|
||||||
// 2) the tests failed
|
if !passed && output != os.Stdout {
|
||||||
// 3) running benchmarks
|
|
||||||
if (passed && testConfig.Verbose) || (!passed) || (testConfig.BenchRegexp != "") {
|
|
||||||
buf.WriteTo(stdout)
|
buf.WriteTo(stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче