Skip to content

Commit

Permalink
Fix path for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Jan 24, 2024
1 parent 535ddd2 commit ba589bf
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 19 deletions.
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
6 changes: 3 additions & 3 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 Down Expand Up @@ -130,7 +130,7 @@ func (fc FileCoverages) PathPrefix() (string, error) { //nostyle:recvtype
p = p[:i]
}
s := strings.Join(p, "/")
if s == "" && strings.HasPrefix(fc[0].File, "/") {
if s == "" && filepath.IsAbs(fc[0].File) {
s = "/"
}
if s == "." {
Expand Down Expand Up @@ -174,7 +174,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
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
45 changes: 45 additions & 0 deletions datastore/datastore_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package datastore

import (
"path/filepath"
"testing"

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

func TestParseUNIX(t *testing.T) {

Check failure on line 10 in datastore/datastore_unix_test.go

View workflow job for this annotation

GitHub Actions / Test for Windows

other declaration of TestParseUNIX
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 TestParseUNIX(t *testing.T) {

Check failure on line 10 in datastore/datastore_windows_test.go

View workflow job for this annotation

GitHub Actions / Test for Windows

TestParseUNIX redeclared in this block
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)
}
})
}
}
6 changes: 6 additions & 0 deletions ratio/ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func Measure(root string, code, test []string) (*Ratio, error) {
ratio := New()
defined := gocloc.NewDefinedLanguages()
opts := gocloc.NewClocOptions()
for i, p := range code {
code[i] = filepath.FromSlash(p)
}
for i, p := range test {
test[i] = filepath.FromSlash(p)
}

if err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if err != nil {
Expand Down

0 comments on commit ba589bf

Please sign in to comment.