Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions filepathx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package filepathx
import (
"os"
"path/filepath"
"runtime"
"strings"
)

Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions filepathx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}