Skip to content

Commit a26dd6b

Browse files
authored
Merge branch 'main' into no-docker-action-test
2 parents e5ea773 + 5b3616e commit a26dd6b

File tree

9 files changed

+83
-16
lines changed

9 files changed

+83
-16
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353

5454
- name: download artifact (main.breakdown)
5555
id: download-main-breakdown
56-
uses: dawidd6/action-download-artifact@v9
56+
uses: dawidd6/action-download-artifact@v10
5757
with:
5858
branch: main
5959
workflow_conclusion: success

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# GO_VERSION: automatically update to most recent via dependabot
2-
FROM golang:1.24.2 as builder
2+
FROM golang:1.24.3 as builder
33
WORKDIR /workspace
44

55
COPY go.mod go.mod

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
const (
12-
Version = "v2.14.1" // VERSION: when changing version update version in other places
12+
Version = "v2.14.3" // VERSION: when changing version update version in other places
1313
Name = "go-test-coverage"
1414
)
1515

pkg/testcoverage/badge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func generateAndSaveBadge(w io.Writer, cfg Config, totalCoverage int) error {
1919
buffer := &bytes.Buffer{}
2020
out := bufio.NewWriter(buffer)
2121

22-
// `out` writer is used as temporall buffer, which will be finally
22+
// `out` writer is used as temporary buffer, which will be finally
2323
// written to `w` in this defer call
2424
defer func() {
2525
out.Flush()

pkg/testcoverage/check.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ func Check(wout io.Writer, cfg Config) (bool, error) {
6363
}
6464

6565
if cfg.LocalPrefixDeprecated != "" { // coverage-ignore
66-
reportGHWarning(w, "Deprecated option",
67-
"local-prefix option is deprecated since v2.13.0, you can safely remove setting this option")
66+
//nolint:lll // relax
67+
msg := "`local-prefix` option is deprecated since v2.13.0, you can safely remove setting this option"
68+
logger.L.Warn().Msg(msg)
69+
reportGHWarning(w, "Deprecated option", msg)
6870
}
6971
}
7072

@@ -152,7 +154,7 @@ func loadBaseCoverageBreakdown(cfg Config) ([]coverage.Stats, error) {
152154

153155
stats, err := coverage.StatsDeserialize(data)
154156
if err != nil {
155-
return nil, fmt.Errorf("parsing file failed: %w", err)
157+
return nil, fmt.Errorf("deserializing stats file failed: %w", err)
156158
}
157159

158160
return stats, nil

pkg/testcoverage/coverage/cover.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"golang.org/x/tools/cover"
1616

17+
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/logger"
1718
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
1819
)
1920

@@ -47,6 +48,7 @@ func GenerateCoverageStats(cfg Config) ([]Stats, error) {
4748
}
4849

4950
if ok := matches(excludeRules, fi.name); ok {
51+
logger.L.Debug().Str("file", fi.name).Msg("file excluded")
5052
continue // this file is excluded
5153
}
5254

@@ -189,8 +191,7 @@ func listAllFiles(rootDir string) []fileInfo {
189191
return name
190192
}
191193

192-
//nolint:errcheck // error ignored because there is fallback mechanism for finding files
193-
filepath.Walk(rootDir, func(file string, info os.FileInfo, err error) error {
194+
err := filepath.Walk(rootDir, func(file string, info os.FileInfo, err error) error {
194195
if err != nil { // coverage-ignore
195196
return err
196197
}
@@ -206,6 +207,9 @@ func listAllFiles(rootDir string) []fileInfo {
206207

207208
return nil
208209
})
210+
if err != nil { // coverage-ignore
211+
logger.L.Error().Err(err).Msg("listing files (.go files search)")
212+
}
209213

210214
return files
211215
}

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: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,79 @@ 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")
2225
}
2326

27+
logger.L.Debug().Str("module", module).Msg("using module directive")
28+
2429
return module
2530
}
2631

2732
func findGoModFile(rootDir string) string {
28-
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
2945

30-
//nolint:errcheck // error ignored because there is fallback mechanism for finding files
31-
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 {
3247
if err != nil { // coverage-ignore
3348
return err
3449
}
3550

3651
if info.Name() == "go.mod" {
37-
goModFile = file
38-
return filepath.SkipAll
52+
goModFiles = append(goModFiles, file)
3953
}
4054

4155
return nil
4256
})
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+
}
4386

44-
return goModFile
87+
return ""
4588
}
4689

4790
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)