Skip to content

Commit a656142

Browse files
committed
Add 'go-project-name' flag to RunTestsCmd and update Runner struct
1 parent 29f9005 commit a656142

File tree

3 files changed

+7
-55
lines changed

3 files changed

+7
-55
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var RunTestsCmd = &cobra.Command{
2727
Run: func(cmd *cobra.Command, args []string) {
2828
// Retrieve flags
2929
projectPath, _ := cmd.Flags().GetString("project-path")
30+
goProject, _ := cmd.Flags().GetString("go-project-name")
3031
testPackagesJson, _ := cmd.Flags().GetString("test-packages-json")
3132
testPackagesArg, _ := cmd.Flags().GetStringSlice("test-packages")
3233
testCmdStrings, _ := cmd.Flags().GetStringArray("test-cmd")
@@ -80,6 +81,7 @@ var RunTestsCmd = &cobra.Command{
8081
// Initialize the runner
8182
testRunner := runner.Runner{
8283
ProjectPath: projectPath,
84+
GoProject: goProject,
8385
Verbose: true,
8486
RunCount: runCount,
8587
Timeout: timeout,
@@ -162,6 +164,8 @@ var RunTestsCmd = &cobra.Command{
162164

163165
func init() {
164166
RunTestsCmd.Flags().StringP("project-path", "r", ".", "The path to the Go project. Default is the current directory. Useful for subprojects")
167+
RunTestsCmd.Flags().String("go-project-name", "", "The name of the Go project. Example: github.com/smartcontractkit/chainlink/v2")
168+
_ = RunTestsCmd.MarkFlagRequired("go-project-name")
165169
RunTestsCmd.Flags().String("test-packages-json", "", "JSON-encoded string of test packages")
166170
RunTestsCmd.Flags().StringSlice("test-packages", nil, "Comma-separated list of test packages to run")
167171
RunTestsCmd.Flags().StringArray("test-cmd", nil,

tools/flakeguard/runner/runner.go

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727
// Runner describes the test run parameters and raw test outputs
2828
type Runner struct {
2929
ProjectPath string // Path to the Go project directory.
30-
prettyProjectPath string // Go project package path, formatted for pretty printing.
30+
GoProject string // Go project package name.
3131
Verbose bool // If true, provides detailed logging.
3232
RunCount int // Number of times to run the tests.
3333
UseRace bool // Enable race detector.
@@ -76,7 +76,7 @@ func (r *Runner) RunTestPackages(packages []string) (*reports.TestReport, error)
7676
return nil, fmt.Errorf("failed to parse test results: %w", err)
7777
}
7878
report := &reports.TestReport{
79-
GoProject: r.prettyProjectPath,
79+
GoProject: r.GoProject,
8080
RaceDetection: r.UseRace,
8181
ExcludedTests: r.SkipTests,
8282
SelectedTests: r.SelectTests,
@@ -112,7 +112,7 @@ func (r *Runner) RunTestCmd(testCmd []string) (*reports.TestReport, error) {
112112
}
113113

114114
report := &reports.TestReport{
115-
GoProject: r.prettyProjectPath,
115+
GoProject: r.GoProject,
116116
RaceDetection: r.UseRace,
117117
ExcludedTests: r.SkipTests,
118118
SelectedTests: r.SelectTests,
@@ -173,11 +173,6 @@ func (r *Runner) runTestPackage(packageName string) (string, bool, error) {
173173
}
174174
defer tmpFile.Close()
175175

176-
r.prettyProjectPath, err = prettyProjectPath(r.ProjectPath)
177-
if err != nil {
178-
r.prettyProjectPath = r.ProjectPath
179-
log.Warn().Err(err).Str("projectPath", r.ProjectPath).Msg("Failed to get pretty project path")
180-
}
181176
// Run the command with output directed to the file
182177
cmd := exec.Command("go", args...)
183178
cmd.Dir = r.ProjectPath
@@ -638,42 +633,3 @@ func parseSubTest(testName string) (parentTestName, subTestName string) {
638633
}
639634
return parts[0], parts[1]
640635
}
641-
642-
// prettyProjectPath returns the project path formatted for pretty printing in results.
643-
func prettyProjectPath(projectPath string) (string, error) {
644-
// Walk up the directory structure to find go.mod
645-
absPath, err := filepath.Abs(projectPath)
646-
if err != nil {
647-
return "", fmt.Errorf("failed to resolve absolute path: %w", err)
648-
}
649-
dir := absPath
650-
for {
651-
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
652-
break
653-
}
654-
655-
parent := filepath.Dir(dir)
656-
if parent == dir { // Reached the root without finding go.mod
657-
return "", fmt.Errorf("go.mod not found in project path, started at %s, ended at %s", projectPath, dir)
658-
}
659-
dir = parent
660-
}
661-
662-
// Read go.mod to extract the module path
663-
goModPath := filepath.Join(dir, "go.mod")
664-
goModData, err := os.ReadFile(goModPath)
665-
if err != nil {
666-
return "", fmt.Errorf("failed to read go.mod: %w", err)
667-
}
668-
669-
for _, line := range strings.Split(string(goModData), "\n") {
670-
if strings.HasPrefix(line, "module ") {
671-
goProject := strings.TrimSpace(strings.TrimPrefix(line, "module "))
672-
relativePath := strings.TrimPrefix(projectPath, dir)
673-
relativePath = strings.TrimLeft(relativePath, string(os.PathSeparator))
674-
return filepath.Join(goProject, relativePath), nil
675-
}
676-
}
677-
678-
return "", fmt.Errorf("module path not found in go.mod")
679-
}

tools/flakeguard/runner/runner_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ type expectedTestResult struct {
3939
seen bool
4040
}
4141

42-
func TestPrettyProjectPath(t *testing.T) {
43-
t.Parallel()
44-
45-
prettyPath, err := prettyProjectPath("./")
46-
require.NoError(t, err)
47-
assert.Equal(t, "github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard", prettyPath)
48-
}
49-
5042
func TestRun(t *testing.T) {
5143
var (
5244
zeroRuns = 0

0 commit comments

Comments
 (0)