Skip to content

collect failed test error message #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/ctrf-io/go-ctrf-json-reporter/ctrf"
Expand Down Expand Up @@ -35,7 +36,7 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R

report := ctrf.NewReport("gotest", env)
report.Results.Summary.Start = time.Now().UnixNano() / int64(time.Millisecond)
for _, event := range testEvents {
for i, event := range testEvents {
if verbose {
jsonEvent, err := json.Marshal(event)
if err != nil {
Expand Down Expand Up @@ -76,6 +77,7 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
Name: event.Test,
Status: ctrf.TestFailed,
Duration: duration,
Message: getMessagesForTest(testEvents, i, event.Package, event.Test),
})
} else if event.Action == "skip" {
report.Results.Summary.Tests++
Expand All @@ -91,6 +93,20 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
}
return report, nil
}
func getMessagesForTest(testEvents []TestEvent, index int, packageName, testName string) string {
var messages []string
for i := index; i >= 0; i-- {
if testEvents[i].Package == packageName && testEvents[i].Test == testName {
if testEvents[i].Action == "output" {
messages = append(messages, testEvents[i].Output)
}
} else {
break
}
}
reverse(messages)
return strings.Join(messages, "")
}

func WriteReportToFile(filename string, report *ctrf.Report) error {
err := report.WriteFile(filename)
Expand All @@ -105,10 +121,16 @@ func secondsToMillis(seconds float64) int64 {
return int64(seconds * 1000)
}

func parseTimeString(timeString string) (int64, error) {
func parseTimeString(timeString string) (int64, error) {
t, err := time.Parse(time.RFC3339Nano, timeString)
if err != nil {
return 0, err
}
return t.UnixNano() / int64(time.Millisecond), nil
}

func reverse(s []string) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}