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 16380b6
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 44 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
18 changes: 9 additions & 9 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 Expand Up @@ -73,11 +73,11 @@ func DetectPrefix(gitRoot, wd string, files, cfiles []string) string {
file := files[j]
fp := strings.TrimSuffix(file, suffix)

// fmt.Printf("gitRoot: %s\nwd: %s\n", gitRoot, wd)
// fmt.Printf("file: %s\ncfile: %s\n", file, cfile)
// fmt.Printf("suffix: %s\n", suffix)
// fmt.Printf("file_prefix: %s\ncfile_prefix: %s\n", fp, cfp)
// fmt.Printf("---\n")
fmt.Printf("gitRoot: %s\nwd: %s\n", gitRoot, wd)
fmt.Printf("file: %s\ncfile: %s\n", file, cfile)
fmt.Printf("suffix: %s\n", suffix)
fmt.Printf("file_prefix: %s\ncfile_prefix: %s\n", fp, cfp)
fmt.Printf("---\n")

if len(fp) < len(gitRoot) {
cfp = filepath.Join(cfp, strings.TrimPrefix(gitRoot, fp))
Expand Down
30 changes: 0 additions & 30 deletions internal/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,3 @@ func TestRootPath(t *testing.T) {
}
}
}

func TestDetectPrefix(t *testing.T) {
tests := []struct {
gitRoot string
wd string
files []string
cfiles []string
want string
}{
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"github.com/owner/repo/foo/file.txt"}, "github.com/owner/repo"},
{"/path/to", "/path/to/foo", []string{"/path/to/foo/file.txt"}, []string{"github.com/owner/repo/foo/file.txt"}, "github.com/owner/repo/foo"},
{"/path/to", "/path/to/bar", []string{"/path/to/foo/file.txt"}, []string{"github.com/owner/repo/foo/file.txt"}, "github.com/owner/repo/bar"},
{"/path/a/b/c/owner/repo", "/path/a/b/c/owner/repo/foo", []string{"/path/a/b/c/owner/repo/foo/bar/bar.txt", "/path/a/b/c/owner/repo/foo/one/two.txt"}, []string{"github.com/owner/repo/foo/bar/bar.txt", "github.com/owner/repo/foo/one/two.txt"}, "github.com/owner/repo/foo"},
{"/path/to", "/path/to", []string{"/path/to/central/central.go"}, []string{"github.com/owner/repo/central/central.go"}, "github.com/owner/repo"},
{"/path/to/github.com/owner/repo", "/path/to/github.com/owner/repo", []string{"/path/to/github.com/owner/repo/central/central.go"}, []string{"github.com/owner/repo/central/central.go"}, "github.com/owner/repo"},
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"/other/to/foo/file.txt"}, "/other/to"},
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"/path/to/foo/file.txt"}, "/path/to"},
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"/path/to/bar/foo/file.txt"}, "/path/to/bar"},
{"/path/to", "/path/to/foo", []string{"/path/to/foo/file.txt"}, []string{"/path/to/bar/foo/file.txt"}, "/path/to/bar/foo"},
{"/path/to", "/path/to/foo", []string{"/path/to/foo/file.txt"}, []string{"./foo/file.txt"}, "foo"},
{"/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)
}
}
}
42 changes: 42 additions & 0 deletions internal/path_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build !windows

package internal

import (
"fmt"
"testing"
)

