-
Notifications
You must be signed in to change notification settings - Fork 341
/
walk_test.go
63 lines (51 loc) · 1.5 KB
/
walk_test.go
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
package hdfs
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWalk(t *testing.T) {
c := getClient(t)
c.Mkdir("/_test/walk", os.ModePerm)
c.Mkdir("/_test/walk/dir", os.ModePerm)
c.Mkdir("/_test/walk/dir/subdir", os.ModePerm)
c.Create("/_test/walk/walkfile")
c.Create("/_test/walk/dir/walkfile1")
c.Create("/_test/walk/dir/walkfile2")
c.Create("/_test/walk/dir/subdir/walkfile1")
c.Create("/_test/walk/dir/subdir/walkfile2")
paths := make([]string, 0, 8)
err := c.Walk("/_test/walk/", walkFnTest(&paths))
assert.Nil(t, err, "unexpected error")
expected := []string{
"/_test/walk/",
"/_test/walk/dir",
"/_test/walk/dir/subdir",
"/_test/walk/dir/subdir/walkfile1",
"/_test/walk/dir/subdir/walkfile2",
"/_test/walk/dir/walkfile1",
"/_test/walk/dir/walkfile2",
"/_test/walk/walkfile"}
assert.Equal(t, expected, paths, "discrepancy between expected and walked paths.")
}
func TestWalkError(t *testing.T) {
c := getClient(t)
errors := make([]error, 0, 1)
c.Walk("/not_existing", walkErrorFn(&errors))
assert.Equal(t, 1, len(errors), "expected a single error")
}
func walkFnTest(encounteredPaths *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
*encounteredPaths = append(*encounteredPaths, path)
return nil
}
}
func walkErrorFn(errors *[]error) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
*errors = append(*errors, err)
}
return nil
}
}