Add contributors to INERT_SNAPSHOT_DIRS in scripts/bundles/payload.py so repository-root contributor attribution files (3,256 tiny files totaling ~0.1 MiB, read only by CI attribution scripts) no longer ship inside macOS, Linux, and Windows desktop payloads.
158 lines
8.0 KiB
Python
158 lines
8.0 KiB
Python
"""Shared bundle operations use real filesystem and entrypoint contracts."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
import pytest
|
|
|
|
from scripts.bundles.payload import INERT_SNAPSHOT_DIRS, relativize_links, snapshot
|
|
from scripts.build.inputs import project_entries
|
|
from scripts.build.agent import plant_surfaces
|
|
from scripts.build.launchers import posix_launcher
|
|
from scripts.bundles.desktop import release_version
|
|
|
|
|
|
def test_sealed_snapshot_keeps_the_whatsapp_bridge(tmp_path):
|
|
"""Desktop and Store payloads exclude scripts/ but still run the WhatsApp bridge."""
|
|
root = Path(__file__).resolve().parents[2]
|
|
destination = tmp_path / "agent"
|
|
snapshot(root, "HEAD", destination, exclude=INERT_SNAPSHOT_DIRS)
|
|
assert (destination / "scripts/whatsapp-bridge/bridge.js").is_file()
|
|
assert not (destination / "scripts/bundles/payload.py").exists()
|
|
|
|
|
|
def test_sealed_snapshot_excludes_contributors_tree(tmp_path):
|
|
"""CI-only repository-root contributors/ stays out of sealed payloads while runtime trees survive."""
|
|
repo = tmp_path / "repo"
|
|
repo.mkdir()
|
|
git = ["git", "-c", "user.name=Fixture", "-c", "user.email=fixture@example.test"]
|
|
subprocess.run(["git", "init", "-q", str(repo)], check=True)
|
|
(repo / "contributors/emails").mkdir(parents=True)
|
|
(repo / "contributors/emails/someone@example.test").write_text("someone\n", encoding="utf-8")
|
|
(repo / "hermes_cli").mkdir()
|
|
(repo / "hermes_cli/main.py").write_text("VALUE = 1\n", encoding="utf-8")
|
|
(repo / "scripts/whatsapp-bridge").mkdir(parents=True)
|
|
(repo / "scripts/whatsapp-bridge/bridge.js").write_text("// bridge\n", encoding="utf-8")
|
|
subprocess.run([*git, "add", "."], cwd=repo, check=True, capture_output=True)
|
|
subprocess.run([*git, "commit", "-qm", "fixture"], cwd=repo, check=True)
|
|
|
|
destination = tmp_path / "agent"
|
|
snapshot(repo, "HEAD", destination, exclude=INERT_SNAPSHOT_DIRS)
|
|
|
|
assert not (destination / "contributors").exists()
|
|
assert (destination / "hermes_cli/main.py").is_file()
|
|
assert (destination / "scripts/whatsapp-bridge/bridge.js").is_file()
|
|
# Source checkout is never mutated by snapshotting.
|
|
assert (repo / "contributors/emails/someone@example.test").is_file()
|
|
|
|
|
|
def test_release_version_comes_from_the_release_identity_not_the_checkout(tmp_path):
|
|
(tmp_path / "pyproject.toml").write_text('[project]\nversion="0.0.0"\n', encoding="utf-8")
|
|
assert release_version(tmp_path, "v1.2.3") == "1.2.3"
|
|
assert release_version(tmp_path, "v1.2.4") == "1.2.4"
|
|
|
|
|
|
def test_snapshot_carries_revision_bytes_regardless_of_builder_eol_config(tmp_path):
|
|
# Git for Windows ships core.autocrlf=true system-wide; set it on the
|
|
# fixture repo so every platform exercises the same builder config.
|
|
repo = tmp_path / "repo"
|
|
repo.mkdir()
|
|
git = ["git", "-c", "user.name=Fixture", "-c", "user.email=fixture@example.test"]
|
|
subprocess.run(["git", "init", "-q", str(repo)], check=True)
|
|
subprocess.run(["git", "config", "core.autocrlf", "true"], cwd=repo, check=True)
|
|
(repo / ".gitattributes").write_bytes(b"*.ps1 text eol=crlf\n")
|
|
(repo / "plain.json").write_bytes(b'{\n"a": 1\n}\n')
|
|
(repo / "script.ps1").write_bytes(b"one\ntwo\n")
|
|
# Archived by the second, runtime-tree pass after scripts/ is excluded.
|
|
(repo / "scripts/whatsapp-bridge").mkdir(parents=True)
|
|
(repo / "scripts/whatsapp-bridge/notes.txt").write_bytes(b"one\ntwo\n")
|
|
subprocess.run([*git, "add", "."], cwd=repo, check=True, capture_output=True)
|
|
subprocess.run([*git, "commit", "-qm", "fixture"], cwd=repo, check=True)
|
|
out = tmp_path / "out"
|
|
snapshot(repo, "HEAD", out, exclude=INERT_SNAPSHOT_DIRS)
|
|
assert (out / "plain.json").read_bytes() == b'{\n"a": 1\n}\n'
|
|
assert (out / "scripts/whatsapp-bridge/notes.txt").read_bytes() == b"one\ntwo\n"
|
|
# Committed eol attributes still apply: only builder config is ignored.
|
|
assert (out / "script.ps1").read_bytes() == b"one\r\ntwo\r\n"
|
|
|
|
|
|
def test_wrapper_rejects_unresolved_template_fields(tmp_path, monkeypatch):
|
|
from scripts.build import launchers
|
|
|
|
template = tmp_path / "launcher_wrapper.py"
|
|
template.write_text('entry = "__HERMES_ENTRY_MODULE__"\nmissing = "__HERMES_NEW__"\n', encoding="utf-8")
|
|
monkeypatch.setattr(launchers, "__file__", str(tmp_path / "payload.py"))
|
|
with pytest.raises(ValueError, match="__HERMES_NEW__"):
|
|
launchers.render_wrapper("entry:run", "../app", "../venv/Lib/site-packages")
|
|
|
|
|
|
@pytest.mark.platforms("posix")
|
|
def test_relocation_preserves_sibling_and_framework_links(tmp_path):
|
|
root = tmp_path / "payload"
|
|
store = root / "tools/python/bin"
|
|
venv = root / "venv/bin"
|
|
store.mkdir(parents=True)
|
|
venv.mkdir(parents=True)
|
|
(store / "python3").write_text("interpreter", encoding="utf-8")
|
|
(venv / "python").symlink_to("/builder/tools/python/bin/python3")
|
|
(venv / "python3").symlink_to("python")
|
|
framework = root / "tools/framework"
|
|
framework.symlink_to("python/bin/python3")
|
|
assert relativize_links(root) == 1
|
|
assert (venv / "python3").read_text(encoding="utf-8-sig") == "interpreter"
|
|
assert os.readlink(venv / "python3") == "python"
|
|
assert os.readlink(framework) == "python/bin/python3"
|
|
assert relativize_links(root) == 0
|
|
(venv / "bad").symlink_to("/usr/bin/python")
|
|
with pytest.raises(ValueError, match="escapes payload"):
|
|
relativize_links(root)
|
|
|
|
|
|
@pytest.mark.platforms("posix")
|
|
@pytest.mark.parametrize("target", ["linux-x64", "linux-arm64-bionic"])
|
|
def test_both_launchers_keep_active_home_and_call_declared_function(tmp_path, target):
|
|
root = tmp_path / "payload with spaces"
|
|
(root / "bin").mkdir(parents=True)
|
|
(root / "app").mkdir()
|
|
(root / "python").symlink_to(sys.executable)
|
|
(root / "app/entry.py").write_text("import json,os,sys\ndef run():\n print(json.dumps([os.environ['HERMES_HOME'],sys.argv[1:]])); return 7\n", encoding="utf-8")
|
|
launcher = root / "bin/custom"
|
|
launcher.write_text(posix_launcher("custom", "entry:run", python="python", repo="app", site="deps", target=target), encoding="utf-8")
|
|
result = subprocess.run(["sh", str(launcher), "two words", "$(nope)", ""], cwd=tmp_path,
|
|
env={**os.environ, "HERMES_HOME": str(tmp_path / "custom/profiles/memory")}, capture_output=True, text=True, check=False)
|
|
assert result.returncode == 7, result.stderr
|
|
assert json.loads(result.stdout) == [str(tmp_path / "custom/profiles/memory"), ["two words", "$(nope)", ""]]
|
|
|
|
|
|
@pytest.mark.platforms("posix")
|
|
def test_launcher_runs_a_named_module_like_the_store_launcher(tmp_path):
|
|
"""A macOS bundle's gateway service runs the launchd stderr wrapper through the app's own launcher."""
|
|
root = tmp_path / "payload with spaces"
|
|
(root / "bin").mkdir(parents=True)
|
|
(root / "app").mkdir()
|
|
(root / "python").symlink_to(sys.executable)
|
|
(root / "app/entry.py").write_text("def run():\n print('entry point'); return 3\n", encoding="utf-8")
|
|
(root / "app/probe.py").write_text("import json, sys\nprint(json.dumps([__name__, sys.argv[1:]])); sys.exit(7)\n",
|
|
encoding="utf-8")
|
|
launcher = root / "bin/custom"
|
|
launcher.write_text(posix_launcher("custom", "entry:run", python="python", repo="app", site="deps",
|
|
target="darwin-arm64"), encoding="utf-8")
|
|
|
|
def run(*args):
|
|
return subprocess.run(["sh", str(launcher), *args], cwd=tmp_path, capture_output=True, text=True, check=False)
|
|
|
|
result = run("--run-module", "probe", "two words", "$(nope)", "")
|
|
assert result.returncode == 7, result.stdout + result.stderr
|
|
assert json.loads(result.stdout) == ["__main__", ["two words", "$(nope)", ""]]
|
|
# The name is spliced into Python source: only a dotted module name gets that far.
|
|
for name in ("", "probe; import os", "probe')", "1probe", "../probe"):
|
|
result = run("--run-module", name)
|
|
assert (result.returncode, result.stdout) == (2, ""), name
|
|
assert "--run-module needs a module" in result.stderr
|
|
assert run("--run-module").returncode == 2
|