-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
167 lines (147 loc) · 2.86 KB
/
main_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestMoveLinkAndReverseWithDirs(t *testing.T) {
moveLinkAndReverse(t, func(from string) error {
return os.Mkdir(from, 0777)
})
}
func TestMoveLinkAndReverse(t *testing.T) {
moveLinkAndReverse(t, func(from string) error {
return ioutil.WriteFile(from, []byte(""), 0777)
})
}
func moveLinkAndReverse(t *testing.T, createFrom func(string) error) {
tmp, rm := TempDir()
defer rm()
from := tmp + "/from"
to := tmp + "/to"
err := createFrom(from)
if err != nil {
t.Fatal(err)
}
err = MoveLink(from, to)
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(to)
if err != nil {
t.Fatal(err)
}
link, err := os.Readlink(from)
if err != nil {
t.Fatal(err)
}
if link != "to" {
t.Fatal("Wrong link target created: ", link)
}
target, err := Reverse(from)
if err != nil {
t.Fatal(err)
}
if target != to {
t.Fatal("Reverse reports wrong link target: ", target)
}
}
func TestExistingLink(t *testing.T) {
tmp, rm := TempDir()
defer rm()
from := tmp + "/from"
to := tmp + "/to"
err := ioutil.WriteFile(to, []byte(""), 0777)
if err != nil {
t.Fatal(err)
}
os.Symlink("to", from)
if err != nil {
t.Fatal(err)
}
err = MoveLink(from, to)
if err != nil {
t.Fatal(err)
}
// MoveLink should do nothing if {from}
// is already a symlink to {to}
link, err := os.Readlink(from)
if err != nil {
t.Fatal(err)
}
if link != "to" {
t.Fatal("MoveLink has changed existing link to: ", link)
}
_, err = os.Stat(to)
if err != nil {
t.Fatal("MoveLink deleted file at: ", to)
}
}
func TestReverseOnNonLink(t *testing.T) {
tmp, rm := TempDir()
defer rm()
fp := tmp + "/file"
err := ioutil.WriteFile(fp, []byte(""), 0777)
target, err := Reverse(fp)
if err != nil {
t.Fatal(err)
}
if target != "" {
t.Fatal("{target} should be empty when file is not a symlink: ", target)
}
}
func TestRelativePath(t *testing.T) {
tmp, rm := TempDir()
defer rm()
from := tmp + "/from"
to := tmp + "/to"
err := ioutil.WriteFile(from, []byte(""), 0777)
if err != nil {
t.Fatal(err)
}
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
relFrom, err := filepath.Rel(wd, from)
if err != nil {
t.Fatal(err)
}
err = MoveLink(relFrom, to)
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(to)
if err != nil {
t.Fatal(err)
}
link, err := os.Readlink(relFrom)
if err != nil {
t.Fatal(err)
}
if link != "to" {
t.Fatal("Wrong link target created: ", link)
}
target, err := Reverse(from)
if err != nil {
t.Fatal(err)
}
if target != to {
t.Fatal("Reverse reports wrong link target:", target, "should be:", to)
}
}
func TempDir() (string, func()) {
path, err := ioutil.TempDir("", "TestMl")
check(err)
path, err = filepath.EvalSymlinks(path)
check(err)
return path, func() {
check(os.RemoveAll(path))
}
}
func check(err error) {
if err != nil {
panic(err)
}
}