跳到主要内容

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

浏览文件

copytool_test.go

196 行 · 7.1 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package copytool
5
6import (
7 "os"
8 "path/filepath"
9 "runtime"
10 "slices"
11 "testing"
12
13 "gotest.tools/v3/assert"
14)
15
16// TestCommandDoesNotMutateOptions verifies that passing opts to Command() does not
17// overwrite the tool's stored Options for subsequent calls.
18func TestCommandDoesNotMutateOptions(t *testing.T) {
19 initial := &Options{Verbose: false, Recursive: false}
20 tool, err := newRsyncTool(initial)
21 if err != nil {
22 t.Skip("rsync not found:", err)
23 }
24
25 override := &Options{Verbose: true, Recursive: true}
26 // Use local paths to avoid instance lookup
27 _, _ = tool.Command(t.Context(), []string{"/tmp/src", "/tmp/dst"}, override)
28
29 assert.Equal(t, tool.Options.Verbose, false, "Command() must not mutate stored Options.Verbose")
30 assert.Equal(t, tool.Options.Recursive, false, "Command() must not mutate stored Options.Recursive")
31}
32
33// TestRsyncCommandEndsOptionParsing verifies that a path starting with a dash is
34// passed after "--", so rsync cannot mistake it for an option such as --rsh.
35func TestRsyncCommandEndsOptionParsing(t *testing.T) {
36 tool, err := newRsyncTool(&Options{})
37 if err != nil {
38 t.Skip("rsync not found:", err)
39 }
40
41 const dashPath = "--rsh=touch pwned"
42 // Use local paths to avoid instance lookup
43 cmd, err := tool.Command(t.Context(), []string{dashPath, "/tmp/dst"}, nil)
44 assert.NilError(t, err)
45
46 sep := slices.Index(cmd.Args, "--")
47 assert.Assert(t, sep != -1, "rsync args must contain the %#q separator: %v", "--", cmd.Args)
48 assert.Assert(t, slices.Index(cmd.Args, dashPath) > sep, "path %#q must come after %#q: %v", dashPath, "--", cmd.Args)
49}
50
51// TestRsyncCommandPaths verifies that the paths of a recursive copy are passed to rsync in
52// the form that makes it behave like `cp -r` and `scp -r`, regardless of how the trailing
53// slashes were spelled by the caller.
54// https://github.com/lima-vm/lima/issues/4468
55// https://github.com/lima-vm/lima/issues/5500
56func TestRsyncCommandPaths(t *testing.T) {
57 if _, err := newRsyncTool(&Options{}); err != nil {
58 t.Skip("rsync not found:", err)
59 }
60
61 // Only local paths are used, so that no guest is involved.
62 tmpDir := t.TempDir()
63 dir := filepath.Join(tmpDir, "dir")
64 assert.NilError(t, os.Mkdir(dir, 0o755))
65 dir2 := filepath.Join(tmpDir, "dir2")
66 assert.NilError(t, os.Mkdir(dir2, 0o755))
67 file := filepath.Join(tmpDir, "file.txt")
68 assert.NilError(t, os.WriteFile(file, []byte("hello"), 0o644))
69 existing := filepath.Join(tmpDir, "existing")
70 assert.NilError(t, os.Mkdir(existing, 0o755))
71 missing := filepath.Join(tmpDir, "missing")
72
73 tests := []struct {
74 name string
75 opts Options
76 args []string
77 expected []string
78 }{
79 {
80 // The destination becomes a copy of the source directory.
81 name: "dir into missing dst", opts: Options{Recursive: true},
82 args: []string{dir, missing}, expected: []string{dir + "/", missing},
83 },
84 {
85 // The source directory is copied into the destination.
86 name: "dir into existing dst", opts: Options{Recursive: true},
87 args: []string{dir, existing}, expected: []string{dir, existing},
88 },
89 {
90 // A trailing slash on the source is not significant.
91 name: "dir with slash into existing dst", opts: Options{Recursive: true},
92 args: []string{dir + "/", existing}, expected: []string{dir, existing},
93 },
94 {
95 name: "dir with slash into missing dst", opts: Options{Recursive: true},
96 args: []string{dir + "/", missing}, expected: []string{dir + "/", missing},
97 },
98 {
99 // A trailing slash would just make rsync fail on a non-directory.
100 name: "file into missing dst", opts: Options{Recursive: true},
101 args: []string{file, missing}, expected: []string{file, missing},
102 },
103 {
104 // Multiple sources are never merged into the destination.
105 name: "multiple sources", opts: Options{Recursive: true},
106 args: []string{dir + "/", dir2, missing}, expected: []string{dir, dir2, missing},
107 },
108 {
109 // Without -r the paths reach rsync as they are, trailing slash included.
110 // `limactl shell --sync` relies on it to mirror the directories.
111 name: "non-recursive", opts: Options{},
112 args: []string{dir + "/", existing}, expected: []string{dir + "/", existing},
113 },
114 }
115 for _, tt := range tests {
116 t.Run(tt.name, func(t *testing.T) {
117 tool, err := newRsyncTool(&tt.opts)
118 assert.NilError(t, err)
119 cmd, err := tool.Command(t.Context(), tt.args, nil)
120 assert.NilError(t, err)
121
122 // The paths are everything after the "--" separator.
123 sep := slices.Index(cmd.Args, "--")
124 assert.Assert(t, sep != -1, "rsync args must contain the %#q separator: %v", "--", cmd.Args)
125 assert.DeepEqual(t, cmd.Args[sep+1:], tt.expected)
126 })
127 }
128}
129
130// TestRsyncCommandUndeterminedPath verifies that a path that can neither be confirmed nor
131// ruled out to be a directory fails the copy, rather than falling back to copying the
132// contents of the source, which would put the files in the wrong place.
133func TestRsyncCommandUndeterminedPath(t *testing.T) {
134 if runtime.GOOS == "windows" {
135 // The test needs a path that can neither be confirmed nor ruled out to be a
136 // directory. A file used as a directory component fails with ENOTDIR on POSIX
137 // systems, but Windows reports it as a missing path instead, which os.Stat
138 // turns into fs.ErrNotExist. See the comment in $GOROOT/src/os/root_windows.go.
139 t.Skip("ENOTDIR for a file used as a directory is POSIX-specific")
140 }
141 tool, err := newRsyncTool(&Options{Recursive: true})
142 if err != nil {
143 t.Skip("rsync not found:", err)
144 }
145
146 tmpDir := t.TempDir()
147 dir := filepath.Join(tmpDir, "dir")
148 assert.NilError(t, os.Mkdir(dir, 0o755))
149 file := filepath.Join(tmpDir, "file.txt")
150 assert.NilError(t, os.WriteFile(file, []byte("hello"), 0o644))
151
152 // Stat fails with ENOTDIR, rather than reporting the destination as missing.
153 _, err = tool.Command(t.Context(), []string{dir, filepath.Join(file, "dst")}, nil)
154 assert.ErrorContains(t, err, "not a directory")
155}
156
157func TestTrimTrailingSlashes(t *testing.T) {
158 tests := []struct{ path, expected string }{
159 {"/tmp/foo", "/tmp/foo"},
160 {"/tmp/foo/", "/tmp/foo"},
161 {"/tmp/foo//", "/tmp/foo"},
162 {"/", "/"},
163 {"", ""},
164 }
165 for _, tt := range tests {
166 assert.Equal(t, trimTrailingSlashes(tt.path), tt.expected, "path=%q", tt.path)
167 }
168}
169
170func TestQuoteRemotePath(t *testing.T) {
171 tests := []struct{ path, expected string }{
172 {"/tmp/foo", "/tmp/foo"},
173 {"foo bar", "'foo bar'"},
174 {"/tmp/$(touch pwned)", `'/tmp/$(touch pwned)'`},
175 // A leading "~" or "~user" has to stay expandable by the shell of the guest.
176 {"~", "~"},
177 {"~/foo bar", "~/'foo bar'"},
178 {"~foo", "~foo"},
179 {"~foo/bar", "~foo/bar"},
180 {"~foo/bar baz", "~foo/'bar baz'"},
181 {"~$(touch pwned)/foo", `'~$(touch pwned)/foo'`},
182 }
183 for _, tt := range tests {
184 assert.Equal(t, quoteRemotePath(tt.path), tt.expected, "path=%q", tt.path)
185 }
186}
187
188// TestNewAutoSurfacesPathError verifies that the default backend reports a bad
189// path as itself, rather than as a missing copy tool.
190func TestNewAutoSurfacesPathError(t *testing.T) {
191 t.Setenv("LIMA_HOME", t.TempDir())
192 paths := []string{"nonexistent-instance-for-test:/tmp/x", "/tmp/y"}
193
194 _, err := New(t.Context(), string(BackendAuto), paths, &Options{})
195 assert.ErrorContains(t, err, "instance `nonexistent-instance-for-test` does not exist")
196}