def test_build_dispatcher_classic_hooks_mandatory_respected(tmp_path, config): """The already included mandatory classic hooks are left untouched.""" 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 = Builder( { "from": tmp_path, "entrypoint": "whatever", "requirement": [], }, config, ) with patch("charmcraft.commands.build.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_classic_hooks_linking_charm_replaced( tmp_path, caplog): """Hooks that are just a symlink to the entrypoint are replaced.""" caplog.set_level(logging.DEBUG, logger="charmcraft") 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 = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) 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" assert expected in [rec.message for rec in caplog.records]
def test_build_dispatcher_modern_dispatch_respected(tmp_path): """The already included dispatcher script is left untouched.""" 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 = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) 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.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / 'somestuff.py' builder = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) 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_modern_dispatch_respected(tmp_path): """The already present dispatcher script is properly transferred.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() already_present_dispatch = tmp_path / DISPATCH_FILENAME already_present_dispatch.touch() builder = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) builder.handle_dispatcher('whatever') included_dispatcher = build_dir / DISPATCH_FILENAME assert included_dispatcher.is_symlink() assert included_dispatcher.resolve() == already_present_dispatch
def test_build_dispatcher_classic_hooks_mandatory_created(tmp_path): """The mandatory classic hooks are implemented ok if not present.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / 'somestuff.py' included_dispatcher = build_dir / DISPATCH_FILENAME builder = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) with patch('charmcraft.commands.build.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
def test_build_dispatcher_modern_dispatch_respected(tmp_path, config): """The already included dispatcher script is left untouched.""" 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 = Builder( { "from": tmp_path, "entrypoint": "whatever", "requirement": [], }, config, ) 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, config): """The dispatcher script is properly built.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / "somestuff.py" builder = Builder( { "from": tmp_path, "entrypoint": "whatever", "requirement": [], }, config, ) 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_respected(tmp_path): """The already included mandatory classic hooks are left untouched.""" 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 = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) with patch('charmcraft.commands.build.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_classic_hooks_mandatory_respected(tmp_path): """The already present mandatory classic hooks are properly transferred.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() charm_hooks_dir = tmp_path / 'hooks' charm_hooks_dir.mkdir() charm_test_hook = charm_hooks_dir / 'testhook' charm_test_hook.touch() linked_entrypoint = build_dir / 'somestuff.py' builder = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) with patch('charmcraft.commands.build.MANDATORY_HOOK_NAMES', {'testhook'}): builder.handle_dispatcher(linked_entrypoint) test_hook = build_dir / 'hooks' / 'testhook' assert test_hook.is_symlink() assert test_hook.resolve() == charm_test_hook
def test_build_dispatcher_classic_hooks_mandatory_created(tmp_path, config): """The mandatory classic hooks are implemented ok if not present.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() linked_entrypoint = build_dir / "somestuff.py" included_dispatcher = build_dir / DISPATCH_FILENAME builder = Builder( { "from": tmp_path, "entrypoint": "whatever", "requirement": [], }, config, ) with patch("charmcraft.commands.build.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)
def test_build_dispatcher_classic_hooks_linking_charm_replaced( tmp_path, caplog): """Hooks that are just a symlink to the entrypoint are kept but replaced.""" caplog.set_level(logging.DEBUG, logger="charmcraft") build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() # simple source code src_dir = tmp_path / 'src' src_dir.mkdir() charm_script = src_dir / 'charm.py' with charm_script.open('wb') as fh: fh.write(b'all the magic') # a test hook, just a symlink to the charm charm_hooks_dir = tmp_path / 'hooks' charm_hooks_dir.mkdir() charm_test_hook = charm_hooks_dir / 'somehook' charm_test_hook.symlink_to(charm_script) linked_entrypoint = build_dir / 'somestuff.py' included_dispatcher = build_dir / DISPATCH_FILENAME builder = Builder({ 'from': tmp_path, 'entrypoint': charm_script, 'requirement': [], }) builder.handle_dispatcher(linked_entrypoint) test_hook = build_dir / 'hooks' / 'somehook' assert test_hook.is_symlink() assert test_hook.resolve() == included_dispatcher expected = "Ignoring existing hook 'somehook' as it's a symlink to the entrypoint" assert expected in [rec.message for rec in caplog.records]
def test_build_dispatcher_classic_hooks_whatever_respected(tmp_path): """Any already present stuff in hooks is respected.""" build_dir = tmp_path / BUILD_DIRNAME build_dir.mkdir() charm_hooks_dir = tmp_path / 'hooks' charm_hooks_dir.mkdir() charm_test_extra_stuff = charm_hooks_dir / 'extra-stuff' charm_test_extra_stuff.touch() linked_entrypoint = build_dir / 'somestuff.py' builder = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) with patch('charmcraft.commands.build.MANDATORY_HOOK_NAMES', {'testhook'}): # no 'extra-stuff' builder.handle_dispatcher(linked_entrypoint) test_stuff = build_dir / 'hooks' / 'extra-stuff' assert test_stuff.is_symlink() assert test_stuff.resolve() == charm_test_extra_stuff