Skip to content

Commit

Permalink
[fix] Complete the test parsing if the test command returns a non-zer…
Browse files Browse the repository at this point in the history
…o exit code
  • Loading branch information
cedric-cordenier committed Sep 8, 2023
1 parent 0b1814d commit 0d7ea7d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tools/flakeytests/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -111,6 +112,10 @@ func parseOutput(readers ...io.Reader) (map[string]map[string]int, error) {
return tests, nil
}

type exitCoder interface {
ExitCode() int
}

func (r *Runner) runTests(failedTests map[string]map[string]int) (io.Reader, error) {
var out bytes.Buffer
for pkg, tests := range failedTests {
Expand All @@ -123,6 +128,10 @@ func (r *Runner) runTests(failedTests map[string]map[string]int) (io.Reader, err
err := r.runTestFn(pkg, ts, r.numReruns, &out)
if err != nil {
log.Printf("Test command errored: %s\n", err)
var exErr exitCoder
if errors.As(err, &exErr) && exErr.ExitCode() > 0 {
return &out, nil
}
return &out, err
}
}
Expand Down
30 changes: 30 additions & 0 deletions tools/flakeytests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,33 @@ func TestRunner_RerunSuccessful(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, m.entries["core/assets"], []string{"TestLink"})
}

type exitError struct{}

func (e *exitError) ExitCode() int { return 1 }

func (e *exitError) Error() string { return "exit code: 1" }

func TestRunner_RerunFailsWithNonzeroExitCode(t *testing.T) {
output := `{"Time":"2023-09-07T15:39:46.378315+01:00","Action":"fail","Package":"github.com/smartcontractkit/chainlink/v2/core/assets","Test":"TestLink","Elapsed":0}`

rerunOutput := `
{"Time":"2023-09-07T15:39:46.378315+01:00","Action":"fail","Package":"github.com/smartcontractkit/chainlink/v2/core/assets","Test":"TestLink","Elapsed":0}
{"Time":"2023-09-07T15:39:46.378315+01:00","Action":"pass","Package":"github.com/smartcontractkit/chainlink/v2/core/assets","Test":"TestLink","Elapsed":0}
`
m := newMockReporter()
r := &Runner{
numReruns: 2,
readers: []io.Reader{strings.NewReader(output)},
runTestFn: func(pkg string, testNames []string, numReruns int, w io.Writer) error {
_, _ = w.Write([]byte(rerunOutput))
return &exitError{}
},
parse: parseOutput,
reporter: m,
}

err := r.Run()
require.NoError(t, err)
assert.Equal(t, m.entries["core/assets"], []string{"TestLink"})
}

0 comments on commit 0d7ea7d

Please sign in to comment.