Skip to content

Commit

Permalink
Merge pull request #315 from k1LoW/windows
Browse files Browse the repository at this point in the history
Fix path handling for Windows
  • Loading branch information
k1LoW authored Jan 24, 2024
2 parents 6769f96 + 0213348 commit 448ec00
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 141 deletions.
32 changes: 29 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:
- name: Check out source code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Go
id: setup-go
Expand Down Expand Up @@ -72,4 +70,32 @@ jobs:
- name: Show rate limit
run: |
curl -sL -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/rate_limit
test-windows:
name: Test for Windows
runs-on: windows-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEBUG: 1
steps:
- name: Use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Check out source code
uses: actions/checkout@v4

- name: Set up Go
id: setup-go
uses: actions/setup-go@v4
with:
go-version-file: go.mod
check-latest: true

- name: Run tests
run: make ci

- name: Run test_central
if: ${{ github.event_name == 'pull_request' }}
run: make test_central
6 changes: 3 additions & 3 deletions central/central.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"text/template"
"time"

"github.com/k1LoW/octocov/badge"
"github.com/k1LoW/octocov/datastore"
"github.com/k1LoW/octocov/datastore/local"
"github.com/k1LoW/octocov/gh"
"github.com/k1LoW/octocov/internal"
"github.com/k1LoW/octocov/badge"
"github.com/k1LoW/octocov/report"
)

