12345678910111213141516171819202122232425262728293031 | package render
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// filepath.Glob would read "out[1]" as a pattern and list out1 instead.
func TestListDirGlobChars(t *testing.T) {
root := t.TempDir()
for _, f := range []string{"out[1]/a.xml", "out[1]/b.txt", "out1/c.xml"} {
p := filepath.Join(root, filepath.FromSlash(f))
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, nil, 0o644); err != nil {
t.Fatal(err)
}
}
xml := func(n string) bool { return strings.HasSuffix(n, ".xml") }
got, err := listDir(filepath.Join(root, "out[1]"), xml)
if want := []string{filepath.Join(root, "out[1]", "a.xml")}; err != nil || !slices.Equal(got, want) {
t.Errorf("listDir = %q, %v; want %q", got, err, want)
}
if got, err := listDir(filepath.Join(root, "missing"), xml); err != nil || got != nil {
t.Errorf("missing dir: %q, %v", got, err)
}
}
|