Skip to content

Commit e459ad4

Browse files
authored
fix(coverage): improved go.mod file search (#192)
1 parent 2e10c49 commit e459ad4

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

pkg/testcoverage/coverage/cover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func listAllFiles(rootDir string) []fileInfo {
208208
return nil
209209
})
210210
if err != nil { // coverage-ignore
211-
logger.L.Error().Err(err).Msg("listing go files")
211+
logger.L.Error().Err(err).Msg("listing files (.go files search)")
212212
}
213213

214214
return files

pkg/testcoverage/coverage/export_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var (
66
FindFuncsAndBlocks = findFuncsAndBlocks
77
ParseProfiles = parseProfiles
88
SumCoverage = sumCoverage
9+
FindGoModFile = findGoModFile
910
)
1011

1112
type Extent = extent

pkg/testcoverage/coverage/module.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ import (
1212
func findModuleDirective(rootDir string) string {
1313
goModFile := findGoModFile(rootDir)
1414
if goModFile == "" {
15-
logger.L.Warn().Str("dir", rootDir).Msg("could not find go.mod file in root dir")
15+
logger.L.Warn().Str("dir", rootDir).
16+
Msg("go.mod file not found in root directory (consider setting up source dir)")
1617
return ""
1718
}
1819

20+
logger.L.Debug().Str("file", goModFile).Msg("go.mod file found")
21+
1922
module := readModuleDirective(goModFile)
2023
if module == "" { // coverage-ignore
2124
logger.L.Warn().Msg("`module` directive not found")
@@ -27,23 +30,61 @@ func findModuleDirective(rootDir string) string {
2730
}
2831

2932
func findGoModFile(rootDir string) string {
30-
var goModFile string
33+
goModFile := findGoModFromRoot(rootDir)
34+
if goModFile != "" {
35+
return goModFile
36+
}
37+
38+
// fallback to find first go mod file wherever it may be
39+
// not really sure if we really need this ???
40+
return findGoModWithWalk(rootDir)
41+
}
42+
43+
func findGoModWithWalk(rootDir string) string { // coverage-ignore
44+
var goModFiles []string
3145

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

3851
if info.Name() == "go.mod" {
39-
goModFile = file
40-
return filepath.SkipAll
52+
goModFiles = append(goModFiles, file)
4153
}
4254

4355
return nil
4456
})
57+
if err != nil {
58+
logger.L.Error().Err(err).Msg("listing files (go.mod search)")
59+
}
60+
61+
if len(goModFiles) == 0 {
62+
logger.L.Warn().Msg("go.mod file not found via walk method")
63+
return ""
64+
}
65+
66+
if len(goModFiles) > 1 {
67+
logger.L.Warn().Msg("found multiple go.mod files via walk method")
68+
return ""
69+
}
70+
71+
return goModFiles[0]
72+
}
73+
74+
func findGoModFromRoot(rootDir string) string {
75+
files, err := os.ReadDir(rootDir)
76+
if err != nil { // coverage-ignore
77+
logger.L.Error().Err(err).Msg("reading directory")
78+
return ""
79+
}
80+
81+
for _, info := range files {
82+
if info.Name() == "go.mod" {
83+
return filepath.Join(rootDir, info.Name())
84+
}
85+
}
4586

46-
return goModFile
87+
return ""
4788
}
4889

4990
func readModuleDirective(filename string) string {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package coverage_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
. "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/coverage"
9+
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
10+
)
11+
12+
func Test_FindGoModFile(t *testing.T) {
13+
t.Parallel()
14+
15+
assert.Empty(t, FindGoModFile(""))
16+
assert.Equal(t, "../../../go.mod", path.NormalizeForTool(FindGoModFile("../../../")))
17+
}

0 commit comments

Comments
 (0)