This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval_symlink_test.go
96 lines (87 loc) · 1.73 KB
/
eval_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
package fs
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEvalSymlinks(t *testing.T) {
tmpDir := t.TempDir()
t.Log(tmpDir)
// Make sure the test base dir is not a symlink.
tmpDir, err := filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Error(err)
}
cases := []struct {
name string
path string
lpath string
target string
targetExist bool
expected string
}{
{
"symlink point to an existing target",
filepath.Join(tmpDir, "lna"),
"",
filepath.Join(tmpDir, "a"),
true,
filepath.Join(tmpDir, "a"),
},
{
"symlink point to a non-existent target",
filepath.Join(tmpDir, "lnb"),
"",
filepath.Join(tmpDir, "b"),
false,
filepath.Join(tmpDir, "b"),
},
{
"symlink point to another symlink",
filepath.Join(tmpDir, "lnd"),
filepath.Join(tmpDir, "lnc"),
filepath.Join(tmpDir, "c"),
false,
filepath.Join(tmpDir, "c"),
},
{
"symlink point to a relative target",
filepath.Join(tmpDir, "lle"),
"",
tmpDir + "/./e",
false,
filepath.Join(tmpDir, "e"),
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if tt.targetExist {
err := os.MkdirAll(tt.target, 0755)
if err != nil {
t.Log(err.Error())
}
}
if tt.lpath != "" {
err := os.Symlink(tt.target, tt.lpath)
if err != nil {
t.Log(err.Error())
}
err = os.Symlink(tt.lpath, tt.path)
if err != nil {
t.Log(err.Error())
}
} else {
err := os.Symlink(tt.target, tt.path)
if err != nil {
t.Log(err.Error())
}
}
actualTarget, err := evalSymlinks(tt.path)
if err != nil {
t.Log(err.Error())
}
assert.Equal(t, tt.expected, actualTarget)
})
}
}