From 675ec7e1d89aa65ce6dbca0938a89b4788db1045 Mon Sep 17 00:00:00 2001 From: Bartek Sokorski Date: Sat, 19 Nov 2022 00:38:03 +0100 Subject: [PATCH] Refactor exceptions into errors as per PEP8 guidelines --- src/cleo/application.py | 26 ++++----- src/cleo/color.py | 6 +- src/cleo/commands/base_command.py | 4 +- .../descriptors/application_description.py | 4 +- src/cleo/events/console_error_event.py | 4 +- src/cleo/exceptions/__init__.py | 58 ++++++++++++------- src/cleo/formatters/formatter.py | 4 +- src/cleo/formatters/style_stack.py | 4 +- src/cleo/io/inputs/argument.py | 6 +- src/cleo/io/inputs/argv_input.py | 16 ++--- src/cleo/io/inputs/definition.py | 12 ++-- src/cleo/io/inputs/input.py | 14 ++--- src/cleo/io/inputs/option.py | 14 ++--- src/cleo/loaders/factory_command_loader.py | 4 +- src/cleo/ui/choice_question.py | 8 +-- src/cleo/ui/ui.py | 8 +-- tests/io/inputs/test_argument.py | 6 +- tests/io/inputs/test_option.py | 12 ++-- tests/loaders/test_factory_command_loader.py | 4 +- tests/test_application.py | 16 ++--- tests/ui/test_choice_question.py | 6 +- 21 files changed, 127 insertions(+), 109 deletions(-) diff --git a/src/cleo/application.py b/src/cleo/application.py index 77caecd4..33749998 100644 --- a/src/cleo/application.py +++ b/src/cleo/application.py @@ -18,11 +18,11 @@ from cleo.events.console_events import ERROR from cleo.events.console_events import TERMINATE from cleo.events.console_terminate_event import ConsoleTerminateEvent -from cleo.exceptions import CleoException -from cleo.exceptions import CleoSimpleException -from cleo.exceptions import CommandNotFoundException -from cleo.exceptions import LogicException -from cleo.exceptions import NamespaceNotFoundException +from cleo.exceptions import CleoCommandNotFoundError +from cleo.exceptions import CleoError +from cleo.exceptions import CleoLogicError +from cleo.exceptions import CleoNamespaceNotFoundError +from cleo.exceptions import CleoUserError from cleo.io.inputs.argument import Argument from cleo.io.inputs.argv_input import ArgvInput from cleo.io.inputs.definition import Definition @@ -186,7 +186,7 @@ def add(self, command: Command) -> Command | None: return None if not command.name: - raise LogicException( + raise CleoLogicError( f'The command "{command.__class__.__name__}" cannot have an empty name' ) @@ -201,11 +201,11 @@ def get(self, name: str) -> Command: self._init() if not self.has(name): - raise CommandNotFoundException(name) + raise CleoCommandNotFoundError(name) if name not in self._commands: # The command was registered in a different name in the command loader - raise CommandNotFoundException(name) + raise CleoCommandNotFoundError(name) command = self._commands[name] @@ -261,7 +261,7 @@ def find_namespace(self, namespace: str) -> str: all_namespaces = self.get_namespaces() if namespace not in all_namespaces: - raise NamespaceNotFoundException(namespace, all_namespaces) + raise CleoNamespaceNotFoundError(namespace, all_namespaces) return namespace @@ -279,7 +279,7 @@ def find(self, name: str) -> Command: name for name, command in self._commands.items() if not command.hidden ] - raise CommandNotFoundException(name, all_commands) + raise CleoCommandNotFoundError(name, all_commands) def all(self, namespace: str | None = None) -> dict[str, Command]: self._init() @@ -370,7 +370,7 @@ def _run(self, io: IO) -> int: # Errors must be ignored, full binding/validation # happens later when the command is known. - with suppress(CleoException): + with suppress(CleoError): # Makes ArgvInput.first_argument() able to # distinguish an option from an argument. io.input.bind(input_definition) @@ -442,7 +442,7 @@ def _run_command(self, command: Command, io: IO) -> int: try: command.merge_application_definition() io.input.bind(command.definition) - except CleoException: + except CleoError: # Ignore invalid option/arguments for now, # to allow the listeners to customize the definition pass @@ -498,7 +498,7 @@ def render_error(self, error: Exception, io: IO) -> None: trace = ExceptionTrace( error, solution_provider_repository=self._solution_provider_repository ) - simple = not io.is_verbose() or isinstance(error, CleoSimpleException) + simple = not io.is_verbose() or isinstance(error, CleoUserError) trace.render(io.error_output, simple) def _configure_io(self, io: IO) -> None: diff --git a/src/cleo/color.py b/src/cleo/color.py index 2e1e77ef..6ddf6a78 100644 --- a/src/cleo/color.py +++ b/src/cleo/color.py @@ -2,7 +2,7 @@ import os -from cleo.exceptions import ValueException +from cleo.exceptions import CleoValueError class Color: @@ -107,14 +107,14 @@ def _parse_color(self, color: str, background: bool) -> str: color = color[0] * 2 + color[1] * 2 + color[2] * 2 if len(color) != 6: - raise ValueException(f'"{color}" is an invalid color') + raise CleoValueError(f'"{color}" is an invalid color') return ("4" if background else "3") + self._convert_hex_color_to_ansi( int(color, 16) ) if color not in self.COLORS: - raise ValueException( + raise CleoValueError( f'"{color}" is an invalid color.' f' It must be one of {", ".join(self.COLORS.keys())}' ) diff --git a/src/cleo/commands/base_command.py b/src/cleo/commands/base_command.py index 006c0b4f..29cf21a9 100644 --- a/src/cleo/commands/base_command.py +++ b/src/cleo/commands/base_command.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING -from cleo.exceptions import CleoException +from cleo.exceptions import CleoError from cleo.io.inputs.definition import Definition @@ -102,7 +102,7 @@ def run(self, io: IO) -> int: try: io.input.bind(self.definition) - except CleoException: + except CleoError: if not self._ignore_validation_errors: raise diff --git a/src/cleo/descriptors/application_description.py b/src/cleo/descriptors/application_description.py index 7b95a027..a40e4f1e 100644 --- a/src/cleo/descriptors/application_description.py +++ b/src/cleo/descriptors/application_description.py @@ -3,7 +3,7 @@ from collections import defaultdict from typing import TYPE_CHECKING -from cleo.exceptions import CommandNotFoundException +from cleo.exceptions import CleoCommandNotFoundError if TYPE_CHECKING: @@ -43,7 +43,7 @@ def command(self, name: str) -> Command: return self._commands[name] if name in self._aliases: return self._aliases[name] - raise CommandNotFoundException(name) + raise CleoCommandNotFoundError(name) def _inspect_application(self) -> None: namespace = None diff --git a/src/cleo/events/console_error_event.py b/src/cleo/events/console_error_event.py index b992478f..c267d87b 100644 --- a/src/cleo/events/console_error_event.py +++ b/src/cleo/events/console_error_event.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING from cleo.events.console_event import ConsoleEvent -from cleo.exceptions import CleoException +from cleo.exceptions import CleoError if TYPE_CHECKING: @@ -31,7 +31,7 @@ def exit_code(self) -> int: if self._exit_code is not None: return self._exit_code - if isinstance(self._error, CleoException) and self._error.exit_code is not None: + if isinstance(self._error, CleoError) and self._error.exit_code is not None: return self._error.exit_code return 1 diff --git a/src/cleo/exceptions/__init__.py b/src/cleo/exceptions/__init__.py index ff61de68..40677c4c 100644 --- a/src/cleo/exceptions/__init__.py +++ b/src/cleo/exceptions/__init__.py @@ -3,42 +3,56 @@ from cleo._utils import find_similar_names -class CleoException(Exception): +class CleoError(Exception): + """ + Base Cleo exception. + """ exit_code: int | None = None -class CleoSimpleException(Exception): +class CleoLogicError(CleoError): + """ + Raised when there is error in command arguments + and/or options configuration logic. + """ - pass +class CleoRuntimeError(CleoError): + """ + Raised when command is called with invalid options or arguments. + """ -class LogicException(CleoException): - pass +class CleoValueError(CleoError): + """ + Raised when wrong value was given to Cleo components. + """ -class RuntimeException(CleoException): +class CleoNoSuchOptionError(CleoError): + """ + Raised when command does not have given option. + """ - pass +class CleoUserError(CleoError): + """ + Base exception for user errors. + """ -class ValueException(CleoException): - pass +class CleoMissingArgumentsError(CleoUserError): + """ + Raised when called command was not given required arguments. + """ -class MissingArgumentsException(CleoSimpleException): +class CleoCommandNotFoundError(CleoUserError): + """ + Raised when called command does not exist. + """ - pass - - -class NoSuchOptionException(CleoException): - - pass - - -class CommandNotFoundException(CleoSimpleException): def __init__(self, name: str, commands: list[str] | None = None) -> None: message = f'The command "{name}" does not exist.' @@ -56,7 +70,11 @@ def __init__(self, name: str, commands: list[str] | None = None) -> None: super().__init__(message) -class NamespaceNotFoundException(CleoSimpleException): +class CleoNamespaceNotFoundError(CleoUserError): + """ + Raised when called namespace has no commands. + """ + def __init__(self, name: str, namespaces: list[str] | None = None) -> None: message = f'There are no commands in the "{name}" namespace.' diff --git a/src/cleo/formatters/formatter.py b/src/cleo/formatters/formatter.py index 6fe010c8..18a81059 100644 --- a/src/cleo/formatters/formatter.py +++ b/src/cleo/formatters/formatter.py @@ -2,7 +2,7 @@ import re -from cleo.exceptions import ValueException +from cleo.exceptions import CleoValueError from cleo.formatters.style import Style from cleo.formatters.style_stack import StyleStack @@ -71,7 +71,7 @@ def has_style(self, name: str) -> bool: def style(self, name: str) -> Style: if not self.has_style(name): - raise ValueException(f'Undefined style: "{name}"') + raise CleoValueError(f'Undefined style: "{name}"') return self._styles[name] diff --git a/src/cleo/formatters/style_stack.py b/src/cleo/formatters/style_stack.py index f4037148..1628f343 100644 --- a/src/cleo/formatters/style_stack.py +++ b/src/cleo/formatters/style_stack.py @@ -1,6 +1,6 @@ from __future__ import annotations -from cleo.exceptions import ValueException +from cleo.exceptions import CleoValueError from cleo.formatters.style import Style @@ -38,4 +38,4 @@ def pop(self, style: Style | None = None) -> Style: return stacked_style - raise ValueException("Invalid nested tag found") + raise CleoValueError("Invalid nested tag found") diff --git a/src/cleo/io/inputs/argument.py b/src/cleo/io/inputs/argument.py index c0cb4369..237bca9d 100644 --- a/src/cleo/io/inputs/argument.py +++ b/src/cleo/io/inputs/argument.py @@ -2,7 +2,7 @@ from typing import Any -from cleo.exceptions import LogicException +from cleo.exceptions import CleoLogicError class Argument: @@ -46,13 +46,13 @@ def is_list(self) -> bool: def set_default(self, default: Any | None = None) -> None: if self._required and default is not None: - raise LogicException("Cannot set a default value for required arguments") + raise CleoLogicError("Cannot set a default value for required arguments") if self._is_list: if default is None: default = [] elif not isinstance(default, list): - raise LogicException( + raise CleoLogicError( "A default value for a list argument must be a list" ) diff --git a/src/cleo/io/inputs/argv_input.py b/src/cleo/io/inputs/argv_input.py index d53d77d0..3705f72f 100644 --- a/src/cleo/io/inputs/argv_input.py +++ b/src/cleo/io/inputs/argv_input.py @@ -5,8 +5,8 @@ from typing import TYPE_CHECKING from typing import Any -from cleo.exceptions import NoSuchOptionException -from cleo.exceptions import RuntimeException +from cleo.exceptions import CleoNoSuchOptionError +from cleo.exceptions import CleoRuntimeError from cleo.io.inputs.input import Input @@ -193,7 +193,7 @@ def _parse_short_option_set(self, name: str) -> None: length = len(name) for i in range(length): if not self._definition.has_shortcut(name[i]): - raise RuntimeException(f'The option "{name[i]}" does not exist') + raise CleoRuntimeError(f'The option "{name[i]}" does not exist') option = self._definition.option_for_shortcut(name[i]) if option.accepts_value(): @@ -259,11 +259,11 @@ def _parse_argument(self, token: str) -> None: else: message = f'No arguments expected, got "{token}"' - raise RuntimeException(message) + raise CleoRuntimeError(message) def _add_short_option(self, shortcut: str, value: Any) -> None: if not self._definition.has_shortcut(shortcut): - raise NoSuchOptionException(f'The option "-{shortcut}" does not exist') + raise CleoNoSuchOptionError(f'The option "-{shortcut}" does not exist') self._add_long_option( self._definition.option_for_shortcut(shortcut).name, value @@ -271,12 +271,12 @@ def _add_short_option(self, shortcut: str, value: Any) -> None: def _add_long_option(self, name: str, value: Any) -> None: if not self._definition.has_option(name): - raise NoSuchOptionException(f'The option "--{name}" does not exist') + raise CleoNoSuchOptionError(f'The option "--{name}" does not exist') option = self._definition.option(name) if value is not None and not option.accepts_value(): - raise RuntimeException(f'The "--{name}" option does not accept a value') + raise CleoRuntimeError(f'The "--{name}" option does not accept a value') if value in ["", None] and option.accepts_value() and self._parsed: # If the option accepts a value, either required or optional, @@ -289,7 +289,7 @@ def _add_long_option(self, name: str, value: Any) -> None: if value is None: if option.requires_value(): - raise RuntimeException(f'The "--{name}" option requires a value') + raise CleoRuntimeError(f'The "--{name}" option requires a value') if not option.is_list() and option.is_flag(): value = True diff --git a/src/cleo/io/inputs/definition.py b/src/cleo/io/inputs/definition.py index 21ea4fbb..5ef07cc6 100644 --- a/src/cleo/io/inputs/definition.py +++ b/src/cleo/io/inputs/definition.py @@ -6,7 +6,7 @@ from typing import Any from typing import Sequence -from cleo.exceptions import LogicException +from cleo.exceptions import CleoLogicError from cleo.io.inputs.option import Option @@ -94,15 +94,15 @@ def add_arguments(self, arguments: list[Argument]) -> None: def add_argument(self, argument: Argument) -> None: if argument.name in self._arguments: - raise LogicException( + raise CleoLogicError( f'An argument with name "{argument.name}" already exists' ) if self._has_a_list_argument: - raise LogicException("Cannot add an argument after a list argument") + raise CleoLogicError("Cannot add an argument after a list argument") if argument.is_required() and self._has_optional: - raise LogicException("Cannot add a required argument after an optional one") + raise CleoLogicError("Cannot add a required argument after an optional one") if argument.is_list(): self._has_a_list_argument = True @@ -149,7 +149,7 @@ def add_options(self, options: list[Option]) -> None: def add_option(self, option: Option) -> None: if option.name in self._options and option != self._options[option.name]: - raise LogicException(f'An option named "{option.name}" already exists') + raise CleoLogicError(f'An option named "{option.name}" already exists') if option.shortcut: for shortcut in option.shortcut.split("|"): @@ -157,7 +157,7 @@ def add_option(self, option: Option) -> None: shortcut in self._shortcuts and option.name != self._shortcuts[shortcut] ): - raise LogicException( + raise CleoLogicError( f'An option with shortcut "{shortcut}" already exists' ) diff --git a/src/cleo/io/inputs/input.py b/src/cleo/io/inputs/input.py index 364e523a..94e52da2 100644 --- a/src/cleo/io/inputs/input.py +++ b/src/cleo/io/inputs/input.py @@ -6,8 +6,8 @@ from typing import TextIO from cleo._compat import shell_quote -from cleo.exceptions import MissingArgumentsException -from cleo.exceptions import ValueException +from cleo.exceptions import CleoMissingArgumentsError +from cleo.exceptions import CleoValueError from cleo.io.inputs.definition import Definition @@ -107,13 +107,13 @@ def validate(self) -> None: missing_arguments.append(argument.name) if missing_arguments: - raise MissingArgumentsException( + raise CleoMissingArgumentsError( f'Not enough arguments (missing: "{", ".join(missing_arguments)}")' ) def argument(self, name: str) -> Any: if not self._definition.has_argument(name): - raise ValueException(f'The argument "{name}" does not exist') + raise CleoValueError(f'The argument "{name}" does not exist') if name in self._arguments: return self._arguments[name] @@ -122,7 +122,7 @@ def argument(self, name: str) -> Any: def set_argument(self, name: str, value: Any) -> None: if not self._definition.has_argument(name): - raise ValueException(f'The argument "{name}" does not exist') + raise CleoValueError(f'The argument "{name}" does not exist') self._arguments[name] = value @@ -131,7 +131,7 @@ def has_argument(self, name: str) -> bool: def option(self, name: str) -> Any: if not self._definition.has_option(name): - raise ValueException(f'The option "--{name}" does not exist') + raise CleoValueError(f'The option "--{name}" does not exist') if name in self._options: return self._options[name] @@ -140,7 +140,7 @@ def option(self, name: str) -> Any: def set_option(self, name: str, value: Any) -> None: if not self._definition.has_option(name): - raise ValueException(f'The option "--{name}" does not exist') + raise CleoValueError(f'The option "--{name}" does not exist') self._options[name] = value diff --git a/src/cleo/io/inputs/option.py b/src/cleo/io/inputs/option.py index 6cf63663..8fcaeeca 100644 --- a/src/cleo/io/inputs/option.py +++ b/src/cleo/io/inputs/option.py @@ -4,8 +4,8 @@ from typing import Any -from cleo.exceptions import LogicException -from cleo.exceptions import ValueException +from cleo.exceptions import CleoLogicError +from cleo.exceptions import CleoValueError class Option: @@ -27,7 +27,7 @@ def __init__( name = name[2:] if not name: - raise ValueException("An option name cannot be empty") + raise CleoValueError("An option name cannot be empty") if shortcut is not None: shortcuts = re.split(r"\|-?", shortcut.lstrip("-")) @@ -35,7 +35,7 @@ def __init__( shortcut = "|".join(shortcuts) if not shortcut: - raise ValueException("An option shortcut cannot be empty") + raise CleoValueError("An option shortcut cannot be empty") self._name = name self._shortcut = shortcut @@ -46,7 +46,7 @@ def __init__( self._default = None if self._is_list and self._flag: - raise LogicException("A flag option cannot be a list as well") + raise CleoLogicError("A flag option cannot be a list as well") self.set_default(default) @@ -80,13 +80,13 @@ def is_list(self) -> bool: def set_default(self, default: Any | None = None) -> None: if self._flag and default is not None: - raise LogicException("A flag option cannot have a default value") + raise CleoLogicError("A flag option cannot have a default value") if self._is_list: if default is None: default = [] elif not isinstance(default, list): - raise LogicException("A default value for a list option must be a list") + raise CleoLogicError("A default value for a list option must be a list") if self._flag: default = False diff --git a/src/cleo/loaders/factory_command_loader.py b/src/cleo/loaders/factory_command_loader.py index f1dde598..4f9dbf05 100644 --- a/src/cleo/loaders/factory_command_loader.py +++ b/src/cleo/loaders/factory_command_loader.py @@ -3,7 +3,7 @@ from typing import Callable from cleo.commands.command import Command -from cleo.exceptions import CommandNotFoundException +from cleo.exceptions import CleoCommandNotFoundError from cleo.loaders.command_loader import CommandLoader @@ -27,7 +27,7 @@ def has(self, name: str) -> bool: def get(self, name: str) -> Command: if name not in self._factories: - raise CommandNotFoundException(name) + raise CleoCommandNotFoundError(name) factory = self._factories[name] diff --git a/src/cleo/ui/choice_question.py b/src/cleo/ui/choice_question.py index 6a8c05e1..d9920fd0 100644 --- a/src/cleo/ui/choice_question.py +++ b/src/cleo/ui/choice_question.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING from typing import Any -from cleo.exceptions import ValueException +from cleo.exceptions import CleoValueError from cleo.ui.question import Question @@ -36,7 +36,7 @@ def validate(self, selected: str | int) -> str | list[str] | None: # Check for a separated comma values _selected = selected.replace(" ", "") if not re.match("^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$", _selected): - raise ValueException(self._question.error_message.format(selected)) + raise CleoValueError(self._question.error_message.format(selected)) selected_choices = _selected.split(",") else: @@ -51,7 +51,7 @@ def validate(self, selected: str | int) -> str | list[str] | None: results.append(key) if len(results) > 1: - raise ValueException( + raise CleoValueError( "The provided answer is ambiguous. " f'Value should be one of {" or ".join(str(r) for r in results)}.' ) @@ -61,7 +61,7 @@ def validate(self, selected: str | int) -> str | list[str] | None: elif value.isdigit() and 0 <= int(value) < len(self._values): result = self._values[int(value)] else: - raise ValueException(self._question.error_message.format(value)) + raise CleoValueError(self._question.error_message.format(value)) multiselect_choices.append(result) diff --git a/src/cleo/ui/ui.py b/src/cleo/ui/ui.py index fd4d00bd..e9a78a38 100644 --- a/src/cleo/ui/ui.py +++ b/src/cleo/ui/ui.py @@ -1,6 +1,6 @@ from __future__ import annotations -from cleo.exceptions import ValueException +from cleo.exceptions import CleoValueError from cleo.ui.component import Component @@ -16,17 +16,17 @@ def __init__(self, components: list[Component] | None = None) -> None: def register(self, component: Component) -> None: if not isinstance(component, Component): - raise ValueException( + raise CleoValueError( "A UI component must inherit from the Component class." ) if not component.name: - raise ValueException("A UI component cannot be anonymous.") + raise CleoValueError("A UI component cannot be anonymous.") self._components[component.name] = component def component(self, name: str) -> Component: if name not in self._components: - raise ValueException(f'UI component "{name}" does not exist.') + raise CleoValueError(f'UI component "{name}" does not exist.') return self._components[name] diff --git a/tests/io/inputs/test_argument.py b/tests/io/inputs/test_argument.py index a735a3a0..0aaac60d 100644 --- a/tests/io/inputs/test_argument.py +++ b/tests/io/inputs/test_argument.py @@ -2,7 +2,7 @@ import pytest -from cleo.exceptions import LogicException +from cleo.exceptions import CleoLogicError from cleo.io.inputs.argument import Argument @@ -44,14 +44,14 @@ def test_list_argument() -> None: def test_required_arguments_do_not_support_default_values() -> None: with pytest.raises( - LogicException, match="Cannot set a default value for required arguments" + CleoLogicError, match="Cannot set a default value for required arguments" ): Argument("foo", description="Foo description", default="bar") def test_list_arguments_do_not_support_non_list_default_values() -> None: with pytest.raises( - LogicException, match="A default value for a list argument must be a list" + CleoLogicError, match="A default value for a list argument must be a list" ): Argument( "foo", diff --git a/tests/io/inputs/test_option.py b/tests/io/inputs/test_option.py index 5744ccc6..81b44515 100644 --- a/tests/io/inputs/test_option.py +++ b/tests/io/inputs/test_option.py @@ -2,8 +2,8 @@ import pytest -from cleo.exceptions import LogicException -from cleo.exceptions import ValueException +from cleo.exceptions import CleoLogicError +from cleo.exceptions import CleoValueError from cleo.io.inputs.option import Option @@ -26,17 +26,17 @@ def test_dashed_name() -> None: def test_fail_if_name_is_empty() -> None: - with pytest.raises(ValueException): + with pytest.raises(CleoValueError): Option("") def test_fail_if_default_value_provided_for_flag() -> None: - with pytest.raises(LogicException): + with pytest.raises(CleoLogicError): Option("option", flag=True, default="default") def test_fail_if_wrong_default_value_for_list_option() -> None: - with pytest.raises(LogicException): + with pytest.raises(CleoLogicError): Option("option", flag=False, is_list=True, default="default") @@ -59,7 +59,7 @@ def test_multiple_shortcuts() -> None: def test_fail_if_shortcut_is_empty() -> None: - with pytest.raises(ValueException): + with pytest.raises(CleoValueError): Option("option", "") diff --git a/tests/loaders/test_factory_command_loader.py b/tests/loaders/test_factory_command_loader.py index 766d32b1..eb02816a 100644 --- a/tests/loaders/test_factory_command_loader.py +++ b/tests/loaders/test_factory_command_loader.py @@ -3,7 +3,7 @@ import pytest from cleo.commands.command import Command -from cleo.exceptions import CommandNotFoundException +from cleo.exceptions import CleoCommandNotFoundError from cleo.loaders.factory_command_loader import FactoryCommandLoader @@ -38,7 +38,7 @@ def test_get_invalid_command_raises_error() -> None: {"foo": lambda: command("foo"), "bar": lambda: command("bar")} ) - with pytest.raises(CommandNotFoundException): + with pytest.raises(CleoCommandNotFoundError): loader.get("baz") diff --git a/tests/test_application.py b/tests/test_application.py index 0ad5671f..6bb6bb65 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -9,8 +9,8 @@ from cleo.application import Application from cleo.commands.command import Command -from cleo.exceptions import CommandNotFoundException -from cleo.exceptions import NamespaceNotFoundException +from cleo.exceptions import CleoCommandNotFoundError +from cleo.exceptions import CleoNamespaceNotFoundError from cleo.io.io import IO from cleo.io.outputs.stream_output import StreamOutput from cleo.testers.application_tester import ApplicationTester @@ -143,7 +143,7 @@ def test_find_ambiguous_namespace(app: Application) -> None: app.add(Foo2Command()) with pytest.raises( - NamespaceNotFoundException, + CleoNamespaceNotFoundError, match=( r'There are no commands in the "f" namespace\.\n\n' r"Did you mean one of these\?\n foo\n foo1" @@ -157,7 +157,7 @@ def test_find_invalid_namespace(app: Application) -> None: app.add(Foo2Command()) with pytest.raises( - NamespaceNotFoundException, + CleoNamespaceNotFoundError, match=r'There are no commands in the "bar" namespace\.', ): app.find_namespace("bar") @@ -169,7 +169,7 @@ def test_find_unique_name_but_namespace_name(app: Application) -> None: app.add(Foo2Command()) with pytest.raises( - CommandNotFoundException, + CleoCommandNotFoundError, match=r'The command "foo1" does not exist\.', ): app.find("foo1") @@ -186,7 +186,7 @@ def test_find_ambiguous_command(app: Application) -> None: app.add(FooCommand()) with pytest.raises( - CommandNotFoundException, + CleoCommandNotFoundError, match=( r'The command "foo b" does not exist\.\n\nDid you mean this\?\n foo bar' ), @@ -200,7 +200,7 @@ def test_find_ambiguous_command_hidden(app: Application) -> None: app.add(foo) with pytest.raises( - CommandNotFoundException, + CleoCommandNotFoundError, match=r'The command "foo b" does not exist\.$', ): app.find("foo b") @@ -225,7 +225,7 @@ def test_set_catch_exceptions(app: Application, environ: dict[str, str]) -> None app.catch_exceptions(False) - with pytest.raises(CommandNotFoundException): + with pytest.raises(CleoCommandNotFoundError): tester.execute("foo", decorated=False) diff --git a/tests/ui/test_choice_question.py b/tests/ui/test_choice_question.py index 2c2af59a..278ad9da 100644 --- a/tests/ui/test_choice_question.py +++ b/tests/ui/test_choice_question.py @@ -4,7 +4,7 @@ import pytest -from cleo.exceptions import ValueException +from cleo.exceptions import CleoValueError from cleo.ui.choice_question import ChoiceQuestion @@ -85,13 +85,13 @@ def test_ask_choice(io: BufferedIO) -> None: question = ChoiceQuestion("What is your favourite superhero?", heroes) question.set_max_attempts(1) - with pytest.raises(ValueException) as e: + with pytest.raises(CleoValueError) as e: question.ask(io) assert str(e.value) == 'Value "4" is invalid' assert question.ask(io) == "Superman" - with pytest.raises(ValueException) as e: + with pytest.raises(CleoValueError) as e: question.ask(io) assert str(e.value) == 'Value "-2" is invalid'