跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

editflags_test.go

308 行 · 8.9 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package editflags
5
6import (
7 "strings"
8 "testing"
9
10 "github.com/spf13/cobra"
11 "gotest.tools/v3/assert"
12
13 "github.com/lima-vm/lima/v2/pkg/localpathutil"
14)
15
16func TestCompleteCPUs(t *testing.T) {
17 assert.DeepEqual(t, []int{1}, completeCPUs(1))
18 assert.DeepEqual(t, []int{1, 2}, completeCPUs(2))
19 assert.DeepEqual(t, []int{1, 2, 4, 8}, completeCPUs(8))
20 assert.DeepEqual(t, []int{1, 2, 4, 8, 16, 20}, completeCPUs(20))
21}
22
23func TestCompleteMemoryGiB(t *testing.T) {
24 assert.DeepEqual(t, []float32{0.5}, completeMemoryGiB(1<<30))
25 assert.DeepEqual(t, []float32{1}, completeMemoryGiB(2<<30))
26 assert.DeepEqual(t, []float32{1, 2}, completeMemoryGiB(4<<30))
27 assert.DeepEqual(t, []float32{1, 2, 4}, completeMemoryGiB(8<<30))
28 assert.DeepEqual(t, []float32{1, 2, 4, 8, 10}, completeMemoryGiB(20<<30))
29}
30
31func TestBuildPortForwardExpression(t *testing.T) {
32 tests := []struct {
33 name string
34 portForwards []string
35 expected string
36 expectError bool
37 }{
38 {
39 name: "empty port forwards",
40 portForwards: []string{},
41 expected: "",
42 },
43 {
44 name: "single dynamic port forward",
45 portForwards: []string{"8080:80"},
46 expected: `.portForwards += [{"guestPort": "80", "hostPort": "8080", "static": false}]`,
47 },
48 {
49 name: "single static port forward",
50 portForwards: []string{"8080:80,static=true"},
51 expected: `.portForwards += [{"guestPort": "80", "hostPort": "8080", "static": true}]`,
52 },
53 {
54 name: "multiple mixed port forwards",
55 portForwards: []string{"8080:80", "2222:22,static=true", "3000:3000"},
56 expected: `.portForwards += [{"guestPort": "80", "hostPort": "8080", "static": false},{"guestPort": "22", "hostPort": "2222", "static": true},{"guestPort": "3000", "hostPort": "3000", "static": false}]`,
57 },
58 {
59 name: "invalid format - missing colon",
60 portForwards: []string{"8080"},
61 expectError: true,
62 },
63 {
64 name: "invalid format - too many colons",
65 portForwards: []string{"8080:80:extra"},
66 expectError: true,
67 },
68 {
69 name: "invalid static parameter",
70 portForwards: []string{"8080:80,invalid=true"},
71 expectError: true,
72 },
73 {
74 name: "too many parameters",
75 portForwards: []string{"8080:80,static=true,extra=value"},
76 expectError: true,
77 },
78 {
79 name: "whitespace handling",
80 portForwards: []string{" 8080 : 80 , static=true "},
81 expected: `.portForwards += [{"guestPort": "80", "hostPort": "8080", "static": true}]`,
82 },
83 }
84
85 for _, tt := range tests {
86 t.Run(tt.name, func(t *testing.T) {
87 result, err := BuildPortForwardExpression(tt.portForwards)
88 if tt.expectError {
89 assert.Check(t, err != nil)
90 } else {
91 assert.NilError(t, err)
92 assert.Equal(t, tt.expected, result)
93 }
94 })
95 }
96}
97
98func TestParsePortForward(t *testing.T) {
99 tests := []struct {
100 name string
101 spec string
102 hostPort string
103 guestPort string
104 isStatic bool
105 expectError bool
106 }{
107 {
108 name: "dynamic port forward",
109 spec: "8080:80",
110 hostPort: "8080",
111 guestPort: "80",
112 isStatic: false,
113 },
114 {
115 name: "static port forward",
116 spec: "8080:80,static=true",
117 hostPort: "8080",
118 guestPort: "80",
119 isStatic: true,
120 },
121 {
122 name: "whitespace handling",
123 spec: " 8080 : 80 , static=true ",
124 hostPort: "8080",
125 guestPort: "80",
126 isStatic: true,
127 },
128 {
129 name: "invalid format - missing colon",
130 spec: "8080",
131 expectError: true,
132 },
133 {
134 name: "invalid format - too many colons",
135 spec: "8080:80:extra",
136 expectError: true,
137 },
138 {
139 name: "invalid parameter",
140 spec: "8080:80,invalid=true",
141 expectError: true,
142 },
143 {
144 name: "too many parameters",
145 spec: "8080:80,static=true,extra=value",
146 expectError: true,
147 },
148 }
149
150 for _, tt := range tests {
151 t.Run(tt.name, func(t *testing.T) {
152 hostPort, guestPort, isStatic, err := ParsePortForward(tt.spec)
153 if tt.expectError {
154 assert.Check(t, err != nil)
155 } else {
156 assert.NilError(t, err)
157 assert.Equal(t, tt.hostPort, hostPort)
158 assert.Equal(t, tt.guestPort, guestPort)
159 assert.Equal(t, tt.isStatic, isStatic)
160 }
161 })
162 }
163}
164
165func TestBuildParamExpressions(t *testing.T) {
166 tests := []struct {
167 name string
168 params []string
169 allowedParams map[string]string
170 expected []string
171 expectError string
172 }{
173 {
174 name: "single param",
175 params: []string{"version=v1.35"},
176 allowedParams: map[string]string{"version": ""},
177 expected: []string{`.param["version"] = "v1.35"`},
178 },
179 {
180 name: "value contains equals",
181 params: []string{"token=a=b"},
182 allowedParams: map[string]string{"token": ""},
183 expected: []string{`.param["token"] = "a=b"`},
184 },
185 {
186 name: "invalid format",
187 params: []string{"version"},
188 allowedParams: map[string]string{"version": ""},
189 expectError: "invalid parameter `version`, expected NAME=VALUE",
190 },
191 {
192 name: "undefined param",
193 params: []string{"missing=value"},
194 allowedParams: map[string]string{"version": ""},
195 expectError: "template does not define param `missing`",
196 },
197 }
198
199 for _, tt := range tests {
200 t.Run(tt.name, func(t *testing.T) {
201 result, err := BuildParamExpressions(tt.params, tt.allowedParams)
202 if tt.expectError != "" {
203 assert.ErrorContains(t, err, tt.expectError)
204 } else {
205 assert.NilError(t, err)
206 assert.DeepEqual(t, tt.expected, result)
207 }
208 })
209 }
210}
211
212func TestYQExpressions(t *testing.T) {
213 expand := func(s string) string {
214 s, err := localpathutil.Expand(s)
215 assert.NilError(t, err)
216 // `D:\foo` -> `D:\\foo` (appears in YAML)
217 s = strings.ReplaceAll(s, "\\", "\\\\")
218 return s
219 }
220 tests := []struct {
221 name string
222 args []string
223 newInstance bool
224 expected []string
225 expectError string
226 }{
227 {
228 name: "mount",
229 args: []string{"--mount", "/foo", "--mount", "./bar:w"},
230 newInstance: false,
231 expected: []string{`.mounts = [{"location": "` + expand("./bar") + `", "mountPoint": "` + expand("./bar") + `", "writable": true},{"location": "` + expand("/foo") + `", "mountPoint": "` + expand("/foo") + `", "writable": false}] + .mounts`},
232 },
233 {
234 name: "mount-only",
235 args: []string{"--mount-only", "/foo", "--mount-only", "/bar:w"},
236 newInstance: false,
237 expected: []string{`.mounts = [{"location": "` + expand("/foo") + `", "mountPoint": "` + expand("/foo") + `", "writable": false},{"location": "` + expand("/bar") + `", "mountPoint": "` + expand("/bar") + `", "writable": true}]`},
238 },
239 {
240 name: "mixture of mount and mount-only",
241 args: []string{"--mount", "/foo", "--mount-only", "/bar:w"},
242 newInstance: false,
243 expectError: "flag `--mount` conflicts with `--mount-only`",
244 },
245 {
246 name: "dns",
247 args: []string{"--dns", "8.8.8.8", "--dns", "8.8.4.4", "--dns", "1.1.1.1"},
248 newInstance: false,
249 expected: []string{`.dns += ["8.8.8.8","8.8.4.4","1.1.1.1"] | .dns |= unique | .hostResolver.enabled=false`},
250 },
251 {
252 name: "network vzNAT",
253 args: []string{"--network", "vzNAT"},
254 newInstance: true,
255 expected: []string{`.networks += [{"vzNAT": true}] | .networks |= unique_by(.lima)`},
256 },
257 {
258 name: "network lima:shared",
259 args: []string{"--network", "lima:shared"},
260 newInstance: true,
261 expected: []string{`.networks += [{"lima": "shared"}] | .networks |= unique_by(.lima)`},
262 },
263 {
264 name: "multiple networks",
265 args: []string{"--network", "vzNAT", "--network", "lima:shared", "--network", "lima:bridged"},
266 newInstance: true,
267 expected: []string{`.networks += [{"vzNAT": true},{"lima": "shared"},{"lima": "bridged"}] | .networks |= unique_by(.lima)`},
268 },
269 {
270 name: "nested-virt",
271 args: []string{"--nested-virt"},
272 newInstance: false,
273 expected: []string{`.nestedVirtualization = true`},
274 },
275 {
276 name: "param",
277 args: []string{"--param", "version=v1.35"},
278 newInstance: false,
279 expected: []string{`.param["version"] = "v1.35"`},
280 },
281 {
282 name: "undefined param",
283 args: []string{"--param", "missing=value"},
284 newInstance: false,
285 expectError: "template does not define param `missing`",
286 },
287 {
288 name: "invalid network",
289 args: []string{"--network", "invalid"},
290 newInstance: true,
291 expectError: "network name must be `vzNAT` or `lima:*`, got `invalid`",
292 },
293 }
294 for _, tt := range tests {
295 t.Run(tt.name, func(t *testing.T) {
296 cmd := &cobra.Command{}
297 RegisterEdit(cmd, "")
298 assert.NilError(t, cmd.ParseFlags(tt.args))
299 expr, err := YQExpressions(cmd.Flags(), tt.newInstance, map[string]string{"version": ""})
300 if tt.expectError != "" {
301 assert.ErrorContains(t, err, tt.expectError)
302 } else {
303 assert.NilError(t, err)
304 assert.DeepEqual(t, tt.expected, expr)
305 }
306 })
307 }
308}