Expand Down Expand Up @@ -266,8 +266,8 @@ func (c *Central) renderIndex(wr io.Writer) error {
d := map[string]any{
"Host": host,
"Reports": c.reports,
"BadgesLinkRel": badgesLinkRel,
"BadgesURLRel": badgesURLRel,
"BadgesLinkRel": filepath.ToSlash(badgesLinkRel),
"BadgesURLRel": filepath.ToSlash(badgesURLRel),
"RootURL": rootURL,
"IsPrivate": isPrivate,
"Query": query,
Expand Down
4 changes: 2 additions & 2 deletions config/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/k1LoW/octocov/internal"
)
Expand All @@ -28,6 +27,7 @@ func (c *Config) Build() {
} else {
var paths []string
for _, p := range c.Coverage.Paths {
p = filepath.FromSlash(p)
paths = append(paths, filepath.Join(filepath.Dir(c.path), p))
}
c.Coverage.Paths = paths
Expand All @@ -45,7 +45,7 @@ func (c *Config) Build() {
if c.Central.Root == "" {
c.Central.Root = "."
}
if !strings.HasPrefix(c.Central.Root, "/") {
if !filepath.IsAbs(c.Central.Root) {
c.Central.Root = filepath.Clean(filepath.Join(c.Root(), c.Central.Root))
}
if len(c.Central.Reports.Datastores) == 0 {
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (c *Config) Setwd(path string) {
}

func (c *Config) Load(path string) error {
path = filepath.FromSlash(path)
if path == "" {
for _, p := range DefaultPaths {
if f, err := os.Stat(filepath.Join(c.wd, p)); err == nil && !f.IsDir() {
Expand All @@ -165,7 +166,7 @@ func (c *Config) Load(path string) error {
if path == "" {
return nil
}
if strings.HasPrefix(path, "/") {
if filepath.IsAbs(path) {
c.path = path
} else {
c.path = filepath.Join(c.wd, path)
Expand Down
8 changes: 6 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,17 @@ func TestCoveragePaths(t *testing.T) {
for _, tt := range tests {
t.Run(fmt.Sprintf("%v", tt.paths), func(t *testing.T) {
c := New()
c.path = tt.configPath
c.path = filepath.FromSlash(tt.configPath)
c.Coverage = &Coverage{
Paths: tt.paths,
}
c.Build()
got := c.Coverage.Paths
if diff := cmp.Diff(got, tt.want, nil); diff != "" {
var want []string
for _, p := range tt.want {
want = append(want, filepath.FromSlash(p))
}
if diff := cmp.Diff(got, want, nil); diff != "" {
t.Error(diff)
}
})
Expand Down
36 changes: 2 additions & 34 deletions coverage/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (fc FileCoverages) FuzzyFindByFile(file string) (*FileCoverage, error) { //
continue
}
// When coverages are recorded in the package path. ( ex. org/repo/package/path/to/Target.kt
if !strings.HasPrefix(c.File, "/") && strings.HasSuffix(file, c.File) {
if !filepath.IsAbs(c.File) && strings.HasSuffix(file, c.File) {
if match == nil || len(match.File) > len(c.File) {
match = c
}
Expand All @@ -107,38 +107,6 @@ func (fc FileCoverages) FuzzyFindByFile(file string) (*FileCoverage, error) { //
return nil, fmt.Errorf("file name not found: %s", file)
}

func (fc FileCoverages) PathPrefix() (string, error) { //nostyle:recvtype
if len(fc) == 0 {
return "", errors.New("no file coverages")
}
p := strings.Split(filepath.Dir(filepath.ToSlash(fc[0].File)), "/")
for _, c := range fc {
d := strings.Split(filepath.Dir(filepath.ToSlash(c.File)), "/")
i := 0
for {
if len(p) <= i {
break
}
if len(d) <= i {
break
}
if p[i] != d[i] {
break
}
i += 1
}
p = p[:i]
}
s := strings.Join(p, "/")
if s == "" && strings.HasPrefix(fc[0].File, "/") {
s = "/"
}
if s == "." {
s = ""
}
return s, nil
}

func (fc *FileCoverage) FindBlocksByLine(n int) BlockCoverages {
if fc == nil {
return BlockCoverages{}
Expand Down Expand Up @@ -174,7 +142,7 @@ func (dc DiffFileCoverages) FuzzyFindByFile(file string) (*DiffFileCoverage, err
continue
}
// When coverages are recorded in the package path. ( ex. org/repo/package/path/to/Target.kt
if !strings.HasPrefix(c.File, "/") && strings.HasSuffix(file, c.File) {
if !filepath.IsAbs(c.File) && strings.HasSuffix(file, c.File) {
if match == nil || len(match.File) > len(c.File) {
match = c
}
Expand Down
51 changes: 0 additions & 51 deletions coverage/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,57 +90,6 @@ func TestCompare(t *testing.T) {
}
}

func TestPathPrefix(t *testing.T) {
tests := []struct {
files FileCoverages
want string
}{
{
FileCoverages{
&FileCoverage{File: "file_a.go"},
},
"",
},
{
FileCoverages{
&FileCoverage{File: "path/to/file_a.go"},
&FileCoverage{File: "path/file_b.go"},
},
"path",
},
{
FileCoverages{
&FileCoverage{File: "/path/to/file_a.go"},
&FileCoverage{File: "/path/file_b.go"},
},
"/path",
},
{
FileCoverages{
&FileCoverage{File: "/path/to/foo/file_a.go"},
&FileCoverage{File: "/path/to/foo/bar/file_b.go"},
},
"/path/to/foo",
},
{
FileCoverages{
&FileCoverage{File: "/to/foo/file_a.go"},
&FileCoverage{File: "/path/to/foo/bar/file_b.go"},
},
"/",
},
}
for _, tt := range tests {
got, err := tt.files.PathPrefix()
if err != nil {
t.Fatal(err)
}
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
}
}

func TestMaxCount(t *testing.T) {
tests := []struct {
blocks BlockCoverages
Expand Down
7 changes: 6 additions & 1 deletion datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"

"cloud.google.com/go/bigquery"
Expand Down Expand Up @@ -222,7 +223,11 @@ func parse(u, root string) (Type, []string, error) {
return Mackerel, []string{service}, nil
default:
p := strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(u, "file://"), "local://"), "/")
if strings.HasPrefix(p, "/") {
if runtime.GOOS == "windows" && strings.HasPrefix(p, "/") {
return UnknownType, nil, fmt.Errorf("invalid file path: %s", u)
}
p = filepath.FromSlash(p)
if filepath.IsAbs(p) {
root = p
} else {
root = filepath.Join(root, p)
Expand Down
7 changes: 0 additions & 7 deletions datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ func TestParse(t *testing.T) {
{"mackerel://service", Mackerel, []string{"service"}, false},
{"mkr://service", Mackerel, []string{"service"}, false},
{"mkr://service/foo", UnknownType, []string{}, true},
{"file://reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"file:///reports", Local, []string{"/reports"}, false},
{"/reports", Local, []string{"/reports"}, false},
{"local://reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"local://./reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"local:///reports", Local, []string{"/reports"}, false},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
Expand Down
47 changes: 47 additions & 0 deletions datastore/datastore_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build !windows

package datastore

import (
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestParseUNIX(t *testing.T) {
var tests = []struct {
in string
wantType Type
wantArgs []string
wantError bool
}{
{"file://reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"file:///reports", Local, []string{"/reports"}, false},
{"/reports", Local, []string{"/reports"}, false},
{"local://reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"local://./reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"local:///reports", Local, []string{"/reports"}, false},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
gotType, gotArgs, err := parse(tt.in, testdataDir(t))
if err != nil {
if !tt.wantError {
t.Errorf("got %v", err)
}
return
}
if err == nil && tt.wantError {
t.Error("want error")
}
if gotType != tt.wantType {
t.Errorf("got %v\nwant %v", gotType, tt.wantType)
}
if diff := cmp.Diff(gotArgs, tt.wantArgs, nil); diff != "" {
t.Error(diff)
}
})
}
}
47 changes: 47 additions & 0 deletions datastore/datastore_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package datastore

import (
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestParseWindows(t *testing.T) {
var tests = []struct {
in string
wantType Type
wantArgs []string
wantError bool
}{
{"file://reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"file:///reports", UnknownType, nil, true},
{"/reports", UnknownType, nil, true},
{"local://reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"local://./reports", Local, []string{filepath.Join(testdataDir(t), "reports")}, false},
{"local:///reports", UnknownType, nil, true},
{"local://C:/reports", Local, []string{"C:\\reports"}, false},
{"local://C:\\reports", Local, []string{"C:\\reports"}, false},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
gotType, gotArgs, err := parse(tt.in, testdataDir(t))
if err != nil {
if !tt.wantError {
t.Errorf("got %v", err)
}
return
}
if err == nil && tt.wantError {
t.Error("want error")
}
if gotType != tt.wantType {
t.Errorf("got %v\nwant %v", gotType, tt.wantType)
}
if diff := cmp.Diff(gotArgs, tt.wantArgs, nil); diff != "" {
t.Error(diff)
}
})
}
}
Loading

0 comments on commit 448ec00

Please sign in to comment.