Skip to content

Commit 2b6bd60

Browse files
feat: add caching in findFile method (#138)
Co-authored-by: Subham Bhardwaj <subham.bhardwaj@zomato.com>
1 parent ad5500f commit 2b6bd60

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

pkg/testcoverage/coverage/cover.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,47 @@ type Config struct {
2323
ExcludePaths []string
2424
}
2525

26+
func findFileCreator() func(file, prefix string) (string, string, error) {
27+
cache := make(map[string]*build.Package)
28+
29+
return func(file, prefix string) (string, string, error) {
30+
profileFile := file
31+
32+
noPrefixName := stripPrefix(file, prefix)
33+
if _, err := os.Stat(noPrefixName); err == nil { // coverage-ignore
34+
return noPrefixName, noPrefixName, nil
35+
}
36+
37+
dir, file := filepath.Split(file)
38+
pkg, exists := cache[dir]
39+
40+
if !exists {
41+
var err error
42+
43+
pkg, err = build.Import(dir, ".", build.FindOnly)
44+
if err != nil {
45+
return "", "", fmt.Errorf("can't find file %q: %w", profileFile, err)
46+
}
47+
48+
cache[dir] = pkg
49+
}
50+
51+
file = filepath.Join(pkg.Dir, file)
52+
if _, err := os.Stat(file); err == nil {
53+
return file, stripPrefix(path.NormalizeForTool(file), path.NormalizeForTool(pkg.Root)), nil
54+
}
55+
56+
return "", "", fmt.Errorf("can't find file %q", profileFile)
57+
}
58+
}
59+
2660
func GenerateCoverageStats(cfg Config) ([]Stats, error) {
2761
profiles, err := parseProfiles(cfg.Profiles)
2862
if err != nil {
2963
return nil, fmt.Errorf("parsing profiles: %w", err)
3064
}
3165

66+
findFile := findFileCreator()
3267
fileStats := make([]Stats, 0, len(profiles))
3368
excludeRules := compileExcludePathRules(cfg.ExcludePaths)
3469

@@ -72,30 +107,6 @@ func GenerateCoverageStats(cfg Config) ([]Stats, error) {
72107
return fileStats, nil
73108
}
74109

75-
// findFile finds the location of the named file in GOROOT, GOPATH etc.
76-
func findFile(file, prefix string) (string, string, error) {
77-
profileFile := file
78-
79-
noPrefixName := stripPrefix(file, prefix)
80-
if _, err := os.Stat(noPrefixName); err == nil { // coverage-ignore
81-
return noPrefixName, noPrefixName, nil
82-
}
83-
84-
dir, file := filepath.Split(file)
85-
86-
pkg, err := build.Import(dir, ".", build.FindOnly)
87-
if err != nil {
88-
return "", "", fmt.Errorf("can't find file %q: %w", profileFile, err)
89-
}
90-
91-
file = filepath.Join(pkg.Dir, file)
92-
if _, err := os.Stat(file); err == nil {
93-
return file, stripPrefix(path.NormalizeForTool(file), path.NormalizeForTool(pkg.Root)), nil
94-
}
95-
96-
return "", "", fmt.Errorf("can't find file %q", profileFile)
97-
}
98-
99110
func findAnnotations(source []byte) ([]extent, error) {
100111
fset := token.NewFileSet()
101112

pkg/testcoverage/coverage/export_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package coverage
22

33
var (
4-
FindFile = findFile
4+
FindFile = findFileCreator()
55
FindAnnotations = findAnnotations
66
FindFuncsAndBlocks = findFuncsAndBlocks
77
ParseProfiles = parseProfiles

0 commit comments

Comments
 (0)