Skip to content

feat: find file via file system search #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions pkg/testcoverage/coverage/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ func findFiles(profiles []*cover.Profile, prefix string) (map[string]fileInfo, e
return result, nil
}

func findFileCreator() func(file, prefix string) (string, string, bool) {
//nolint:maintidx // relax
func findFileCreator() func(file, prefix string) (string, string, bool) { // coverage-ignore
cache := make(map[string]*build.Package)
files := []string(nil)

findRelative := func(file, prefix string) (string, string, bool) {
noPrefixName := stripPrefix(file, prefix)
Expand Down Expand Up @@ -153,8 +155,23 @@ func findFileCreator() func(file, prefix string) (string, string, bool) {
return file, noPrefixName, err == nil
}

findWalk := func(file, prefix string) (string, string, bool) {
if files == nil {
files = listAllFiles("./")
}

noPrefixName := stripPrefix(file, prefix)
f, found := hasFile(files, noPrefixName)

return path.NormalizeForOS(f), noPrefixName, found
}

return func(file, prefix string) (string, string, bool) {
if fPath, fNoPrefix, found := findRelative(file, prefix); found { // coverage-ignore
if fPath, fNoPrefix, found := findRelative(file, prefix); found {
return fPath, fNoPrefix, found
}

if fPath, fNoPrefix, found := findWalk(file, prefix); found {
return fPath, fNoPrefix, found
}

Expand All @@ -166,6 +183,43 @@ func findFileCreator() func(file, prefix string) (string, string, bool) {
}
}

func listAllFiles(rootDir string) []string {
var files []string

//nolint:errcheck // error ignored because there is fallback mechanism for finding files
filepath.Walk(rootDir, func(file string, info os.FileInfo, err error) error {
if err != nil { // coverage-ignore
return err
}

if !info.IsDir() &&
strings.HasSuffix(file, ".go") &&
!strings.HasSuffix(file, "_test.go") {
files = append(files, path.NormalizeForTool(file))
}

return nil
})

return files
}

func hasFile(files []string, search string) (string, bool) {
var result string

for _, f := range files {
if strings.HasSuffix(f, search) {
if result != "" {
return "", false
}

result = f
}
}

return result, result != ""
}

func findAnnotations(source []byte) ([]extent, error) {
fset := token.NewFileSet()

Expand Down
Loading