跳到主要内容

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

浏览文件

abs_test.go

320 行 · 7.7 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package limatmpl
5
6import (
7 "os"
8 "path/filepath"
9 "runtime"
10 "strings"
11 "testing"
12
13 "gotest.tools/v3/assert"
14)
15
16type useAbsLocatorsTestCase struct {
17 description string
18 locator string
19 template string
20 expected string
21}
22
23var useAbsLocatorsTestCases = []useAbsLocatorsTestCase{
24 {
25 "Template without base or script file",
26 "template:foo",
27 `arch: aarch64`,
28 `arch: aarch64`,
29 },
30 {
31 "Single string base template",
32 "template:foo",
33 `base: bar.yaml`,
34 `base: template:bar.yaml`,
35 },
36 {
37 "Legacy template:// base template",
38 "template://foo",
39 `base: bar.yaml`,
40 `base: template:bar.yaml`,
41 },
42 {
43 "Flow style array of one base template",
44 "template:foo",
45 `base: [{url: bar.yaml, digest: deadbeef}]`,
46 // not sure why the quotes around the URL were added; maybe because we don't copy the style from the source
47 `base: [{url: 'template:bar.yaml', digest: deadbeef}]`,
48 },
49 {
50 "Flow style array of sequence of two base URLs",
51 "template:foo",
52 `base: [bar.yaml, baz.yaml]`,
53 `base: ['template:bar.yaml', 'template:baz.yaml']`,
54 },
55 {
56 "Flow style array of sequence of two base locator objects",
57 "template:foo",
58 `base: [{url: bar.yaml, digest: deadbeef}, {url: baz.yaml, digest: decafbad}]`,
59 `base: [{url: 'template:bar.yaml', digest: deadbeef}, {url: 'template:baz.yaml', digest: decafbad}]`,
60 },
61 {
62 "Block style array of one base template",
63 "template:foo",
64 `
65base:
66- bar.yaml
67`,
68 `
69base:
70- template:bar.yaml`,
71 },
72 {
73 "Block style of four base templates",
74 "template:foo",
75 `
76base:
77- bar.yaml
78- template:my
79- https://example.com/my.yaml
80- baz.yaml
81`,
82 `
83base:
84- template:bar.yaml
85- template:my
86- https://example.com/my.yaml
87- template:baz.yaml
88`,
89 },
90 {
91 "Provisioning and probe scripts",
92 "template:experimental/foo",
93 `
94provision:
95- mode: user
96 file: userscript.sh
97- mode: system
98 file:
99 url: systemscript.sh
100 digest: abc123
101probes:
102- file: probe.sh
103- file:
104 url: probe.sh
105 digest: digest
106`,
107 `
108provision:
109- mode: user
110 file: template:experimental/userscript.sh
111- mode: system
112 file:
113 url: template:experimental/systemscript.sh
114 digest: abc123
115probes:
116- file: template:experimental/probe.sh
117- file:
118 url: template:experimental/probe.sh
119 digest: digest
120`,
121 },
122}
123
124func TestUseAbsLocators(t *testing.T) {
125 for _, tc := range useAbsLocatorsTestCases {
126 t.Run(tc.description, func(t *testing.T) { RunUseAbsLocatorTest(t, tc) })
127 }
128}
129
130func RunUseAbsLocatorTest(t *testing.T, tc useAbsLocatorsTestCase) {
131 tmpl := &Template{
132 Bytes: []byte(strings.TrimSpace(tc.template)),
133 Locator: tc.locator,
134 }
135 err := tmpl.UseAbsLocators(t.Context())
136 assert.NilError(t, err, tc.description)
137
138 actual := strings.TrimSpace(string(tmpl.Bytes))
139 expected := strings.TrimSpace(tc.expected)
140 assert.Equal(t, actual, expected, tc.description)
141}
142
143func TestBasePath(t *testing.T) {
144 // On Windows the root will be something like "C:\"
145 root, err := filepath.Abs("/")
146 assert.NilError(t, err)
147 volume := filepath.VolumeName(root)
148
149 t.Run("", func(t *testing.T) {
150 actual, err := basePath("/foo")
151 assert.NilError(t, err)
152 assert.Equal(t, actual, root)
153 })
154
155 t.Run("", func(t *testing.T) {
156 actual, err := basePath("/foo/bar")
157 assert.NilError(t, err)
158 assert.Equal(t, actual, filepath.Clean(volume+"/foo"))
159 })
160
161 t.Run("", func(t *testing.T) {
162 actual, err := basePath("template:foo")
163 assert.NilError(t, err)
164 assert.Equal(t, actual, "template:")
165 })
166
167 t.Run("", func(t *testing.T) {
168 actual, err := basePath("template:foo/bar")
169 assert.NilError(t, err)
170 assert.Equal(t, actual, "template:foo")
171 })
172
173 t.Run("", func(t *testing.T) {
174 actual, err := basePath("http://host/foo")
175 assert.NilError(t, err)
176 assert.Equal(t, actual, "http://host")
177 })
178
179 t.Run("", func(t *testing.T) {
180 actual, err := basePath("http://host/foo/bar")
181 assert.NilError(t, err)
182 assert.Equal(t, actual, "http://host/foo")
183 })
184
185 t.Run("", func(t *testing.T) {
186 actual, err := basePath("file:///foo")
187 assert.NilError(t, err)
188 assert.Equal(t, actual, "file:///")
189 })
190
191 t.Run("", func(t *testing.T) {
192 actual, err := basePath("file:///foo/bar")
193 assert.NilError(t, err)
194 assert.Equal(t, actual, "file:///foo")
195 })
196}
197
198func TestAbsPath(t *testing.T) {
199 root, err := filepath.Abs("/")
200 assert.NilError(t, err)
201 volume := filepath.VolumeName(root)
202
203 t.Run("If the locator is already an absolute path, it is returned unchanged", func(t *testing.T) {
204 actual, err := absPath(volume+"/foo", volume+"/root")
205 assert.NilError(t, err)
206 assert.Equal(t, actual, filepath.Clean(volume+"/foo"))
207 })
208
209 t.Run("If the locator is a rooted path without volume name, then the volume will be added", func(t *testing.T) {
210 actual, err := absPath("/foo", volume+"/root")
211 assert.NilError(t, err)
212 assert.Equal(t, actual, filepath.Clean(volume+"/foo"))
213 })
214
215 t.Run("If the locator starts with ~/, then it will be expanded to an absolute path", func(t *testing.T) {
216 actual, err := absPath("~/foo", volume+"/root")
217 assert.NilError(t, err)
218 homeDir, err := os.UserHomeDir()
219 assert.NilError(t, err)
220 // homeDir already includes the volume
221 assert.Equal(t, actual, filepath.Join(homeDir, "foo"))
222 })
223
224 t.Run("", func(t *testing.T) {
225 actual, err := absPath("template:foo", volume+"/root")
226 assert.NilError(t, err)
227 assert.Equal(t, actual, "template:foo")
228 })
229
230 t.Run("", func(t *testing.T) {
231 actual, err := absPath("http://host/foo", volume+"/root")
232 assert.NilError(t, err)
233 assert.Equal(t, actual, "http://host/foo")
234 })
235
236 t.Run("", func(t *testing.T) {
237 actual, err := absPath("file:///foo", volume+"/root")
238 assert.NilError(t, err)
239 assert.Equal(t, actual, "file:///foo")
240 })
241
242 t.Run("Can't have relative path when reading from STDIN", func(t *testing.T) {
243 _, err = absPath("foo", "-")
244 assert.ErrorContains(t, err, "STDIN")
245 })
246
247 t.Run("Relative paths must be underneath the basePath", func(t *testing.T) {
248 _, err = absPath("../foo", volume+"/root")
249 assert.ErrorContains(t, err, "'..'")
250 })
251
252 t.Run("Relative paths must not use backslash traversal", func(t *testing.T) {
253 _, err = absPath(`..\foo`, volume+"/root")
254 assert.ErrorContains(t, err, "'..'")
255 })
256
257 t.Run("locator must not be empty", func(t *testing.T) {
258 _, err = absPath("", "foo")
259 assert.ErrorContains(t, err, "locator is empty")
260 })
261
262 t.Run("basePath must not be empty", func(t *testing.T) {
263 _, err = absPath("foo", "")
264 assert.ErrorContains(t, err, "basePath is empty")
265 })
266
267 t.Run("", func(t *testing.T) {
268 _, err = absPath("./foo", "")
269 assert.ErrorContains(t, err, "empty")
270 })
271
272 t.Run("", func(t *testing.T) {
273 actual, err := absPath("./foo", volume+"/root")
274 assert.NilError(t, err)
275 assert.Equal(t, actual, filepath.Clean(volume+"/root/foo"))
276 })
277
278 if runtime.GOOS == "windows" {
279 t.Run("Relative locators must not include volume names", func(t *testing.T) {
280 _, err := absPath(volume+"foo", volume+"/root")
281 assert.ErrorContains(t, err, "volume")
282 })
283 }
284
285 t.Run("", func(t *testing.T) {
286 actual, err := absPath("foo", "template:")
287 assert.NilError(t, err)
288 assert.Equal(t, actual, "template:foo")
289 })
290
291 t.Run("", func(t *testing.T) {
292 actual, err := absPath("bar", "template:foo")
293 assert.NilError(t, err)
294 assert.Equal(t, actual, "template:foo/bar")
295 })
296
297 t.Run("", func(t *testing.T) {
298 actual, err := absPath("foo", "http://host")
299 assert.NilError(t, err)
300 assert.Equal(t, actual, "http://host/foo")
301 })
302
303 t.Run("", func(t *testing.T) {
304 actual, err := absPath("bar", "http://host/foo")
305 assert.NilError(t, err)
306 assert.Equal(t, actual, "http://host/foo/bar")
307 })
308
309 t.Run("", func(t *testing.T) {
310 actual, err := absPath("foo", "file:///")
311 assert.NilError(t, err)
312 assert.Equal(t, actual, "file:///foo")
313 })
314
315 t.Run("", func(t *testing.T) {
316 actual, err := absPath("bar", "file:///foo")
317 assert.NilError(t, err)
318 assert.Equal(t, actual, "file:///foo/bar")
319 })
320}