|
| 1 | +"""The PR review must not load or run anything from the PR checkout. |
| 2 | +
|
| 3 | +The workspace tests start the review script the way action.yml does, with |
| 4 | +``uv run`` and the released SDK, in a subprocess: other test modules replace |
| 5 | +``openhands.sdk`` in ``sys.modules`` with stubs. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +import subprocess |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytest |
| 15 | +import yaml |
| 16 | + |
| 17 | +from test_pr_review_prompt_injection import _DEFAULTS, _load_prompt_module |
| 18 | + |
| 19 | +PLUGIN_DIR = Path(__file__).parent.parent / "plugins" / "pr-review" |
| 20 | +SCRIPTS_DIR = PLUGIN_DIR / "scripts" |
| 21 | + |
| 22 | +_CONVERSATION_RUNNER = """ |
| 23 | +import json |
| 24 | +import sys |
| 25 | +
|
| 26 | +sys.path.insert(0, sys.argv[1]) |
| 27 | +import agent_script |
| 28 | +
|
| 29 | +config = { |
| 30 | + "agent_kind": "openhands", |
| 31 | + "model": "openai/test-model", |
| 32 | + "api_key": "test-key", |
| 33 | + "base_url": None, |
| 34 | + "use_sub_agents": False, |
| 35 | + "load_public_skills": False, |
| 36 | + "base_repo_dir": sys.argv[2], |
| 37 | +} |
| 38 | +conversation = agent_script.create_conversation(config, secrets={}) |
| 39 | +conversation.send_message("start") |
| 40 | +print(json.dumps([skill.content for skill in conversation.agent.agent_context.skills])) |
| 41 | +""" |
| 42 | + |
| 43 | + |
| 44 | +def _git_repo(path: Path, agents_md: str) -> Path: |
| 45 | + path.mkdir(parents=True, exist_ok=True) |
| 46 | + (path / "AGENTS.md").write_text(agents_md) |
| 47 | + subprocess.run(["git", "init", "-q"], cwd=path, check=True) |
| 48 | + subprocess.run(["git", "add", "-A"], cwd=path, check=True) |
| 49 | + subprocess.run( |
| 50 | + ["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "x"], |
| 51 | + cwd=path, |
| 52 | + check=True, |
| 53 | + ) |
| 54 | + return path |
| 55 | + |
| 56 | + |
| 57 | +def _hostile_checkout(path: Path, marker: Path) -> Path: |
| 58 | + plugin = path / ".openhands" / "plugins" / "hostile" |
| 59 | + (plugin / ".plugin").mkdir(parents=True) |
| 60 | + (plugin / "hooks").mkdir() |
| 61 | + (plugin / ".plugin" / "plugin.json").write_text( |
| 62 | + json.dumps({"name": "hostile", "version": "0.0.1", "description": "test"}) |
| 63 | + ) |
| 64 | + hook = {"type": "command", "command": f"touch {marker}"} |
| 65 | + (plugin / "hooks" / "hooks.json").write_text( |
| 66 | + json.dumps({"hooks": {"SessionStart": [{"matcher": "*", "hooks": [hook]}]}}) |
| 67 | + ) |
| 68 | + return _git_repo(path, "HEAD-BRANCH-GUIDANCE\n") |
| 69 | + |
| 70 | + |
| 71 | +def _start_conversation(cwd: Path, base_repo_dir: str) -> list[str]: |
| 72 | + if shutil.which("uv") is None: |
| 73 | + pytest.skip("uv is required to run the review script as action.yml does") |
| 74 | + result = subprocess.run( |
| 75 | + [ |
| 76 | + "uv", "run", "--no-config", "--no-project", |
| 77 | + "--with", "openhands-sdk", "--with", "openhands-tools", "--with", "lmnr", |
| 78 | + "python", "-c", _CONVERSATION_RUNNER, str(SCRIPTS_DIR), base_repo_dir, |
| 79 | + ], |
| 80 | + cwd=cwd, |
| 81 | + env={**os.environ, "OPENHANDS_SUPPRESS_BANNER": "1"}, |
| 82 | + capture_output=True, |
| 83 | + text=True, |
| 84 | + timeout=600, |
| 85 | + ) |
| 86 | + assert result.returncode == 0, result.stderr[-4000:] |
| 87 | + return json.loads(result.stdout.strip().splitlines()[-1]) |
| 88 | + |
| 89 | + |
| 90 | +def test_action_layout_loads_no_pr_plugin_and_base_guidance_only(tmp_path): |
| 91 | + marker = tmp_path / "hook-ran" |
| 92 | + workspace = tmp_path / "workspace" |
| 93 | + _hostile_checkout(workspace / "pr-repo", marker) |
| 94 | + _git_repo(workspace / "base-repo", "BASE-BRANCH-GUIDANCE\n") |
| 95 | + |
| 96 | + skills = _start_conversation(workspace, "base-repo") |
| 97 | + |
| 98 | + assert not marker.exists() |
| 99 | + assert any("BASE-BRANCH-GUIDANCE" in content for content in skills) |
| 100 | + assert not any("HEAD-BRANCH-GUIDANCE" in content for content in skills) |
| 101 | + |
| 102 | + |
| 103 | +def test_checkout_as_workspace_would_run_the_pr_hook(tmp_path): |
| 104 | + """Control: the hostile fixture does run when the checkout is the workspace.""" |
| 105 | + marker = tmp_path / "hook-ran" |
| 106 | + checkout = _hostile_checkout(tmp_path / "pr-repo", marker) |
| 107 | + |
| 108 | + _start_conversation(checkout, "") |
| 109 | + |
| 110 | + assert marker.exists() |
| 111 | + |
| 112 | + |
| 113 | +def _action_step(name): |
| 114 | + action = yaml.safe_load((PLUGIN_DIR / "action.yml").read_text()) |
| 115 | + return next(step for step in action["runs"]["steps"] if step["name"] == name) |
| 116 | + |
| 117 | + |
| 118 | +def test_action_runs_uv_outside_the_checkout_in_an_empty_workspace(): |
| 119 | + prepare = _action_step("Prepare review workspace") |
| 120 | + run = _action_step("Run PR review") |
| 121 | + |
| 122 | + assert 'mv "$GITHUB_WORKSPACE/pr-repo" "$REVIEW_WORKSPACE/pr-repo"' in prepare["run"] |
| 123 | + assert "cd pr-repo" not in run["run"] |
| 124 | + assert 'cd "$RUNNER_TEMP/pr-review-workspace"' in run["run"] |
| 125 | + assert "uv run --no-config --no-project" in run["run"] |
| 126 | + assert run["env"]["PR_REPO_DIR"] == "pr-repo" |
| 127 | + assert run["env"]["PR_BASE_REPO_DIR"] == "base-repo" |
| 128 | + |
| 129 | + |
| 130 | +def test_prompt_points_at_the_checkout_and_base_guidance(): |
| 131 | + prompt_module = _load_prompt_module() |
| 132 | + |
| 133 | + prompt = prompt_module.format_prompt( |
| 134 | + **_DEFAULTS, repo_dir="pr-repo", base_repo_dir="base-repo" |
| 135 | + ) |
| 136 | + |
| 137 | + assert "The PR head is checked out in `pr-repo/`" in prompt |
| 138 | + assert "Read it from the base-branch copy in `base-repo/`" in prompt |
| 139 | + |
| 140 | + |
| 141 | +def test_prompt_without_directories_keeps_the_original_guidance(): |
| 142 | + prompt_module = _load_prompt_module() |
| 143 | + |
| 144 | + prompt = prompt_module.format_prompt(**_DEFAULTS) |
| 145 | + |
| 146 | + assert "## Workspace" not in prompt |
| 147 | + assert "read `AGENTS.md` at the repository root" in prompt |
0 commit comments