forked from caddyserver/xcaddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
143 lines (139 loc) · 3.45 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
package xcaddycmd
import (
"runtime"
"testing"
)
func TestSplitWith(t *testing.T) {
for i, tc := range []struct {
input string
expectModule string
expectVersion string
expectReplace string
expectErr bool
}{
{
input: "module",
expectModule: "module",
},
{
input: "module@version",
expectModule: "module",
expectVersion: "version",
},
{
input: "module@version=replace",
expectModule: "module",
expectVersion: "version",
expectReplace: "replace",
},
{
input: "module=replace",
expectModule: "module",
expectReplace: "replace",
},
{
input: "module=replace@version",
expectModule: "module",
expectReplace: "replace@version",
},
{
input: "module@version=replace@version",
expectModule: "module",
expectVersion: "version",
expectReplace: "replace@version",
},
{
input: "=replace",
expectErr: true,
},
{
input: "@version",
expectErr: true,
},
{
input: "@version=replace",
expectErr: true,
},
{
input: "",
expectErr: true,
},
{
// issue #109
input: "/home/devin/projects/@relay/caddy-bin@version",
expectModule: "/home/devin/projects/@relay/caddy-bin",
expectVersion: "version",
},
} {
actualModule, actualVersion, actualReplace, actualErr := splitWith(tc.input)
if actualModule != tc.expectModule {
t.Errorf("Test %d: Expected module '%s' but got '%s' (input=%s)",
i, tc.expectModule, actualModule, tc.input)
}
if tc.expectErr {
if actualErr == nil {
t.Errorf("Test %d: Expected error but did not get one (input='%s')", i, tc.input)
}
continue
}
if !tc.expectErr && actualErr != nil {
t.Errorf("Test %d: Expected no error but got: %s (input='%s')", i, actualErr, tc.input)
}
if actualVersion != tc.expectVersion {
t.Errorf("Test %d: Expected version '%s' but got '%s' (input='%s')",
i, tc.expectVersion, actualVersion, tc.input)
}
if actualReplace != tc.expectReplace {
t.Errorf("Test %d: Expected module '%s' but got '%s' (input='%s')",
i, tc.expectReplace, actualReplace, tc.input)
}
}
}
func TestNormalizeImportPath(t *testing.T) {
type (
args struct {
currentModule string
cwd string
moduleDir string
}
testCaseType []struct {
name string
args args
want string
}
)
tests := testCaseType{
{"linux-path", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "/xcaddy",
moduleDir: "/xcaddy",
}, "github.com/caddyserver/xcaddy"},
{"linux-subpath", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "/xcaddy/subdir",
moduleDir: "/xcaddy",
}, "github.com/caddyserver/xcaddy/subdir"},
}
windowsTests := testCaseType{
{"windows-path", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "c:\\xcaddy",
moduleDir: "c:\\xcaddy",
}, "github.com/caddyserver/xcaddy"},
{"windows-subpath", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "c:\\xcaddy\\subdir",
moduleDir: "c:\\xcaddy",
}, "github.com/caddyserver/xcaddy/subdir"},
}
if runtime.GOOS == "windows" {
tests = append(tests, windowsTests...)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeImportPath(tt.args.currentModule, tt.args.cwd, tt.args.moduleDir); got != tt.want {
t.Errorf("normalizeImportPath() = %v, want %v", got, tt.want)
}
})
}
}