Skip to content

Commit 1c35e64

Browse files
committed
fix
1 parent 831be3f commit 1c35e64

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

tools/flakeguard/reports/codebase_scanner.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package reports
22

33
import (
4+
"fmt"
45
"go/ast"
56
"go/parser"
67
"go/token"
@@ -16,8 +17,14 @@ type TestFileMap map[string]string
1617
func ScanTestFiles(rootDir string) (TestFileMap, error) {
1718
testFileMap := make(TestFileMap)
1819

20+
// Ensure rootDir is absolute
21+
rootDir, err := filepath.Abs(rootDir)
22+
if err != nil {
23+
return nil, fmt.Errorf("error normalizing rootDir: %v", err)
24+
}
25+
1926
// Walk through the root directory to find test files
20-
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
27+
err = filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
2128
if err != nil {
2229
return err
2330
}
@@ -27,11 +34,18 @@ func ScanTestFiles(rootDir string) (TestFileMap, error) {
2734
return nil
2835
}
2936

37+
// Normalize path relative to rootDir
38+
relPath, err := filepath.Rel(rootDir, path)
39+
if err != nil {
40+
return fmt.Errorf("error getting relative path for %s: %v", path, err)
41+
}
42+
relPath = filepath.ToSlash(relPath) // Ensure Unix-style paths
43+
3044
// Parse the Go file
3145
fset := token.NewFileSet()
3246
node, err := parser.ParseFile(fset, path, nil, parser.AllErrors)
3347
if err != nil {
34-
return err
48+
return fmt.Errorf("error parsing file %s: %v", path, err)
3549
}
3650

3751
// Traverse the AST to find test or fuzz functions
@@ -43,8 +57,8 @@ func ScanTestFiles(rootDir string) (TestFileMap, error) {
4357

4458
// Match both "Test" and "Fuzz" prefixes
4559
if strings.HasPrefix(funcDecl.Name.Name, "Test") || strings.HasPrefix(funcDecl.Name.Name, "Fuzz") {
46-
// Add the function to the map
47-
testFileMap[funcDecl.Name.Name] = path
60+
// Add the function to the map with relative path
61+
testFileMap[funcDecl.Name.Name] = relPath
4862
}
4963
return true
5064
})

tools/flakeguard/reports/path_mapper.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,37 @@
11
package reports
22

33
import (
4-
"fmt"
5-
"path/filepath"
64
"strings"
75
)
86

97
// MapTestResultsToPaths maps test results to their corresponding file paths.
108
func MapTestResultsToPaths(report *TestReport, rootDir string) error {
119
// Scan the codebase for test functions
12-
rootDir, err := filepath.Abs(rootDir)
13-
if err != nil {
14-
return fmt.Errorf("error normalizing rootDir: %v", err)
15-
}
16-
1710
testFileMap, err := ScanTestFiles(rootDir)
1811
if err != nil {
1912
return err
2013
}
2114

22-
fmt.Printf("Root Directory: %s\n", rootDir)
23-
fmt.Printf("Test File Map: %+v\n", testFileMap)
24-
2515
// Assign file paths to each test result
2616
for i, result := range report.Results {
2717
testName := result.TestName
2818
var filePath string
2919

20+
// Handle subtests
3021
if strings.Contains(testName, "/") {
31-
parentTestName := strings.SplitN(testName, "/", 2)[0]
22+
parentTestName := strings.SplitN(testName, "/", 2)[0] // Extract parent test
3223
if path, exists := testFileMap[parentTestName]; exists {
3324
filePath = path
3425
}
3526
} else if path, exists := testFileMap[testName]; exists {
27+
// Handle normal tests
3628
filePath = path
3729
}
3830

3931
if filePath != "" {
40-
relFilePath, err := filepath.Rel(rootDir, filePath)
41-
if err != nil {
42-
return fmt.Errorf("error getting relative path: %v", err)
43-
}
44-
report.Results[i].TestPath = filepath.ToSlash(relFilePath)
32+
report.Results[i].TestPath = filePath
4533
} else {
46-
fmt.Printf("TestName not mapped: %s\n", testName)
34+
// Log or mark tests not found in the codebase
4735
report.Results[i].TestPath = "NOT FOUND"
4836
}
4937
}

0 commit comments

Comments
 (0)