Skip to content

Commit 8a2ea31

Browse files
committed
Add a test.
1 parent caefb7a commit 8a2ea31

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

vfs_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package tiledb
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
78
"path/filepath"
9+
"slices"
810
"testing"
911

1012
"github.com/stretchr/testify/assert"
@@ -254,6 +256,77 @@ func TestVFSList(t *testing.T) {
254256
tmpFilePath2, "file://" + tmpFilePath3}, fileList)
255257
}
256258

259+
// TestVFSList validates vfs VisitRecursive operation is successful
260+
func TestVFSVisitRecursive(t *testing.T) {
261+
config, err := NewConfig()
262+
require.NoError(t, err)
263+
264+
context, err := NewContext(config)
265+
require.NoError(t, err)
266+
267+
vfs, err := NewVFS(context, config)
268+
require.NoError(t, err)
269+
270+
tmpPath := filepath.Join(t.TempDir(), "somedir")
271+
tmpPath2 := filepath.Join(tmpPath, "subdir")
272+
tmpPath3 := filepath.Join(tmpPath, "subdir2")
273+
274+
tmpFilePath := filepath.Join(tmpPath, "somefile")
275+
tmpFilePath2 := filepath.Join(tmpPath, "somefile2")
276+
tmpFilePath3 := filepath.Join(tmpPath, "somefile3")
277+
278+
// Create directories
279+
require.NoError(t, vfs.CreateDir(tmpPath))
280+
require.NoError(t, vfs.CreateDir(tmpPath2))
281+
require.NoError(t, vfs.CreateDir(tmpPath3))
282+
283+
// Create Files
284+
createFile(t, vfs, tmpFilePath)
285+
createFile(t, vfs, tmpFilePath2)
286+
createFile(t, vfs, tmpFilePath3)
287+
288+
var fileList []string
289+
err = vfs.VisitRecursive(tmpPath, func(path string, size uint64) (bool, error) {
290+
fileExists, err := vfs.IsFile(path)
291+
if err != nil {
292+
return false, err
293+
}
294+
if !fileExists {
295+
dirExists, err := vfs.IsDir(path)
296+
if err != nil {
297+
return false, err
298+
}
299+
if !dirExists {
300+
return false, fmt.Errorf("%s does not exist neither as a file nor as a directory", path)
301+
}
302+
} else {
303+
if size != 3 {
304+
return false, fmt.Errorf("file %s has unexpected size (%d)", path, size)
305+
}
306+
fileList = append(fileList, path)
307+
}
308+
return true, nil
309+
})
310+
require.NoError(t, err)
311+
slices.Sort(fileList)
312+
assert.EqualValues(t, []string{"file://" + tmpFilePath, "file://" +
313+
tmpFilePath2, "file://" + tmpFilePath3}, fileList)
314+
315+
expectedErr := errors.New("dummy")
316+
err = vfs.VisitRecursive(tmpPath, func(path string, size uint64) (bool, error) {
317+
return false, expectedErr
318+
})
319+
assert.Equal(t, expectedErr, err)
320+
321+
count := 0
322+
err = vfs.VisitRecursive(tmpPath, func(path string, size uint64) (bool, error) {
323+
count++
324+
return count < 2, nil
325+
})
326+
require.NoError(t, err)
327+
assert.Equal(t, 2, count)
328+
}
329+
257330
func createFile(t testing.TB, vfs *VFS, path string) {
258331
t.Helper()
259332
require.NoError(t, vfs.Touch(path))

0 commit comments

Comments
 (0)