|
| 1 | +"""End to end: build the example bundles and read the components back. |
| 2 | +
|
| 3 | +These tests need a working .NET runtime, so they are marked `dotnet` and skip |
| 4 | +themselves when there is none. Set `GHPYTHON_COMPONENTIZER_REQUIRE_DOTNET=1` to |
| 5 | +turn that skip into a failure, which is what CI does. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from ghpython_componentizer import run_componentizer |
| 15 | +from ghpython_componentizer.bundle import discover_source_bundles |
| 16 | + |
| 17 | +EXAMPLES = Path(__file__).resolve().parent.parent / "examples" / "cpy" |
| 18 | +PREFIX = "(TEST) " |
| 19 | +VERSION = "1.2.3" |
| 20 | + |
| 21 | +pytestmark = pytest.mark.dotnet |
| 22 | + |
| 23 | + |
| 24 | +def dotnet_is_available(): |
| 25 | + try: |
| 26 | + import clr # noqa: F401 |
| 27 | + except Exception: |
| 28 | + return False |
| 29 | + |
| 30 | + return True |
| 31 | + |
| 32 | + |
| 33 | +if not EXAMPLES.exists(): |
| 34 | + pytest.skip("example bundles are not part of the distribution", allow_module_level=True) |
| 35 | + |
| 36 | +if not dotnet_is_available() and os.environ.get("GHPYTHON_COMPONENTIZER_REQUIRE_DOTNET") != "1": |
| 37 | + pytest.skip("no .NET runtime available", allow_module_level=True) |
| 38 | + |
| 39 | + |
| 40 | +def read_component(path): |
| 41 | + """Read a .ghuser file back into the chunk it was serialized from.""" |
| 42 | + from ghpython_componentizer.ghio import ensure_ghio |
| 43 | + |
| 44 | + ensure_ghio() |
| 45 | + |
| 46 | + import System |
| 47 | + from GH_IO.Serialization import GH_LooseChunk |
| 48 | + |
| 49 | + chunk = GH_LooseChunk("UserObject") |
| 50 | + chunk.Deserialize_Binary(System.IO.File.ReadAllBytes(str(path))) |
| 51 | + |
| 52 | + return chunk |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(scope="module") |
| 56 | +def built(tmp_path_factory): |
| 57 | + """Build every example bundle once, and return the target folder.""" |
| 58 | + target = tmp_path_factory.mktemp("ghuser") |
| 59 | + run_componentizer(EXAMPLES, target, version=VERSION, prefix=PREFIX) |
| 60 | + |
| 61 | + return target |
| 62 | + |
| 63 | + |
| 64 | +def test_builds_one_component_per_bundle(built): |
| 65 | + expected = sorted(name + ".ghuser" for name in discover_source_bundles(str(EXAMPLES))) |
| 66 | + |
| 67 | + assert sorted(p.name for p in built.iterdir()) == expected |
| 68 | + |
| 69 | + |
| 70 | +def test_components_are_readable_by_gh_io(built): |
| 71 | + for bundle in discover_source_bundles(str(EXAMPLES)): |
| 72 | + metadata = json.loads((EXAMPLES / bundle / "metadata.json").read_text()) |
| 73 | + component = read_component(built / (bundle + ".ghuser")) |
| 74 | + |
| 75 | + assert component.GetString("Name") == PREFIX + metadata["name"] |
| 76 | + assert component.GetString("NickName") == metadata["nickname"] |
| 77 | + assert component.GetString("Category") == metadata["category"] |
| 78 | + assert component.GetString("SubCategory") == metadata["subcategory"] |
| 79 | + |
| 80 | + |
| 81 | +def test_components_carry_their_icon(built): |
| 82 | + for bundle in discover_source_bundles(str(EXAMPLES)): |
| 83 | + component = read_component(built / (bundle + ".ghuser")) |
| 84 | + |
| 85 | + icon = component.GetByteArray("Icon") |
| 86 | + assert icon.Length == (EXAMPLES / bundle / "icon.png").stat().st_size |
0 commit comments