def test_subcommand_not_found(): """Ensure an error for invalid subcommand/action""" test_config = ApplicationConfiguration( application_name="test_config1", internals=Internals(action_packages=["ansible_navigator.actions"]), post_processor=NavigatorPostProcessor(), subcommands=[ SubCommand(name="__test_action", description="test_action"), ], entries=[ Entry( name="app", short_description="test_app", value=EntryValue(current="__test_action"), ), Entry( name="mode", short_description="mode", value=EntryValue(), ), ], ) configurator = Configurator(params=[], application_configuration=test_config) configurator._post_process() error = "Unable to find an action for '__test_action', tried: 'ansible_navigator.actions'" assert error in configurator._errors
def test_many_subcommand(): """Ensure a Config without a subparse entry fails""" test_config = ApplicationConfiguration( application_name="test_config1", post_processor=None, subcommands=[ SubCommand(name="subcommand1", description="subcommand1"), ], entries=[ Entry( name="sb1", short_description="Subcommands", subcommand_value=True, value=EntryValue(default="welcome"), ), Entry( name="sb2", short_description="Subcommands", subcommand_value=True, value=EntryValue(default="welcome"), ), ], ) with pytest.raises(ValueError, match="Multiple entries with subparser value defined"): Configurator(params=[], application_configuration=test_config).configure()
def test_import_error(): """Ensure an error for invalid action_package""" test_config = ApplicationConfiguration( application_name="test_config1", internals=Internals(action_packages=["__ansible_navigator.__actions"]), post_processor=NavigatorPostProcessor(), subcommands=[ SubCommand(name="subcommand1", description="subcommand1"), ], entries=[ Entry( name="app", short_description="test_app", value=EntryValue(current="subcommand1"), ), Entry( name="mode", short_description="mode", value=EntryValue(), ), ], ) configurator = Configurator(params=[], application_configuration=test_config) configurator._post_process() message = "Unable to load action package: '__ansible_navigator.__actions':" message += " No module named '__ansible_navigator'" assert message in (entry.message for entry in configurator._messages) error = "Unable to find an action for 'subcommand1', tried: '__ansible_navigator.__actions'" assert error in configurator._errors
def test_cutom_nargs_for_postional(): """Ensure a nargs for a positional are carried forward""" test_config = ApplicationConfiguration( application_name="test_config1", post_processor=None, subcommands=[ SubCommand(name="subcommand1", description="subcommand1"), ], entries=[ Entry( name="sb1", short_description="Subcommands", subcommand_value=True, value=EntryValue(default="welcome"), ), Entry( name="e1", cli_parameters=CliParameters(positional=True, nargs=3), short_description="ex1", value=EntryValue(), subcommands=["subcommand1"], ), ], ) parser = Parser(test_config) assert parser.parser._actions[2].choices["subcommand1"]._actions[ 2].nargs == 3
def _params_row_for_entry(entry: Entry, param_details: Dict) -> Tuple: # pylint: disable=too-many-branches """create a row entry for a param""" if entry.cli_parameters is None: cli_parameters = "positional" else: if entry.cli_parameters.short: if entry.cli_parameters.long_override: long = entry.cli_parameters.long_override else: long = f"--{entry.name_dashed}" cli_parameters = f"``{entry.cli_parameters.short}`` or ``{long}``" else: cli_parameters = "positional" path = entry.settings_file_path("ansible-navigator") yaml_like = ["", " .. code-block:: yaml", ""] for idx, path_part in enumerate(path.split(".")): yaml_like.append(f"{(2*idx+12) * ' '}{path_part}:") yaml_like.append("") path = entry.settings_file_path(APP) + ".default-value-override" default_override = _params_get_param_file_entry( param_details=param_details, path=path, ) logger.debug( "%s: default_value_override: %s", entry.name, default_override, ) if isinstance(default_override, str): default = default_override else: if entry.value.default is C.NOT_SET: default = "No default value set" else: default = entry.value.default choices = oxfordcomma(entry.choices, "or") envvar = entry.environment_variable(APP.replace("-", "_")) settings = [] if choices: settings.append(f"**Choices:** {choices}") if default is not None: settings.append(f"**Default:** {default}") if cli_parameters is not None: settings.append(f"**CLI:** {cli_parameters}") if envvar is not None: settings.append(f"**ENV:** {envvar}") settings.extend(["**Settings file:**", *yaml_like]) row = (entry.name_dashed, entry.short_description, tuple(settings)) return row
def test_apply_cli_subset_none(): """Ensure subset none works for apply CLI""" test_config = ApplicationConfiguration( application_name="test_application", post_processor=None, subcommands=[ SubCommand(name="list", description="list"), SubCommand(name="run", description="run"), ], entries=[ Entry( name="subcommand", short_description="Subcommands", subcommand_value=True, value=EntryValue(default="run"), ), Entry( name="z", apply_to_subsequent_cli=C.NONE, cli_parameters=CliParameters(short="-z"), short_description="the z parameter", value=EntryValue(), ), ], ) configurator = Configurator(params=["list", "-z", "zebra"], application_configuration=test_config, initial=True) _messages, exit_messages = configurator.configure() assert not exit_messages assert isinstance(test_config.initial, ApplicationConfiguration) expected = [ ("subcommand", "list"), ("z", "zebra"), ] for expect in expected: assert test_config.entry(expect[0]).value.current == expect[1] assert test_config.entry(expect[0]).value.source is C.USER_CLI configurator = Configurator(params=["run"], application_configuration=test_config, apply_previous_cli_entries=C.ALL) _messages, exit_messages = configurator.configure() assert not exit_messages expected = [ ("subcommand", "run", C.USER_CLI), ("z", C.NOT_SET, C.NOT_SET), ] for expect in expected: assert test_config.entry(expect[0]).value.current == expect[1] assert test_config.entry(expect[0]).value.source is expect[2]
def test_invalid_choice_not_set(): """Ensure an error is raised for no choice""" test_config = ApplicationConfiguration( application_name="test_config1", post_processor=None, subcommands=[ SubCommand(name="subcommand1", description="subcommand1"), ], entries=[ Entry( name="sb1", short_description="Subcommands", subcommand_value=True, value=EntryValue(default="welcome"), ), Entry( name="e1", short_description="ex1", value=EntryValue(), ), ], ) with pytest.raises(ValueError, match="Current source not set for e1"): test_config.entry("e1").invalid_choice # pylint: disable=expression-not-assigned
def test_cmdline_source_not_set(): """Ensure a Config without a subparse entry fails""" test_config = ApplicationConfiguration( application_name="test_config1", post_processor=NavigatorPostProcessor(), subcommands=[ SubCommand(name="subcommand1", description="subcommand1"), ], entries=[ Entry( name="cmdline", short_description="cmdline", value=EntryValue(), ), ], ) configurator = Configurator(params=[], application_configuration=test_config) configurator._post_process() assert "Completed post processing for cmdline" in configurator._messages[ 0][1] assert configurator._exit_messages == []