| # Licensed to the Apache Software Foundation (ASF) under one | |
| # or more contributor license agreements. See the NOTICE file | |
| # distributed with this work for additional information | |
| # regarding copyright ownership. The ASF licenses this file | |
| # to you under the Apache License, Version 2.0 (the | |
| # "License"); you may not use this file except in compliance | |
| # with the License. You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, | |
| # software distributed under the License is distributed on an | |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| # KIND, either express or implied. See the License for the | |
| # specific language governing permissions and limitations | |
| # under the License. | |
| """check-no-bare-python: framework Python starts through `magpie-python`.""" | |
| import importlib.util | |
| import subprocess | |
| import time | |
| from pathlib import Path | |
| import pytest | |
| REPO_ROOT = Path(__file__).resolve().parents[3] | |
| LAUNCHER = REPO_ROOT / "plugins/magpie-setup/bin/magpie-python" | |
| _spec = importlib.util.spec_from_file_location( | |
| "check_no_bare_python", REPO_ROOT / "tools/dev/check-no-bare-python.py" | |
| ) | |
| assert _spec is not None and _spec.loader is not None | |
| mod = importlib.util.module_from_spec(_spec) | |
| _spec.loader.exec_module(mod) | |
| @pytest.mark.parametrize( | |
| "line", | |
| [ | |
| "python3 -m setup_preflight --project-root .", | |
| " entry: python3 tools/dev/check-doc-sync.py", | |
| "PYTHONPATH=tools/x/src python3 -P -m pkg", | |
| 'echo "$x" | python3 -c "import json"', | |
| "python tools/dev/bump.py", | |
| "python3.12 -m pkg", | |
| '"command": "python3 \\"$ROOT/tools/x.py\\""', | |
| "uv run python -m pkg", # outside CI: the working directory's .venv | |
| "uv run --no-project --python 3.14 python x.py", | |
| "flags=$(python3 - tools/dev/shared/ty.toml <<'PY'", | |
| "python3 - <<'PY' 2>&1 || true", | |
| "python3 -I -", | |
| ], | |
| ) | |
| def test_a_direct_start_is_reported(line: str) -> None: | |
| assert mod.findings_in("doc.md", line) | |
| @pytest.mark.parametrize( | |
| "line", | |
| [ | |
| "magpie-python -m setup_preflight --project-root .", | |
| "PYTHONPATH=tools/x/src magpie-python -P -m pkg", | |
| "sh plugins/magpie-setup/bin/magpie-python tools/dev/check-doc-sync.py", | |
| " entry: sh plugins/magpie-setup/bin/magpie-python tools/dev/check-doc-sync.py", | |
| "uv run --project tools/pr-management python -m pr_management.stack_review.stack_chain", | |
| "uv run --directory tools/x --project . python -m pytest", | |
| "Python 3.14 on PATH", | |
| "the python3 on PATH resolves to uv's interpreter", | |
| "pip install foo # python package", | |
| "python3 --version", | |
| "python3 -V", | |
| "sh plugins/magpie-setup/bin/magpie-python - tools/dev/shared/ty.toml <<'PY'", | |
| ], | |
| ) | |
| def test_the_launcher_member_runs_and_prose_are_not(line: str) -> None: | |
| assert mod.findings_in("doc.md", line) == [] | |
| def test_other_uv_run_starts_are_accepted_only_in_ci() -> None: | |
| line = "uv run --no-config --no-project --python 3.14 python release/reproducible_archive.py epoch" | |
| assert mod.findings_in(".github/workflows/x.yml", line) == [] | |
| assert mod.findings_in("plugins/magpie-setup/templates/workflows/release-candidate.yml", line) == [] | |
| assert mod.findings_in("docs/x.md", line) | |
| @pytest.mark.parametrize( | |
| "line", | |
| ["uvx python@3.14 -m pkg", "uvx --no-config python@3.14 -m pkg", "uvx --python 3.14 python -c pass"], | |
| ) | |
| def test_uvx_running_python_is_reported(line: str) -> None: | |
| # uvx runs an ephemeral venv out of uv's cache, which the sandbox can write. | |
| assert any("uv's sandbox-writable cache" in found for found in mod.findings_in("doc.md", line)) | |
| @pytest.mark.parametrize("unit", ["-- ", "--- ", "-- -", " -- --"]) | |
| def test_a_run_of_dashes_after_uvx_is_matched_in_linear_time(unit: str) -> None: | |
| # CodeQL's ReDoS check: an option pattern that can split one token two | |
| # ways backtracks exponentially when the match finally fails. | |
| started = time.perf_counter() | |
| assert mod.UVX_PYTHON.search("uvx " + unit * 40 + "x") is None | |
| assert time.perf_counter() - started < 1 | |
| def test_uvx_running_a_tool_is_not() -> None: | |
| assert ( | |
| mod.findings_in("doc.md", "uvx --from ~/.claude/magpie/vetted-ops vetted-op-read --caller x op") == [] | |
| ) | |
| def test_a_package_run_by_its_init_path_is_reported_even_under_the_launcher() -> None: | |
| (found,) = mod.findings_in("doc.md", "magpie-python tools/x/src/x/__init__.py --check") | |
| assert "__init__.py by path" in found | |
| def test_a_bare_shebang_is_reported_and_the_launcher_one_is_not() -> None: | |
| assert mod.findings_in("tools/x.py", "#!/usr/bin/env python3\nprint(1)\n") | |
| assert mod.findings_in("tools/x.sh", "#!/usr/bin/env python3\n") | |
| assert mod.findings_in("tools/x.py", "#!/usr/bin/env magpie-python\nprint(1)\n") == [] | |
| def test_python_files_are_checked_for_their_shebang_only() -> None: | |
| # A .py file mentions interpreters in strings and docstrings; only its | |
| # first line starts it. | |
| assert mod.findings_in("tools/x.py", '"""Run `python3 -m x`."""\n') == [] | |
| def test_a_hook_blocked_by_a_missing_launcher_prints_the_fix_itself() -> None: | |
| # A hook that blocks for a missing launcher blocks the agent too, so its | |
| # message carries the install commands; pointing at a skill would need the | |
| # very agent it just blocked. A skill's diagnostic script (the doctor's | |
| # probes) checks for the launcher to report it, not to block, so it is | |
| # not a hook. | |
| listed = subprocess.run( | |
| [ | |
| "git", | |
| "grep", | |
| "-l", | |
| "command -v magpie-python", | |
| "--", | |
| ".", | |
| ":!plugins/magpie-*/tools/**", | |
| ":!plugins/magpie-*/skills/*/scripts/**", | |
| ], | |
| cwd=REPO_ROOT, | |
| capture_output=True, | |
| text=True, | |
| check=False, | |
| ).stdout.split() | |
| assert listed, "no hook checks for the launcher" | |
| for name in listed: | |
| for line in (REPO_ROOT / name).read_text(encoding="utf-8").splitlines(): | |
| if "command -v magpie-python" not in line or "tests/" in name: | |
| continue | |
| assert "uv python install 3.14" in line, f"{name}: {line[:120]}" | |
| assert "install -m 0755" in line, f"{name}: {line[:120]}" | |
| assert "/magpie-setup:upgrade" not in line, f"{name}: {line[:120]}" | |
| assert "exit 2" in line, f"{name}: {line[:120]}" | |
| def test_every_exception_names_a_reason() -> None: | |
| assert mod.ALLOWED | |
| assert all(reason.strip() for reason in mod.ALLOWED.values()) | |
| def test_the_tree_is_clean() -> None: | |
| done = subprocess.run( | |
| ["sh", str(LAUNCHER), str(REPO_ROOT / "tools/dev/check-no-bare-python.py")], | |
| cwd=REPO_ROOT, | |
| capture_output=True, | |
| text=True, | |
| check=False, | |
| ) | |
| assert done.returncode == 0, done.stdout + done.stderr |