summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-19 20:57:20 +0000
committerClaude <noreply@anthropic.com>2026-10-19 21:53:45 +0000
commit8680640bffd91d9a7fc33ad1ede46a031d36abef (patch)
tree57fc1a74f4a485228227c6bb2da762dbc96bf422 /plugins
parentdb13ec7d8e38109495f808adbcd4fe56c7bb8b31 (diff)
security-guidance: let the tests that start a stand-in claude run on Windowsmhegazy/sg-gateway-login-wintest
The tests' stand-ins for claude were files with a #! line, which Windows cannot start, so the gateway sign-in tests and the inner-CLI warning test failed there. On Windows a stand-in is now an .exe built the way pip builds a console script, and the gateway one writes its call log as UTF-8. Two tests that move the home directory also set USERPROFILE, and the one test that needs the Agent SDK without saying so is skipped without it.
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/tests/conftest.py45
-rw-r--r--plugins/security-guidance/tests/test_gateway_sign_in.py20
2 files changed, 51 insertions, 14 deletions
diff --git a/plugins/security-guidance/tests/conftest.py b/plugins/security-guidance/tests/conftest.py
index 669adc4f..f1addb9b 100644
--- a/plugins/security-guidance/tests/conftest.py
+++ b/plugins/security-guidance/tests/conftest.py
@@ -1,10 +1,14 @@
import http.server
+import io
import json
import os
+import struct
import subprocess
import sys
+import sysconfig
import threading
import time
+import zipfile
from pathlib import Path
import pytest
@@ -280,14 +284,47 @@ def env(hook_env, gateway):
return e
+def _windows_exe(script):
+ """`script` as the bytes of a Windows .exe: Windows starts neither a
+ `#!` file nor, without a shell, a .cmd with multi-line arguments. Built
+ the way pip builds a console script: its launcher stub, then the
+ interpreter to run, then a zip holding the script as __main__.py."""
+ distlib = pytest.importorskip("pip._vendor.distlib")
+ stub = "t%s%s.exe" % ("64" if struct.calcsize("P") == 8 else "32",
+ "-arm" if sysconfig.get_platform() == "win-arm64" else "")
+ stub = os.path.join(os.path.dirname(distlib.__file__), stub)
+ if not os.path.isfile(stub):
+ pytest.skip(f"pip has no script launcher {os.path.basename(stub)}")
+ with open(stub, "rb") as f:
+ launcher = f.read()
+ python = f'"{sys.executable}"' if " " in sys.executable else sys.executable
+ archive = io.BytesIO()
+ with zipfile.ZipFile(archive, "w") as zf:
+ zf.writestr("__main__.py", script)
+ return launcher + f"#!{python}\n".encode("utf-8") + archive.getvalue()
+
+
+def write_cli(tmp_path, name, script):
+ """A stand-in for `claude` in `tmp_path` that runs the Python `script`;
+ returns its path."""
+ if os.name == "nt":
+ cli = tmp_path / f"{name}.exe"
+ cli.write_bytes(_windows_exe(script))
+ else:
+ cli = tmp_path / name
+ cli.write_text(f"#!{sys.executable}\n{script}")
+ cli.chmod(0o755)
+ return cli
+
+
@pytest.fixture
def failing_inner_cli(tmp_path):
"""An inner CLI that prints a warning on stderr and exits 1; the review
then falls back to a single-shot call."""
- cli = tmp_path / "claude"
- cli.write_text("#!/bin/sh\necho 'claude.ai connectors are disabled' >&2\nexit 1\n")
- cli.chmod(0o755)
- return str(cli)
+ return str(write_cli(tmp_path, "claude", (
+ "import sys\n"
+ "sys.stderr.write('claude.ai connectors are disabled\\n')\n"
+ "sys.exit(1)\n")))
@pytest.fixture
diff --git a/plugins/security-guidance/tests/test_gateway_sign_in.py b/plugins/security-guidance/tests/test_gateway_sign_in.py
index e2469fb7..7b6e9fc4 100644
--- a/plugins/security-guidance/tests/test_gateway_sign_in.py
+++ b/plugins/security-guidance/tests/test_gateway_sign_in.py
@@ -2,6 +2,7 @@
Claude Code gives hooks no credential."""
import asyncio
import json
+import os
import sys
import types
@@ -9,7 +10,7 @@ import pytest
from conftest import (
VULN_PY, bash_payload, commit_file, edit_payload, git, metrics_of, run_hook,
- stop_payload, ups_payload,
+ stop_payload, ups_payload, write_cli,
)
import _base
@@ -26,10 +27,8 @@ def make_cli(tmp_path, auth_status, *, name="claude", hang=False, linger=False):
waits 5 s before answering; `linger` leaves a process running that holds
stdout open after the CLI exits."""
calls = tmp_path / f"{name}.calls"
- cli = tmp_path / name
- cli.write_text(f"""#!{sys.executable}
-import os, subprocess, sys, time
-with open({str(calls)!r}, "a") as f:
+ script = f"""import os, subprocess, sys, time
+with open({str(calls)!r}, "a", encoding="utf-8") as f:
f.write(" ".join(sys.argv[1:]) + " | SGD=" + os.environ.get("SECURITY_GUIDANCE_DISABLE", "")
+ " | CWD=" + os.getcwd() + "\\n")
if sys.argv[1:3] != ["auth", "status"]:
@@ -41,13 +40,12 @@ sys.stdout.write({auth_status!r})
sys.stdout.flush()
if {linger!r}:
subprocess.Popen([sys.executable, "-c", "import time; time.sleep(5)"])
-""")
- cli.chmod(0o755)
- return cli, calls
+"""
+ return write_cli(tmp_path, name, script), calls
def calls_of(calls):
- return calls.read_text().splitlines() if calls.exists() else []
+ return calls.read_text(encoding="utf-8").splitlines() if calls.exists() else []
def review_calls(calls):
@@ -108,7 +106,7 @@ class TestGatewaySignInCheck:
cli, calls = make_cli(tmp_path, SIGNED_IN)
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("HOME", str(tmp_path))
- monkeypatch.setenv("SG_AGENTIC_CLI_PATH", "./claude")
+ monkeypatch.setenv("SG_AGENTIC_CLI_PATH", f"./{cli.name}")
assert llm.can_review("s1") is False
assert calls_of(calls) == []
@@ -118,6 +116,7 @@ class TestGatewaySignInCheck:
home.mkdir()
repo.mkdir()
monkeypatch.setenv("HOME", str(home))
+ monkeypatch.setenv("USERPROFILE", str(home)) # "~" on Windows
monkeypatch.chdir(repo)
monkeypatch.setenv("SG_AGENTIC_CLI_PATH", str(cli))
assert llm.can_review("s1")
@@ -379,6 +378,7 @@ class TestHooksInASignedInSession:
def test_unanswered_sign_in_check_keeps_the_turn_for_the_next_stop(
self, hook_env, tmp_path, workspace):
+ pytest.importorskip("claude_agent_sdk")
env = without_credential(hook_env)
cli, calls = make_cli(tmp_path, "")
env["SG_AGENTIC_CLI_PATH"] = str(cli)