Skip to content

Commit bc5b5a6

Browse files
committed
Allow to run all test packages and check dependencies
1 parent 51c70de commit bc5b5a6

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"log"
78
"os"
9+
"os/exec"
810

911
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
1012
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/runner"
@@ -18,12 +20,18 @@ var RunTestsCmd = &cobra.Command{
1820
projectPath, _ := cmd.Flags().GetString("project-path")
1921
testPackagesJson, _ := cmd.Flags().GetString("test-packages-json")
2022
testPackagesArg, _ := cmd.Flags().GetStringSlice("test-packages")
23+
runAllPackages, _ := cmd.Flags().GetBool("run-all-packages")
2124
runCount, _ := cmd.Flags().GetInt("run-count")
2225
useRace, _ := cmd.Flags().GetBool("race")
2326
outputPath, _ := cmd.Flags().GetString("output-json")
2427
threshold, _ := cmd.Flags().GetFloat64("threshold")
2528
skipTests, _ := cmd.Flags().GetStringSlice("skip-tests")
2629

30+
// Check if project dependencies are correctly set up
31+
if err := checkDependencies(projectPath); err != nil {
32+
log.Fatalf("Error: %v", err)
33+
}
34+
2735
var testPackages []string
2836
if testPackagesJson != "" {
2937
if err := json.Unmarshal([]byte(testPackagesJson), &testPackages); err != nil {
@@ -36,15 +44,17 @@ var RunTestsCmd = &cobra.Command{
3644
}
3745

3846
runner := runner.Runner{
39-
ProjectPath: projectPath,
40-
Verbose: true,
41-
RunCount: runCount,
42-
UseRace: useRace,
43-
FailFast: threshold == 1.0, // Fail test on first test run if threshold is 1.0
44-
SkipTests: skipTests,
47+
ProjectPath: projectPath,
48+
Verbose: true,
49+
RunCount: runCount,
50+
UseRace: useRace,
51+
FailFast: threshold == 1.0, // Fail test on first test run if threshold is 1.0
52+
SkipTests: skipTests,
53+
RunAllTestPackages: runAllPackages,
54+
SelectedTestPackages: testPackages,
4555
}
4656

47-
testResults, err := runner.RunTests(testPackages)
57+
testResults, err := runner.RunTests()
4858
if err != nil {
4959
fmt.Printf("Error running tests: %v\n", err)
5060
os.Exit(1)
@@ -87,10 +97,26 @@ func init() {
8797
RunTestsCmd.Flags().StringP("project-path", "r", ".", "The path to the Go project. Default is the current directory. Useful for subprojects")
8898
RunTestsCmd.Flags().String("test-packages-json", "", "JSON-encoded string of test packages")
8999
RunTestsCmd.Flags().StringSlice("test-packages", nil, "Comma-separated list of test packages to run")
100+
RunTestsCmd.Flags().Bool("run-all-packages", false, "Run all test packages in the project. This flag overrides --test-packages and --test-packages-json")
90101
RunTestsCmd.Flags().IntP("run-count", "c", 1, "Number of times to run the tests")
91102
RunTestsCmd.Flags().Bool("race", false, "Enable the race detector")
92103
RunTestsCmd.Flags().Bool("fail-fast", false, "Stop on the first test failure")
93104
RunTestsCmd.Flags().String("output-json", "", "Path to output the test results in JSON format")
94105
RunTestsCmd.Flags().Float64("threshold", 0.8, "Threshold for considering a test as flaky")
95106
RunTestsCmd.Flags().StringSlice("skip-tests", nil, "Comma-separated list of test names to skip from running")
96107
}
108+
109+
func checkDependencies(projectPath string) error {
110+
cmd := exec.Command("go", "mod", "tidy")
111+
cmd.Dir = projectPath
112+
113+
var out bytes.Buffer
114+
cmd.Stdout = &out
115+
cmd.Stderr = &out
116+
117+
if err := cmd.Run(); err != nil {
118+
return fmt.Errorf("dependency check failed: %v\n%s\nPlease run 'go mod tidy' to fix missing or unused dependencies", err, out.String())
119+
}
120+
121+
return nil
122+
}

tools/flakeguard/runner/runner.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,28 @@ import (
1414
)
1515

1616
type Runner struct {
17-
ProjectPath string // Path to the Go project directory.
18-
Verbose bool // If true, provides detailed logging.
19-
RunCount int // Number of times to run the tests.
20-
UseRace bool // Enable race detector.
21-
FailFast bool // Stop on first test failure.
22-
SkipTests []string // Test names to exclude.
17+
ProjectPath string // Path to the Go project directory.
18+
Verbose bool // If true, provides detailed logging.
19+
RunCount int // Number of times to run the tests.
20+
UseRace bool // Enable race detector.
21+
FailFast bool // Stop on first test failure.
22+
SkipTests []string // Test names to exclude.
23+
RunAllTestPackages bool // Run all test packages if true.
24+
SelectedTestPackages []string // Explicitly selected packages to run.
2325
}
2426

2527
// RunTests executes the tests for each provided package and aggregates all results.
2628
// It returns all test results and any error encountered during testing.
27-
func (r *Runner) RunTests(packages []string) ([]reports.TestResult, error) {
29+
func (r *Runner) RunTests() ([]reports.TestResult, error) {
2830
var jsonOutputs [][]byte
31+
packages := r.SelectedTestPackages
32+
if r.RunAllTestPackages {
33+
packages = []string{"./..."}
34+
}
2935

3036
for _, p := range packages {
3137
for i := 0; i < r.RunCount; i++ {
32-
jsonOutput, passed, err := r.runTestPackage(p)
38+
jsonOutput, passed, err := r.runTests(p)
3339
if err != nil {
3440
return nil, fmt.Errorf("failed to run tests in package %s: %w", p, err)
3541
}
@@ -49,20 +55,16 @@ type exitCoder interface {
4955

5056
// runTestPackage executes the test command for a single test package.
5157
// It returns the command output, a boolean indicating success, and any error encountered.
52-
func (r *Runner) runTestPackage(testPackage string) ([]byte, bool, error) {
53-
args := []string{"test", "-json", "-count=1"} // Enable JSON output
58+
func (r *Runner) runTests(packageName string) ([]byte, bool, error) {
59+
args := []string{"test", packageName, "-json", "-count=1"} // Enable JSON output for parsing
5460
if r.UseRace {
5561
args = append(args, "-race")
5662
}
57-
58-
// Construct regex pattern from ExcludedTests slice
5963
if len(r.SkipTests) > 0 {
6064
skipPattern := strings.Join(r.SkipTests, "|")
6165
args = append(args, fmt.Sprintf("-skip=%s", skipPattern))
6266
}
6367

64-
args = append(args, testPackage)
65-
6668
if r.Verbose {
6769
log.Printf("Running command: go %s\n", strings.Join(args, " "))
6870
}
@@ -79,7 +81,7 @@ func (r *Runner) runTestPackage(testPackage string) ([]byte, bool, error) {
7981
var exErr exitCoder
8082
// Check if the error is due to a non-zero exit code
8183
if errors.As(err, &exErr) && exErr.ExitCode() == 0 {
82-
return nil, false, fmt.Errorf("test command failed at %s: %w", testPackage, err)
84+
return nil, false, fmt.Errorf("test command failed at %s: %w", packageName, err)
8385
}
8486
return out.Bytes(), false, nil // Test failed
8587
}

0 commit comments

Comments
 (0)