Skip to content

Commit 1d0203b

Browse files
committed
fix windows path
1 parent f48c192 commit 1d0203b

File tree

9 files changed

+114
-13
lines changed

9 files changed

+114
-13
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ${{ matrix.os }}
99
strategy:
1010
matrix:
11-
os: [ubuntu-latest, macos-latest]
11+
os: [ubuntu-latest, macos-latest, windows-latest]
1212

1313
steps:
1414
- name: checkout

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

@@ -81,7 +83,7 @@ func findFile(file, prefix string) (string, string, error) {
8183

8284
dir, file := filepath.Split(file)
8385

84-
pkg, err := build.Import(dir, ".", build.FindOnly)
86+
pkg, err := build.Import(path.Normalize(dir), ".", build.FindOnly)
8587
if err != nil {
8688
return "", "", fmt.Errorf("can't find file %q: %w", profileFile, err)
8789
}

pkg/testcoverage/coverage/cover_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package coverage_test
22

33
import (
4+
"runtime"
45
"strings"
56
"testing"
67

@@ -20,11 +21,16 @@ const (
2021
profileNOK = testdataDir + testdata.ProfileNOK
2122
profileNOKInvalidLength = testdataDir + testdata.ProfileNOKInvalidLength
2223
profileNOKInvalidData = testdataDir + testdata.ProfileNOKInvalidData
23-
24-
prefix = "github.com/vladopajic/go-test-coverage/v2"
25-
coverFilename = "pkg/testcoverage/coverage/cover.go"
2624
)
2725

26+
var prefix = func() string { //nolint:gochecknoglobals // relax
27+
if runtime.GOOS == "windows" {
28+
return "go-test-coverage/go-test-coverage"
29+
}
30+
31+
return "github.com/vladopajic/go-test-coverage/v2"
32+
}()
33+
2834
func Test_GenerateCoverageStats(t *testing.T) {
2935
t.Parallel()
3036

@@ -92,7 +98,7 @@ func Test_findFile(t *testing.T) {
9298
return
9399
}
94100

95-
const filename = "pkg/testcoverage/coverage/cover.go"
101+
filename := "pkg/testcoverage/coverage/cover.go"
96102

97103
file, noPrefixName, err := FindFile(prefix+"/"+filename, "")
98104
assert.NoError(t, err)

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
CoverageForFile = coverageForFile
9+
StripPrefix = stripPrefix
910
)
1011

1112
type Extent = extent

pkg/testcoverage/coverage/types.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ func stripPrefix(name, prefix string) string {
4040
prefix += "/"
4141
}
4242

43-
return strings.Replace(name, prefix, "", 1)
43+
prefix = path.Normalize(prefix)
44+
45+
if i := strings.Index(name, prefix); i >= 0 {
46+
return name[i+len(prefix):]
47+
}
48+
49+
return name
4450
}
4551

4652
func matches(regexps []*regexp.Regexp, str string) bool {
@@ -61,7 +67,6 @@ func compileExcludePathRules(excludePaths []string) []*regexp.Regexp {
6167
compiled := make([]*regexp.Regexp, len(excludePaths))
6268

6369
for i, pattern := range excludePaths {
64-
pattern = path.NormalizePathInRegex(pattern)
6570
compiled[i] = regexp.MustCompile(pattern)
6671
}
6772

pkg/testcoverage/coverage/types_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/stretchr/testify/assert"
77

88
. "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/coverage"
9+
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
910
)
1011

1112
func TestCoveredPercentage(t *testing.T) {
@@ -28,3 +29,39 @@ func TestCoveredPercentage(t *testing.T) {
2829
assert.Equal(t, tc.percentage, CoveredPercentage(tc.total, tc.covered))
2930
}
3031
}
32+
33+
func TestStripPrefix(t *testing.T) {
34+
t.Parallel()
35+
36+
tests := []struct {
37+
file string
38+
prefix string
39+
noPrefix string
40+
}{
41+
{
42+
file: path.Normalize("github.com/example/foo/bar.go"),
43+
prefix: "github.com/example",
44+
noPrefix: path.Normalize("foo/bar.go"),
45+
},
46+
{
47+
file: path.Normalize("github.com/example/foo/bar.go"),
48+
prefix: "github.com/example/",
49+
noPrefix: path.Normalize("foo/bar.go"),
50+
},
51+
{
52+
file: path.Normalize("github.com/example/foo/bar.go"),
53+
prefix: "example/foo",
54+
noPrefix: path.Normalize("bar.go"),
55+
},
56+
{
57+
file: path.Normalize("github.com/example/foo/bar.go"),
58+
prefix: "github.com/example1/",
59+
noPrefix: path.Normalize("github.com/example/foo/bar.go"),
60+
},
61+
}
62+
63+
for _, tc := range tests {
64+
name := StripPrefix(tc.file, tc.prefix)
65+
assert.Equal(t, tc.noPrefix, name)
66+
}
67+
}

pkg/testcoverage/path/path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
const separatorToReplace = string(filepath.Separator)
1010

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

pkg/testcoverage/path/path_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package path_test
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
. "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/path"
10+
)
11+
12+
type testCase struct {
13+
input string
14+
output string
15+
}
16+
17+
func Test_NormalizePath_NoWin(t *testing.T) {
18+
t.Parallel()
19+
20+
if runtime.GOOS == "windows" {
21+
return
22+
}
23+
24+
tests := []testCase{
25+
{
26+
input: "foo/bar",
27+
output: "foo/bar",
28+
},
29+
}
30+
31+
for _, tc := range tests {
32+
assert.Equal(t, tc.output, Normalize(tc.input))
33+
}
34+
}
35+
36+
func Test_NormalizePath_Win(t *testing.T) {
37+
t.Parallel()
38+
39+
if runtime.GOOS != "windows" {
40+
return
41+
}
42+
43+
tests := []testCase{
44+
{
45+
input: "foo/bar",
46+
output: "foo\\bar",
47+
},
48+
}
49+
50+
for _, tc := range tests {
51+
assert.Equal(t, tc.output, Normalize(tc.input))
52+
}
53+
}

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)