Skip to content

Commit

Permalink
[chore] Fix false positive when running tests in the root-level direc…
Browse files Browse the repository at this point in the history
…tory (#10648)
  • Loading branch information
cedric-cordenier authored Sep 15, 2023
1 parent 04665a7 commit eccbeef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
11 changes: 3 additions & 8 deletions tools/flakeytests/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
43 changes: 32 additions & 11 deletions tools/flakeytests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"})
}

0 comments on commit eccbeef

Please sign in to comment.