diff --git a/doublestar.go b/doublestar.go index 49cc5b5..50e506b 100644 --- a/doublestar.go +++ b/doublestar.go @@ -5,6 +5,7 @@ import ( "os" "path" "path/filepath" + "sort" "strings" "unicode/utf8" ) @@ -405,6 +406,8 @@ func doGlob(basedir, pattern string, matches []string) (m []string, e error) { defer dir.Close() files, _ := dir.Readdir(-1) + sort.Slice(files, func(i, j int) bool { return files[i].Name() < files[j].Name() }) + slashIdx := indexRuneWithEscaping(pattern, '/') lastComponent := slashIdx == -1 if lastComponent { diff --git a/doublestar_test.go b/doublestar_test.go index b23a41d..b21d20a 100644 --- a/doublestar_test.go +++ b/doublestar_test.go @@ -313,6 +313,26 @@ func symlink(oldname, newname string) { } } +func TestGlobSorted(t *testing.T) { + expected := []string{"a", "abc", "abcd", "abcde", "abxbbxdbxebxczzx", "abxbbxdbxebxczzy", "axbxcxdxe", "axbxcxdxexxx", "a☺b"} + matches, err := Glob(joinWithoutClean("test", "a*")) + if err != nil { + t.Errorf("Unexpected error %v", err) + return + } + + if len(matches) != len(expected) { + t.Errorf("Glob returned %#v; expected %#v", matches, expected) + return + } + for idx, match := range matches { + if match != joinWithoutClean("test", expected[idx]) { + t.Errorf("Glob returned %#v; expected %#v", matches, expected) + return + } + } +} + func TestMain(m *testing.M) { // create the test directory mkdirp("test", "a", "b", "c")