跳到主要内容

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

浏览文件

install_test.go

100 行 · 3.7 KB历史下载
1package cli
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "github.com/sylvinus/agent-vm/internal/paths"
10 "github.com/sylvinus/agent-vm/internal/vm/vmtest"
11)
12
13func TestInstall(t *testing.T) {
14 home := t.TempDir()
15 t.Setenv("HOME", home)
16 // Go reads the home from USERPROFILE on Windows, not HOME.
17 t.Setenv("USERPROFILE", home)
18 bin := filepath.Join(t.TempDir(), "bin")
19 t.Setenv("AGENT_VM_BIN_DIR", bin)
20 te := newTestEnv(t, vmtest.New())
21 exe, _ := self()
22 link, err := binLink()
23 if err != nil {
24 t.Fatal(err)
25 }
26
27 if te.run("install") != 0 || !strings.Contains(te.out(), "Linked "+link+" -> "+exe) || !strings.Contains(te.out(), "is not on your PATH") {
28 t.Fatalf("install: %s", te.out())
29 }
30 if !strings.Contains(te.out(), "agent-vm setup # build the base VM, once") {
31 t.Errorf("next step: %s", te.out())
32 }
33 if t2, _ := os.Readlink(link); t2 != exe {
34 t.Errorf("link: %q", t2)
35 }
36 if te.run("install") != 0 || !strings.Contains(te.out(), "already linked") {
37 t.Errorf("again: %s", te.out())
38 }
39 // On Windows: the hint is PowerShell's, and PATH is compared as the
40 // file system does.
41 old := paths.Windows
42 paths.Windows = func() bool { return true }
43 if te.run("install"); !strings.Contains(te.out(), "Add it from PowerShell") || !strings.Contains(te.out(), "SetEnvironmentVariable('Path', '"+bin+";'") {
44 t.Errorf("Windows hint: %s", te.out())
45 }
46 t.Setenv("PATH", strings.ToUpper(bin)+"/"+string(os.PathListSeparator)+os.Getenv("PATH"))
47 if te.run("install"); strings.Contains(te.out(), "not on your PATH") {
48 t.Errorf("on PATH, as Windows compares it: %s", te.out())
49 }
50 paths.Windows = old
51 if te.run("install", "x") != 2 {
52 t.Error("an argument")
53 }
54 os.WriteFile(filepath.Join(home, ".zshrc"), []byte("source ~/agent-vm/agent-vm.sh\n"), 0o644)
55 if te.run("uninstall") != 0 || !strings.Contains(te.out(), "Removed "+link) || !strings.Contains(te.out(), "still sources agent-vm.sh") {
56 t.Errorf("uninstall: %s", te.out())
57 }
58 if _, err := os.Lstat(link); err == nil {
59 t.Error("not removed")
60 }
61 // Someone else's file: never replaced, never removed.
62 os.WriteFile(link, []byte("#!/bin/sh\n"), 0o755)
63 if te.run("install") != 1 || !strings.Contains(te.out(), "already exists and is not a link to") {
64 t.Errorf("foreign file, install: %s", te.out())
65 }
66 if te.run("uninstall") != 0 || !strings.Contains(te.out(), "is not a link to this agent-vm: left alone.") {
67 t.Errorf("foreign file, uninstall: %s", te.out())
68 }
69 if b, _ := os.ReadFile(link); string(b) != "#!/bin/sh\n" {
70 t.Error("the foreign file changed")
71 }
72 // A dangling link of someone else's: refused, left.
73 os.Remove(link)
74 os.Symlink(filepath.Join(home, "gone"), link)
75 if te.run("install") != 1 || !strings.Contains(te.out(), "already exists and is not a link to") {
76 t.Errorf("dangling link: %s", te.out())
77 }
78 // 0.2's link to its agent-vm.sh: replaced.
79 os.Remove(link)
80 os.Symlink(filepath.Join(home, "agent-vm", "agent-vm.sh"), link)
81 if te.run("install") != 0 || !strings.Contains(te.out(), "Replacing "+link) {
82 t.Errorf("0.2's link: %s", te.out())
83 }
84 if t2, _ := os.Readlink(link); t2 != exe {
85 t.Errorf("link after 0.2's: %q", t2)
86 }
87 // A copy an earlier install recorded, since updated: replaced, and
88 // removed by uninstall.
89 os.Remove(link)
90 os.WriteFile(link, []byte("old agent-vm"), 0o755)
91 os.MkdirAll(string(te.state), 0o755)
92 os.WriteFile(te.state.Path(installedCopy), []byte(sha256Of(link)+"\n"), 0o644)
93 if te.run("uninstall") != 0 || !strings.Contains(te.out(), "Removed "+link) {
94 t.Errorf("recorded copy, uninstall: %s", te.out())
95 }
96 os.WriteFile(link, []byte("old agent-vm"), 0o755)
97 if te.run("install") != 0 || !strings.Contains(te.out(), "Replacing "+link) {
98 t.Errorf("recorded copy, install: %s", te.out())
99 }
100}