-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcopy_test.go
62 lines (48 loc) · 1.01 KB
/
copy_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
package main
import (
"crypto/rand"
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func Test__copy(t *testing.T) {
f, err := ioutil.TempFile("", "test")
if err != nil {
t.Error(err)
}
defer os.Remove(f.Name())
f.WriteString("abc")
if err := f.Close(); err != nil {
t.Error(err)
}
b := make([]byte, 10)
if _, err := rand.Read(b); err != nil {
t.Error(err)
}
newpath := filepath.Join(os.TempDir(), hex.EncodeToString(b))
if _, err := os.Stat(f.Name()); err != nil {
t.Error("should exist file")
}
if _, err := os.Stat(newpath); err == nil {
t.Error("should not exist file")
}
if err := _copy(f.Name(), newpath); err != nil {
t.Error(err)
}
defer os.Remove(newpath)
if _, err := os.Stat(f.Name()); err == nil {
t.Error("should not exist file")
}
if _, err := os.Stat(newpath); err != nil {
t.Error("should exist file")
}
s, err := ioutil.ReadFile(newpath)
if err != nil {
t.Error(err)
}
if string(s) != "abc" {
t.Error("should abc but got", string(s))
}
}