-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_windows_test.go
119 lines (103 loc) · 2.32 KB
/
os_windows_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
package os_test
import (
"io/ioutil"
"os"
osexec "os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
)
var supportJunctionLinks = true
func init() {
tmpdir, err := ioutil.TempDir("", "symtest")
if err != nil {
panic("failed to create temp directory: " + err.Error())
}
defer os.RemoveAll(tmpdir)
err = os.Symlink("target", filepath.Join(tmpdir, "symlink"))
if err != nil {
err = err.(*os.LinkError).Err
switch err {
case syscall.EWINDOWS, syscall.ERROR_PRIVILEGE_NOT_HELD:
supportsSymlinks = false
}
}
defer os.Remove("target")
b, _ := osexec.Command("cmd", "/c", "mklink", "/?").Output()
if !strings.Contains(string(b), " /J ") {
supportJunctionLinks = false
}
}
func TestSameWindowsFile(t *testing.T) {
temp, err := ioutil.TempDir("", "TestSameWindowsFile")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(temp)
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(temp)
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
f, err := os.Create("a")
if err != nil {
t.Fatal(err)
}
f.Close()
ia1, err := os.Stat("a")
if err != nil {
t.Fatal(err)
}
path, err := filepath.Abs("a")
if err != nil {
t.Fatal(err)
}
ia2, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if !os.SameFile(ia1, ia2) {
t.Errorf("files should be same")
}
p := filepath.VolumeName(path) + filepath.Base(path)
if err != nil {
t.Fatal(err)
}
ia3, err := os.Stat(p)
if err != nil {
t.Fatal(err)
}
if !os.SameFile(ia1, ia3) {
t.Errorf("files should be same")
}
}
func TestStatJunctionLink(t *testing.T) {
if !supportJunctionLinks {
t.Skip("skipping because junction links are not supported")
}
dir, err := ioutil.TempDir("", "go-build")
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
link := filepath.Join(filepath.Dir(dir), filepath.Base(dir)+"-link")
output, err := osexec.Command("cmd", "/c", "mklink", "/J", link, dir).CombinedOutput()
if err != nil {
t.Fatalf("failed to run mklink %v %v: %v %q", link, dir, err, output)
}
defer os.Remove(link)
fi, err := os.Stat(link)
if err != nil {
t.Fatalf("failed to stat link %v: %v", link, err)
}
expected := filepath.Base(dir)
got := fi.Name()
if !fi.IsDir() || expected != got {
t.Fatalf("link should point to %v but points to %v instead", expected, got)
}
}