def test_build_dispatcher_classic_hooks_linking_charm_replaced(tmp_path, emitter): """Hooks that are just a symlink to the entrypoint are replaced.""" metadata = tmp_path / CHARM_METADATA metadata.write_text("name: crazycharm") build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() # simple source code src_dir = build_dir / "src" src_dir.mkdir() built_charm_script = src_dir / "charm.py" with built_charm_script.open("wb") as fh: fh.write(b"all the magic") # a test hook, just a symlink to the charm built_hooks_dir = build_dir / "hooks" built_hooks_dir.mkdir() test_hook = built_hooks_dir / "somehook" test_hook.symlink_to(built_charm_script) included_dispatcher = build_dir / DISPATCH_FILENAME builder = CharmBuilder( charmdir=tmp_path, builddir=build_dir, entrypoint=pathlib.Path("whatever"), ) builder.handle_dispatcher(built_charm_script) # the test hook is still there and a symlink, but now pointing to the dispatcher assert test_hook.is_symlink() assert test_hook.resolve() == included_dispatcher expected = "Replacing existing hook 'somehook' as it's a symlink to the entrypoint" emitter.assert_trace(expected)
def test_build_dispatcher_classic_hooks_mandatory_respected(tmp_path): """The already included mandatory classic hooks are left untouched.""" metadata = tmp_path / CHARM_METADATA metadata.write_text("name: crazycharm") build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() built_hooks_dir = build_dir / "hooks" built_hooks_dir.mkdir() test_hook = built_hooks_dir / "testhook" with test_hook.open("wb") as fh: fh.write(b"abc") linked_entrypoint = build_dir / "somestuff.py" builder = CharmBuilder( charmdir=tmp_path, builddir=build_dir, entrypoint=pathlib.Path("whatever"), ) with patch("charmcraft.charm_builder.MANDATORY_HOOK_NAMES", {"testhook"}): builder.handle_dispatcher(linked_entrypoint) with test_hook.open("rb") as fh: assert fh.read() == b"abc"
def test_build_dispatcher_modern_dispatch_respected(tmp_path): """The already included dispatcher script is left untouched.""" metadata = tmp_path / CHARM_METADATA metadata.write_text("name: crazycharm") build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() already_present_dispatch = build_dir / DISPATCH_FILENAME with already_present_dispatch.open("wb") as fh: fh.write(b"abc") builder = CharmBuilder( charmdir=tmp_path, builddir=build_dir, entrypoint=pathlib.Path("whatever"), ) builder.handle_dispatcher("whatever") with already_present_dispatch.open("rb") as fh: assert fh.read() == b"abc"
def test_build_dispatcher_modern_dispatch_created(tmp_path): """The dispatcher script is properly built.""" metadata = tmp_path / CHARM_METADATA metadata.write_text("name: crazycharm") build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / "somestuff.py" builder = CharmBuilder( charmdir=tmp_path, builddir=build_dir, entrypoint=pathlib.Path("whatever"), ) builder.handle_dispatcher(linked_entrypoint) included_dispatcher = build_dir / DISPATCH_FILENAME with included_dispatcher.open("rt", encoding="utf8") as fh: dispatcher_code = fh.read() assert dispatcher_code == DISPATCH_CONTENT.format(entrypoint_relative_path="somestuff.py")
def test_build_dispatcher_classic_hooks_mandatory_created(tmp_path): """The mandatory classic hooks are implemented ok if not present.""" metadata = tmp_path / CHARM_METADATA metadata.write_text("name: crazycharm") build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / "somestuff.py" included_dispatcher = build_dir / DISPATCH_FILENAME builder = CharmBuilder( charmdir=tmp_path, builddir=build_dir, entrypoint=pathlib.Path("whatever"), ) with patch("charmcraft.charm_builder.MANDATORY_HOOK_NAMES", {"testhook"}): builder.handle_dispatcher(linked_entrypoint) test_hook = build_dir / "hooks" / "testhook" assert test_hook.is_symlink() assert test_hook.resolve() == included_dispatcher real_link = os.readlink(str(test_hook)) assert real_link == os.path.join("..", DISPATCH_FILENAME)