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 082e500 commit c7b5fde
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions datastore/datastore_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

package datastore

import (
Expand Down
6 changes: 3 additions & 3 deletions datastore/datastore_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestParseUNIX(t *testing.T) {
func TestParseWindows(t *testing.T) {
var tests = []struct {
in string
wantType Type
Expand All @@ -21,8 +21,8 @@ func TestParseUNIX(t *testing.T) {
{"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},
{"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) {
Expand Down
8 changes: 4 additions & 4 deletions internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func RootPath(base string) (string, error) {
if fi, err := os.Stat(gitConfig); err == nil && !fi.IsDir() {
return p, nil
}
if p == "/" {
if filepath.Dir(p) == p {
// root directory
break
}
p = filepath.Dir(p)
Expand All @@ -36,15 +37,14 @@ func RootPath(base string) (string, error) {
func DetectPrefix(gitRoot, wd string, files, cfiles []string) string {
var rcfiles [][]string
for _, f := range cfiles {
s := strings.Split(f, "/")
s := strings.Split(filepath.FromSlash(f), string(filepath.Separator))
reverse(s)
rcfiles = append(rcfiles, s)
}

var rfiles [][]string
for _, f := range files {
s := strings.Split(f, "/")
// reverse slice
s := strings.Split(filepath.FromSlash(f), string(filepath.Separator))
reverse(s)
rfiles = append(rfiles, s)
}
Expand Down
16 changes: 11 additions & 5 deletions internal/path_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -68,10 +69,15 @@ func TestDetectPrefix(t *testing.T) {
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"./foo/file.txt"}, ""},
// {"/path/a/b/c/github.com/owner/repo/", "/path/a/b/c/github.com/owner/repo/foo", []string{"/path/a/b/c/github.com/owner/repo/bar/config/config.go", "/path/a/b/c/github.com/owner/repo/foo/config/config.go", "/path/a/b/c/github.com/owner/repo/foo/entity/hello.go"}, []string{"github.com/owner/repo/foo/config/config.go", "github.com/owner/repo/foo/entity/hello.go"}, "github.com/owner/repo/foo"},
}
for _, tt := range tests {
got := DetectPrefix(tt.gitRoot, tt.wd, tt.files, tt.cfiles)
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
for i, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Parallel()
got := DetectPrefix(tt.gitRoot, tt.wd, tt.files, tt.cfiles)
want := filepath.FromSlash(tt.want)
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
})
}
}
4 changes: 2 additions & 2 deletions ratio/ratio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestPathMatch(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want := "ratio/ratio_test.go"
want := filepath.FromSlash("ratio/ratio_test.go")
ok := false
for _, f := range got.CodeFiles {
if f.Path == want {
Expand All @@ -124,7 +124,7 @@ func TestPathMatch(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want := "ratio/ratio_test.go"
want := filepath.FromSlash("ratio/ratio_test.go")
ok := false
for _, f := range got.CodeFiles {
if f.Path == want {
Expand Down

0 comments on commit c7b5fde

Please sign in to comment.