|
| 1 | +# Copyright 2026-present the HuggingFace Inc. team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Runs the real Claude Code CLI through safeagents, set up as in docs/claude-code.md. |
| 15 | +
|
| 16 | +Skipped unless `claude` is installed (or `CLAUDE_BIN` points at it). |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import os |
| 22 | +import shutil |
| 23 | +import subprocess |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +import pytest |
| 27 | +from fake_model import FakeModel, scenario_prompt |
| 28 | +from stack import BLOCKED_SCENARIOS, check_blocked, check_ran, running_proxy |
| 29 | + |
| 30 | +CLAUDE_BIN = os.environ.get("CLAUDE_BIN") or shutil.which("claude") |
| 31 | +pytestmark = pytest.mark.skipif(CLAUDE_BIN is None, reason="Claude Code is not installed") |
| 32 | + |
| 33 | +SHELL_TOOL = "Bash" |
| 34 | +# Variables from the calling shell that would change which account, endpoint or |
| 35 | +# settings Claude Code uses. |
| 36 | +INHERITED_PREFIXES = ("ANTHROPIC_", "CLAUDE") |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture(scope="module") |
| 40 | +def stack(tmp_path_factory): |
| 41 | + with FakeModel() as model, running_proxy(model.url, tmp_path_factory.mktemp("proxy")) as proxy: |
| 42 | + yield model, proxy |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def claude_home(tmp_path) -> Path: |
| 47 | + """An empty home directory, so no user settings, logins or CLAUDE.md files apply.""" |
| 48 | + home = tmp_path / "home" |
| 49 | + home.mkdir() |
| 50 | + return home |
| 51 | + |
| 52 | + |
| 53 | +def run_claude(claude_home: Path, workdir: Path, base_url: str, scenario: str) -> str: |
| 54 | + """Runs one headless Claude Code prompt and returns its JSON event stream.""" |
| 55 | + env = {} |
| 56 | + for name, value in os.environ.items(): |
| 57 | + if not name.startswith(INHERITED_PREFIXES): |
| 58 | + env[name] = value |
| 59 | + env.update( |
| 60 | + { |
| 61 | + "HOME": str(claude_home), |
| 62 | + "CLAUDE_CONFIG_DIR": str(claude_home / ".claude"), |
| 63 | + # As in docs/claude-code.md: the proxy URL, without /v1. |
| 64 | + "ANTHROPIC_BASE_URL": base_url, |
| 65 | + "ANTHROPIC_API_KEY": "test-key", |
| 66 | + "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1", |
| 67 | + "DISABLE_AUTOUPDATER": "1", |
| 68 | + } |
| 69 | + ) |
| 70 | + # The prompt comes first: `--tools` and `--allowedTools` take every value after them. |
| 71 | + command = [ |
| 72 | + CLAUDE_BIN, |
| 73 | + "--print", |
| 74 | + scenario_prompt(scenario, tool=SHELL_TOOL), |
| 75 | + "--output-format", |
| 76 | + "stream-json", |
| 77 | + "--verbose", |
| 78 | + "--no-session-persistence", |
| 79 | + "--tools", |
| 80 | + SHELL_TOOL, |
| 81 | + "--allowedTools", |
| 82 | + SHELL_TOOL, |
| 83 | + ] |
| 84 | + completed = subprocess.run( |
| 85 | + command, |
| 86 | + cwd=workdir, |
| 87 | + env=env, |
| 88 | + stdin=subprocess.DEVNULL, |
| 89 | + capture_output=True, |
| 90 | + text=True, |
| 91 | + timeout=120, |
| 92 | + check=False, |
| 93 | + ) |
| 94 | + assert completed.returncode == 0, f"Claude Code failed:\n{completed.stderr}" |
| 95 | + return completed.stdout |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize(("scenario", "reason"), BLOCKED_SCENARIOS) |
| 99 | +def test_flagged_call_is_blocked(stack, claude_home, tmp_path, scenario, reason): |
| 100 | + model, proxy = stack |
| 101 | + model.requests.clear() |
| 102 | + output = run_claude(claude_home, tmp_path, proxy.url, scenario) |
| 103 | + check_blocked(model, proxy, output, scenario, SHELL_TOOL, reason) |
| 104 | + |
| 105 | + |
| 106 | +def test_clean_call_runs(stack, claude_home, tmp_path): |
| 107 | + model, proxy = stack |
| 108 | + model.requests.clear() |
| 109 | + output = run_claude(claude_home, tmp_path, proxy.url, "clean") |
| 110 | + check_ran(model, output) |
0 commit comments