From c3264183b2b77ffe7fdc4a32c1a16ac9f856934e 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 | 22 ++++++++------- internal/path_test.go | 30 --------------------- internal/path_unix_test.go | 42 +++++++++++++++++++++++++++++ internal/path_windows_test.go | 39 +++++++++++++++++++++++++++ ratio/ratio_test.go | 4 +-- 7 files changed, 101 insertions(+), 44 deletions(-) create mode 100644 internal/path_unix_test.go create mode 100644 internal/path_windows_test.go 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..f7555f49 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,16 @@ 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)) + fmt.Println("cfile", f, s) 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)) + fmt.Println("file", f, s) reverse(s) rfiles = append(rfiles, s) } @@ -66,18 +68,20 @@ func DetectPrefix(gitRoot, wd string, files, cfiles []string) string { detect := func(s []string, i, j int) string { // reverse slice + fmt.Println(s) reverse(s) + fmt.Println(s) suffix := filepath.Join(s...) cfile := cfiles[i] cfp := strings.TrimSuffix(cfile, suffix) 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)) diff --git a/internal/path_test.go b/internal/path_test.go index f84b3945..2024f45e 100644 --- a/internal/path_test.go +++ b/internal/path_test.go @@ -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) - } - } -} diff --git a/internal/path_unix_test.go b/internal/path_unix_test.go new file mode 100644 index 00000000..42b1933e --- /dev/null +++ b/internal/path_unix_test.go @@ -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) + } + }) + } +} diff --git a/internal/path_windows_test.go b/internal/path_windows_test.go new file mode 100644 index 00000000..40dd231d --- /dev/null +++ b/internal/path_windows_test.go @@ -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) + } + }) + } +} 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 {