-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_test.go
78 lines (59 loc) · 2.21 KB
/
io_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
package duckdbreplicator
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestCopyDirEmptyDir(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()
err := os.RemoveAll(dest)
require.NoError(t, err)
require.NoDirExists(t, dest)
err = copyDir(dest, src)
require.NoError(t, err)
require.DirExists(t, dest)
require.DirExists(t, src)
}
func TestCopyDirEmptyNestedDir(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()
err := os.RemoveAll(dest)
require.NoError(t, err)
require.NoDirExists(t, dest)
err = os.MkdirAll(filepath.Join(src, "nested1", "nested"), os.ModePerm)
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(src, "nested2"), os.ModePerm)
require.NoError(t, err)
err = copyDir(dest, src)
require.NoError(t, err)
require.DirExists(t, dest)
require.DirExists(t, filepath.Join(dest, "nested1"))
require.DirExists(t, filepath.Join(dest, "nested2"))
require.DirExists(t, filepath.Join(dest, "nested1", "nested"))
}
func TestCopyDirWithFile(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()
require.NoError(t, os.Mkdir(filepath.Join(dest, "existing"), os.ModePerm))
err := os.MkdirAll(filepath.Join(src, "nested1", "nested"), os.ModePerm)
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(src, "nested1", "file.txt"), []byte("nested1"), os.ModePerm))
require.NoError(t, os.WriteFile(filepath.Join(src, "nested1", "nested", "file.txt"), []byte("nested1-nested"), os.ModePerm))
err = os.MkdirAll(filepath.Join(src, "nested2"), os.ModePerm)
require.NoError(t, os.WriteFile(filepath.Join(src, "nested2", "file.txt"), []byte("nested2"), os.ModePerm))
require.NoError(t, err)
err = copyDir(dest, src)
require.NoError(t, err)
contents, err := os.ReadFile(filepath.Join(dest, "nested1", "file.txt"))
require.NoError(t, err)
require.Equal(t, "nested1", string(contents))
contents, err = os.ReadFile(filepath.Join(dest, "nested1", "nested", "file.txt"))
require.NoError(t, err)
require.Equal(t, "nested1-nested", string(contents))
contents, err = os.ReadFile(filepath.Join(dest, "nested2", "file.txt"))
require.NoError(t, err)
require.Equal(t, "nested2", string(contents))
require.DirExists(t, filepath.Join(dest, "existing"))
}