-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
helpers_windows_test.go
115 lines (104 loc) · 2.61 KB
/
helpers_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
//go:build windows
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"testing"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
type testEvalSymlinksMode int
const (
testEvalSymlinksNotLink testEvalSymlinksMode = iota
testEvalSymlinksSymbolicLink
testEvalSymlinksJunction
)
func Test_evalSymlinks(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
mode testEvalSymlinksMode
linkBasePath string
args args
want string
wantErr bool
}{
{
name: "not link",
mode: testEvalSymlinksNotLink,
args: args{
path: filepath.Join(os.TempDir(), "not_link"),
},
want: filepath.Join(os.TempDir(), "not_link"),
wantErr: false,
},
{
name: "symbolic link",
mode: testEvalSymlinksSymbolicLink,
linkBasePath: filepath.Join(os.TempDir(), "link_base"),
args: args{
path: filepath.Join(os.TempDir(), "symbolic_link"),
},
want: filepath.Join(os.TempDir(), "link_base"),
wantErr: false,
},
{
name: "junction",
mode: testEvalSymlinksJunction,
linkBasePath: filepath.Join(os.TempDir(), "link_base"),
args: args{
path: filepath.Join(os.TempDir(), "junction"),
},
want: filepath.Join(os.TempDir(), "link_base"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := createLink(tt.linkBasePath, tt.args.path, tt.mode); err != nil {
t.Errorf("failed to create link: %v", err)
return
}
got, err := evalSymlinks(tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("evalSymlinks() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("evalSymlinks() = %v, want %v", got, tt.want)
}
})
}
}
func createLink(linkBasePath, path string, mode testEvalSymlinksMode) error {
if err := os.RemoveAll(path); err != nil {
return err
}
if mode == testEvalSymlinksNotLink {
return os.MkdirAll(path, 0755)
}
if err := os.MkdirAll(linkBasePath, 0755); err != nil {
return err
}
switch mode {
case testEvalSymlinksSymbolicLink:
return os.Symlink(linkBasePath, path)
case testEvalSymlinksJunction:
output, err := exec.Command("cmd", "/c", "mklink", "/J", path, linkBasePath).CombinedOutput()
if err != nil {
output, err := io.ReadAll(transform.NewReader(bytes.NewBuffer(output), japanese.ShiftJIS.NewDecoder()))
if err != nil {
return fmt.Errorf("failed to transform output: %w", err)
}
return fmt.Errorf("failed to create junction: %s, %w", string(output), err)
}
return nil
}
return nil
}