Skip to content

Commit

Permalink
sort results from Glob
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatcuk committed Apr 14, 2020
1 parent 8a4278f commit ac81ccc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doublestar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"unicode/utf8"
)
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions doublestar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit ac81ccc

Please sign in to comment.