summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorClaude <noreply@anthropic.com>2026-11-04 20:06:08 +0000
committerClaude <noreply@anthropic.com>2026-11-04 20:06:08 +0000
commit0cb9409fbe594ceb27fab3c32114704752de0087 (patch)
tree15573f7a80fbe6e162ed574b89060d1a7853e74a /plugins
parent18b39784432502087fefa2afe459ed23b56e9dd9 (diff)
security-guidance: run the shell pattern test with a working bashmhegazy/sg-count-unmatched-bash-starts
On Windows the bash on PATH can be the WSL launcher. Use the bash shipped with git there, and skip when no bash answers.
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/tests/test_unmatched_bash_metric.py28
1 files changed, 26 insertions, 2 deletions
diff --git a/plugins/security-guidance/tests/test_unmatched_bash_metric.py b/plugins/security-guidance/tests/test_unmatched_bash_metric.py
index c6aec681..8933bbd1 100644
--- a/plugins/security-guidance/tests/test_unmatched_bash_metric.py
+++ b/plugins/security-guidance/tests/test_unmatched_bash_metric.py
@@ -4,6 +4,7 @@ import json
import shutil
import subprocess
import sys
+from pathlib import Path
import pytest
@@ -106,12 +107,35 @@ def test_text_check_on_known_strings():
assert hook._shell_precheck_passes(_raw(command)) is passes, command
-@pytest.mark.skipif(not shutil.which("bash"), reason="needs bash")
+def _working_bash():
+ """A bash that runs a script. On Windows that is the one shipped with
+ git: the `bash` on PATH there can be the WSL launcher, which is not one."""
+ found = []
+ git = shutil.which("git")
+ if sys.platform == "win32" and git:
+ root = Path(git).resolve().parent.parent
+ found += [root / "bin" / "bash.exe", root / "usr" / "bin" / "bash.exe"]
+ if shutil.which("bash"):
+ found.append(Path(shutil.which("bash")))
+ for bash in found:
+ try:
+ r = subprocess.run([str(bash), "-c", "echo ok"], capture_output=True, timeout=30)
+ except (OSError, subprocess.SubprocessError):
+ continue
+ if r.returncode == 0 and r.stdout.strip() == b"ok":
+ return str(bash)
+ return None
+
+
def test_text_check_agrees_with_the_shell_pattern():
+ bash = _working_bash()
+ if not bash:
+ pytest.skip("needs a working bash")
script = f'input=$(cat); case "$input" in {SHELL_PATTERN}) echo pass ;; *) echo skip ;; esac'
for command, _ in CASES:
raw = _raw(command)
- r = subprocess.run(["bash", "-c", script], input=raw.encode(), capture_output=True, timeout=30)
+ r = subprocess.run([bash, "-c", script], input=raw.encode(), capture_output=True, timeout=30)
+ assert r.stdout.strip() in (b"pass", b"skip"), r.stderr
assert (r.stdout.strip() == b"pass") is hook._shell_precheck_passes(raw), command