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
2 changes: 1 addition & 1 deletion filepathx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
58 changes: 54 additions & 4 deletions filepathx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filepathx

import (
"os"
"path/filepath"
"testing"
)

Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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])
Expand All @@ -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)
}
Expand All @@ -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])
}
}
}