-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall_test.py
More file actions
179 lines (143 loc) · 7.55 KB
/
Copy pathinstall_test.py
File metadata and controls
179 lines (143 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
from __future__ import annotations
import json
import os
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path
from unittest import mock
import install
class InstallHelpersTest(unittest.TestCase):
def test_parse_args_defaults_scope_to_global(self) -> None:
with mock.patch("sys.argv", ["install.py", "install", "--target", "opencode"]):
args = install.parse_args()
self.assertEqual(args.scope, "global")
def test_shared_runtime_root_uses_absolute_xdg_data_home(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
data_home = Path(temp_dir) / "xdg-data"
with mock.patch.dict(os.environ, {"XDG_DATA_HOME": str(data_home)}, clear=False):
self.assertEqual(install.shared_runtime_root(), data_home / "claude-bash-approve")
def test_shared_runtime_root_ignores_relative_xdg_data_home(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
home = Path(temp_dir) / "home"
home.mkdir()
with mock.patch.dict(os.environ, {"HOME": str(home), "XDG_DATA_HOME": "relative"}, clear=False):
self.assertEqual(install.shared_runtime_root(), home / ".local" / "share" / "claude-bash-approve")
def test_ensure_codex_feature_enabled_migrates_deprecated_key(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
config_path = Path(temp_dir) / "config.toml"
config_path.write_text("[features]\ncodex_hooks = true\nmulti_agent = true\n")
install.ensure_codex_feature_enabled(config_path)
text = config_path.read_text()
self.assertIn("hooks = true", text)
self.assertIn("multi_agent = true", text)
self.assertNotIn("codex_hooks", text)
class InstallCLITestCase(unittest.TestCase):
maxDiff = None
def setUp(self) -> None:
self.temp_dir = tempfile.TemporaryDirectory()
self.addCleanup(self.temp_dir.cleanup)
self.temp_path = Path(self.temp_dir.name)
self.project_root = self.temp_path / "project"
self.home = self.temp_path / "home"
self.project_root.mkdir()
self.home.mkdir()
self.env = os.environ.copy()
self.env.update(
{
"HOME": str(self.home),
"XDG_DATA_HOME": str(self.temp_path / "xdg-data"),
"GOMODCACHE": str(self.temp_path / "gomodcache"),
"GOCACHE": str(self.temp_path / "gocache"),
}
)
self.install_py = install.repo_root() / "install.py"
def run_install(self, *args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
["python3", str(self.install_py), *args, "--project-root", str(self.project_root)],
env=self.env,
text=True,
capture_output=True,
check=True,
)
@property
def runtime_root(self) -> Path:
return self.temp_path / "xdg-data" / "claude-bash-approve"
class ClaudeInstallTest(InstallCLITestCase):
def test_install_and_uninstall_claude(self) -> None:
self.run_install("install", "--target", "claude")
settings_path = self.home / ".claude" / "settings.json"
self.assertTrue((self.runtime_root / "approve-bash").is_file())
self.assertTrue((self.runtime_root / "run-hook.sh").is_file())
runtime_metadata = json.loads((self.runtime_root / "plugin.json").read_text())
source_metadata = json.loads((install.repo_root() / ".claude-plugin" / "plugin.json").read_text())
self.assertEqual(runtime_metadata["version"], source_metadata["version"])
self.assertFalse((self.home / ".claude" / "hooks" / "bash-approve").exists())
settings = json.loads(settings_path.read_text())
pre_tool = settings["hooks"]["PreToolUse"]
self.assertEqual(len(pre_tool), 2)
self.assertEqual({entry["matcher"] for entry in pre_tool}, {"Bash", "Read|Grep"})
for entry in pre_tool:
self.assertEqual(entry["hooks"][0]["command"], str(self.runtime_root / "run-hook.sh"))
self.run_install("uninstall", "--target", "claude")
updated = json.loads(settings_path.read_text())
self.assertFalse(updated.get("hooks"))
class OpenCodeInstallTest(InstallCLITestCase):
def test_install_and_uninstall_opencode(self) -> None:
(self.project_root / "opencode.json").write_text(
json.dumps(
{
"permission": {
"bash": {
"*": "deny",
"ls *": "ask",
}
}
}
)
)
self.run_install("install", "--target", "opencode", "--scope", "both")
project_plugin = self.project_root / ".opencode" / "plugins" / "bash-approve.ts"
global_plugin = self.home / ".config" / "opencode" / "plugins" / "bash-approve.ts"
global_config = self.home / ".config" / "opencode" / "opencode.json"
self.assertTrue((self.runtime_root / "approve-bash").is_file())
self.assertTrue(project_plugin.is_file())
self.assertTrue(global_plugin.is_file())
self.assertFalse((self.home / ".config" / "opencode" / "bash-approve").exists())
self.assertIn(str(self.runtime_root / "run-opencode-hook.sh"), project_plugin.read_text())
self.assertIn(str(self.runtime_root / "run-opencode-hook.sh"), global_plugin.read_text())
project_config = json.loads((self.project_root / "opencode.json").read_text())
self.assertEqual(project_config["permission"]["bash"]["*"], "ask")
self.assertEqual(json.loads(global_config.read_text())["permission"]["bash"]["*"], "ask")
self.run_install("uninstall", "--target", "opencode", "--scope", "both")
self.assertFalse(project_plugin.exists())
self.assertFalse(global_plugin.exists())
class CodexInstallTest(InstallCLITestCase):
def test_install_and_uninstall_codex(self) -> None:
codex_root = self.home / ".codex"
codex_root.mkdir()
(codex_root / "config.toml").write_text("[features]\nmulti_agent = true\n")
self.run_install("install", "--target", "codex", "--scope", "both")
project_config = self.project_root / ".codex" / "config.toml"
project_hooks = self.project_root / ".codex" / "hooks.json"
global_config = codex_root / "config.toml"
global_hooks = codex_root / "hooks.json"
self.assertTrue((self.runtime_root / "approve-bash").is_file())
self.assertIn("hooks = true", project_config.read_text())
self.assertIn("multi_agent = true", global_config.read_text())
self.assertIn("hooks = true", global_config.read_text())
self.assertIn(str(self.runtime_root / "run-codex-hook.sh"), project_hooks.read_text())
self.assertIn(str(self.runtime_root / "run-codex-hook.sh"), global_hooks.read_text())
self.run_install("uninstall", "--target", "codex", "--scope", "both")
self.assertNotIn(str(self.runtime_root / "run-codex-hook.sh"), project_hooks.read_text())
self.assertNotIn(str(self.runtime_root / "run-codex-hook.sh"), global_hooks.read_text())
class AllUninstallTest(InstallCLITestCase):
def test_uninstall_all_removes_shared_runtime(self) -> None:
self.run_install("install", "--target", "all")
self.assertTrue(self.runtime_root.exists())
self.run_install("uninstall", "--target", "all")
self.assertFalse(self.runtime_root.exists())
if __name__ == "__main__":
unittest.main()