forked from tonistiigi/fsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalker_test.go
358 lines (312 loc) · 7.58 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package fsutil
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/tonistiigi/fsutil/types"
)
func TestWalkerSimple(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD foo file",
"ADD foo2 file",
}))
assert.NoError(t, err)
defer os.RemoveAll(d)
b := &bytes.Buffer{}
err = Walk(context.Background(), d, nil, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, string(b.Bytes()), `file foo
file foo2
`)
}
func TestWalkerInclude(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD bar dir",
"ADD bar/foo file",
"ADD foo2 file",
}))
assert.NoError(t, err)
defer os.RemoveAll(d)
b := &bytes.Buffer{}
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"bar"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir bar
file bar/foo
`, string(b.Bytes()))
b.Reset()
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"bar/foo"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir bar
file bar/foo
`, string(b.Bytes()))
b.Reset()
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"b*"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir bar
file bar/foo
`, string(b.Bytes()))
b.Reset()
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"bar/f*"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir bar
file bar/foo
`, string(b.Bytes()))
b.Reset()
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"bar/g*"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir bar
`, string(b.Bytes()))
b.Reset()
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"f*"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `file foo2
`, string(b.Bytes()))
b.Reset()
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"b*/f*"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir bar
file bar/foo
`, string(b.Bytes()))
b.Reset()
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"b*/foo"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir bar
file bar/foo
`, string(b.Bytes()))
b.Reset()
err = Walk(context.Background(), d, &WalkOpt{
IncludePatterns: []string{"b*/"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir bar
file bar/foo
`, string(b.Bytes()))
}
func TestWalkerExclude(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD bar file",
"ADD foo dir",
"ADD foo2 file",
"ADD foo/bar2 file",
}))
assert.NoError(t, err)
defer os.RemoveAll(d)
b := &bytes.Buffer{}
err = Walk(context.Background(), d, &WalkOpt{
ExcludePatterns: []string{"foo*", "!foo/bar2"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `file bar
dir foo
file foo/bar2
`, string(b.Bytes()))
}
func TestWalkerFollowLinks(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD bar file",
"ADD foo dir",
"ADD foo/l1 symlink /baz/one",
"ADD foo/l2 symlink /baz/two",
"ADD baz dir",
"ADD baz/one file",
"ADD baz/two symlink ../bax",
"ADD bax file",
"ADD bay file", // not included
}))
assert.NoError(t, err)
defer os.RemoveAll(d)
b := &bytes.Buffer{}
err = Walk(context.Background(), d, &WalkOpt{
FollowPaths: []string{"foo/l*", "bar"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `file bar
file bax
dir baz
file baz/one
symlink:../bax baz/two
dir foo
symlink:/baz/one foo/l1
symlink:/baz/two foo/l2
`, string(b.Bytes()))
}
func TestWalkerFollowLinksToRoot(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD foo symlink .",
"ADD bar file",
"ADD bax file",
"ADD bay dir",
"ADD bay/baz file",
}))
assert.NoError(t, err)
defer os.RemoveAll(d)
b := &bytes.Buffer{}
err = Walk(context.Background(), d, &WalkOpt{
FollowPaths: []string{"foo"},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `file bar
file bax
dir bay
file bay/baz
symlink:. foo
`, string(b.Bytes()))
}
func TestWalkerMap(t *testing.T) {
d, err := tmpDir(changeStream([]string{
"ADD bar file",
"ADD foo dir",
"ADD foo2 file",
"ADD foo/bar2 file",
}))
assert.NoError(t, err)
defer os.RemoveAll(d)
b := &bytes.Buffer{}
err = Walk(context.Background(), d, &WalkOpt{
Map: func(_ string, s *types.Stat) bool {
if strings.HasPrefix(s.Path, "foo") {
s.Path = "_" + s.Path
return true
}
return false
},
}, bufWalk(b))
assert.NoError(t, err)
assert.Equal(t, `dir _foo
file _foo/bar2
file _foo2
`, string(b.Bytes()))
}
func TestMatchPrefix(t *testing.T) {
ok, partial := matchPrefix("foo", "foo")
assert.Equal(t, true, ok)
assert.Equal(t, false, partial)
ok, partial = matchPrefix("foo/bar/baz", "foo")
assert.Equal(t, true, ok)
assert.Equal(t, true, partial)
ok, partial = matchPrefix("foo/bar/baz", "foo/bar")
assert.Equal(t, true, ok)
assert.Equal(t, true, partial)
ok, partial = matchPrefix("foo/bar/baz", "foo/bax")
assert.Equal(t, false, ok)
assert.Equal(t, true, partial)
ok, partial = matchPrefix("foo/bar/baz", "foo/bar/baz")
assert.Equal(t, true, ok)
assert.Equal(t, false, partial)
ok, partial = matchPrefix("f*", "foo")
assert.Equal(t, true, ok)
assert.Equal(t, false, partial)
ok, partial = matchPrefix("foo/bar/*", "foo")
assert.Equal(t, true, ok)
assert.Equal(t, true, partial)
ok, partial = matchPrefix("foo/*/baz", "foo")
assert.Equal(t, true, ok)
assert.Equal(t, true, partial)
ok, partial = matchPrefix("*/*/baz", "foo")
assert.Equal(t, true, ok)
assert.Equal(t, true, partial)
ok, partial = matchPrefix("*/bar/baz", "foo/bar")
assert.Equal(t, true, ok)
assert.Equal(t, true, partial)
ok, partial = matchPrefix("*/bar/baz", "foo/bax")
assert.Equal(t, false, ok)
assert.Equal(t, true, partial)
ok, partial = matchPrefix("*/*/baz", "foo/bar/baz")
assert.Equal(t, true, ok)
assert.Equal(t, false, partial)
}
func bufWalk(buf *bytes.Buffer) filepath.WalkFunc {
return func(path string, fi os.FileInfo, err error) error {
stat, ok := fi.Sys().(*types.Stat)
if !ok {
return errors.Errorf("invalid symlink %s", path)
}
t := "file"
if fi.IsDir() {
t = "dir"
}
if fi.Mode()&os.ModeSymlink != 0 {
t = "symlink:" + stat.Linkname
}
fmt.Fprintf(buf, "%s %s", t, path)
if fi.Mode()&os.ModeSymlink == 0 && stat.Linkname != "" {
fmt.Fprintf(buf, " >%s", stat.Linkname)
}
fmt.Fprintln(buf)
return nil
}
}
func tmpDir(inp []*change) (dir string, retErr error) {
tmpdir, err := ioutil.TempDir("", "diff")
if err != nil {
return "", err
}
defer func() {
if retErr != nil {
os.RemoveAll(tmpdir)
}
}()
for _, c := range inp {
if c.kind == ChangeKindAdd {
p := filepath.Join(tmpdir, c.path)
stat, ok := c.fi.Sys().(*types.Stat)
if !ok {
return "", errors.Errorf("invalid symlink change %s", p)
}
if c.fi.IsDir() {
if err := os.Mkdir(p, 0700); err != nil {
return "", err
}
} else if c.fi.Mode()&os.ModeSymlink != 0 {
if err := os.Symlink(stat.Linkname, p); err != nil {
return "", err
}
} else if len(stat.Linkname) > 0 {
if err := os.Link(filepath.Join(tmpdir, stat.Linkname), p); err != nil {
return "", err
}
} else if c.fi.Mode()&os.ModeSocket != 0 {
// not closing listener because it would remove the socket file
if _, err := net.Listen("unix", p); err != nil {
return "", err
}
} else {
f, err := os.Create(p)
if err != nil {
return "", err
}
if len(c.data) > 0 {
if _, err := f.Write([]byte(c.data)); err != nil {
return "", err
}
}
f.Close()
}
}
}
return tmpdir, nil
}