-
Notifications
You must be signed in to change notification settings - Fork 6
/
walk_test.go
124 lines (103 loc) · 2.65 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
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
package straw_test
import (
"errors"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/uw-labs/straw"
)
func TestWalk(t *testing.T) {
assert := assert.New(t)
ss, _ := straw.Open("mem://")
ss.Mkdir("a", 0755)
writeFile(ss, "a/1")
writeFile(ss, "b")
ss.Mkdir("c", 0755)
var found []string
var fiNames []string
var fiIsDirs []bool
err := straw.Walk(ss, "/", func(name string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
found = append(found, name)
fiNames = append(fiNames, fi.Name())
fiIsDirs = append(fiIsDirs, fi.IsDir())
return nil
})
assert.NoError(err)
assert.Equal([]string{"/", "/a", "/a/1", "/b", "/c"}, found)
assert.Equal([]string{"", "a", "1", "b", "c"}, fiNames)
assert.Equal([]bool{true, true, false, false, true}, fiIsDirs)
}
func TestWalkSkipDir(t *testing.T) {
assert := assert.New(t)
ss, _ := straw.Open("mem://")
ss.Mkdir("a", 0755)
writeFile(ss, "a/1")
writeFile(ss, "b")
ss.Mkdir("c", 0755)
var found []string
var fiNames []string
var fiIsDirs []bool
err := straw.Walk(ss, "/", func(name string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if name == "/a" {
return straw.SkipDir
}
found = append(found, name)
fiNames = append(fiNames, fi.Name())
fiIsDirs = append(fiIsDirs, fi.IsDir())
return nil
})
assert.NoError(err)
assert.Equal([]string{"/", "/b", "/c"}, found)
assert.Equal([]string{"", "b", "c"}, fiNames)
assert.Equal([]bool{true, false, true}, fiIsDirs)
}
func TestWalkExitOnErr(t *testing.T) {
assert := assert.New(t)
ss, _ := straw.Open("mem://")
ss.Mkdir("a", 0755)
writeFile(ss, "a/1")
writeFile(ss, "b")
ss.Mkdir("c", 0755)
var found []string
var fiNames []string
var fiIsDirs []bool
someError := errors.New("some random error")
err := straw.Walk(ss, "/", func(name string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if name == "/b" {
return someError
}
found = append(found, name)
fiNames = append(fiNames, fi.Name())
fiIsDirs = append(fiIsDirs, fi.IsDir())
return nil
})
assert.Equal(err, someError)
assert.Equal([]string{"/", "/a", "/a/1"}, found)
assert.Equal([]string{"", "a", "1"}, fiNames)
assert.Equal([]bool{true, true, false}, fiIsDirs)
}
func TestWalkRootNotExist(t *testing.T) {
assert := assert.New(t)
ss, _ := straw.Open("mem://")
err := straw.Walk(ss, "/this/doesnt/exist", func(name string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
panic("won't get here")
})
assert.Error(err, "file does not exist")
}
func writeFile(ss straw.StreamStore, name string) {
wc, _ := ss.CreateWriteCloser(name)
wc.Write([]byte{0})
wc.Close()
}