1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
package godirwalk
import (
"os"
"path/filepath"
"testing"
)
func TestReadDirents(t *testing.T) {
t.Run("without symlinks", func(t *testing.T) {
testroot := filepath.Join(scaffolingRoot, "d0")
actual, err := ReadDirents(testroot, nil)
ensureError(t, err)
expected := Dirents{
&Dirent{
name: maxName,
path: testroot,
modeType: os.FileMode(0),
},
&Dirent{
name: "d1",
path: testroot,
modeType: os.ModeDir,
},
&Dirent{
name: "f1",
path: testroot,
modeType: os.FileMode(0),
},
&Dirent{
name: "skips",
path: testroot,
modeType: os.ModeDir,
},
&Dirent{
name: "symlinks",
path: testroot,
modeType: os.ModeDir,
},
}
ensureDirentsMatch(t, actual, expected)
})
t.Run("with symlinks", func(t *testing.T) {
testroot := filepath.Join(scaffolingRoot, "d0/symlinks")
actual, err := ReadDirents(testroot, nil)
ensureError(t, err)
// Because some platforms set multiple mode type bits, when we create
// the expected slice, we need to ensure the mode types are set
// appropriately for this platform. We have another test function to
// ensure NewDirent does this correctly, so let's call NewDirent for
// each of the expected children entries.
var expected Dirents
for _, child := range []string{"nothing", "toAbs", "toD1", "toF1", "d4"} {
de, err := NewDirent(filepath.Join(testroot, child))
if err != nil {
t.Fatal(err)
}
expected = append(expected, de)
}
ensureDirentsMatch(t, actual, expected)
})
}
func TestReadDirnames(t *testing.T) {
actual, err := ReadDirnames(filepath.Join(scaffolingRoot, "d0"), nil)
ensureError(t, err)
expected := []string{maxName, "d1", "f1", "skips", "symlinks"}
ensureStringSlicesMatch(t, actual, expected)
}
func BenchmarkReadDirnamesStandardLibrary(b *testing.B) {
if testing.Short() {
b.Skip("Skipping benchmark using user's Go source directory")
}
f := func(osDirname string) ([]string, error) {
dh, err := os.Open(osDirname)
if err != nil {
return nil, err
}
return dh.Readdirnames(-1)
}
var count int
for i := 0; i < b.N; i++ {
actual, err := f(goPrefix)
if err != nil {
b.Fatal(err)
}
count = len(actual)
}
_ = count
}
func BenchmarkReadDirnamesGodirwalk(b *testing.B) {
if testing.Short() {
b.Skip("Skipping benchmark using user's Go source directory")
}
var count int
for i := 0; i < b.N; i++ {
actual, err := ReadDirnames(goPrefix, nil)
if err != nil {
b.Fatal(err)
}
count = len(actual)
}
_ = count
}
|