Blacksite/scripts/editor/test_build_progress.py

132 lines
4.7 KiB
Python
Executable File

#!/usr/bin/env python3
import importlib.util
from pathlib import Path
import unittest
MODULE_PATH = Path(__file__).with_name("build_progress.py")
LAUNCHER_PATH = Path(__file__).with_name("blacksite-editor")
SPEC = importlib.util.spec_from_file_location("build_progress", MODULE_PATH)
assert SPEC is not None and SPEC.loader is not None
BUILD_PROGRESS = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(BUILD_PROGRESS)
class BuildProgressTests(unittest.TestCase):
def test_desktop_wrapper_releases_the_build_lock_before_editor_exec(self) -> None:
wrapper = LAUNCHER_PATH.read_text(encoding="utf-8")
self.assertIn('exec 9>"$STATE_DIR/build.lock"', wrapper)
self.assertNotIn('exec 9>"$STATE_DIR/launch.lock"', wrapper)
unlock = wrapper.index("flock -u 9")
close = wrapper.index("exec 9>&-")
editor_exec = wrapper.index('exec "$binary"')
self.assertLess(unlock, close)
self.assertLess(close, editor_exec)
def test_desktop_wrapper_builds_before_focusing_an_existing_editor(self) -> None:
wrapper = LAUNCHER_PATH.read_text(encoding="utf-8")
capture_existing = wrapper.index('existing_pid="$(find_existing || true)"')
build = wrapper.index("if ! build_current_editor")
existing_handoff = wrapper.index(
'if [[ -n "$existing_pid" ]] && kill -0 "$existing_pid"'
)
self.assertLess(capture_existing, build)
self.assertLess(build, existing_handoff)
self.assertIn("running_binary_is_current", wrapper)
self.assertIn("Blacksite update ready", wrapper)
self.assertNotIn("if ! $build_only && focus_existing", wrapper)
def test_build_commands_use_the_managed_default_feature_lane(self) -> None:
cargo_lane = Path("scripts/codex/cargo_lane.py")
metadata = BUILD_PROGRESS.cargo_metadata(
cargo_lane, "dev", "x86_64-unknown-linux-gnu"
)
build = BUILD_PROGRESS.cargo_build(cargo_lane, "dev")
self.assertEqual(
metadata[:7],
[
BUILD_PROGRESS.sys.executable,
str(cargo_lane),
"exec",
"dev",
"--",
"cargo",
"metadata",
],
)
self.assertIn("--filter-platform", metadata)
self.assertIn("--bins", build)
self.assertNotIn("--features", metadata)
self.assertNotIn("--features", build)
def test_editor_package_closure_includes_normal_and_build_edges(self) -> None:
metadata = {
"packages": [
{"id": "editor-id", "name": "editor"},
{"id": "normal-id", "name": "normal"},
{"id": "build-id", "name": "build"},
{"id": "dev-id", "name": "dev"},
],
"resolve": {
"nodes": [
{
"id": "editor-id",
"deps": [
{
"pkg": "normal-id",
"dep_kinds": [{"kind": None}],
},
{
"pkg": "build-id",
"dep_kinds": [{"kind": "build"}],
},
{
"pkg": "dev-id",
"dep_kinds": [{"kind": "dev"}],
},
],
},
{"id": "normal-id", "deps": []},
{"id": "build-id", "deps": []},
{"id": "dev-id", "deps": []},
]
},
}
self.assertEqual(
BUILD_PROGRESS.editor_package_closure(
BUILD_PROGRESS.json.dumps(metadata)
),
{"editor-id", "normal-id", "build-id"},
)
def test_completed_crate_counts_each_package_once(self) -> None:
completed: set[str] = set()
message = {
"reason": "compiler-artifact",
"package_id": "path+file:///workspace#editor@0.1.0",
"target": {"name": "editor"},
}
self.assertEqual(
BUILD_PROGRESS.completed_crate(message, completed), (True, "editor")
)
self.assertEqual(
BUILD_PROGRESS.completed_crate(message, completed), (False, "")
)
def test_non_artifact_does_not_advance_progress(self) -> None:
completed: set[str] = set()
self.assertEqual(
BUILD_PROGRESS.completed_crate({"reason": "build-script-executed"}, completed),
(False, ""),
)
self.assertFalse(completed)
if __name__ == "__main__":
unittest.main()