forked from jlaffaye/ftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walker_test.go
211 lines (170 loc) · 3.5 KB
/
walker_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
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
package ftp
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWalkReturnsCorrectlyPopulatedWalker(t *testing.T) {
mock, err := newFtpMock(t, "127.0.0.1")
if err != nil {
t.Fatal(err)
}
defer mock.Close()
c, cErr := Connect(mock.Addr())
if cErr != nil {
t.Fatal(err)
}
w := c.Walk("root")
assert.Equal(t, "root/", w.root)
assert.Equal(t, &c, &w.serverConn)
}
func TestFieldsReturnCorrectData(t *testing.T) {
w := Walker{
cur: &item{
path: "/root/",
err: fmt.Errorf("this is an error"),
entry: &Entry{
Name: "root",
Size: 123,
Time: time.Now(),
Type: EntryTypeFolder,
},
},
}
assert.Equal(t, "this is an error", w.Err().Error())
assert.Equal(t, "/root/", w.Path())
assert.Equal(t, EntryTypeFolder, w.Stat().Type)
}
func TestSkipDirIsCorrectlySet(t *testing.T) {
w := Walker{}
w.SkipDir()
assert.Equal(t, false, w.descend)
}
func TestNoDescendDoesNotAddToStack(t *testing.T) {
mock, err := newFtpMock(t, "127.0.0.1")
if err != nil {
t.Fatal(err)
}
defer mock.Close()
c, cErr := Connect(mock.Addr())
if cErr != nil {
t.Fatal(err)
}
w := c.Walk("/root")
w.cur = &item{
path: "/root/",
err: nil,
entry: &Entry{
Name: "root",
Size: 123,
Time: time.Now(),
Type: EntryTypeFolder,
},
}
w.stack = []*item{
{
path: "file",
err: nil,
entry: &Entry{
Name: "file",
Size: 123,
Time: time.Now(),
Type: EntryTypeFile,
},
},
}
w.SkipDir()
result := w.Next()
assert.Equal(t, true, result, "Result should return true")
assert.Equal(t, 0, len(w.stack))
assert.Equal(t, true, w.descend)
}
func TestEmptyStackReturnsFalse(t *testing.T) {
assert, require := assert.New(t), require.New(t)
mock, err := newFtpMock(t, "127.0.0.1")
require.Nil(err)
defer mock.Close()
c, cErr := Connect(mock.Addr())
require.Nil(cErr)
w := c.Walk("/root")
w.cur = &item{
path: "/root/",
err: nil,
entry: &Entry{
Name: "root",
Size: 123,
Time: time.Now(),
Type: EntryTypeFolder,
},
}
w.stack = []*item{}
w.SkipDir()
result := w.Next()
assert.Equal(false, result, "Result should return false")
}
func TestCurAndStackSetCorrectly(t *testing.T) {
assert, require := assert.New(t), require.New(t)
mock, err := newFtpMock(t, "127.0.0.1")
require.Nil(err)
defer mock.Close()
c, cErr := Connect(mock.Addr())
require.Nil(cErr)
w := c.Walk("/root")
w.cur = &item{
path: "root/file1",
err: nil,
entry: &Entry{
Name: "file1",
Size: 123,
Time: time.Now(),
Type: EntryTypeFile,
},
}
w.stack = []*item{
{
path: "file",
err: nil,
entry: &Entry{
Name: "file",
Size: 123,
Time: time.Now(),
Type: EntryTypeFile,
},
},
{
path: "root/file1",
err: nil,
entry: &Entry{
Name: "file1",
Size: 123,
Time: time.Now(),
Type: EntryTypeFile,
},
},
}
result := w.Next()
assert.Equal(true, result, "Result should return true")
result = w.Next()
assert.Equal(true, result, "Result should return true")
assert.Equal(0, len(w.stack))
assert.Equal("file", w.cur.entry.Name)
}
func TestCurInit(t *testing.T) {
mock, err := newFtpMock(t, "127.0.0.1")
if err != nil {
t.Fatal(err)
}
defer mock.Close()
c, cErr := Connect(mock.Addr())
if cErr != nil {
t.Fatal(err)
}
w := c.Walk("/root")
result := w.Next()
// mock fs has one file 'lo'
assert.Equal(t, true, result, "Result should return false")
assert.Equal(t, 0, len(w.stack))
assert.Equal(t, "/root/lo", w.Path())
}