|
4 | 4 | package storage
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "os" |
| 10 | + "path/filepath" |
7 | 11 | "testing"
|
8 | 12 |
|
9 | 13 | "github.com/stretchr/testify/assert"
|
@@ -50,3 +54,41 @@ func TestBuildLocalPath(t *testing.T) {
|
50 | 54 | })
|
51 | 55 | }
|
52 | 56 | }
|
| 57 | + |
| 58 | +func TestLocalStorageIterator(t *testing.T) { |
| 59 | + dir := filepath.Join(os.TempDir(), "TestLocalStorageIteratorTestDir") |
| 60 | + l, err := NewLocalStorage(context.Background(), LocalStorageConfig{Path: dir}) |
| 61 | + assert.NoError(t, err) |
| 62 | + |
| 63 | + testFiles := [][]string{ |
| 64 | + {"a/1.txt", "a1"}, |
| 65 | + {"/a/1.txt", "aa1"}, // same as above, but with leading slash that will be trim |
| 66 | + {"b/1.txt", "b1"}, |
| 67 | + {"b/2.txt", "b2"}, |
| 68 | + {"b/3.txt", "b3"}, |
| 69 | + {"b/x 4.txt", "bx4"}, |
| 70 | + } |
| 71 | + for _, f := range testFiles { |
| 72 | + _, err = l.Save(f[0], bytes.NewBufferString(f[1]), -1) |
| 73 | + assert.NoError(t, err) |
| 74 | + } |
| 75 | + |
| 76 | + expectedList := map[string][]string{ |
| 77 | + "a": {"a/1.txt"}, |
| 78 | + "b": {"b/1.txt", "b/2.txt", "b/3.txt", "b/x 4.txt"}, |
| 79 | + "": {"a/1.txt", "b/1.txt", "b/2.txt", "b/3.txt", "b/x 4.txt"}, |
| 80 | + "/": {"a/1.txt", "b/1.txt", "b/2.txt", "b/3.txt", "b/x 4.txt"}, |
| 81 | + "a/b/../../a": {"a/1.txt"}, |
| 82 | + } |
| 83 | + for dir, expected := range expectedList { |
| 84 | + count := 0 |
| 85 | + err = l.IterateObjects(dir, func(path string, f Object) error { |
| 86 | + defer f.Close() |
| 87 | + assert.Contains(t, expected, path) |
| 88 | + count++ |
| 89 | + return nil |
| 90 | + }) |
| 91 | + assert.NoError(t, err) |
| 92 | + assert.Equal(t, count, len(expected)) |
| 93 | + } |
| 94 | +} |
0 commit comments