Skip to content

Commit e52fc6e

Browse files
committed
refactor: update hook removal logic in process_settings
- Changed the logic to remove Python and TypeScript hooks based on filenames instead of predefined constants. - Updated tests to reflect the new hook removal behavior and ensure correct functionality with absolute paths. - Enhanced test cases for better clarity and coverage of hook management.
1 parent 43f03e1 commit e52fc6e

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

.claude/hooks/file_checker_ts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def main() -> int:
436436
print("", file=sys.stderr)
437437
print(f"{GREEN}✅ TypeScript: All checks passed{NC}", file=sys.stderr)
438438
debug_log("Exiting with code 0 (success)")
439-
return 0 # Fixed: was 2, should be 0
439+
return 0
440440

441441

442442
if __name__ == "__main__":

installer/steps/claude_files.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ def process_settings(settings_content: str, install_python: bool, install_typesc
4848
"""
4949
config: dict[str, Any] = json.loads(settings_content)
5050

51-
hooks_to_remove = []
51+
# Match by filename, not full path (source file may have absolute paths)
52+
files_to_remove: list[str] = []
5253
if not install_python:
53-
hooks_to_remove.append(PYTHON_CHECKER_HOOK)
54+
files_to_remove.append("file_checker_python.py")
5455
if not install_typescript:
55-
hooks_to_remove.append(TYPESCRIPT_CHECKER_HOOK)
56+
files_to_remove.append("file_checker_ts.py")
5657

57-
if hooks_to_remove:
58+
if files_to_remove:
5859
try:
5960
for hook_group in config["hooks"]["PostToolUse"]:
6061
hook_group["hooks"] = [
61-
h for h in hook_group["hooks"] if h.get("command") not in hooks_to_remove
62+
h for h in hook_group["hooks"]
63+
if not any(f in h.get("command", "") for f in files_to_remove)
6264
]
6365
except (KeyError, TypeError, AttributeError):
6466
pass

tests/unit/installer/steps/test_claude_files.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ class TestProcessSettings:
1313

1414
def test_process_settings_preserves_python_hook_when_enabled(self):
1515
"""process_settings keeps Python hook when install_python=True."""
16-
from installer.steps.claude_files import PYTHON_CHECKER_HOOK, process_settings
16+
from installer.steps.claude_files import process_settings
1717

18+
# Use absolute path like real source file
19+
python_hook = "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_python.py"
1820
settings = {
1921
"hooks": {
2022
"PostToolUse": [
2123
{
2224
"matcher": "Write|Edit|MultiEdit",
2325
"hooks": [
24-
{"type": "command", "command": "python3 .claude/hooks/file_checker_qlty.py"},
25-
{"type": "command", "command": PYTHON_CHECKER_HOOK},
26+
{"type": "command", "command": "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_qlty.py"},
27+
{"type": "command", "command": python_hook},
2628
],
2729
}
2830
]
@@ -34,21 +36,23 @@ def test_process_settings_preserves_python_hook_when_enabled(self):
3436

3537
hooks = parsed["hooks"]["PostToolUse"][0]["hooks"]
3638
commands = [h["command"] for h in hooks]
37-
assert PYTHON_CHECKER_HOOK in commands
39+
assert any("file_checker_python.py" in cmd for cmd in commands)
3840
assert len(hooks) == 2
3941

4042
def test_process_settings_removes_python_hook_when_disabled(self):
4143
"""process_settings removes Python hook when install_python=False."""
42-
from installer.steps.claude_files import PYTHON_CHECKER_HOOK, process_settings
44+
from installer.steps.claude_files import process_settings
4345

46+
# Use absolute path like real source file
47+
python_hook = "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_python.py"
4448
settings = {
4549
"hooks": {
4650
"PostToolUse": [
4751
{
4852
"matcher": "Write|Edit|MultiEdit",
4953
"hooks": [
50-
{"type": "command", "command": "python3 .claude/hooks/file_checker_qlty.py"},
51-
{"type": "command", "command": PYTHON_CHECKER_HOOK},
54+
{"type": "command", "command": "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_qlty.py"},
55+
{"type": "command", "command": python_hook},
5256
],
5357
}
5458
]
@@ -60,7 +64,7 @@ def test_process_settings_removes_python_hook_when_disabled(self):
6064

6165
hooks = parsed["hooks"]["PostToolUse"][0]["hooks"]
6266
commands = [h["command"] for h in hooks]
63-
assert PYTHON_CHECKER_HOOK not in commands
67+
assert not any("file_checker_python.py" in cmd for cmd in commands)
6468
assert any("file_checker_qlty.py" in cmd for cmd in commands)
6569
assert len(hooks) == 1
6670

@@ -78,8 +82,10 @@ def test_process_settings_handles_missing_hooks(self):
7882

7983
def test_process_settings_preserves_other_settings(self):
8084
"""process_settings preserves all other settings unchanged."""
81-
from installer.steps.claude_files import PYTHON_CHECKER_HOOK, process_settings
85+
from installer.steps.claude_files import process_settings
8286

87+
# Use absolute path like real source file
88+
python_hook = "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_python.py"
8389
settings = {
8490
"model": "opus",
8591
"env": {"DISABLE_TELEMETRY": "true"},
@@ -88,7 +94,7 @@ def test_process_settings_preserves_other_settings(self):
8894
"PostToolUse": [
8995
{
9096
"matcher": "Write|Edit|MultiEdit",
91-
"hooks": [{"type": "command", "command": PYTHON_CHECKER_HOOK}],
97+
"hooks": [{"type": "command", "command": python_hook}],
9298
}
9399
]
94100
},
@@ -124,16 +130,18 @@ def test_process_settings_handles_malformed_structure(self):
124130

125131
def test_process_settings_removes_typescript_hook_when_disabled(self):
126132
"""process_settings removes TypeScript hook when install_typescript=False."""
127-
from installer.steps.claude_files import TYPESCRIPT_CHECKER_HOOK, process_settings
133+
from installer.steps.claude_files import process_settings
128134

135+
# Use absolute path like real source file
136+
ts_hook = "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_ts.py"
129137
settings = {
130138
"hooks": {
131139
"PostToolUse": [
132140
{
133141
"matcher": "Write|Edit|MultiEdit",
134142
"hooks": [
135-
{"type": "command", "command": "python3 .claude/hooks/file_checker_qlty.py"},
136-
{"type": "command", "command": TYPESCRIPT_CHECKER_HOOK},
143+
{"type": "command", "command": "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_qlty.py"},
144+
{"type": "command", "command": ts_hook},
137145
],
138146
}
139147
]
@@ -145,23 +153,26 @@ def test_process_settings_removes_typescript_hook_when_disabled(self):
145153

146154
hooks = parsed["hooks"]["PostToolUse"][0]["hooks"]
147155
commands = [h["command"] for h in hooks]
148-
assert TYPESCRIPT_CHECKER_HOOK not in commands
156+
assert not any("file_checker_ts.py" in cmd for cmd in commands)
149157
assert any("file_checker_qlty.py" in cmd for cmd in commands)
150158
assert len(hooks) == 1
151159

152160
def test_process_settings_removes_both_hooks_when_both_disabled(self):
153161
"""process_settings removes both Python and TypeScript hooks when both disabled."""
154-
from installer.steps.claude_files import PYTHON_CHECKER_HOOK, TYPESCRIPT_CHECKER_HOOK, process_settings
162+
from installer.steps.claude_files import process_settings
155163

164+
# Use absolute paths like real source file
165+
python_hook = "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_python.py"
166+
ts_hook = "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_ts.py"
156167
settings = {
157168
"hooks": {
158169
"PostToolUse": [
159170
{
160171
"matcher": "Write|Edit|MultiEdit",
161172
"hooks": [
162-
{"type": "command", "command": "python3 .claude/hooks/file_checker_qlty.py"},
163-
{"type": "command", "command": PYTHON_CHECKER_HOOK},
164-
{"type": "command", "command": TYPESCRIPT_CHECKER_HOOK},
173+
{"type": "command", "command": "python3 /workspaces/claude-codepro/.claude/hooks/file_checker_qlty.py"},
174+
{"type": "command", "command": python_hook},
175+
{"type": "command", "command": ts_hook},
165176
],
166177
}
167178
]
@@ -173,8 +184,8 @@ def test_process_settings_removes_both_hooks_when_both_disabled(self):
173184

174185
hooks = parsed["hooks"]["PostToolUse"][0]["hooks"]
175186
commands = [h["command"] for h in hooks]
176-
assert PYTHON_CHECKER_HOOK not in commands
177-
assert TYPESCRIPT_CHECKER_HOOK not in commands
187+
assert not any("file_checker_python.py" in cmd for cmd in commands)
188+
assert not any("file_checker_ts.py" in cmd for cmd in commands)
178189
assert any("file_checker_qlty.py" in cmd for cmd in commands)
179190
assert len(hooks) == 1
180191

0 commit comments

Comments
 (0)