forked from otiai10/copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_test.go
96 lines (81 loc) · 2.67 KB
/
all_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package copy
import (
"os"
"path/filepath"
"testing"
. "github.com/otiai10/mint"
)
func TestMain(m *testing.M) {
setup(m)
code := m.Run()
teardown(m)
os.Exit(code)
}
func setup(m *testing.M) {
os.MkdirAll("testdata.copy", os.ModePerm)
os.Symlink("testdata/case01", "testdata/case03/case01")
}
func teardown(m *testing.M) {
os.RemoveAll("testdata/case03/case01")
os.RemoveAll("testdata.copy")
}
func TestCopy(t *testing.T) {
err := Copy("./testdata/case00", "./testdata.copy/case00")
Expect(t, err).ToBe(nil)
info, err := os.Stat("./testdata.copy/case00/README.md")
Expect(t, err).ToBe(nil)
Expect(t, info.IsDir()).ToBe(false)
When(t, "specified src doesn't exist", func(t *testing.T) {
err := Copy("NOT/EXISTING/SOURCE/PATH", "anywhere")
Expect(t, err).Not().ToBe(nil)
})
When(t, "specified src is just a file", func(t *testing.T) {
err := Copy("testdata/case01/README.md", "testdata.copy/case01/README.md")
Expect(t, err).ToBe(nil)
})
When(t, "too long name is given", func(t *testing.T) {
dest := "foobar"
for i := 0; i < 8; i++ {
dest = dest + dest
}
err := Copy("testdata/case00", filepath.Join("testdata/case00", dest))
Expect(t, err).Not().ToBe(nil)
Expect(t, err).TypeOf("*os.PathError")
})
When(t, "try to create not permitted location", func(t *testing.T) {
err := Copy("testdata/case00", "/case00")
Expect(t, err).Not().ToBe(nil)
Expect(t, err).TypeOf("*os.PathError")
})
When(t, "try to create a directory on existing file name", func(t *testing.T) {
err := Copy("testdata/case02", "testdata.copy/case00/README.md")
Expect(t, err).Not().ToBe(nil)
Expect(t, err).TypeOf("*os.PathError")
})
When(t, "source directory includes symbolic link", func(t *testing.T) {
err := Copy("testdata/case03", "testdata.copy/case03")
Expect(t, err).ToBe(nil)
info, err := os.Lstat("testdata.copy/case03/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(0)
})
When(t, "try to copy a file to existing path", func(t *testing.T) {
err := Copy("testdata/case04/README.md", "testdata/case04")
Expect(t, err).Not().ToBe(nil)
err = Copy("testdata/case04/README.md", "testdata/case04/README.md/foobar")
Expect(t, err).Not().ToBe(nil)
})
When(t, "try to copy a directory that has no write permission and copy file inside along with it", func(t *testing.T) {
src := "testdata/case05"
dest := "testdata.copy/case05"
err := os.Chmod(src, os.FileMode(0555))
Expect(t, err).ToBe(nil)
err = Copy(src, dest)
Expect(t, err).ToBe(nil)
info, err := os.Lstat(dest)
Expect(t, err).ToBe(nil)
Expect(t, info.Mode().Perm()).ToBe(os.FileMode(0555))
err = os.Chmod(dest, 0755)
Expect(t, err).ToBe(nil)
})
}