跳到主要内容

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

浏览文件

envcmd_test.go

155 行 · 6.7 KB历史下载
1package cli
2
3import (
4 "context"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "github.com/sylvinus/agent-vm/internal/bashref"
12 "github.com/sylvinus/agent-vm/internal/state"
13)
14
15// Each sequence of env and project-env commands gives the same output,
16// status and files as 0.2, run each in its own project and state folder.
17func TestEnvBash(t *testing.T) {
18 type step struct {
19 args []string
20 stdin string
21 }
22 seqs := [][]step{
23 {{[]string{"env", "list"}, ""}, {[]string{"env", "get", "A"}, ""}, {[]string{"env", "has", "A"}, ""}},
24 {{[]string{"env", "set", "A", "O'Brien"}, ""}, {[]string{"env", "get", "A"}, ""}, {[]string{"env"}, ""}},
25 {{[]string{"env", "set", "A"}, "piped\n"}, {[]string{"env", "set", "B"}, "multi\nline\n"}, {[]string{"env", "get", "B"}, ""},
26 {[]string{"env", "set", "A", "again"}, ""}, {[]string{"env", "list"}, ""}, {[]string{"env", "unset", "A"}, ""}, {[]string{"env", "list"}, ""}},
27 {{[]string{"env", "set", "A"}, ""}, {[]string{"env", "set", "1A", "x"}, ""}, {[]string{"env", "set"}, ""}, {[]string{"env", "set", "A", "b", "c"}, ""},
28 {[]string{"env", "frob"}, ""}, {[]string{"env", "get", "A", "B"}, ""}, {[]string{"env", "list", "x"}, ""}, {[]string{"env", "unset", "NOPE"}, ""}},
29 {{[]string{"project-env", "set", "K", "v"}, ""}, {[]string{"project-env", "get", "K"}, ""}, {[]string{"project-env", "list"}, ""}},
30 }
31 for i, seq := range seqs {
32 // Resolved: the bash recordings read back under one spelling
33 // (/var is a link to /private/var on macOS).
34 gproj, _ := filepath.EvalSymlinks(t.TempDir())
35 bproj, _ := filepath.EvalSymlinks(t.TempDir())
36 gst, _ := filepath.EvalSymlinks(t.TempDir())
37 bst, _ := filepath.EvalSymlinks(t.TempDir())
38 bfiles := []string{filepath.Join(bst, "env"), filepath.Join(bproj, ".agent-vm.env")}
39 var want bashref.Result
40 for _, s := range seq {
41 te := newTestEnv(t, nil)
42 te.state = state.Dir(gst)
43 te.io.Stdin = strings.NewReader(s.stdin)
44 t.Chdir(gproj)
45 t.Setenv("PWD", gproj)
46 code := te.main(context.Background(), s.args)
47 got := bashref.Result{Stdout: te.stdout.String(), Stderr: te.stderr.String(), Code: code}
48
49 want = bashStdin(t, bproj, []string{"AGENT_VM_STATE_DIR=" + bst}, s.stdin, bfiles, s.args...)
50 got.Stderr = strings.ReplaceAll(got.Stderr, gst, "STATE")
51 want.Stderr = strings.ReplaceAll(want.Stderr, bst, "STATE")
52 got.Stderr = strings.ReplaceAll(got.Stderr, gproj, "PROJ")
53 want.Stderr = strings.ReplaceAll(want.Stderr, bproj, "PROJ")
54 if got.Stdout != want.Stdout || got.Stderr != want.Stderr || got.Code != want.Code {
55 t.Errorf("seq %d, %q:\n go: %+v\n bash: %+v", i, s.args, got, want)
56 }
57 }
58 for j, f := range []string{filepath.Join(gst, "env"), filepath.Join(gproj, ".agent-vm.env")} {
59 gb, gerr := os.ReadFile(f)
60 bb, bok := want.Files[bfiles[j]]
61 if string(gb) != bb || (gerr == nil) != bok {
62 t.Errorf("seq %d: %s: go %q, bash %q", i, filepath.Base(f), gb, bb)
63 }
64 if gerr == nil {
65 if fi, _ := os.Stat(f); fi.Mode().Perm() != 0o600 {
66 t.Errorf("seq %d: %s mode %v", i, f, fi.Mode())
67 }
68 }
69 }
70 }
71}
72
73func bashStdin(t *testing.T, dir string, envs []string, stdin string, files []string, args ...string) bashref.Result {
74 t.Helper()
75 return bashref.Exec(t, bashref.Cmd{Dir: dir, Env: append([]string{"PWD=" + dir}, envs...), Stdin: stdin,
76 Snippet: `agent-vm "$@"`, Args: args, Files: files})
77}
78
79// After a set, the same warnings as 0.2 when git would commit the file.
80func TestProjectEnvGitBash(t *testing.T) {
81 if _, err := exec.LookPath("git"); err != nil {
82 t.Skip("no git")
83 }
84 t.Setenv("GIT_CONFIG_GLOBAL", "/dev/null")
85 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
86 for _, tracked := range []bool{false, true} {
87 // Resolved, as above: git prints --show-toplevel resolved too.
88 gproj, _ := filepath.EvalSymlinks(t.TempDir())
89 bproj, _ := filepath.EvalSymlinks(t.TempDir())
90 for _, p := range []string{gproj, bproj} {
91 sub := filepath.Join(p, "sub")
92 os.MkdirAll(sub, 0o755)
93 exec.Command("git", "-C", p, "init", "-q").Run()
94 if tracked {
95 os.WriteFile(filepath.Join(sub, ".agent-vm.env"), []byte("X=1\n"), 0o600)
96 exec.Command("git", "-C", p, "add", "sub/.agent-vm.env").Run()
97 }
98 }
99 te := newTestEnv(t, nil)
100 t.Chdir(filepath.Join(gproj, "sub"))
101 t.Setenv("PWD", filepath.Join(gproj, "sub"))
102 code := te.main(context.Background(), []string{"project-env", "set", "K", "v"})
103 want := bashStdin(t, filepath.Join(bproj, "sub"), []string{"GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_NOSYSTEM=1"}, "", nil, "project-env", "set", "K", "v")
104 got := strings.ReplaceAll(te.stderr.String(), gproj, "PROJ")
105 if code != want.Code || got != strings.ReplaceAll(want.Stderr, bproj, "PROJ") || got == "" {
106 t.Errorf("tracked %v:\n go: %d %q\n bash: %d %q", tracked, code, got, want.Code, want.Stderr)
107 }
108 }
109}
110
111// A repository the VM planted in the project (a bare one made a work tree,
112// its config naming a command): the warning after a set runs nothing of it,
113// whatever the user's safe.bareRepository.
114func TestProjectEnvPlantedRepo(t *testing.T) {
115 if _, err := exec.LookPath("git"); err != nil {
116 t.Skip("no git")
117 }
118 t.Setenv("GIT_CONFIG_GLOBAL", "/dev/null")
119 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
120 proj, marker := t.TempDir(), filepath.Join(t.TempDir(), "ran")
121 cmd := filepath.Join(t.TempDir(), "fsm.sh")
122 os.WriteFile(cmd, []byte("#!/bin/sh\necho ran >> "+marker+"\n"), 0o755)
123 exec.Command("git", "init", "-q", "--bare", proj).Run()
124 for _, kv := range [][2]string{{"core.bare", "false"}, {"core.worktree", proj}, {"core.fsmonitor", cmd}, {"core.pager", cmd}} {
125 exec.Command("git", "-C", proj, "config", kv[0], kv[1]).Run()
126 }
127 te := newTestEnv(t, nil)
128 t.Chdir(proj)
129 t.Setenv("PWD", proj)
130 if code := te.main(context.Background(), []string{"project-env", "set", "K", "v"}); code != 0 {
131 t.Fatalf("set: %d %s", code, te.out())
132 }
133 if _, err := os.Stat(marker); err == nil {
134 t.Error("ran the planted repository's command")
135 }
136}
137
138// A project env file that is a symlink is never followed.
139func TestProjectEnvSymlink(t *testing.T) {
140 proj := t.TempDir()
141 secret := filepath.Join(t.TempDir(), "secret")
142 os.WriteFile(secret, []byte("TOPSECRET=1\n"), 0o600)
143 os.Symlink(secret, filepath.Join(proj, ".agent-vm.env"))
144 t.Chdir(proj)
145 t.Setenv("PWD", proj)
146 for _, args := range [][]string{{"project-env", "get", "TOPSECRET"}, {"project-env", "list"}, {"project-env", "set", "K", "v"}, {"project-env", "unset", "TOPSECRET"}} {
147 te := newTestEnv(t, nil)
148 if code := te.main(context.Background(), args); code != 2 || !strings.Contains(te.stderr.String(), "is a symlink") || strings.Contains(te.stdout.String(), "TOPSECRET") {
149 t.Errorf("%q: %d %s %s", args, code, te.stdout.String(), te.stderr.String())
150 }
151 }
152 if b, _ := os.ReadFile(secret); string(b) != "TOPSECRET=1\n" {
153 t.Error("the target changed")
154 }
155}