|
| 1 | +"""Run the production launcher with synthetic apps and command stand-ins.""" |
| 2 | +import ast |
| 3 | +import json |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +import tempfile |
| 9 | +import unittest |
| 10 | + |
| 11 | +ROOT = Path(__file__).resolve().parents[2] |
| 12 | + |
| 13 | + |
| 14 | +class KiCadOpenTest(unittest.TestCase): |
| 15 | + def setUp(self): |
| 16 | + self.tmp = tempfile.TemporaryDirectory(prefix="bp-kicad-open-") |
| 17 | + self.addCleanup(self.tmp.cleanup) |
| 18 | + self.home = Path(self.tmp.name) |
| 19 | + self.bin = self.home / "commands" |
| 20 | + self.bin.mkdir() |
| 21 | + self.runtime = self.home / "runtime with spaces" |
| 22 | + self.runtime.mkdir() |
| 23 | + self.record = self.home / "args.json" |
| 24 | + text = (ROOT / "src/server/kicad.bend").read_text() |
| 25 | + self.script = ast.literal_eval(re.search(r'def KiCad.open.script\(\) -> String:\n ("[^\n]+")', text)[1]) |
| 26 | + self.command("uname", "#!/bin/sh\nprintf '%s\\n' \"$TEST_PLATFORM\"\n") |
| 27 | + for name in ["open", "setsid"]: |
| 28 | + self.command(name, "#!/usr/bin/env python3\nimport json,os,sys\nopen(os.environ['TEST_RECORD'],'w').write(json.dumps(sys.argv[1:]))\n") |
| 29 | + |
| 30 | + def command(self, name, text): |
| 31 | + path = self.bin / name |
| 32 | + path.write_text(text) |
| 33 | + path.chmod(0o755) |
| 34 | + |
| 35 | + def run_launcher(self, platform, files): |
| 36 | + return subprocess.run(["sh", "-c", self.script, "sh", str(self.runtime), *files], |
| 37 | + env={**os.environ, "PATH": str(self.bin) + os.pathsep + os.environ["PATH"], |
| 38 | + "TEST_PLATFORM": platform, "TEST_RECORD": str(self.record)}, |
| 39 | + text=True, capture_output=True, timeout=5) |
| 40 | + |
| 41 | + def test_mac_routes_project_and_lone_editors(self): |
| 42 | + for files, app, target in [(["project with spaces.kicad_pro", "b", "s"], "KiCad.app", "project with spaces.kicad_pro"), |
| 43 | + (["", "board.kicad_pcb", "s"], "KiCad.app/Contents/Applications/pcbnew.app", "board.kicad_pcb"), |
| 44 | + (["", "", "sheet.kicad_sch"], "KiCad.app/Contents/Applications/eeschema.app", "sheet.kicad_sch")]: |
| 45 | + with self.subTest(app=app): |
| 46 | + (self.runtime / app).mkdir(parents=True, exist_ok=True) |
| 47 | + result = self.run_launcher("Darwin", files) |
| 48 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 49 | + self.assertEqual(json.loads(self.record.read_text()), ["-a", str(self.runtime / app), target]) |
| 50 | + |
| 51 | + def test_missing_package_and_project_refuse(self): |
| 52 | + for files, diagnostic in [(["p.kicad_pro", "", ""], "Backplane KiCad has no kicad"), |
| 53 | + (["", "", ""], "no KiCad project here")]: |
| 54 | + result = self.run_launcher("Darwin", files) |
| 55 | + self.assertNotEqual(result.returncode, 0) |
| 56 | + self.assertIn(diagnostic, result.stdout) |
| 57 | + self.assertFalse(self.record.exists()) |
| 58 | + |
| 59 | + def test_other_platform_launchers_and_exe_fallback(self): |
| 60 | + (self.runtime / "bin").mkdir() |
| 61 | + for platform, binary in [("Linux", "kicad"), ("MINGW64_NT", "kicad.exe")]: |
| 62 | + with self.subTest(platform=platform): |
| 63 | + path = self.runtime / "bin" / binary |
| 64 | + path.write_text("#!/bin/sh\nexit 0\n") |
| 65 | + path.chmod(0o755) |
| 66 | + result = self.run_launcher(platform, ["p.kicad_pro", "", ""]) |
| 67 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 68 | + self.assertEqual(json.loads(self.record.read_text()), ["-f", str(path), "p.kicad_pro"]) |
| 69 | + path.unlink() |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + unittest.main() |
0 commit comments