From b81ee6315eaf8d08ddcb7a80e797a811fef17345 Mon Sep 17 00:00:00 2001 From: Dmytro Leshchenko Date: Wed, 27 Dec 2023 04:31:57 +0100 Subject: [PATCH] Add test for validate_required method from GitCommand and update implementation --- sources/options/options.py | 2 +- .../unit_tests/pygit/options/test_options.py | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/sources/options/options.py b/sources/options/options.py index 8fdccb7..a0b9a75 100644 --- a/sources/options/options.py +++ b/sources/options/options.py @@ -220,7 +220,7 @@ def validate_required(self, options: Union[GitOption, List[GitOption]]) -> Tuple missing_definitions = [] for required_definition in required_definitions: aliases = '|'.join(required_definition.get_names()) - missing_definitions.append(f'({aliases})') + missing_definitions.append(f'{aliases}') return len(required_definitions) == 0, missing_definitions def validate_choices(self, option: GitOption, definition: Optional[GitOptionDefinition] = None) -> bool: diff --git a/tests/unit_tests/pygit/options/test_options.py b/tests/unit_tests/pygit/options/test_options.py index 7860331..6a69421 100644 --- a/tests/unit_tests/pygit/options/test_options.py +++ b/tests/unit_tests/pygit/options/test_options.py @@ -303,6 +303,68 @@ def test_validate_positional_list_positive(self, positional_definitions: List[Tu }) assert command.validate_positional_list() == expected + @pytest.mark.parametrize("option_definitions,expected", [ + (['option', 'long-option', 's'], (True, [])), + (['o', 'long-option', 's'], (True, [])), + (['option', 's'], (False, ['long-option'])), + (['option', 'long-option'], (False, ['s'])), + (['long-option', 's'], (False, ['option|o'])), + (['option'], (False, ['long-option', 's'])), + ], ids=("All required options are present (long alias for two aliases option)", + "All required options are present (short alias for two aliases option)", + "Long option is missing", "Short option is missing", "Required option with two aliases is missing", + "Long option and short option are missing")) + def test_validate_required_positive(self, option_definitions: List[str], expected: Tuple[bool, Optional[str]]): + """ + Method tests that 'validate_required' method from 'GitCommand' class returns Tuple[status, missing options], + where missing options is a list of options that are required, but missing. + + :param option_definitions: List of options. + :type option_definitions: List[str] + :param expected: The expected value that shall be returned by method. + :type expected: Tuple[bool, Optional[str]] + """ + options = [] + for option in option_definitions: + options.append(GitOption(name=option, value='value')) + command = GitCommandGenerator.from_dict({ + 'command': 'demo-command', + 'definitions': [ + { + 'aliases': [ + { + 'name': 'option', + 'short-option': False + }, + { + 'name': 'o', + 'short-option': True + } + ], + 'required': True + }, + { + 'aliases': [ + { + 'name': 'long-option', + 'short-option': False + } + ], + 'required': True + }, + { + 'aliases': [ + { + 'name': 's', + 'short-option': True + } + ], + 'required': True + }, + ] + }) + assert command.validate_required(options) == expected + def test_transform_to_command_positive(self): """ Method tests that 'transform_to_command' method from 'GitCommand' class is able to correctly transform