Skip to content

Commit 4a0c753

Browse files
authored
fix: paths normalize for windows (#121)
1 parent fbeef4f commit 4a0c753

File tree

8 files changed

+31
-23
lines changed

8 files changed

+31
-23
lines changed

.testcoverage.example.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,4 @@ exclude:
4343
# Exclude files or packages matching their paths
4444
paths:
4545
- \.pb\.go$ # excludes all protobuf generated files
46-
- ^pkg/bar # exclude package `pkg/bar`
47-
48-
# NOTES:
49-
# - symbol `/` in all path regexps will be replaced by current OS file path separator
50-
# to properly work on Windows.
46+
- ^pkg/bar # exclude package `pkg/bar`

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ exclude:
125125
paths:
126126
- \.pb\.go$ # excludes all protobuf generated files
127127
- ^pkg/bar # exclude package `pkg/bar`
128-
129-
# NOTES:
130-
# - symbol `/` in all path regexps will be replaced by current OS file path separator
131-
# to properly work on Windows.
132128
```
133129

134130
### Exclude Code from Coverage

pkg/testcoverage/badgestorer/github_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package badgestorer_test
22

33
import (
44
"context"
5+
crand "crypto/rand"
6+
"encoding/hex"
57
"fmt"
68
"os"
7-
"runtime"
89
"testing"
9-
"time"
1010

1111
"github.com/google/go-github/v56/github"
1212
"github.com/stretchr/testify/assert"
@@ -56,7 +56,7 @@ func Test_Github(t *testing.T) {
5656
Branch: "badges-integration-test",
5757
// badge name must be unique because two tests running from different platforms
5858
// in CI can cause race condition if badge has the same name
59-
FileName: fmt.Sprintf("badge_%s_%d.svg", runtime.GOOS, time.Now().UnixNano()),
59+
FileName: fmt.Sprintf("badge_%s.svg", randName()),
6060
}
6161
s := NewGithub(cfg)
6262

@@ -110,3 +110,14 @@ func deleteFile(t *testing.T, cfg Git) {
110110
)
111111
assert.NoError(t, err)
112112
}
113+
114+
func randName() string {
115+
buf := make([]byte, 20)
116+
117+
_, err := crand.Read(buf)
118+
if err != nil {
119+
panic(err) //nolint:forbidigo // okay here because it is only used for tests
120+
}
121+
122+
return hex.EncodeToString(buf)
123+
}

pkg/testcoverage/coverage/cover.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13+
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
14+
1315
"golang.org/x/tools/cover"
1416
)
1517

@@ -88,7 +90,7 @@ func findFile(file, prefix string) (string, string, error) {
8890

8991
file = filepath.Join(pkg.Dir, file)
9092
if _, err := os.Stat(file); err == nil {
91-
return file, stripPrefix(file, pkg.Root), nil
93+
return file, stripPrefix(path.NormalizeForTool(file), path.NormalizeForTool(pkg.Root)), nil
9294
}
9395

9496
return "", "", fmt.Errorf("can't find file %q", profileFile)

pkg/testcoverage/coverage/cover_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"golang.org/x/tools/cover"
99

1010
. "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/coverage"
11+
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
1112
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/testdata"
1213
)
1314

@@ -97,12 +98,12 @@ func Test_findFile(t *testing.T) {
9798
file, noPrefixName, err := FindFile(prefix+"/"+filename, "")
9899
assert.NoError(t, err)
99100
assert.Equal(t, filename, noPrefixName)
100-
assert.True(t, strings.HasSuffix(file, filename))
101+
assert.True(t, strings.HasSuffix(file, path.NormalizeForOS(filename)))
101102

102103
file, noPrefixName, err = FindFile(prefix+"/"+filename, prefix)
103104
assert.NoError(t, err)
104105
assert.Equal(t, filename, noPrefixName)
105-
assert.True(t, strings.HasSuffix(file, filename))
106+
assert.True(t, strings.HasSuffix(file, path.NormalizeForOS(filename)))
106107

107108
_, _, err = FindFile(prefix+"/main1.go", "")
108109
assert.Error(t, err)

pkg/testcoverage/coverage/types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package coverage
33
import (
44
"regexp"
55
"strings"
6-
7-
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
86
)
97

108
type Stats struct {
@@ -61,7 +59,6 @@ func compileExcludePathRules(excludePaths []string) []*regexp.Regexp {
6159
compiled := make([]*regexp.Regexp, len(excludePaths))
6260

6361
for i, pattern := range excludePaths {
64-
pattern = path.NormalizePathInRegex(pattern)
6562
compiled[i] = regexp.MustCompile(pattern)
6663
}
6764

pkg/testcoverage/path/path.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ import (
88

99
const separatorToReplace = string(filepath.Separator)
1010

11-
func NormalizePathInRegex(path string) string {
11+
func NormalizeForOS(path string) string {
1212
if runtime.GOOS != "windows" {
1313
return path
1414
}
1515

1616
return strings.ReplaceAll(path, "/", separatorToReplace) // coverage-ignore
1717
}
18+
19+
func NormalizeForTool(path string) string {
20+
if runtime.GOOS != "windows" {
21+
return path
22+
}
23+
24+
return strings.ReplaceAll(path, separatorToReplace, "/") // coverage-ignore
25+
}

pkg/testcoverage/utils.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package testcoverage
22

33
import (
44
"regexp"
5-
6-
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
75
)
86

97
type regRule struct {
@@ -29,9 +27,8 @@ func compileOverridePathRules(cfg Config) []regRule {
2927
compiled := make([]regRule, len(cfg.Override))
3028

3129
for i, o := range cfg.Override {
32-
pattern := path.NormalizePathInRegex(o.Path)
3330
compiled[i] = regRule{
34-
reg: regexp.MustCompile(pattern),
31+
reg: regexp.MustCompile(o.Path),
3532
threshold: o.Threshold,
3633
}
3734
}

0 commit comments

Comments
 (0)