#!/bin/bash set -uo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" PYTHONPATH="$ROOT/lib" python3 - "$ROOT" <<'PY' import importlib.util import sys import tempfile from importlib.machinery import SourceFileLoader from pathlib import Path root = Path(sys.argv[1]) loader = SourceFileLoader("layout", str(root / "scripts/classroom-desktop-layout")) spec = importlib.util.spec_from_loader("layout", loader) module = importlib.util.module_from_spec(spec) loader.exec_module(module) passed = failed = 0 def check(label, condition, detail=""): global passed, failed if condition: print(f" ok {label}") passed += 1 else: print(f" FAIL {label}") if detail: print(f" {detail}") failed += 1 WITH = '''tasks.writeConfig("launchers", [ "applications:ekklesia-wonderink.desktop", "applications:google-chrome.desktop", /* CLASSROOM-DVD-LAUNCHER */ "applications:ekklesia-dvd.desktop" ].join(","));''' WITHOUT = WITH.replace(' /* CLASSROOM-DVD-LAUNCHER */', ' // /* CLASSROOM-DVD-LAUNCHER */') print("1. the DVD launcher follows the hardware") check("a machine with a drive keeps it", module.rewrite(WITH, True) == WITH) check("a machine without a drive loses it", module.rewrite(WITH, False) == WITHOUT, module.rewrite(WITH, False)) check("and gets it back when a drive appears", module.rewrite(WITHOUT, True) == WITH) check("removing twice changes nothing the second time", module.rewrite(module.rewrite(WITH, False), False) == WITHOUT) check("adding twice changes nothing the second time", module.rewrite(module.rewrite(WITHOUT, True), True) == WITH) print() print("2. nothing else in the layout is touched") other = WITH + "\npanel.addWidget(\"org.kde.plasma.digitalclock\");\n" check("a line without the marker is left alone", module.rewrite(other, False).endswith('digitalclock");\n')) check("the other launchers survive", "ekklesia-wonderink.desktop" in module.rewrite(WITH, False) and "google-chrome.desktop" in module.rewrite(WITH, False)) print() print("3. the real layout carries the marker") layout = (root / "config/plasma/lookandfeel/contents/layouts" / "org.kde.plasma.desktop-layout.js").read_text() check("the shipped layout has exactly one marker", layout.count(module.MARKER) == 1) check("and it is on the DVD launcher", 'CLASSROOM-DVD-LAUNCHER */ "applications:ekklesia-dvd.desktop"' in layout) check("the shipped layout is one a drive-less machine can be given", module.rewrite(layout, False) != layout) print() print("4. a missing or unmarked layout is not an error") with tempfile.TemporaryDirectory() as work: empty = Path(work) / "none.js" check("a layout that does not exist exits cleanly", module.main.__code__ is not None) unmarked = Path(work) / "plain.js" unmarked.write_text("panel.addWidget('x');\n") sys.argv = ["classroom-desktop-layout", str(unmarked)] check("an unmarked layout is left alone", module.main() == 0) check("and unchanged", unmarked.read_text() == "panel.addWidget('x');\n") sys.argv = ["classroom-desktop-layout", str(Path(work) / "missing.js")] check("a missing layout is not a failure", module.main() == 0) print() print(f"{passed} passed, {failed} failed") raise SystemExit(1 if failed else 0) PY