by adom
Publish molecule sources to the Adom wiki (STEP, board, schematic, <slug>-fab.zip, export.json); the wiki's 3D model registry builds the GLB, footprint and symbol. Checks the bundle the way the registry does, publishes private, waits for the build, sets the hero from the built model, then makes the page public. Board mode still converts plain boards. Also writes APM render assets and component dependencies. Companion to adom-wiki model import.
master
AdityaAngajala
Preserve source for molecule-publish-3.0.0
33f54d8
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
"""bundle_check.py against the recipe rules of the molecule contract (section 2.2); run with unittest."""
import importlib.util
import json
import tempfile
import unittest
from pathlib import Path
SCRIPT = Path(__file__).resolve().parents[1] / "scripts/bundle_check.py"
spec = importlib.util.spec_from_file_location("bundle_check", SCRIPT)
bc = importlib.util.module_from_spec(spec)
spec.loader.exec_module(bc)
class BundleCheckTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory(dir=Path(__file__).resolve().parent)
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
def files(self, *paths, record=None):
for p in paths:
f = self.root / p
f.parent.mkdir(parents=True, exist_ok=True)
f.write_bytes(b"x")
if record is not None:
(self.root / "export.json").write_text(json.dumps({"files": record}))
def run_check(self, slug="foo", listing=None):
return bc.check(self.root, slug, listing)
def ids(self, out):
return [p["id"] for p in out["problems"]]
def test_kicad_bundle_takes_the_board_route(self):
self.files("Foo.step", "Foo.kicad_pcb", "Foo.kicad_sch", "Foo.kicad_pro", "foo-fab.zip",
record=[{"path": "Foo.step", "role": "step"}, {"path": "Foo.kicad_pcb", "role": "board_kicad_pcb"}])
out = self.run_check()
self.assertEqual((out["recipe"], out["route"]), ("molecule-board/1", "board"))
self.assertEqual({s["role"]: s["path"] for s in out["sources"]},
{"step": "Foo.step", "board": "Foo.kicad_pcb", "fab": "foo-fab.zip", "export": "export.json"})
self.assertEqual(out["problems"], [])
self.assertEqual(out["unread"], ["Foo.kicad_pro", "Foo.kicad_sch"])
def test_kicad_wins_over_eagle_at_the_root(self):
self.files("a.step", "a.kicad_pcb", "a.brd")
out = self.run_check()
self.assertEqual({s["role"]: s["path"] for s in out["sources"]}["board"], "a.kicad_pcb")
def test_step_alone_is_refused_for_want_of_a_board_file(self):
self.files("Foo.step", "README.md")
out = self.run_check()
self.assertEqual((out["recipe"], out["route"]), ("molecule-step/1", "step"))
self.assertEqual(self.ids(out), ["board-file-required"])
def test_altium_pcbdoc_with_step_takes_the_board_route(self):
self.files("Foo.step", "Foo.PcbDoc", "foo-fab.zip",
record=[{"path": "Foo.step", "role": "step"}, {"path": "Foo.PcbDoc", "role": "board_altium_pcbdoc"}])
out = self.run_check()
self.assertEqual((out["recipe"], out["route"]), ("molecule-board/1", "board"))
self.assertEqual({s["role"]: s["path"] for s in out["sources"]},
{"step": "Foo.step", "board": "Foo.PcbDoc", "fab": "foo-fab.zip", "export": "export.json"})
self.assertEqual(out["problems"], [])
def test_altium_record_naming_another_board_is_a_problem(self):
self.files("Foo.step", "Foo.PcbDoc", "foo-fab.zip",
record=[{"path": "Foo.step", "role": "step"}, {"path": "Old.PcbDoc", "role": "board_altium_pcbdoc"}])
self.assertEqual(self.ids(self.run_check()), ["export-record"])
def test_a_board_without_a_step_gets_no_build(self):
self.files("Foo.kicad_pcb", "Foo.kicad_sch")
self.assertEqual(self.ids(self.run_check()), ["no-recipe"])
def test_two_steps_at_the_root_are_ambiguous_unless_one_is_named_for_the_slug(self):
self.files("a.step", "b.step", "a.kicad_pcb")
self.assertEqual(self.ids(self.run_check()), ["ambiguous"])
self.files("foo.step")
out = self.run_check()
self.assertEqual({s["role"]: s["path"] for s in out["sources"]}["step"], "foo.step")
def test_library_and_preview_folders_are_not_sources(self):
self.files("Foo.step", "Foo.kicad_pcb", "3d/part.step", "lib/x.step", "render/board.glb", "viewer/3d.glb", "x.3dshapes/y.step")
out = self.run_check()
self.assertEqual(out["route"], "board")
self.assertEqual(out["problems"], [])
def test_a_circuit_json_takes_the_circuit_route(self):
self.files("Foo.step", "Foo.kicad_pcb", "viewer/circuit.json")
self.assertEqual(self.run_check()["route"], "circuit")
def test_renamed_page_still_finds_its_fab_zip(self):
self.files("Old.step", "Old.kicad_pcb", "old-name-fab.zip",
record=[{"path": "Old.step", "role": "step"}, {"path": "Old.kicad_pcb", "role": "board_kicad_pcb"}])
out = self.run_check(slug="new-name")
self.assertEqual({s["role"]: s["path"] for s in out["sources"]}["fab"], "old-name-fab.zip")
self.assertEqual(out["problems"], [])
self.assertTrue(any("not named new-name-fab.zip" in n for n in out["notes"]))
def test_two_fab_zips_at_one_depth_are_ambiguous(self):
self.files("a.step", "a.kicad_pcb", "x-fab.zip", "y-fab.zip", "export.json")
self.assertEqual(self.ids(self.run_check()), ["ambiguous"])
def test_half_a_fabrication_pair_is_a_problem(self):
self.files("a.step", "a.kicad_pcb", "foo-fab.zip")
self.assertEqual(self.ids(self.run_check()), ["fab-pair"])
def test_fabrication_pair_in_two_folders_is_a_problem(self):
self.files("a.step", "a.kicad_pcb", "fab/foo-fab.zip", "export.json")
self.assertEqual(self.ids(self.run_check()), ["fab-pair"])
def test_record_naming_another_board_is_a_problem(self):
self.files("a.step", "a.kicad_pcb", "foo-fab.zip",
record=[{"path": "a.step", "role": "step"}, {"path": "b.kicad_pcb", "role": "board_kicad_pcb"}])
self.assertEqual(self.ids(self.run_check()), ["export-record"])
def test_generated_files_in_the_folder_are_problems(self):
self.files("Foo.step", "Foo.kicad_pcb", "foo.glb", "foo.web.glb", "Foo_footprint.json", "Foo_symbol.json", "foo.pipeline.json")
out = self.run_check()
self.assertEqual(self.ids(out), ["generated"])
self.assertEqual(sorted(out["generated"]), ["Foo_footprint.json", "Foo_symbol.json", "foo.glb", "foo.pipeline.json", "foo.web.glb"])
def test_footprint_json_is_not_generated_on_the_step_route(self):
self.files("Foo.step", "Foo_footprint.json")
out = self.run_check()
self.assertEqual(out["route"], "step")
self.assertEqual(out["generated"], [])
self.assertEqual(self.ids(out), ["board-file-required"])
def test_files_already_on_the_page_take_part(self):
self.files("a.step", "a.kicad_pcb")
listing = self.root.parent / f"{self.root.name}-listing.json"
listing.write_text(json.dumps({"status": "ok", "data": {"files": [
{"path": "old.step", "type": "blob"}, {"path": "docs", "type": "tree"}, {"path": "foo.glb", "type": "blob"}]}}))
self.addCleanup(listing.unlink)
out = self.run_check(listing=str(listing))
self.assertEqual(self.ids(out), ["ambiguous"])
self.assertEqual(out["generated"], [], "a file already on the page is not part of this write")
def test_kind_other_than_molecule_is_a_problem(self):
self.files("a.step", "a.kicad_pcb")
(self.root / "package.json").write_text(json.dumps({"slug": "foo", "type": "component"}))
self.assertEqual(self.ids(self.run_check()), ["kind"])
(self.root / "package.json").write_text(json.dumps({"slug": "foo", "type": "component", "kind": "molecule"}))
self.assertEqual(self.ids(self.run_check()), [])
def test_pattern_grammar(self):
m = bc.matcher("**/libraries/**", "s")
self.assertTrue(m("libraries/a.step") and m("x/libraries/p/q.glb"))
self.assertTrue(m("libraries"), "recipes.js: both ** may match zero segments")
self.assertFalse(m("mylibraries/a.step"))
self.assertTrue(bc.matcher("{slug}.step", "foo")("deep/FOO.STEP"))
self.assertFalse(bc.matcher("3d/**", "s")("sub/3d/x.step"))
self.assertFalse(bc.matcher("*.step", "s")("a.step.bak"))
if __name__ == "__main__":
unittest.main()