From c7b5fde945d3f5c11dac47cbe72a9e3ecfd62e2c Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 24 Jan 2024 12:02:39 +0900 Subject: [PATCH] Fix path for Windows --- datastore/datastore_unix_test.go | 2 ++ datastore/datastore_windows_test.go | 6 +++--- internal/path.go | 8 ++++---- internal/path_test.go | 16 +++++++++++----- ratio/ratio_test.go | 4 ++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/datastore/datastore_unix_test.go b/datastore/datastore_unix_test.go index 54ab6d8d..c326ef4d 100644 --- a/datastore/datastore_unix_test.go +++ b/datastore/datastore_unix_test.go @@ -1,3 +1,5 @@ +//go:build !windows + package datastore import ( diff --git a/datastore/datastore_windows_test.go b/datastore/datastore_windows_test.go index b0c97a8c..18d8b755 100644 --- a/datastore/datastore_windows_test.go +++ b/datastore/datastore_windows_test.go @@ -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 @@ -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) { diff --git a/internal/path.go b/internal/path.go index 7551fdf1..6b689479 100644 --- a/internal/path.go +++ b/internal/path.go @@ -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) @@ -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) } diff --git a/internal/path_test.go b/internal/path_test.go index f84b3945..ca0cb4c6 100644 --- a/internal/path_test.go +++ b/internal/path_test.go @@ -1,6 +1,7 @@ package internal import ( + "fmt" "os" "path/filepath" "testing" @@ -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) + } + }) } } diff --git a/ratio/ratio_test.go b/ratio/ratio_test.go index be275996..2719ff4e 100644 --- a/ratio/ratio_test.go +++ b/ratio/ratio_test.go @@ -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 { @@ -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 {