From ccab3933e2cc803b04c9a38e8ca0535a10b646b0 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Thu, 8 Dec 2022 13:08:43 +0100 Subject: [PATCH] Fix single start into double star Single star into double star did not work as expected. Pattern `a/b/*/e.f/**` would not match `a/b/c/e.f/g` since the top level glob would be performed on a path ending with a `/`. Using `filepath.Join(match, glob)` will remove the trailing slash. Two tests included for regression tests. And as a side bonus tests will now also pass on Windows. --- filepathx.go | 2 +- filepathx_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/filepathx.go b/filepathx.go index 0129314..93ce702 100644 --- a/filepathx.go +++ b/filepathx.go @@ -29,7 +29,7 @@ func (globs Globs) Expand() ([]string, error) { var hits []string var hitMap = map[string]bool{} for _, match := range matches { - paths, err := filepath.Glob(match + glob) + paths, err := filepath.Glob(filepath.Join(match, glob)) if err != nil { return nil, err } diff --git a/filepathx_test.go b/filepathx_test.go index 599ce11..4b1d97d 100644 --- a/filepathx_test.go +++ b/filepathx_test.go @@ -2,6 +2,7 @@ package filepathx import ( "os" + "path/filepath" "testing" ) @@ -19,7 +20,7 @@ func TestGlob_ZeroDoubleStars_oneMatch(t *testing.T) { if len(matches) != 1 { t.Fatalf("got %d matches, expected 1", len(matches)) } - expected := "a/b/c.d" + expected := filepath.Join("a", "b", "c.d") if matches[0] != expected { t.Fatalf("matched [%s], expected [%s]", matches[0], expected) } @@ -39,7 +40,7 @@ func TestGlob_OneDoubleStar_oneMatch(t *testing.T) { if len(matches) != 1 { t.Fatalf("got %d matches, expected 1", len(matches)) } - expected := "a/b/c.d/e.f" + expected := filepath.Join("a", "b", "c.d", "e.f") if matches[0] != expected { t.Fatalf("matched [%s], expected [%s]", matches[0], expected) } @@ -59,7 +60,7 @@ func TestGlob_OneDoubleStar_twoMatches(t *testing.T) { if len(matches) != 2 { t.Fatalf("got %d matches, expected 2", len(matches)) } - expected := []string{"a/b/c.d", "a/b/c.d/e.f"} + expected := []string{filepath.Join("a", "b", "c.d"), filepath.Join("a", "b", "c.d", "e.f")} for i, match := range matches { if match != expected[i] { t.Fatalf("matched [%s], expected [%s]", match, expected[i]) @@ -81,7 +82,7 @@ func TestGlob_TwoDoubleStars_oneMatch(t *testing.T) { if len(matches) != 1 { t.Fatalf("got %d matches, expected 1", len(matches)) } - expected := "a/b/c.d/e.f" + expected := filepath.Join("a", "b", "c.d", "e.f") if matches[0] != expected { t.Fatalf("matched [%s], expected [%s]", matches[0], expected) } @@ -97,3 +98,52 @@ func TestExpand_DirectCall_emptySlice(t *testing.T) { t.Fatalf("got %d matches, expected 0", len(matches)) } } + +func TestGlob_Stars_oneMatch(t *testing.T) { + // test two double-stars + path := "./a/b/c.d/e.f" + err := os.MkdirAll(path, 0755) + if err != nil { + t.Fatalf("os.MkdirAll: %s", err) + } + matches, err := Glob("./*/b/**/*.f") + if err != nil { + t.Fatalf("Glob: %s", err) + } + if len(matches) != 1 { + t.Fatalf("got %d matches, expected 1", len(matches)) + } + expected := filepath.Join("a", "b", "c.d", "e.f") + if matches[0] != expected { + t.Fatalf("matched [%s], expected [%s]", matches[0], expected) + } +} + +func TestGlob_SingleDoubleStarAbsFolder(t *testing.T) { + // Test with absolute path. + tmp, err := os.MkdirTemp("", "SingleDoubleStarAbsFolder-") + if err != nil { + t.Fatalf("os.CreateTemp: %s", err) + } + defer os.RemoveAll(tmp) + + t.Log(tmp) + path := filepath.Join(tmp, "a/b/c.d/e.f/g") + err = os.MkdirAll(path, 0755) + if err != nil { + t.Fatalf("os.MkdirAll: %s", err) + } + matches, err := Glob(filepath.Join(tmp, "a/b/*/e.f/**")) + if err != nil { + t.Fatalf("Glob: %s", err) + } + expected := []string{filepath.Join(tmp, "a", "b", "c.d", "e.f"), filepath.Join(tmp, "a", "b", "c.d", "e.f", "g")} + if len(matches) != len(expected) { + t.Fatalf("got %d matches, expected %d", len(matches), len(expected)) + } + for i, match := range matches { + if match != expected[i] { + t.Fatalf("matched [%s], expected [%s]", match, expected[i]) + } + } +}