From 0d7ea7d49bceb371a0f11c71de1c035290a27802 Mon Sep 17 00:00:00 2001 From: Cedric Cordenier Date: Fri, 8 Sep 2023 16:33:38 +0100 Subject: [PATCH] [fix] Complete the test parsing if the test command returns a non-zero exit code --- tools/flakeytests/runner.go | 9 +++++++++ tools/flakeytests/runner_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/tools/flakeytests/runner.go b/tools/flakeytests/runner.go index 87777e85109..67ef27fca6d 100644 --- a/tools/flakeytests/runner.go +++ b/tools/flakeytests/runner.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "encoding/json" + "errors" "fmt" "io" "log" @@ -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 { @@ -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 } } diff --git a/tools/flakeytests/runner_test.go b/tools/flakeytests/runner_test.go index 63c7e7e3a68..4aadf916976 100644 --- a/tools/flakeytests/runner_test.go +++ b/tools/flakeytests/runner_test.go @@ -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"}) +}