From 28444548dac0698df14b7776e268ed33c29986e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20N=C3=BCtzi?= Date: Sat, 1 May 2021 22:51:24 +0200 Subject: [PATCH 1/2] Failing test --- filepathx_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/filepathx_test.go b/filepathx_test.go index 599ce11..fc10439 100644 --- a/filepathx_test.go +++ b/filepathx_test.go @@ -5,6 +5,13 @@ import ( "testing" ) +func cleanup(t *testing.T) { + err := os.RemoveAll("./a") + if err != nil { + t.Fatalf("os.Removall: %s", err) + } +} + func TestGlob_ZeroDoubleStars_oneMatch(t *testing.T) { // test passthru to vanilla path/filepath path := "./a/b/c.d/e.f" @@ -23,6 +30,7 @@ func TestGlob_ZeroDoubleStars_oneMatch(t *testing.T) { if matches[0] != expected { t.Fatalf("matched [%s], expected [%s]", matches[0], expected) } + cleanup(t) } func TestGlob_OneDoubleStar_oneMatch(t *testing.T) { @@ -43,6 +51,7 @@ func TestGlob_OneDoubleStar_oneMatch(t *testing.T) { if matches[0] != expected { t.Fatalf("matched [%s], expected [%s]", matches[0], expected) } + cleanup(t) } func TestGlob_OneDoubleStar_twoMatches(t *testing.T) { @@ -65,6 +74,7 @@ func TestGlob_OneDoubleStar_twoMatches(t *testing.T) { t.Fatalf("matched [%s], expected [%s]", match, expected[i]) } } + cleanup(t) } func TestGlob_TwoDoubleStars_oneMatch(t *testing.T) { @@ -85,6 +95,7 @@ func TestGlob_TwoDoubleStars_oneMatch(t *testing.T) { if matches[0] != expected { t.Fatalf("matched [%s], expected [%s]", matches[0], expected) } + cleanup(t) } func TestExpand_DirectCall_emptySlice(t *testing.T) { @@ -96,4 +107,28 @@ func TestExpand_DirectCall_emptySlice(t *testing.T) { if len(matches) != 0 { t.Fatalf("got %d matches, expected 0", len(matches)) } + cleanup(t) +} + +func TestExpand_TwoDoubleStarts_escapeCharactersInPath(t *testing.T) { + // test a single double-star + path := "./a/b/c.d/[" + err := os.MkdirAll(path, 0755) + if err != nil { + t.Fatalf("os.MkdirAll: %s", err) + } + matches, err := Glob("./a/**/*.*") + if err != nil { + t.Fatalf("Glob: %s", err) + } + if len(matches) != 2 { + t.Fatalf("got %d matches, expected 2", len(matches)) + } + expected := []string{"a/b/c.d", "a/b/c.d/["} + for i, match := range matches { + if match != expected[i] { + t.Fatalf("matched [%s], expected [%s]", match, expected[i]) + } + } + cleanup(t) } From 4a00f82cd359bb0fdbfd2d50e7be0a10495ddd5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20N=C3=BCtzi?= Date: Sat, 1 May 2021 22:52:34 +0200 Subject: [PATCH 2/2] Replace pattern syntax in paths - A path `/usr/bin/[` results in a pattern error. This fix resolves this. --- filepathx.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/filepathx.go b/filepathx.go index 0129314..c3b11ab 100644 --- a/filepathx.go +++ b/filepathx.go @@ -6,6 +6,7 @@ package filepathx import ( "os" "path/filepath" + "runtime" "strings" ) @@ -24,11 +25,28 @@ func Glob(pattern string) ([]string, error) { // Expand finds matches for the provided Globs. func (globs Globs) Expand() ([]string, error) { + + // Escape `filepath.Match` syntax. + // On Unix escaping works with `\\`, + // on windows it is disabled, therefore + // replace it by '?' := any character. + var escapeChar string = "\\" + if runtime.GOOS == "windows" { + escapeChar = "?" + } + var matches = []string{""} // accumulate here for _, glob := range globs { var hits []string var hitMap = map[string]bool{} for _, match := range matches { + + match = strings.ReplaceAll( + strings.ReplaceAll( + strings.ReplaceAll(match, "*", escapeChar+"*"), + "[", escapeChar+"["), + "]", escapeChar+"]") + paths, err := filepath.Glob(match + glob) if err != nil { return nil, err