forked from nofun97/afero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symlink_test.go
160 lines (133 loc) · 5.03 KB
/
symlink_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
package afero
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestSymlinkIfPossible(t *testing.T) {
wd, _ := os.Getwd()
defer func() {
os.Chdir(wd)
}()
osFs := &OsFs{}
workDir, err := TempDir(osFs, "", "afero-symlink")
if err != nil {
t.Fatal(err)
}
defer func() {
osFs.RemoveAll(workDir)
}()
memWorkDir := "/sym"
memFs := NewMemMapFs()
overlayFs1 := &CopyOnWriteFs{base: osFs, layer: memFs}
overlayFs2 := &CopyOnWriteFs{base: memFs, layer: osFs}
overlayFsMemOnly := &CopyOnWriteFs{base: memFs, layer: NewMemMapFs()}
basePathFs := &BasePathFs{source: osFs, path: workDir}
basePathFsMem := &BasePathFs{source: memFs, path: memWorkDir}
roFs := &ReadOnlyFs{source: osFs}
roFsMem := &ReadOnlyFs{source: memFs}
pathFileMem := filepath.Join(memWorkDir, "aferom.txt")
osPath := filepath.Join(workDir, "afero.txt")
WriteFile(osFs, osPath, []byte("Hi, Afero!"), 0777)
WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0777)
testLink := func(l Linker, source, destination string, output *string) {
if fs, ok := l.(Fs); ok {
dir := filepath.Dir(destination)
if dir != "" {
fs.MkdirAll(dir, 0777)
}
}
err := l.SymlinkIfPossible(source, destination)
if (err == nil) && (output != nil) {
t.Fatalf("Error creating symlink, succeeded when expecting error %v", *output)
} else if (err != nil) && (output == nil) {
t.Fatalf("Error creating symlink, expected success, got %v", err)
} else if err != nil && err.Error() != *output && !strings.HasSuffix(err.Error(), *output) {
t.Fatalf("Error creating symlink, expected error '%v', instead got output '%v'", *output, err)
} else {
// test passed, if expecting a successful link, check the link with lstat if able
if output == nil {
if lst, ok := l.(Lstater); ok {
_, ok, err := lst.LstatIfPossible(destination)
if !ok {
if err != nil {
t.Fatalf("Error calling lstat on file after successful link, got: %v", err)
} else {
t.Fatalf("Error calling lstat on file after successful link, result didn't use lstat (not link)")
}
return
}
}
}
}
}
notSupported := ErrNoSymlink.Error()
testLink(osFs, osPath, filepath.Join(workDir, "os/link.txt"), nil)
testLink(overlayFs1, osPath, filepath.Join(workDir, "overlay/link1.txt"), ¬Supported)
testLink(overlayFs2, pathFileMem, filepath.Join(workDir, "overlay2/link2.txt"), nil)
testLink(overlayFsMemOnly, pathFileMem, filepath.Join(memWorkDir, "overlay3/link.txt"), ¬Supported)
testLink(basePathFs, "afero.txt", "basepath/link.txt", nil)
testLink(basePathFsMem, pathFileMem, "link/file.txt", ¬Supported)
testLink(roFs, osPath, filepath.Join(workDir, "ro/link.txt"), ¬Supported)
testLink(roFsMem, pathFileMem, filepath.Join(memWorkDir, "ro/link.txt"), ¬Supported)
}
func TestReadlinkIfPossible(t *testing.T) {
wd, _ := os.Getwd()
defer func() {
os.Chdir(wd)
}()
osFs := &OsFs{}
workDir, err := TempDir(osFs, "", "afero-readlink")
if err != nil {
t.Fatal(err)
}
defer func() {
osFs.RemoveAll(workDir)
}()
memWorkDir := "/read"
memFs := NewMemMapFs()
overlayFs1 := &CopyOnWriteFs{base: osFs, layer: memFs}
overlayFs2 := &CopyOnWriteFs{base: memFs, layer: osFs}
overlayFsMemOnly := &CopyOnWriteFs{base: memFs, layer: NewMemMapFs()}
basePathFs := &BasePathFs{source: osFs, path: workDir}
basePathFsMem := &BasePathFs{source: memFs, path: memWorkDir}
roFs := &ReadOnlyFs{source: osFs}
roFsMem := &ReadOnlyFs{source: memFs}
pathFileMem := filepath.Join(memWorkDir, "aferom.txt")
osPath := filepath.Join(workDir, "afero.txt")
WriteFile(osFs, osPath, []byte("Hi, Afero!"), 0777)
WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0777)
createLink := func(l Linker, source, destination string) error {
if fs, ok := l.(Fs); ok {
dir := filepath.Dir(destination)
if dir != "" {
fs.MkdirAll(dir, 0777)
}
}
return l.SymlinkIfPossible(source, destination)
}
testRead := func(r LinkReader, name string, output *string) {
_, err := r.ReadlinkIfPossible(name)
if (err != nil) && (output == nil) {
t.Fatalf("Error reading link, expected success, got error: %v", err)
} else if (err == nil) && (output != nil) {
t.Fatalf("Error reading link, succeeded when expecting error: %v", *output)
} else if err != nil && err.Error() != *output && !strings.HasSuffix(err.Error(), *output) {
t.Fatalf("Error reading link, expected error '%v', instead received '%v'", *output, err)
}
}
notSupported := ErrNoReadlink.Error()
err = createLink(osFs, osPath, filepath.Join(workDir, "os/link.txt"))
if err != nil {
t.Fatal("Error creating test link: ", err)
}
testRead(osFs, filepath.Join(workDir, "os/link.txt"), nil)
testRead(overlayFs1, filepath.Join(workDir, "os/link.txt"), nil)
testRead(overlayFs2, filepath.Join(workDir, "os/link.txt"), nil)
testRead(overlayFsMemOnly, pathFileMem, ¬Supported)
testRead(basePathFs, "os/link.txt", nil)
testRead(basePathFsMem, pathFileMem, ¬Supported)
testRead(roFs, filepath.Join(workDir, "os/link.txt"), nil)
testRead(roFsMem, pathFileMem, ¬Supported)
}