跳到主要内容

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

浏览文件

shell_test.go

194 行 · 4.8 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
7 "testing"
8
9 "gotest.tools/v3/assert"
10)
11
12// TestPathDepth covers both host path forms, because `limactl shell --sync`
13// compares the depth against rsyncMinimumSrcDirDepth to refuse a directory too
14// close to the root. A Windows path must count the same as its Unix equivalent,
15// or the same threshold means two different things. The windows column is what
16// the call site passes for runtime.GOOS, so both platforms are covered wherever
17// this runs.
18func TestPathDepth(t *testing.T) {
19 tests := []struct {
20 path string
21 windows bool
22 expected int
23 }{
24 // A home directory sits at 3 on either platform, below the minimum of 4.
25 {"/Users/jan", false, 3},
26 {`C:\Users\jan`, true, 3},
27 {"/Users/jan/proj", false, 4},
28 {`C:\Users\jan\proj`, true, 4},
29 {"/", false, 2},
30 {`C:\`, true, 2},
31 // filepath.Abs normalises this form away before Lima ever sees it.
32 // Counting it anyway keeps the helper independent of the host separator.
33 {"C:/Users/jan/proj", true, 4},
34 // On Unix a backslash is an ordinary filename character. Counting it as
35 // a separator would score this single directory just below the root at
36 // 4 and pass the guard.
37 {`/x\y\z`, false, 2},
38 {`/foo\..\..\..\bar`, false, 2},
39 // The guard must refuse a share root and allow a directory inside it;
40 // counting both leading separators would pass the root.
41 {`\\server\share`, true, 3},
42 {`\\server\share\proj`, true, 4},
43 // Go's Clean keeps a separator run inside a UNC volume, and whether
44 // Windows collapses it earlier is unverified, so the guard collapses
45 // the run itself.
46 {`\\server\\share`, true, 3},
47 {`\\\server\share`, true, 3},
48 // An extended-length or device prefix spells a path the plain form
49 // also spells.
50 {`\\?\C:\`, true, 2},
51 {`\\?\C:\Users\jan\proj`, true, 4},
52 {`\\.\C:\`, true, 2},
53 {`\\?\UNC\server\share`, true, 3},
54 // filepath.Clean keeps a root's trailing separator and strips one below
55 // a root, so only the first form reaches Lima. Counting both alike
56 // keeps the trim from changing an ordinary path's depth.
57 {`\\server\share\`, true, 3},
58 {`C:\Users\jan\proj\`, true, 4},
59 // An empty path returns 1, so the guard refuses one if it ever reaches
60 // here.
61 {"", false, 1},
62 }
63 for _, tt := range tests {
64 t.Run(tt.path, func(t *testing.T) {
65 assert.Equal(t, pathDepth(tt.path, tt.windows), tt.expected)
66 })
67 }
68}
69
70func TestParseRsyncStats(t *testing.T) {
71 tests := []struct {
72 name string
73 output string
74 expected *rsyncStats
75 }{
76 {
77 name: "mixed output",
78 output: `
79>f+++++++++ new-file.txt
80cd+++++++++ dir/
81cL+++++++++ new-symlink -> target
82*deleting deleted.txt
83`,
84 expected: &rsyncStats{
85 Added: 3,
86 Deleted: 1,
87 Modified: 0,
88 },
89 },
90 {
91 name: "metadata-only changes",
92 output: `
93.d..t...... ./
94.f...p..... existing.txt
95`,
96 expected: &rsyncStats{
97 Added: 0,
98 Deleted: 0,
99 Modified: 0,
100 Metadata: 2,
101 },
102 },
103 {
104 name: "many changes",
105 output: `
106<f+++++++++ file1
107<f+++++++++ file2
108*deleting file3
109*deleting file4
110*deleting file5
111<f..T...... file6
112`,
113 expected: &rsyncStats{
114 Added: 2,
115 Deleted: 3,
116 Modified: 1,
117 },
118 },
119 }
120
121 for _, tt := range tests {
122 t.Run(tt.name, func(t *testing.T) {
123 got := parseRsyncStats(tt.output)
124 assert.DeepEqual(t, got, tt.expected)
125 })
126 }
127}
128
129func TestRsyncStatsString(t *testing.T) {
130 tests := []struct {
131 name string
132 stats *rsyncStats
133 expected string
134 }{
135 {
136 name: "empty",
137 stats: &rsyncStats{},
138 expected: "",
139 },
140 {
141 name: "all stats",
142 stats: &rsyncStats{
143 Added: 1,
144 Deleted: 2,
145 Modified: 3,
146 Metadata: 4,
147 },
148 expected: "added: 1, deleted: 2, modified: 3, metadata: 4",
149 },
150 }
151
152 for _, tt := range tests {
153 t.Run(tt.name, func(t *testing.T) {
154 assert.Equal(t, tt.stats.String(), tt.expected)
155 })
156 }
157}
158
159func TestWindowsShellCommandFlag(t *testing.T) {
160 tests := []struct {
161 shell string
162 want string
163 }{
164 {"cmd.exe", "/c"},
165 {"CMD.EXE", "/c"},
166 {`C:\Windows\System32\cmd.exe`, "/c"},
167 {`C:/Windows/System32/cmd.exe`, "/c"},
168 {"powershell.exe", "-Command"},
169 {"pwsh.exe", "-Command"},
170 {`C:\Program Files\PowerShell\7\pwsh.exe`, "-Command"},
171 }
172 for _, tt := range tests {
173 t.Run(tt.shell, func(t *testing.T) {
174 assert.Equal(t, tt.want, windowsShellCommandFlag(tt.shell))
175 })
176 }
177}
178
179func TestWindowsQuoteShell(t *testing.T) {
180 tests := []struct {
181 shell string
182 want string
183 }{
184 {"cmd.exe", "cmd.exe"},
185 {`C:\Windows\System32\cmd.exe`, `C:\Windows\System32\cmd.exe`},
186 {`C:\Program Files\PowerShell\7\pwsh.exe`, `"C:\Program Files\PowerShell\7\pwsh.exe"`},
187 {`"C:\already\quoted.exe"`, `"C:\already\quoted.exe"`},
188 }
189 for _, tt := range tests {
190 t.Run(tt.shell, func(t *testing.T) {
191 assert.Equal(t, tt.want, windowsQuoteShell(tt.shell))
192 })
193 }
194}