diff --git a/parser/gotest/gotest.go b/parser/gotest/gotest.go index 1894758..375f8de 100644 --- a/parser/gotest/gotest.go +++ b/parser/gotest/gotest.go @@ -109,10 +109,21 @@ func SetSubtestMode(mode SubtestMode) Option { } } +// AssumeNoBuildOutput is an Option disables "build_output" event parsing. This +// option is most useful when running tests that are known to build correctly, +// or have already been built (e.g. via go test -c, or an alternative build +// system). +func AssumeNoBuildOutput() Option { + return func(p *Parser) { + p.assumeNoBuildOutput = true + } +} + // Parser is a Go test output Parser. type Parser struct { - packageName string - subtestMode SubtestMode + packageName string + subtestMode SubtestMode + assumeNoBuildOutput bool timestampFunc func() time.Time @@ -208,7 +219,7 @@ func (p *Parser) parseLine(line string) (events []Event) { return p.benchSummary(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6]) } else if matches := regexEndBenchmark.FindStringSubmatch(line); len(matches) == 3 { return p.endBench(matches[1], matches[2]) - } else if strings.HasPrefix(line, "# ") { + } else if !p.assumeNoBuildOutput && strings.HasPrefix(line, "# ") { fields := strings.Fields(strings.TrimPrefix(line, "# ")) if len(fields) == 1 || len(fields) == 2 { return p.buildOutput(fields[0]) diff --git a/parser/gotest/gotest_test.go b/parser/gotest/gotest_test.go index c6300d9..481cf79 100644 --- a/parser/gotest/gotest_test.go +++ b/parser/gotest/gotest_test.go @@ -225,3 +225,40 @@ func TestParseLine(t *testing.T) { }) } } + +func TestParseLine_AssumeNoBuildOutput(t *testing.T) { + tests := []parseLineTest{ + { + "# package/name/failing1", + []Event{{Type: "output", Data: "# package/name/failing1"}}, + }, + { + "# package/name/failing2 [package/name/failing2.test]", + []Event{{Type: "output", Data: "# package/name/failing2 [package/name/failing2.test]"}}, + }, + } + + // Include all the other tests that don't produce build_output as well, they + // should continue functioning normally. +Outer: + for _, test := range parseLineTests { + for _, event := range test.events { + if event.Type == "build_output" { + continue Outer + } + } + + tests = append(tests, test) + } + + for i, test := range tests { + name := fmt.Sprintf("%d-%s", i, test.Name()) + t.Run(name, func(t *testing.T) { + parser := NewParser(AssumeNoBuildOutput()) + events := parser.parseLine(test.input) + if diff := cmp.Diff(test.events, events); diff != "" { + t.Errorf("parseLine(%q) returned unexpected events, diff (-want, +got):\n%v", test.input, diff) + } + }) + } +}