func TestDetectPrefix(t *testing.T) {
tests := []struct {
gitRoot string
wd string
files []string
cfiles []string
want string
}{
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"github.com/owner/repo/foo/file.txt"}, "github.com/owner/repo"},
{"/path/to", "/path/to/foo", []string{"/path/to/foo/file.txt"}, []string{"github.com/owner/repo/foo/file.txt"}, "github.com/owner/repo/foo"},
{"/path/to", "/path/to/bar", []string{"/path/to/foo/file.txt"}, []string{"github.com/owner/repo/foo/file.txt"}, "github.com/owner/repo/bar"},
{"/path/a/b/c/owner/repo", "/path/a/b/c/owner/repo/foo", []string{"/path/a/b/c/owner/repo/foo/bar/bar.txt", "/path/a/b/c/owner/repo/foo/one/two.txt"}, []string{"github.com/owner/repo/foo/bar/bar.txt", "github.com/owner/repo/foo/one/two.txt"}, "github.com/owner/repo/foo"},
{"/path/to", "/path/to", []string{"/path/to/central/central.go"}, []string{"github.com/owner/repo/central/central.go"}, "github.com/owner/repo"},
{"/path/to/github.com/owner/repo", "/path/to/github.com/owner/repo", []string{"/path/to/github.com/owner/repo/central/central.go"}, []string{"github.com/owner/repo/central/central.go"}, "github.com/owner/repo"},
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"/other/to/foo/file.txt"}, "/other/to"},
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"/path/to/foo/file.txt"}, "/path/to"},
{"/path/to", "/path/to", []string{"/path/to/foo/file.txt"}, []string{"/path/to/bar/foo/file.txt"}, "/path/to/bar"},
{"/path/to", "/path/to/foo", []string{"/path/to/foo/file.txt"}, []string{"/path/to/bar/foo/file.txt"}, "/path/to/bar/foo"},
{"/path/to", "/path/to/foo", []string{"/path/to/foo/file.txt"}, []string{"./foo/file.txt"}, "foo"},
{"/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 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)
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
})
}
}
39 changes: 39 additions & 0 deletions internal/path_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal

import (
"fmt"
"testing"
)

func TestDetectPrefix(t *testing.T) {
tests := []struct {
gitRoot string
wd string
files []string
cfiles []string
want string
}{
// {"C:\\path\\to", "C:\\path\\to", []string{"C:\\path\\to\\foo\\file.txt"}, []string{"github.com\\owner\\repo\\foo\\file.txt"}, "github.com\\owner\\repo"},
// {"C:\\path\\to", "C:\\path\\to\\foo", []string{"C:\\path\\to\\foo\\file.txt"}, []string{"github.com\\owner\\repo\\foo\\file.txt"}, "github.com\\owner\\repo\\foo"},
// {"C:\\path\\to", "C:\\path\\to\\bar", []string{"C:\\path\\to\\foo\\file.txt"}, []string{"github.com\\owner\\repo\\foo\\file.txt"}, "github.com\\owner\\repo\\bar"},
// {"C:\\path\\a\\b\\c\\owner\\repo", "C:\\path\\a\\b\\c\\owner\\repo\\foo", []string{"C:\\path\\a\\b\\c\\owner\\repo\\foo\\bar\\bar.txt", "C:\\path\\a\\b\\c\\owner\\repo\\foo\\one\\two.txt"}, []string{"github.com\\owner\\repo\\foo\\bar\\bar.txt", "github.com\\owner\\repo\\foo\\one\\two.txt"}, "github.com\\owner\\repo\\foo"},
// {"C:\\path\\to", "C:\\path\\to", []string{"C:\\path\\to\\central\\central.go"}, []string{"github.com\\owner\\repo\\central\\central.go"}, "github.com\\owner\\repo"},
// {"C:\\path\\to\\github.com\\owner\\repo", "C:\\path\\to\\github.com\\owner\\repo", []string{"C:\\path\\to\\github.com\\owner\\repo\\central\\central.go"}, []string{"github.com\\owner\\repo\\central\\central.go"}, "github.com\\owner\\repo"},
// {"C:\\path\\to", "C:\\path\\to", []string{"C:\\path\\to\\foo\\file.txt"}, []string{"C:\\other\\to\\foo\\file.txt"}, "C:\\other\\to"},
{"C:\\path\\to", "C:\\path\\to", []string{"C:\\path\\to\\foo\\file.txt"}, []string{"C:\\path\\to\\foo\\file.txt"}, "C:\\path\\to"},
// {"C:\\path\\to", "C:\\path\\to", []string{"C:\\path\\to\\foo\\file.txt"}, []string{"C:\\path\\to\\bar\\foo\\file.txt"}, "C:\\path\\to\\bar"},
// {"C:\\path\\to", "C:\\path\\to\\foo", []string{"C:\\path\\to\\foo\\file.txt"}, []string{"C:\\path\\to\\bar\\foo\\file.txt"}, "C:\\path\\to\\bar\\foo"},
// {"C:\\path\\to", "C:\\path\\to\\foo", []string{"C:\\path\\to\\foo\\file.txt"}, []string{".\\foo\\file.txt"}, "foo"},
// {"C:\\path\\to", "C:\\path\\to", []string{"C:\\path\\to\\foo\\file.txt"}, []string{".\\foo\\file.txt"}, ""},
}
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)
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.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 16380b6

Please sign in to comment.