Skip to content

Commit 13891e8

Browse files
committed
Revert "Add 'go-project-name' flag to RunTestsCmd and update Runner struct"
This reverts commit a656142.
1 parent a656142 commit 13891e8

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ 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")
3130
testPackagesJson, _ := cmd.Flags().GetString("test-packages-json")
3231
testPackagesArg, _ := cmd.Flags().GetStringSlice("test-packages")
3332
testCmdStrings, _ := cmd.Flags().GetStringArray("test-cmd")
@@ -81,7 +80,6 @@ var RunTestsCmd = &cobra.Command{
8180
// Initialize the runner
8281
testRunner := runner.Runner{
8382
ProjectPath: projectPath,
84-
GoProject: goProject,
8583
Verbose: true,
8684
RunCount: runCount,
8785
Timeout: timeout,
@@ -164,8 +162,6 @@ var RunTestsCmd = &cobra.Command{
164162

165163
func init() {
166164
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")
169165
RunTestsCmd.Flags().String("test-packages-json", "", "JSON-encoded string of test packages")
170166
RunTestsCmd.Flags().StringSlice("test-packages", nil, "Comma-separated list of test packages to run")
171167
RunTestsCmd.Flags().StringArray("test-cmd", nil,

tools/flakeguard/runner/runner.go

Lines changed: 47 additions & 3 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-
GoProject string // Go project package name.
30+
prettyProjectPath string // Go project package path, formatted for pretty printing.
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.GoProject,
79+
GoProject: r.prettyProjectPath,
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.GoProject,
115+
GoProject: r.prettyProjectPath,
116116
RaceDetection: r.UseRace,
117117
ExcludedTests: r.SkipTests,
118118
SelectedTests: r.SelectTests,
@@ -173,6 +173,11 @@ 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+
}
176181
// Run the command with output directed to the file
177182
cmd := exec.Command("go", args...)
178183
cmd.Dir = r.ProjectPath
@@ -633,3 +638,42 @@ func parseSubTest(testName string) (parentTestName, subTestName string) {
633638
}
634639
return parts[0], parts[1]
635640
}
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ 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+
4250
func TestRun(t *testing.T) {
4351
var (
4452
zeroRuns = 0

0 commit comments

Comments
 (0)