|
1 | 1 | package tiledb
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "fmt"
|
5 | 6 | "io"
|
6 | 7 | "os"
|
7 | 8 | "path/filepath"
|
| 9 | + "slices" |
8 | 10 | "testing"
|
9 | 11 |
|
10 | 12 | "github.com/stretchr/testify/assert"
|
@@ -254,6 +256,77 @@ func TestVFSList(t *testing.T) {
|
254 | 256 | tmpFilePath2, "file://" + tmpFilePath3}, fileList)
|
255 | 257 | }
|
256 | 258 |
|
| 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 | + |
257 | 330 | func createFile(t testing.TB, vfs *VFS, path string) {
|
258 | 331 | t.Helper()
|
259 | 332 | require.NoError(t, vfs.Touch(path))
|
|
0 commit comments