|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package vfsutil_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/cockroachdb/cockroach/pkg/util/vfsutil" |
| 12 | + "github.com/cockroachdb/pebble/vfs" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestCopyRecursive(t *testing.T) { |
| 17 | + // Create source memFS with test data |
| 18 | + srcFS := vfs.NewMem() |
| 19 | + |
| 20 | + // Create a directory structure |
| 21 | + require.NoError(t, srcFS.MkdirAll("testdir/subdir", 0755)) |
| 22 | + |
| 23 | + // Create some test files |
| 24 | + f1, err := srcFS.Create("testdir/file1.txt", vfs.WriteCategoryUnspecified) |
| 25 | + require.NoError(t, err) |
| 26 | + _, err = f1.Write([]byte("hello world")) |
| 27 | + require.NoError(t, err) |
| 28 | + require.NoError(t, f1.Close()) |
| 29 | + |
| 30 | + f2, err := srcFS.Create("testdir/subdir/file2.txt", vfs.WriteCategoryUnspecified) |
| 31 | + require.NoError(t, err) |
| 32 | + _, err = f2.Write([]byte("nested file")) |
| 33 | + require.NoError(t, err) |
| 34 | + require.NoError(t, f2.Close()) |
| 35 | + |
| 36 | + // Create destination FS (real filesystem for this example) |
| 37 | + dstFS := vfs.Default |
| 38 | + |
| 39 | + // Copy from memFS to real filesystem in a temp directory |
| 40 | + tempDir := t.TempDir() |
| 41 | + require.NoError(t, vfsutil.CopyRecursive(srcFS, dstFS, "testdir", dstFS.PathJoin(tempDir, "copied"))) |
| 42 | + |
| 43 | + // Verify the copy worked |
| 44 | + files, err := dstFS.List(dstFS.PathJoin(tempDir, "copied")) |
| 45 | + require.NoError(t, err) |
| 46 | + require.Contains(t, files, "file1.txt") |
| 47 | + require.Contains(t, files, "subdir") |
| 48 | + |
| 49 | + // Verify nested file |
| 50 | + content, err := dstFS.Open(dstFS.PathJoin(tempDir, "copied", "subdir", "file2.txt")) |
| 51 | + require.NoError(t, err) |
| 52 | + defer content.Close() |
| 53 | + buf := make([]byte, 100) |
| 54 | + n, err := content.Read(buf) |
| 55 | + require.NoError(t, err) |
| 56 | + require.Equal(t, "nested file", string(buf[:n])) |
| 57 | +} |
0 commit comments