kuri-tool-guard/tests/test_bounded_output.py
XLion 06a7f93d18
refactor(kuri-tool-guard): modularise monolithic __init__.py and tests
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
2026-07-31 02:23:02 +08:00

95 lines
3.8 KiB
Python

"""Tests for kuri-tool-guard — bounded-output terminal predicate."""
from __future__ import annotations
import unittest
from tests._support import load_mod, patch_alice_identity, submodule
class TestBoundedOutputPredicate(unittest.TestCase):
"""Test _bounded_output — unbounded output protection."""
def setUp(self):
self.mod = load_mod()
self.pred = submodule(self.mod, "_predicates")
self._restore = patch_alice_identity(self.mod)
def tearDown(self):
self._restore()
def _call(self, command: str) -> bool:
return self.pred._bounded_output(command) is not None
# ── Block ─────────────────────────────────────────────────────
def test_block_find_type_f(self):
self.assertTrue(self._call("find /home/alice -type f"))
def test_block_find_type_f_sort(self):
self.assertTrue(self._call("find /home/alice -type f | sort"))
def test_block_find_type_f_sort_head(self):
self.assertTrue(self._call("find /home/alice -type f | sort | head -200"))
def test_block_find_type_d(self):
"""-type d should not match -type f check."""
self.assertFalse(self._call("find /home/alice -type d"))
def test_block_find_type_f_print_only(self):
"""-print alone without -quit is still unbounded."""
self.assertTrue(self._call("find /home/alice -type f -print"))
def test_block_find_type_f_quit_only(self):
"""-quit alone without -print is still unbounded."""
self.assertTrue(self._call("find /home/alice -type f -quit"))
def test_block_rg_hidden_no_ignore(self):
self.assertTrue(self._call("rg --files --hidden --no-ignore /home/alice"))
def test_block_fd_hidden_no_ignore(self):
self.assertTrue(self._call("fd --hidden --no-ignore /home/alice"))
# ── Wrapper-aware block ───────────────────────────────────────
def test_block_command_wrapper_find(self):
self.assertTrue(self._call("command find /home/alice -type f"))
def test_block_env_wrapper_find(self):
self.assertTrue(self._call("env LC_ALL=C find /home/alice -type f"))
def test_block_nice_wrapper_find(self):
self.assertTrue(self._call("nice -n 10 find /home/alice -type f"))
def test_block_time_wrapper_find(self):
self.assertTrue(self._call("time find /home/alice -type f"))
def test_block_command_wrapper_rg(self):
self.assertTrue(self._call("command rg --files --hidden --no-ignore /home/alice"))
def test_block_env_wrapper_fd(self):
self.assertTrue(self._call("env fd --hidden --no-ignore /home/alice"))
# ── Allow ─────────────────────────────────────────────────────
def test_allow_find_maxdepth(self):
self.assertFalse(self._call("find /home/alice -maxdepth 2 -type f"))
def test_allow_find_print_quit(self):
self.assertFalse(self._call("find /home/alice -type f -print -quit"))
def test_allow_find_head(self):
"""find -type f piped directly to head is bounded (head limits output)."""
self.assertFalse(self._call("find /home/alice -type f | head -200"))
def test_allow_git_ls_files(self):
self.assertFalse(self._call("git -C /home/alice ls-files"))
def test_allow_git_status(self):
self.assertFalse(self._call("git -C /home/alice status --short"))
def test_allow_locale_independent(self):
self.assertFalse(self._call("locate something"))
def test_allow_ls_recursive(self):
self.assertFalse(self._call("ls -R /home/alice"))