Skip to content

The agent that grows with you

test: fold the bun/deno tests into two parametrized invariants

teknium14cefeed7debc
diff --git a/tests/tools/test_approval_bun_deno_eval.py b/tests/tools/test_approval_bun_deno_eval.py
index 347bf268933..8240cb0ec03 100644
--- a/tests/tools/test_approval_bun_deno_eval.py
+++ b/tests/tools/test_approval_bun_deno_eval.py
@@ -4,55 +4,41 @@ tables did not know: ``bun -e/--eval``, and Deno's bare ``eval`` subcommand
``node -e`` / ``python -c``. See issue #116012.
"""
+import pytest
+
from tools.approval import detect_dangerous_command
class TestBunDenoInlineScriptExecution:
- def test_inline_eval_detected(self):
- for cmd in (
- 'bun -e "console.log(1)"',
- 'bun --eval "console.log(1)"',
- 'deno eval "console.log(1)"',
- 'deno -e "console.log(1)"',
- 'deno --eval "console.log(1)"',
+ @pytest.mark.parametrize(
+ "cmd, expected_key",
+ [
+ ('bun -e "console.log(1)"', "script execution via -e/-c flag"),
+ ('bun --eval "console.log(1)"', "script execution via -e/-c flag"),
+ ('deno eval "console.log(1)"', "script execution via -e/-c flag"),
+ ('deno -e "console.log(1)"', "script execution via -e/-c flag"),
+ ('deno --eval "console.log(1)"', "script execution via -e/-c flag"),
# Command-position wrappers must not shield the interpreter.
- 'sudo bun -e "console.log(1)"',
- 'sudo deno eval "console.log(1)"',
- ):
- dangerous, key, _ = detect_dangerous_command(cmd)
- assert dangerous is True, cmd
- assert key == "script execution via -e/-c flag", cmd
-
- def test_windows_executable_spelling_detected(self):
- # Windows resolves executable names case-insensitively; `_interpreter_family`
- # lowercases the basename, so BUN.EXE / Deno.exe must be classified too.
- for cmd in (
- 'BUN.EXE -e "console.log(1)"',
- 'Deno.exe eval "console.log(1)"',
- ):
- dangerous, key, _ = detect_dangerous_command(cmd)
- assert dangerous is True, cmd
- assert key == "script execution via -e/-c flag", cmd
-
- def test_interpreter_heredoc_detected(self):
- for cmd in (
- 'bun << "EOF"\nconsole.log("pwned")\nEOF',
- "deno << 'EOF'\nconsole.log(\"pwned\")\nEOF",
- ):
- dangerous, key, _ = detect_dangerous_command(cmd)
- assert dangerous is True, cmd
- assert key == "script execution via heredoc", cmd
-
- def test_malformed_quoting_fails_closed(self):
- # An unterminated quote on a bun/deno inline script must fail closed like the
- # other interpreter families, not silently skip the family.
- dangerous, key, _ = detect_dangerous_command('bun -e "console.log(1)')
- assert dangerous is True
- assert key == "command parser limit or malformed executable payload"
-
- def test_plain_bun_deno_invocations_not_flagged(self):
- """Everyday subcommands that run project files must stay safe."""
- for cmd in (
+ ('sudo bun -e "console.log(1)"', "script execution via -e/-c flag"),
+ ('sudo deno eval "console.log(1)"', "script execution via -e/-c flag"),
+ # Windows resolves executable names case-insensitively; `_interpreter_family`
+ # lowercases the basename, so BUN.EXE / Deno.exe must be classified too.
+ ('BUN.EXE -e "console.log(1)"', "script execution via -e/-c flag"),
+ ('Deno.exe eval "console.log(1)"', "script execution via -e/-c flag"),
+ ('bun << "EOF"\nconsole.log("pwned")\nEOF', "script execution via heredoc"),
+ ("deno << 'EOF'\nconsole.log(\"pwned\")\nEOF", "script execution via heredoc"),
+ # An unterminated quote fails closed like the other interpreter families.
+ ('bun -e "console.log(1)', "command parser limit or malformed executable payload"),
+ ],
+ )
+ def test_inline_eval_classified_like_node_and_python(self, cmd, expected_key):
+ dangerous, key, _ = detect_dangerous_command(cmd)
+ assert dangerous is True, cmd
+ assert key == expected_key, cmd
+
+ @pytest.mark.parametrize(
+ "cmd",
+ [
"bun install",
"bun run build",
"deno run server.ts",
@@ -60,11 +46,9 @@ class TestBunDenoInlineScriptExecution:
"deno lint",
# A file literally named eval.ts is a `run` operand, not the eval subcommand.
"deno run eval.ts",
- ):
- dangerous, _, _ = detect_dangerous_command(cmd)
- assert dangerous is False, cmd
-
- def test_quoted_prose_about_deno_eval_not_flagged(self):
- # `deno eval` inside quotes is data for echo, not a command position.
- dangerous, _, _ = detect_dangerous_command("echo 'use deno eval for that'")
- assert dangerous is False
+ # `deno eval` inside quotes is data for echo, not a command position.
+ "echo 'use deno eval for that'",
+ ],
+ )
+ def test_everyday_bun_deno_invocations_stay_unflagged(self, cmd):
+ assert detect_dangerous_command(cmd) == (False, None, None), cmd