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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
package godirwalk
import (
"os"
"path/filepath"
"sort"
"testing"
)
func filepathWalk(tb testing.TB, osDirname string) []string {
tb.Helper()
var entries []string
err := filepath.Walk(osDirname, func(osPathname string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == "skip" {
return filepath.SkipDir
}
entries = append(entries, filepath.FromSlash(osPathname))
return nil
})
ensureError(tb, err)
return entries
}
func godirwalkWalk(tb testing.TB, osDirname string) []string {
tb.Helper()
var entries []string
err := Walk(osDirname, &Options{
Callback: func(osPathname string, dirent *Dirent) error {
if dirent.Name() == "skip" {
return filepath.SkipDir
}
entries = append(entries, filepath.FromSlash(osPathname))
return nil
},
})
ensureError(tb, err)
return entries
}
func godirwalkWalkUnsorted(tb testing.TB, osDirname string) []string {
tb.Helper()
var entries []string
err := Walk(osDirname, &Options{
Callback: func(osPathname string, dirent *Dirent) error {
if dirent.Name() == "skip" {
return filepath.SkipDir
}
entries = append(entries, filepath.FromSlash(osPathname))
return nil
},
Unsorted: true,
})
ensureError(tb, err)
return entries
}
// Ensure the results from calling this library's Walk function exactly match
// those returned by filepath.Walk
func ensureSameAsStandardLibrary(tb testing.TB, osDirname string) {
tb.Helper()
osDirname = filepath.Join(scaffolingRoot, osDirname)
actual := godirwalkWalk(tb, osDirname)
sort.Strings(actual)
expected := filepathWalk(tb, osDirname)
ensureStringSlicesMatch(tb, actual, expected)
}
// Test the entire test root hierarchy with all of its artifacts. This library
// advertises itself as visiting the same file system entries as the standard
// library, and responding to discovered errors the same way, including
// responding to filepath.SkipDir exactly like the standard library does. This
// test ensures that behavior is correct by enumerating the contents of the test
// root directory.
func TestWalkCompatibleWithFilepathWalk(t *testing.T) {
t.Run("test root", func(t *testing.T) {
ensureSameAsStandardLibrary(t, "d0")
})
t.Run("ignore skips", func(t *testing.T) {
// When filepath.SkipDir is returned, the remainder of the children in a
// directory are not visited. This causes results to be different when
// visiting in lexicographical order or natural order. For this test, we
// want to ensure godirwalk can optimize traversals when unsorted using
// the Scanner, but recognize that we cannot test against standard
// library when we skip any nodes within it.
osDirname := filepath.Join(scaffolingRoot, "d0/d1")
actual := godirwalkWalkUnsorted(t, osDirname)
sort.Strings(actual)
expected := filepathWalk(t, osDirname)
ensureStringSlicesMatch(t, actual, expected)
})
}
// Test cases for encountering the filepath.SkipDir error at different
// relative positions from the invocation argument.
func TestWalkSkipDir(t *testing.T) {
t.Run("skip file at root", func(t *testing.T) {
ensureSameAsStandardLibrary(t, "d0/skips/d2")
})
t.Run("skip dir at root", func(t *testing.T) {
ensureSameAsStandardLibrary(t, "d0/skips/d3")
})
t.Run("skip nodes under root", func(t *testing.T) {
ensureSameAsStandardLibrary(t, "d0/skips")
})
t.Run("SkipDirOnSymlink", func(t *testing.T) {
var actual []string
err := Walk(filepath.Join(scaffolingRoot, "d0/skips"), &Options{
Callback: func(osPathname string, dirent *Dirent) error {
if dirent.Name() == "skip" {
return filepath.SkipDir
}
actual = append(actual, filepath.FromSlash(osPathname))
return nil
},
FollowSymbolicLinks: true,
})
ensureError(t, err)
expected := []string{
filepath.Join(scaffolingRoot, "d0/skips"),
filepath.Join(scaffolingRoot, "d0/skips/d2"),
filepath.Join(scaffolingRoot, "d0/skips/d2/f3"),
filepath.Join(scaffolingRoot, "d0/skips/d3"),
filepath.Join(scaffolingRoot, "d0/skips/d3/f4"),
filepath.Join(scaffolingRoot, "d0/skips/d3/z2"),
}
ensureStringSlicesMatch(t, actual, expected)
})
}
func TestWalkFollowSymbolicLinks(t *testing.T) {
var actual []string
var errorCallbackVisited bool
err := Walk(filepath.Join(scaffolingRoot, "d0/symlinks"), &Options{
Callback: func(osPathname string, _ *Dirent) error {
actual = append(actual, filepath.FromSlash(osPathname))
return nil
},
ErrorCallback: func(osPathname string, err error) ErrorAction {
if filepath.Base(osPathname) == "nothing" {
errorCallbackVisited = true
return SkipNode
}
return Halt
},
FollowSymbolicLinks: true,
})
ensureError(t, err)
if got, want := errorCallbackVisited, true; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
expected := []string{
filepath.Join(scaffolingRoot, "d0/symlinks"),
filepath.Join(scaffolingRoot, "d0/symlinks/d4"),
filepath.Join(scaffolingRoot, "d0/symlinks/d4/toSD1"), // chained symbolic link
filepath.Join(scaffolingRoot, "d0/symlinks/d4/toSD1/f2"), // chained symbolic link
filepath.Join(scaffolingRoot, "d0/symlinks/d4/toSF1"), // chained symbolic link
filepath.Join(scaffolingRoot, "d0/symlinks/nothing"),
filepath.Join(scaffolingRoot, "d0/symlinks/toAbs"),
filepath.Join(scaffolingRoot, "d0/symlinks/toD1"),
filepath.Join(scaffolingRoot, "d0/symlinks/toD1/f2"),
filepath.Join(scaffolingRoot, "d0/symlinks/toF1"),
}
ensureStringSlicesMatch(t, actual, expected)
}
// While filepath.Walk will deliver the no access error to the regular callback,
// godirwalk should deliver it first to the ErrorCallback handler, then take
// action based on the return value of that callback function.
func TestErrorCallback(t *testing.T) {
t.Run("halt", func(t *testing.T) {
var callbackVisited, errorCallbackVisited bool
err := Walk(filepath.Join(scaffolingRoot, "d0/symlinks"), &Options{
Callback: func(osPathname string, dirent *Dirent) error {
switch dirent.Name() {
case "nothing":
callbackVisited = true
}
return nil
},
ErrorCallback: func(osPathname string, err error) ErrorAction {
switch filepath.Base(osPathname) {
case "nothing":
errorCallbackVisited = true
return Halt // Direct Walk to propagate error to caller
}
t.Fatalf("unexpected error callback for %s: %s", osPathname, err)
return SkipNode
},
FollowSymbolicLinks: true,
})
ensureError(t, err, "nothing") // Ensure caller receives propagated access error
if got, want := callbackVisited, true; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
if got, want := errorCallbackVisited, true; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
})
t.Run("skipnode", func(t *testing.T) {
var callbackVisited, errorCallbackVisited bool
err := Walk(filepath.Join(scaffolingRoot, "d0/symlinks"), &Options{
Callback: func(osPathname string, dirent *Dirent) error {
switch dirent.Name() {
case "nothing":
callbackVisited = true
}
return nil
},
ErrorCallback: func(osPathname string, err error) ErrorAction {
switch filepath.Base(osPathname) {
case "nothing":
errorCallbackVisited = true
return SkipNode // Direct Walk to ignore this error
}
t.Fatalf("unexpected error callback for %s: %s", osPathname, err)
return Halt
},
FollowSymbolicLinks: true,
})
ensureError(t, err) // Ensure caller receives no access error
if got, want := callbackVisited, true; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
if got, want := errorCallbackVisited, true; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
})
}
// Invokes PostChildrenCallback for all directories and nothing else.
func TestPostChildrenCallback(t *testing.T) {
var actual []string
err := Walk(filepath.Join(scaffolingRoot, "d0"), &Options{
Callback: func(_ string, _ *Dirent) error { return nil },
PostChildrenCallback: func(osPathname string, _ *Dirent) error {
actual = append(actual, osPathname)
return nil
},
})
ensureError(t, err)
expected := []string{
filepath.Join(scaffolingRoot, "d0"),
filepath.Join(scaffolingRoot, "d0/d1"),
filepath.Join(scaffolingRoot, "d0/skips"),
filepath.Join(scaffolingRoot, "d0/skips/d2"),
filepath.Join(scaffolingRoot, "d0/skips/d3"),
filepath.Join(scaffolingRoot, "d0/skips/d3/skip"),
filepath.Join(scaffolingRoot, "d0/symlinks"),
filepath.Join(scaffolingRoot, "d0/symlinks/d4"),
}
ensureStringSlicesMatch(t, actual, expected)
}
const flameIterations = 10
var goPrefix = filepath.Join(os.Getenv("GOPATH"), "src")
func BenchmarkFilepath(b *testing.B) {
if testing.Short() {
b.Skip("Skipping benchmark using user's Go source directory")
}
for i := 0; i < b.N; i++ {
_ = filepathWalk(b, goPrefix)
}
}
func BenchmarkGodirwalk(b *testing.B) {
if testing.Short() {
b.Skip("Skipping benchmark using user's Go source directory")
}
for i := 0; i < b.N; i++ {
_ = godirwalkWalk(b, goPrefix)
}
}
func BenchmarkGodirwalkUnsorted(b *testing.B) {
if testing.Short() {
b.Skip("Skipping benchmark using user's Go source directory")
}
for i := 0; i < b.N; i++ {
_ = godirwalkWalkUnsorted(b, goPrefix)
}
}
func BenchmarkFlameGraphFilepath(b *testing.B) {
for i := 0; i < flameIterations; i++ {
_ = filepathWalk(b, goPrefix)
}
}
func BenchmarkFlameGraphGodirwalk(b *testing.B) {
for i := 0; i < flameIterations; i++ {
_ = godirwalkWalk(b, goPrefix)
}
}
|