diff --git a/silverback/_click_ext.py b/silverback/_click_ext.py index ebc6e2f7..dbe388a6 100644 --- a/silverback/_click_ext.py +++ b/silverback/_click_ext.py @@ -345,4 +345,11 @@ def bot_path_callback(ctx: click.Context, param: click.Parameter, path: str | No try: return import_from_string(path) except ImportFromStringError: - return import_from_string(f"bots.{path}") + try: + return import_from_string(f"bots.{path}") + except ModuleNotFoundError: + # This may happen if accidentally running `silverback run` + # with no bots arguments outside of your bots-project directory. + raise click.BadParameter( + "Nothing to run: No bot argument(s) given and no bots module found." + ) diff --git a/tests/test_cli.py b/tests/test_cli.py index 7fae51e7..43842316 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,19 @@ +def test_run_no_bots(cli, runner): + result = runner.invoke(cli, "run") + assert result.exit_code != 0 + expected = ( + "Usage: cli run [OPTIONS] [BOT]\n" + "Try 'cli run --help' for help.\n\n" + "Error: Invalid value for '[BOT]': " + "Nothing to run: No bot argument(s) given and no bots module found.\n" + ) + assert result.output == expected + + def test_run_verbosity(cli, runner): """ A test showing the verbosity option works. If it didn't work, the exit code would not be 0 here. """ - result = runner.invoke(cli, ["run", "--help", "--verbosity", "DEBUG"]) + result = runner.invoke(cli, ["run", "--help", "--verbosity", "DEBUG"], catch_exceptions=False) assert result.exit_code == 0