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
564 lines
23 KiB
Python
564 lines
23 KiB
Python
"""Tests for kuri-tool-guard — terminal guard predicates."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from tests._support import load_mod, submodule
|
|
|
|
|
|
class TestTerminalGuardPredicates(unittest.TestCase):
|
|
"""Test each individual predicate function."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
self.pred = submodule(self.mod, "_predicates")
|
|
|
|
# ── _cat_direct_read ────────────────────────────────────────────
|
|
|
|
# ── Allow (return None) ──
|
|
|
|
def test_cat_direct_read_pipe_head(self):
|
|
"""cat file | head — pipeline, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file | head"))
|
|
|
|
def test_cat_direct_read_pipe_tail(self):
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file | tail"))
|
|
|
|
def test_cat_direct_read_pipe_grep(self):
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file | grep pattern"))
|
|
|
|
def test_cat_direct_read_pipe_wc(self):
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file | wc -l"))
|
|
|
|
def test_cat_direct_read_pipe_jq(self):
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file | jq ."))
|
|
|
|
def test_cat_direct_read_not_cat_cmd(self):
|
|
"""printf 'cat file\\n' — cat in argument, not command."""
|
|
self.assertIsNone(self.pred._cat_direct_read("printf 'cat file\\n'"))
|
|
|
|
def test_cat_direct_read_echo_cat_arg(self):
|
|
"""echo \"cat file\" — cat in argument, not command."""
|
|
self.assertIsNone(self.pred._cat_direct_read('echo "cat file"'))
|
|
|
|
def test_cat_direct_read_grep_cat(self):
|
|
"""grep -F cat README.md — cat in argument, not command."""
|
|
self.assertIsNone(self.pred._cat_direct_read("grep -F cat README.md"))
|
|
|
|
def test_cat_direct_read_git_commit(self):
|
|
"""git commit -m \"replace cat usage\" — cat in string."""
|
|
self.assertIsNone(self.pred._cat_direct_read('git commit -m "replace cat usage"'))
|
|
|
|
def test_cat_direct_read_quoted_chain_ops(self):
|
|
"""printf with semicolon in quotes should not split chain."""
|
|
self.assertIsNone(self.pred._cat_direct_read("printf '%s\\n' 'a;b'"))
|
|
|
|
def test_cat_direct_read_quoted_pipe(self):
|
|
"""grep with pipe in quotes should not split pipeline."""
|
|
self.assertIsNone(self.pred._cat_direct_read("grep 'x|y' file"))
|
|
|
|
def test_cat_direct_read_here_string(self):
|
|
"""<<< here-string should not register as file write."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat <<< 'hello' | head"))
|
|
|
|
def test_cat_direct_read_proc_sub(self):
|
|
""">(...) process sub should not be treated as file redirect."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat >(some_command) | head"))
|
|
|
|
# ── Redirect-form process substitution >(cmd) ──
|
|
|
|
def test_cat_direct_read_proc_sub_redirect(self):
|
|
"""cat file > >(head) — redirect to process sub, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file > >(head)"))
|
|
|
|
def test_cat_direct_read_proc_sub_redirect_grep(self):
|
|
"""cat file > >(grep pattern) — redirect to process sub, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file > >(grep pattern)"))
|
|
|
|
def test_cat_direct_read_proc_sub_redirect_chain(self):
|
|
"""cat file > >(head) | tail — redirect to process sub + pipe."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file > >(head) | tail"))
|
|
|
|
def test_cat_direct_read_devnull(self):
|
|
"""cat file >/dev/null — stdout discarded, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file >/dev/null"))
|
|
|
|
def test_cat_direct_read_devnull_explicit_stdout(self):
|
|
"""cat file 1>/dev/null — explicit stdout discard, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file 1>/dev/null"))
|
|
|
|
def test_cat_direct_read_devnull_amp(self):
|
|
"""cat file &>/dev/null — combined discard, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file &>/dev/null"))
|
|
|
|
def test_cat_direct_read_pipe_devnull(self):
|
|
"""cat file | head >/dev/null — pipeline to discard, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file | head >/dev/null"))
|
|
|
|
# ── Quoted devnull targets ──
|
|
|
|
def test_cat_direct_read_quoted_devnull_double(self):
|
|
"""cat file > \"/dev/null\" — quoted discard target."""
|
|
self.assertIsNone(self.pred._cat_direct_read('cat file > "/dev/null"'))
|
|
|
|
def test_cat_direct_read_quoted_devnull_single(self):
|
|
"""cat file > '/dev/null' — single-quoted discard target."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file > '/dev/null'"))
|
|
|
|
# ── Redirect ordering — allow ──
|
|
|
|
def test_cat_direct_read_fd_then_devnull(self):
|
|
"""cat file >&2 >/dev/null — last redirect is devnull, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file >&2 >/dev/null"))
|
|
|
|
def test_cat_direct_read_fd_then_devnull_explicit(self):
|
|
"""cat file 1>&2 > /dev/null — last redirect is devnull, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("cat file 1>&2 > /dev/null"))
|
|
|
|
# ── command -v (introspection, not execution) ──
|
|
|
|
def test_cat_direct_read_command_v(self):
|
|
"""command -v cat — query path, not execution."""
|
|
self.assertIsNone(self.pred._cat_direct_read("command -v cat"))
|
|
|
|
def test_cat_direct_read_command_v_capital(self):
|
|
"""command -V cat — query description, not execution."""
|
|
self.assertIsNone(self.pred._cat_direct_read("command -V cat"))
|
|
|
|
def test_cat_direct_read_command_p_v(self):
|
|
"""command -p -v cat — introspection with -p flag."""
|
|
self.assertIsNone(self.pred._cat_direct_read("command -p -v cat"))
|
|
|
|
def test_cat_direct_read_no_cat(self):
|
|
"""ls — unrelated command, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("ls -la"))
|
|
|
|
# ── Block terminal (return 'cat_terminal') ──
|
|
|
|
def test_cat_direct_read_basic(self):
|
|
"""cat file — direct terminal output."""
|
|
self.assertEqual(self.pred._cat_direct_read("cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_command_wrapper(self):
|
|
"""command cat file — wrapper, still terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("command cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_proc_version(self):
|
|
"""cat /proc/version — direct terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("cat /proc/version"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_path_cat(self):
|
|
"""/bin/cat file — path command, still terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("/bin/cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_env_var(self):
|
|
"""LC_ALL=C cat file — env var, terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("LC_ALL=C cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_env_wrapper(self):
|
|
"""env FOO=1 cat file — wrapper, terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("env FOO=1 cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_time_wrapper(self):
|
|
"""time cat file — wrapper, terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("time cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_stderr_only(self):
|
|
"""cat file 2>/dev/null — stdout still terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("cat file 2>/dev/null"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_fd_redirect(self):
|
|
"""cat file >&2 — stdout to stderr, still terminal output."""
|
|
self.assertEqual(self.pred._cat_direct_read("cat file >&2"), "cat_terminal")
|
|
|
|
# ── Redirect ordering ──
|
|
|
|
def test_cat_direct_read_devnull_then_fd(self):
|
|
"""cat file >/dev/null >&2 — last redirect wins: fd → terminal."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file >/dev/null >&2"),
|
|
"cat_terminal",
|
|
)
|
|
|
|
def test_cat_direct_read_devnull_then_fd_explicit(self):
|
|
"""cat file > /dev/null 1>&2 — last redirect is to fd."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file > /dev/null 1>&2"),
|
|
"cat_terminal",
|
|
)
|
|
|
|
def test_cat_direct_read_env_var_pipe_before_cat(self):
|
|
"""echo x | cat file — pipe before cat, cat is last, terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("echo x | cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_double_cat_first(self):
|
|
"""cat a | cat b — second cat outputs to terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("cat a | cat b"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_triple_cat_last(self):
|
|
"""cat a | grep x | cat — last cat outputs to terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("cat a | grep x | cat"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_chain_first(self):
|
|
"""cat first; echo hello | grep h — first cat terminal."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat first; echo hello | grep h"),
|
|
"cat_terminal",
|
|
)
|
|
|
|
# ── Wrapper with option arguments ──
|
|
|
|
def test_cat_direct_read_nice_n10(self):
|
|
"""nice -n 10 cat file — terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("nice -n 10 cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_env_u(self):
|
|
"""env -u PATH cat file — terminal."""
|
|
self.assertEqual(self.pred._cat_direct_read("env -u PATH cat file"), "cat_terminal")
|
|
|
|
# ── time/env -v (option, not argument-consuming) ──
|
|
|
|
def test_cat_direct_read_time_v(self):
|
|
"""time -v cat file — -v is verbose flag, still executes cat."""
|
|
self.assertEqual(self.pred._cat_direct_read("time -v cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_env_v(self):
|
|
"""env -v cat file — -v is debug flag, still executes cat."""
|
|
self.assertEqual(self.pred._cat_direct_read("env -v cat file"), "cat_terminal")
|
|
|
|
def test_cat_direct_read_time_v_pipe(self):
|
|
"""time -v cat file | head — pipeline, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("time -v cat file | head"))
|
|
|
|
def test_cat_direct_read_env_v_pipe(self):
|
|
"""env -v cat file | head — pipeline, allow."""
|
|
self.assertIsNone(self.pred._cat_direct_read("env -v cat file | head"))
|
|
|
|
# ── Options with embedded value (--option=value) ──
|
|
|
|
def test_cat_direct_read_nice_adjustment_eq(self):
|
|
"""nice --adjustment=10 cat file — embedded value, terminal."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("nice --adjustment=10 cat file"),
|
|
"cat_terminal",
|
|
)
|
|
|
|
def test_cat_direct_read_env_unset_eq(self):
|
|
"""env --unset=PATH cat file — embedded value, terminal."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("env --unset=PATH cat file"),
|
|
"cat_terminal",
|
|
)
|
|
|
|
def test_cat_direct_read_time_format_eq(self):
|
|
"""time --format=%E cat file — embedded value, terminal."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("time --format=%E cat file"),
|
|
"cat_terminal",
|
|
)
|
|
|
|
def test_cat_direct_read_nice_adjustment_eq_pipe(self):
|
|
"""nice --adjustment=10 cat file | head — pipeline, allow."""
|
|
self.assertIsNone(
|
|
self.pred._cat_direct_read("nice --adjustment=10 cat file | head"),
|
|
)
|
|
|
|
def test_cat_direct_read_env_unset_eq_pipe(self):
|
|
"""env --unset=PATH cat file | head — pipeline, allow."""
|
|
self.assertIsNone(
|
|
self.pred._cat_direct_read("env --unset=PATH cat file | head"),
|
|
)
|
|
|
|
# ── Block file write (return 'cat_file_write') ──
|
|
|
|
def test_cat_direct_read_redirect_file(self):
|
|
"""cat file > output — stdout file write."""
|
|
self.assertEqual(self.pred._cat_direct_read("cat file > output"), "cat_file_write")
|
|
|
|
def test_cat_direct_read_redirect_append(self):
|
|
"""cat file >> output — append file write."""
|
|
self.assertEqual(self.pred._cat_direct_read("cat file >> output"), "cat_file_write")
|
|
|
|
def test_cat_direct_read_pipe_redirect(self):
|
|
"""cat file | grep x > output — pipeline writes to file."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file | grep x > output"),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_pipe_stderr_file(self):
|
|
"""cat file | grep x 2> errors.log — stderr file in pipeline."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file | grep x 2> errors.log"),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_pipe_amp_redirect(self):
|
|
"""cat file | grep x &> output — combined file write."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file | grep x &> output"),
|
|
"cat_file_write",
|
|
)
|
|
|
|
# ── Mixed stderr/file writes ──
|
|
|
|
def test_cat_direct_read_devnull_with_stderr(self):
|
|
"""cat file >/dev/null 2>errors.log — stderr writes file."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file >/dev/null 2>errors.log"),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_stderr_file_in_pipeline(self):
|
|
"""cat file 2>errors.log | head — stderr writes file."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file 2>errors.log | head"),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_devnull_then_stdout(self):
|
|
"""cat file >/dev/null 1>output — 1>output overrides, writes file."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file >/dev/null 1>output"),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_stdout_then_devnull(self):
|
|
"""cat file 1>output >/dev/null — output file gets created."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file 1>output >/dev/null"),
|
|
"cat_file_write",
|
|
)
|
|
|
|
# ── Quoted redirect targets ──
|
|
|
|
def test_cat_direct_read_quoted_output_double(self):
|
|
"""cat file > \"output file\" — double-quoted target, block."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read('cat file > "output file"'),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_quoted_output_single(self):
|
|
"""cat file > 'output file' — single-quoted target, block."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read("cat file > 'output file'"),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_quoted_var(self):
|
|
"""cat file > \"$TMP/out\" — quoted var, block."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read('cat file > "$TMP/out"'),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_pipe_quoted_output(self):
|
|
"""cat file | grep x > \"$TMP/out\" — pipeline quoted, block."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read('cat file | grep x > "$TMP/out"'),
|
|
"cat_file_write",
|
|
)
|
|
|
|
def test_cat_direct_read_pipe_quoted_stderr(self):
|
|
"""cat file | grep x 2> \"errors.log\" — quoted stderr, block."""
|
|
self.assertEqual(
|
|
self.pred._cat_direct_read('cat file | grep x 2> "errors.log"'),
|
|
"cat_file_write",
|
|
)
|
|
|
|
# ── Wrapper with option arguments — pipeline allow ──
|
|
|
|
def test_cat_direct_read_nice_n10_pipe(self):
|
|
self.assertIsNone(self.pred._cat_direct_read("nice -n 10 cat file | head"))
|
|
|
|
def test_cat_direct_read_env_u_pipe(self):
|
|
self.assertIsNone(self.pred._cat_direct_read("env -u PATH cat file | head"))
|
|
|
|
# ── _heredoc_direct ─────────────────────────────────────────
|
|
|
|
def test_heredoc_direct_quoted(self):
|
|
self.assertTrue(self.pred._heredoc_direct("<<'EOF' > /tmp/x"))
|
|
|
|
def test_heredoc_direct_unquoted(self):
|
|
self.assertTrue(self.pred._heredoc_direct("<<EOF > /tmp/x"))
|
|
|
|
def test_heredoc_direct_no_redirect(self):
|
|
self.assertTrue(self.pred._heredoc_direct("<<'EOF' /tmp/x"))
|
|
|
|
def test_heredoc_direct_no_file_ref(self):
|
|
self.assertTrue(self.pred._heredoc_direct("<<EOF"))
|
|
|
|
def test_heredoc_direct_not_present(self):
|
|
self.assertFalse(self.pred._heredoc_direct("echo hello > /tmp/x"))
|
|
|
|
# ── _echo_redirect ──────────────────────────────────────────
|
|
|
|
def test_echo_redirect_basic(self):
|
|
self.assertTrue(self.pred._echo_redirect("echo 'hello' > /tmp/x"))
|
|
|
|
def test_echo_redirect_append(self):
|
|
self.assertTrue(self.pred._echo_redirect("echo 'hello' >> /tmp/x"))
|
|
|
|
def test_echo_redirect_double_quote(self):
|
|
self.assertTrue(self.pred._echo_redirect('echo "hello" > /tmp/x'))
|
|
|
|
def test_echo_redirect_no_quote(self):
|
|
self.assertTrue(self.pred._echo_redirect("echo hello > /tmp/x"))
|
|
|
|
def test_echo_redirect_gt_in_quoted_string(self):
|
|
"""Literal > inside quotes only — not a redirect."""
|
|
self.assertFalse(self.pred._echo_redirect('echo "price > 100"'))
|
|
|
|
def test_echo_redirect_gt_in_quoted_with_real_redirect(self):
|
|
"""> inside quotes AND a real redirect after — block."""
|
|
self.assertTrue(
|
|
self.pred._echo_redirect('echo "price > 100" > /tmp/x'),
|
|
)
|
|
|
|
def test_echo_redirect_to_devnull(self):
|
|
"""echo to /dev/null is discard, not a file write."""
|
|
self.assertFalse(self.pred._echo_redirect("echo 'hi' > /dev/null"))
|
|
|
|
def test_echo_redirect_after_semicolon(self):
|
|
"""echo and > separated by ; — different commands."""
|
|
self.assertFalse(self.pred._echo_redirect("echo ok ; > /dev/null"))
|
|
|
|
def test_echo_redirect_pipe_no_redirect(self):
|
|
self.assertFalse(self.pred._echo_redirect("echo hello | grep h"))
|
|
|
|
# ── _echo_redirect — no-space ──────────────────────────
|
|
|
|
def test_echo_redirect_no_space(self):
|
|
self.assertTrue(self.pred._echo_redirect("echo hi>/tmp/x"))
|
|
|
|
def test_echo_redirect_partial_space(self):
|
|
self.assertTrue(self.pred._echo_redirect("echo hi >/tmp/x"))
|
|
|
|
# ── _echo_redirect — /dev/null at target only ──────────
|
|
|
|
def test_echo_redirect_real_file_with_devnull_stderr(self):
|
|
self.assertTrue(self.pred._echo_redirect("echo hi > /tmp/x 2>/dev/null"))
|
|
|
|
def test_echo_redirect_devnull_only(self):
|
|
self.assertFalse(self.pred._echo_redirect("echo hi > /dev/null 2>&1"))
|
|
|
|
# ── _echo_redirect — multi-command scan ────────────
|
|
|
|
def test_echo_redirect_first_ok_second_bad(self):
|
|
self.assertTrue(self.pred._echo_redirect("echo ok; echo bad > /tmp/x"))
|
|
|
|
def test_echo_redirect_grep_echo_then_bad(self):
|
|
self.assertTrue(self.pred._echo_redirect("grep echo README.md; echo bad > /tmp/x"))
|
|
|
|
# ── _printf_redirect ────────────────────────────────────────
|
|
|
|
def test_printf_redirect_basic(self):
|
|
self.assertTrue(self.pred._printf_redirect("printf '%s\\n' > /tmp/x"))
|
|
|
|
def test_printf_redirect_append(self):
|
|
self.assertTrue(self.pred._printf_redirect("printf 'x' >> /tmp/x"))
|
|
|
|
def test_printf_redirect_gt_in_format(self):
|
|
"""Format string contains > but no redirect."""
|
|
self.assertFalse(self.pred._printf_redirect("printf '%s > %s' a b"))
|
|
|
|
def test_printf_redirect_to_devnull(self):
|
|
self.assertFalse(self.pred._printf_redirect("printf 'x' > /dev/null"))
|
|
|
|
# ── _printf_redirect — no-space + /dev/null target ────
|
|
|
|
def test_printf_redirect_no_space(self):
|
|
self.assertTrue(self.pred._printf_redirect("printf x>/tmp/x"))
|
|
|
|
def test_printf_redirect_real_file_with_devnull_stderr(self):
|
|
self.assertTrue(self.pred._printf_redirect("printf x > /tmp/x 2>/dev/null"))
|
|
|
|
# ── _printf_redirect — multi-command scan ─────────
|
|
|
|
def test_printf_redirect_first_ok_second_bad(self):
|
|
self.assertTrue(self.pred._printf_redirect("printf ok; printf bad > /tmp/x"))
|
|
|
|
# ── _bare_redirect ──────────────────────────────────────────
|
|
|
|
def test_bare_redirect_start(self):
|
|
self.assertTrue(self.pred._bare_redirect("> /tmp/x"))
|
|
|
|
def test_bare_redirect_append(self):
|
|
self.assertTrue(self.pred._bare_redirect(">> /tmp/x"))
|
|
|
|
def test_bare_redirect_colon(self):
|
|
self.assertTrue(self.pred._bare_redirect(": > /tmp/x"))
|
|
|
|
def test_bare_redirect_semicolon(self):
|
|
self.assertTrue(self.pred._bare_redirect("echo a ; > /tmp/x"))
|
|
|
|
def test_bare_redirect_devnull(self):
|
|
self.assertFalse(self.pred._bare_redirect("> /dev/null"))
|
|
|
|
def test_bare_redirect_stderr_to_stdout(self):
|
|
"""2>&1 is not a file write."""
|
|
self.assertFalse(self.pred._bare_redirect("cmd 2>&1"))
|
|
|
|
# ── _bare_redirect — no-space + /dev/null target ────
|
|
|
|
def test_bare_redirect_no_space(self):
|
|
self.assertTrue(self.pred._bare_redirect(">/tmp/x"))
|
|
|
|
def test_bare_redirect_real_file_with_devnull_stderr(self):
|
|
self.assertTrue(self.pred._bare_redirect("> /tmp/x 2>/dev/null"))
|
|
|
|
# ── _tee_write ──────────────────────────────────────────────
|
|
|
|
def test_tee_write_basic(self):
|
|
self.assertTrue(self.pred._tee_write("echo foo | tee /tmp/x"))
|
|
|
|
def test_tee_write_append(self):
|
|
self.assertTrue(self.pred._tee_write("echo foo | tee -a /tmp/x"))
|
|
|
|
def test_tee_write_multi(self):
|
|
self.assertTrue(self.pred._tee_write("echo foo | tee f1 f2"))
|
|
|
|
def test_tee_write_not_present(self):
|
|
self.assertFalse(self.pred._tee_write("echo foo > /tmp/x"))
|
|
|
|
def test_tee_write_in_commit_message(self):
|
|
"""tee in a quoted commit message — not a real tee command."""
|
|
self.assertFalse(
|
|
self.pred._tee_write(
|
|
'git commit -m "add: tee, test, docs"',
|
|
),
|
|
)
|
|
|
|
# ── _tee_write — command position only ──────────────
|
|
|
|
def test_tee_write_grep_word(self):
|
|
"""tee as a word argument, not a command."""
|
|
self.assertFalse(self.pred._tee_write("grep tee README.md"))
|
|
|
|
def test_tee_write_path_component(self):
|
|
"""tee in a file path, not a command."""
|
|
self.assertFalse(self.pred._tee_write("ls /tmp/tee"))
|
|
|
|
def test_tee_write_pipe_grep(self):
|
|
"""tee after pipe but not as the command — grep tee."""
|
|
self.assertFalse(self.pred._tee_write("echo foo | grep tee"))
|
|
|
|
def test_tee_write_command_position(self):
|
|
"""tee at start of command — should block."""
|
|
self.assertTrue(self.pred._tee_write("tee /tmp/x"))
|
|
|
|
# ── _tee_write — command wrappers + subshell ─────
|
|
|
|
def test_tee_write_command_wrapper(self):
|
|
self.assertTrue(self.pred._tee_write("command tee /tmp/x"))
|
|
|
|
def test_tee_write_env_wrapper(self):
|
|
self.assertTrue(self.pred._tee_write("env tee /tmp/x"))
|
|
|
|
def test_tee_write_subshell(self):
|
|
self.assertTrue(self.pred._tee_write("(tee /tmp/x)"))
|
|
|
|
def test_tee_write_pipe_command_wrapper(self):
|
|
self.assertTrue(self.pred._tee_write("echo foo | command tee /tmp/x"))
|