Split the 1341-line __init__.py into 5 focused modules: _state.py - home root helpers, mutable poll state _parser.py - shell tokenizer (independent) _predicates.py - terminal predicates + bounded output _guards.py - all 9 guard implementations + AST helpers __init__.py - thin entry point + dispatcher + re-exports Split the 2226-line test_guard.py into 14 dedicated files: 13 test_*.py files + tests/_support.py (shared helpers) Key changes: - Package-relative imports throughout (Hermes loader compatible) - No top-level module pollution (hermes_plugins.kuri_tool_guard.* only) - Typechecked via ty with symlink alias for hyphenated dir name - All 10 guard policies, messages, and block/allow behaviour preserved - 338 tests (336 original + 2 new: isolation + loader contract) - ty, ruff, bandit, vulture all pass in just verify
63 lines
2 KiB
Python
63 lines
2 KiB
Python
"""Tests for kuri-tool-guard — write-file guard."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from tests._support import load_mod
|
|
|
|
|
|
class TestWriteFileGuard(unittest.TestCase):
|
|
"""Test _guard_write_file — blocks write on existing files."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
fd, self._existing = tempfile.mkstemp(suffix=".txt")
|
|
os.close(fd)
|
|
with open(self._existing, "w") as f:
|
|
f.write("existing content\n")
|
|
self._nonexistent = self._existing + ".nonexistent"
|
|
|
|
def tearDown(self):
|
|
os.unlink(self._existing)
|
|
if os.path.exists(self._nonexistent):
|
|
os.unlink(self._nonexistent)
|
|
|
|
def test_blocks_existing_file(self):
|
|
result = self.mod._guards._guard_write_file({"path": self._existing})
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("patch", result["message"])
|
|
|
|
def test_passes_new_file(self):
|
|
result = self.mod._guards._guard_write_file({"path": self._nonexistent})
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_empty_path(self):
|
|
result = self.mod._guards._guard_write_file({"path": ""})
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_none_path(self):
|
|
result = self.mod._guards._guard_write_file({"path": None})
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_no_path_key(self):
|
|
result = self.mod._guards._guard_write_file({"content": "hello"})
|
|
self.assertIsNone(result)
|
|
|
|
def test_blocks_via_dispatcher(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="write_file",
|
|
args={"path": self._existing},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_passes_via_dispatcher_new(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="write_file",
|
|
args={"path": self._nonexistent},
|
|
)
|
|
self.assertIsNone(result)
|