Skip to content

Commit

Permalink
Simplify error in RunResult and update printed error message
Browse files Browse the repository at this point in the history
  • Loading branch information
nprizal committed Dec 9, 2024
1 parent b1283ad commit 26bebe1
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 21 deletions.
4 changes: 2 additions & 2 deletions internal/runner/jest.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ func (j Jest) Run(result *RunResult, testCases []plan.TestCase, retry bool) erro
}
}

for i := 0; i < report.NumRuntimeErrorTestSuites; i++ {
result.errors = append(result.errors, ErrOutsideOfTest)
if report.NumRuntimeErrorTestSuites > 0 {
result.error = fmt.Errorf("Jest failed with runtime error test suites")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/runner/playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (p Playwright) Run(result *RunResult, testCases []plan.TestCase, retry bool
}
}

for _, reportError := range report.Errors {
result.errors = append(result.errors, fmt.Errorf("Playwright error: %s", reportError.Message))
if len(report.Errors) > 0 {
result.error = fmt.Errorf("Playwright failed with errors")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/runner/rspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func (r Rspec) Run(result *RunResult, testCases []plan.TestCase, retry bool) err
result.RecordTestResult(mapExampleToTestCase(example), status)
}

for i := 0; i < report.Summary.ErrorsOutsideOfExamplesCount; i++ {
result.errors = append(result.errors, ErrOutsideOfTest)
if report.Summary.ErrorsOutsideOfExamplesCount > 0 {
result.error = fmt.Errorf("RSpec failed with errors outside of examples")
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions internal/runner/run_result.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package runner

import (
"errors"

"github.com/buildkite/test-engine-client/internal/plan"
)

Expand All @@ -20,8 +18,6 @@ const (
RunStatusUnknown RunStatus = "unknown"
)

var ErrOutsideOfTest = errors.New("errors outside of tests")

// RunResult is a struct to keep track the results of a test run.
// It contains the logics to record test results, calculate the status of the run.
type RunResult struct {
Expand All @@ -30,7 +26,7 @@ type RunResult struct {
// mutedTestLookup is a map containing the test identifiers of muted tests.
// This list might contain tests that are not part of the current run (i.e. belong to a different node).
mutedTestLookup map[string]bool
errors []error
error error
}

func NewRunResult(mutedTests []plan.TestCase) *RunResult {
Expand Down Expand Up @@ -105,7 +101,7 @@ func (r *RunResult) MutedTests() []TestResult {
// If there are failed tests, it returns RunStatusFailed.
// Otherwise, it returns RunStatusPassed.
func (r *RunResult) Status() RunStatus {
if len(r.errors) > 0 {
if r.error != nil {
return RunStatusError
}

Expand All @@ -120,6 +116,10 @@ func (r *RunResult) Status() RunStatus {
return RunStatusPassed
}

func (r *RunResult) Error() error {
return r.error
}

type RunStatistics struct {
Total int
PassedOnFirstRun int
Expand Down
3 changes: 2 additions & 1 deletion internal/runner/run_result_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"fmt"
"slices"
"strings"
"testing"
Expand Down Expand Up @@ -224,7 +225,7 @@ func TestRunStatus_Failed(t *testing.T) {

func TestRunStatus_Error(t *testing.T) {
r := NewRunResult([]plan.TestCase{})
r.errors = append(r.errors, ErrOutsideOfTest)
r.error = fmt.Errorf("error")
if r.Status() != RunStatusError {
t.Errorf("Status() is %s, want %s", r.Status(), RunStatusError)
}
Expand Down
12 changes: 4 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,6 @@ func main() {
}

func printReport(runResult runner.RunResult) {
statistics := runResult.Statistics()

if statistics.Total == 0 {
return
}

// Print statistics
fmt.Println("+++ ========== Buildkite Test Engine Report ==========")

switch runResult.Status() {
Expand All @@ -145,9 +138,12 @@ func printReport(runResult runner.RunResult) {
case runner.RunStatusFailed:
fmt.Println("❌ Some tests failed.")
case runner.RunStatusError:
fmt.Println("🚨 Errors encountered outside of tests.")
fmt.Printf("🚨 %s\n", runResult.Error())
}
fmt.Println("")

// Print statistics
statistics := runResult.Statistics()
data := [][]string{
{"Passed", "first run", strconv.Itoa(statistics.PassedOnFirstRun)},
{"Passed", "on retry", strconv.Itoa(statistics.PassedOnRetry)},
Expand Down

0 comments on commit 26bebe1

Please sign in to comment.