11package reports
22
33import (
4+ "fmt"
45 "go/ast"
56 "go/parser"
67 "go/token"
@@ -16,8 +17,14 @@ type TestFileMap map[string]string
1617func 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 })
0 commit comments