This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
walk_test.go
80 lines (66 loc) · 1.93 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
package storage
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestList(t *testing.T) {
withTestTree(t, func(dir string) {
ctx := context.Background()
fs := NewLocalFS(dir)
list, err := List(ctx, fs, "")
assert.NoError(t, err)
// Note how directories are not listed and that output is sorted
assert.Equal(t, []string{"/baz", "/foo/bar"}, list)
list, err = List(ctx, fs, "foo")
assert.NoError(t, err)
assert.Equal(t, []string{"/foo/bar"}, list)
// Error if subpath does not exist
list, err = List(ctx, fs, "non-existent")
assert.Errorf(t, err, "lstat %s/non-existent: no such file or directory", dir)
assert.Equal(t, []string(nil), list)
// Error if root directory does not exist
list, err = List(ctx, NewLocalFS(filepath.Join(dir, "non-existent")), "")
assert.Errorf(t, err, "lstat %s/non-existent: no such file or directory", dir)
assert.Equal(t, []string(nil), list)
})
}
func TestWalkN(t *testing.T) {
withTestTree(t, func(dir string) {
var list []string
c := make(chan string)
done := make(chan struct{})
go func() {
for path := range c {
list = append(list, path)
}
close(done)
}()
ctx := context.Background()
fs := NewLocalFS(dir)
// 5 workers for 2 items
err := WalkN(ctx, fs, "", 5, func(path string) error {
c <- path
return nil
})
close(c)
<-done
assert.NoError(t, err)
// Note how directories are not listed and that output is not necessarily sorted
assert.ElementsMatch(t, []string{"/baz", "/foo/bar"}, list)
})
}
func withTestTree(t *testing.T, cb func(dir string)) {
t.Helper()
dir, err := os.MkdirTemp("", "go-storage-walk-test")
assert.NoError(t, err)
defer os.RemoveAll(dir)
assert.NoError(t, os.Mkdir(filepath.Join(dir, "foo"), 0o755))
_, err = os.Create(filepath.Join(dir, "foo", "bar"))
assert.NoError(t, err)
_, err = os.Create(filepath.Join(dir, "baz"))
assert.NoError(t, err)
cb(dir)
}