diff --git a/src/blueapi/cli/cli.py b/src/blueapi/cli/cli.py index 3cb1d494d..3c0af6e68 100644 --- a/src/blueapi/cli/cli.py +++ b/src/blueapi/cli/cli.py @@ -1,6 +1,5 @@ import json import logging -import sys from collections import deque from functools import wraps from pathlib import Path @@ -46,8 +45,7 @@ def main(ctx: click.Context, config: Path | None | tuple[Path, ...]) -> None: if path.exists(): config_loader.use_values_from_yaml(path) else: - print(f"Cannot find file: {path}") - sys.exit(1) + raise FileNotFoundError(f"Cannot find file: {path}") ctx.ensure_object(dict) loaded_config: ApplicationConfig = config_loader.load() @@ -110,7 +108,6 @@ def wrapper(*args, **kwargs): func(*args, **kwargs) except ConnectionError: print("Failed to establish connection to FastAPI server.") - return return wrapper @@ -144,8 +141,7 @@ def listen_to_events(obj: dict) -> None: StompMessagingTemplate.autoconfigured(config.stomp) ) else: - print("Message bus needs to be configured") - sys.exit(1) + raise RuntimeError("Message bus needs to be configured") def on_event( context: MessageContext, @@ -186,8 +182,9 @@ def run_plan( if config.stomp is not None: _message_template = StompMessagingTemplate.autoconfigured(config.stomp) else: - pprint("ERROR: Cannot run plans without Stomp configuration to track progress") - return + raise RuntimeError( + "Cannot run plans without Stomp configuration to track progress" + ) event_bus_client = EventBusClient(_message_template) finished_event: deque[WorkerEvent] = deque() @@ -280,7 +277,7 @@ def stop(obj: dict) -> None: """ client: BlueapiRestClient = obj["rest_client"] - print(client.cancel_current_task(state=WorkerState.STOPPING)) + pprint(client.cancel_current_task(state=WorkerState.STOPPING)) @controller.command(name="env") @@ -311,7 +308,8 @@ def env(obj: dict, reload: bool | None) -> None: print(deserialized) except BlueskyRemoteError: - sys.stderr.write("Failed to reload the environment") + pprint("Failed to reload the environment") + quit("Exiting the environment inspection") # Initialize a variable to keep track of the environment status environment_initialized = False diff --git a/tests/test_cli.py b/tests/test_cli.py index 199642303..5be26458d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -127,7 +127,6 @@ def test_get_plans_and_devices( def test_invalid_config_path_handling(runner: CliRunner): # test what happens if you pass an invalid config file... result = runner.invoke(main, ["-c", "non_existent.yaml"]) - assert result.output == "Cannot find file: non_existent.yaml\n" assert result.exit_code == 1 @@ -168,16 +167,18 @@ def test_config_passed_down_to_command_children( def test_invalid_stomp_config_for_listener(runner: CliRunner): result = runner.invoke(main, ["controller", "listen"]) - assert result.exit_code == 1 - assert result.output == "Message bus needs to be configured\n" + assert ( + isinstance(result.exception, RuntimeError) + and str(result.exception) == "Message bus needs to be configured" + ) def test_cannot_run_plans_without_stomp_config(runner: CliRunner): result = runner.invoke(main, ["controller", "run", "sleep", '{"time": 5}']) assert result.exit_code == 1 assert ( - "Cannot run plans without Stomp configuration to track progress" - in result.output + isinstance(result.exception, RuntimeError) + and str(result.exception) == "Message bus needs to be configured" )