113 lines
4.5 KiB
Python
113 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: MIT
|
|
"""Check that unknown changes cannot take a scoped CI path."""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
|
|
import importlib.util
|
|
|
|
|
|
SCRIPT = Path(__file__).with_name("pr-surface.py")
|
|
spec = importlib.util.spec_from_file_location("pr_surface", SCRIPT)
|
|
assert spec and spec.loader
|
|
pr_surface = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(pr_surface)
|
|
|
|
|
|
def git(directory: Path, *args: str) -> str:
|
|
return subprocess.check_output(
|
|
["git", "-C", str(directory), *args], text=True,
|
|
stderr=subprocess.DEVNULL,
|
|
).strip()
|
|
|
|
|
|
def main() -> None:
|
|
assert pr_surface.documentation_only(["README.md", "docs/setup.md"])
|
|
for paths in ([], ["docs/generated.json"], ["src/main.rs"],
|
|
["README.md", "ci/pr-surface.py"]):
|
|
assert not pr_surface.documentation_only(paths), paths
|
|
assert pr_surface.headless_packages(["crates/alkane-cli/src/main.rs"]) == ["alkane-cli"]
|
|
assert pr_surface.headless_packages(["crates/alkane-artifacts/src/lib.rs"]) == [
|
|
"alkane-artifacts", "alkane-cli", "alkane-flash",
|
|
]
|
|
assert pr_surface.headless_packages([
|
|
"crates/alkane-access/src/lib.rs", "crates/alkane-flash/tests/update.rs",
|
|
]) == ["alkane-access", "alkane-flash"]
|
|
for paths in ([], ["crates/alkane-core/src/lib.rs"],
|
|
["crates/alkane-discovery/src/lib.rs"],
|
|
["crates/alkane-gui/src/main.rs"],
|
|
["crates/alkane-artifacts/Cargo.toml"],
|
|
["crates/alkane-cli/src/main.rs", "Cargo.lock"],
|
|
["crates/alkane-cli/src/main.rs", "ci/pr-surface.py"],
|
|
["crates/alkane-cli/README.md"]):
|
|
assert pr_surface.headless_packages(paths) == [], paths
|
|
|
|
with tempfile.TemporaryDirectory(prefix="alkane-toolkit-graph-") as directory:
|
|
root = Path(directory)
|
|
for name, extra in (
|
|
("alkane-gui", ""),
|
|
("alkane-artifacts", ""),
|
|
("alkane-flash", 'alkane-artifacts = { path = "../alkane-artifacts" }\n'),
|
|
):
|
|
crate = root / "crates" / name
|
|
crate.mkdir(parents=True)
|
|
(crate / "Cargo.toml").write_text(
|
|
f'[package]\nname = "{name}"\nversion = "0.1.0"\n'
|
|
f'[dependencies]\n{extra}'
|
|
)
|
|
saved_root = pr_surface.ROOT
|
|
try:
|
|
pr_surface.ROOT = root
|
|
changed = ["crates/alkane-artifacts/src/lib.rs"]
|
|
assert pr_surface.headless_packages(changed) == [
|
|
"alkane-artifacts", "alkane-flash",
|
|
]
|
|
with (root / "crates/alkane-gui/Cargo.toml").open("a") as stream:
|
|
stream.write('alkane-flash = { path = "../alkane-flash" }\n')
|
|
assert pr_surface.headless_packages(changed) == []
|
|
finally:
|
|
pr_surface.ROOT = saved_root
|
|
|
|
with tempfile.TemporaryDirectory(prefix="alkane-toolkit-pr-surface-") as directory:
|
|
repo = Path(directory)
|
|
git(repo, "init", "-q")
|
|
git(repo, "config", "user.name", "CI Test")
|
|
git(repo, "config", "user.email", "ci@example.invalid")
|
|
(repo / "README.md").write_text("initial\n")
|
|
git(repo, "add", "README.md")
|
|
git(repo, "commit", "-qm", "base")
|
|
base = git(repo, "rev-parse", "HEAD")
|
|
git(repo, "update-ref", "refs/remotes/origin/main", base)
|
|
(repo / "README.md").write_text("documentation change\n")
|
|
git(repo, "commit", "-qam", "docs")
|
|
saved_root = pr_surface.ROOT
|
|
saved_env = dict(os.environ)
|
|
try:
|
|
pr_surface.ROOT = repo
|
|
os.environ["GITHUB_EVENT_NAME"] = "pull_request"
|
|
os.environ["GITHUB_BASE_REF"] = "main"
|
|
assert pr_surface.changed_paths() == ["README.md"]
|
|
git(repo, "checkout", "-q", "-b", "rename-check", base)
|
|
git(repo, "mv", "README.md", "code.rs")
|
|
git(repo, "commit", "-qm", "rename")
|
|
assert set(pr_surface.changed_paths()) == {"README.md", "code.rs"}
|
|
os.environ.pop("GITHUB_BASE_REF")
|
|
try:
|
|
pr_surface.changed_paths()
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
raise AssertionError("missing base must fall back to full checks")
|
|
finally:
|
|
pr_surface.ROOT = saved_root
|
|
os.environ.clear()
|
|
os.environ.update(saved_env)
|
|
print("Toolkit PR change surface: documentation, headless, and fallback PASS")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|