From eccbeefc1ccca9503c943473a887fe999003ebf6 Mon Sep 17 00:00:00 2001 From: Cedric Date: Fri, 15 Sep 2023 10:51:51 +0100 Subject: [PATCH] [chore] Fix false positive when running tests in the root-level directory (#10648) --- tools/flakeytests/runner.go | 11 +++----- tools/flakeytests/runner_test.go | 43 ++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/tools/flakeytests/runner.go b/tools/flakeytests/runner.go index 28522b2aa70..ab8f2c62229 100644 --- a/tools/flakeytests/runner.go +++ b/tools/flakeytests/runner.go @@ -45,8 +45,9 @@ func NewRunner(readers []io.Reader, reporter reporter, numReruns int) *Runner { } func runGoTest(pkg string, tests []string, numReruns int, w io.Writer) error { + pkg = strings.Replace(pkg, "github.com/smartcontractkit/chainlink/v2", "", -1) testFilter := strings.Join(tests, "|") - cmd := exec.Command("./tools/bin/go_core_tests", fmt.Sprintf("./%s", pkg)) //#nosec + cmd := exec.Command("./tools/bin/go_core_tests", fmt.Sprintf(".%s", pkg)) //#nosec cmd.Env = append(os.Environ(), fmt.Sprintf("TEST_FLAGS=-count %d -run %s", numReruns, testFilter)) cmd.Stdout = io.MultiWriter(os.Stdout, w) cmd.Stderr = io.MultiWriter(os.Stderr, w) @@ -65,13 +66,7 @@ type TestEvent struct { func newEvent(b []byte) (*TestEvent, error) { e := &TestEvent{} err := json.Unmarshal(b, e) - if err != nil { - return nil, err - } - - e.Package = strings.Replace(e.Package, "github.com/smartcontractkit/chainlink/v2/", "", -1) - return e, nil - + return e, err } func parseOutput(readers ...io.Reader) (map[string]map[string]int, error) { diff --git a/tools/flakeytests/runner_test.go b/tools/flakeytests/runner_test.go index 6c1d1068765..7def5092b01 100644 --- a/tools/flakeytests/runner_test.go +++ b/tools/flakeytests/runner_test.go @@ -31,8 +31,8 @@ func TestParser(t *testing.T) { require.NoError(t, err) assert.Len(t, ts, 1) - assert.Len(t, ts["core/assets"], 1) - assert.Equal(t, ts["core/assets"]["TestLink"], 1) + assert.Len(t, ts["github.com/smartcontractkit/chainlink/v2/core/assets"], 1) + assert.Equal(t, ts["github.com/smartcontractkit/chainlink/v2/core/assets"]["TestLink"], 1) } func TestParser_SkipsNonJSON(t *testing.T) { @@ -46,8 +46,8 @@ func TestParser_SkipsNonJSON(t *testing.T) { require.NoError(t, err) assert.Len(t, ts, 1) - assert.Len(t, ts["core/assets"], 1) - assert.Equal(t, ts["core/assets"]["TestLink"], 1) + assert.Len(t, ts["github.com/smartcontractkit/chainlink/v2/core/assets"], 1) + assert.Equal(t, ts["github.com/smartcontractkit/chainlink/v2/core/assets"]["TestLink"], 1) } func TestParser_PanicDueToLogging(t *testing.T) { @@ -60,8 +60,8 @@ func TestParser_PanicDueToLogging(t *testing.T) { require.NoError(t, err) assert.Len(t, ts, 1) - assert.Len(t, ts["core/assets"], 1) - assert.Equal(t, ts["core/assets"]["TestAssets_LinkScanValue"], 1) + assert.Len(t, ts["github.com/smartcontractkit/chainlink/v2/core/assets"], 1) + assert.Equal(t, ts["github.com/smartcontractkit/chainlink/v2/core/assets"]["TestAssets_LinkScanValue"], 1) } func TestParser_SuccessfulOutput(t *testing.T) { @@ -105,7 +105,7 @@ func TestRunner_WithFlake(t *testing.T) { err := r.Run() require.NoError(t, err) assert.Len(t, m.entries, 1) - assert.Equal(t, m.entries["core/assets"], []string{"TestLink"}) + assert.Equal(t, m.entries["github.com/smartcontractkit/chainlink/v2/core/assets"], []string{"TestLink"}) } func TestRunner_WithFailedPackage(t *testing.T) { @@ -130,7 +130,7 @@ func TestRunner_WithFailedPackage(t *testing.T) { err := r.Run() require.NoError(t, err) assert.Len(t, m.entries, 1) - assert.Equal(t, m.entries["core/assets"], []string{"TestLink"}) + assert.Equal(t, m.entries["github.com/smartcontractkit/chainlink/v2/core/assets"], []string{"TestLink"}) } func TestRunner_AllFailures(t *testing.T) { @@ -178,7 +178,28 @@ func TestRunner_RerunSuccessful(t *testing.T) { err := r.Run() require.NoError(t, err) - assert.Equal(t, m.entries["core/assets"], []string{"TestLink"}) + assert.Equal(t, m.entries["github.com/smartcontractkit/chainlink/v2/core/assets"], []string{"TestLink"}) +} + +func TestRunner_RootLevelTest(t *testing.T) { + output := `{"Time":"2023-09-07T15:39:46.378315+01:00","Action":"fail","Package":"github.com/smartcontractkit/chainlink/v2/","Test":"TestConfigDocs","Elapsed":0}` + + rerunOutput := `` + m := newMockReporter() + r := &Runner{ + numReruns: 2, + readers: []io.Reader{strings.NewReader(output)}, + runTestFn: func(pkg string, testNames []string, numReruns int, w io.Writer) error { + _, err := w.Write([]byte(rerunOutput)) + return err + }, + parse: parseOutput, + reporter: m, + } + + err := r.Run() + require.NoError(t, err) + assert.Equal(t, m.entries["github.com/smartcontractkit/chainlink/v2/"], []string{"TestConfigDocs"}) } type exitError struct{} @@ -208,7 +229,7 @@ func TestRunner_RerunFailsWithNonzeroExitCode(t *testing.T) { err := r.Run() require.NoError(t, err) - assert.Equal(t, m.entries["core/assets"], []string{"TestLink"}) + assert.Equal(t, m.entries["github.com/smartcontractkit/chainlink/v2/core/assets"], []string{"TestLink"}) } func TestRunner_RerunWithNonZeroExitCodeDoesntStopCommand(t *testing.T) { @@ -251,5 +272,5 @@ func TestRunner_RerunWithNonZeroExitCodeDoesntStopCommand(t *testing.T) { require.NoError(t, err) calls := index assert.Equal(t, 2, calls) - assert.Equal(t, m.entries["core/assets"], []string{"TestLink"}) + assert.Equal(t, m.entries["github.com/smartcontractkit/chainlink/v2/core/assets"], []string{"TestLink"}) }