Exemple #1
0
def test_invalid_choice_not_set():
    """Ensure an error is raised for no choice"""
    test_config = ApplicationConfiguration(
        application_name="test_config1",
        application_version="1.0",
        internals=Internals(),
        post_processor=None,
        subcommands=[
            SubCommand(
                name="subcommand1",
                description="subcommand1",
                version_added="v0.0",
            ),
        ],
        entries=[
            SettingsEntry(
                name="sb1",
                short_description="Subcommands",
                subcommand_value=True,
                value=SettingsEntryValue(default="welcome"),
                version_added="v0.0",
            ),
            SettingsEntry(
                name="e1",
                short_description="ex1",
                value=SettingsEntryValue(),
                version_added="v0.0",
            ),
        ],
    )
    with pytest.raises(ValueError, match="Current source not set for e1"):
        test_config.entry("e1").invalid_choice  # pylint: disable=expression-not-assigned
Exemple #2
0
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_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_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
Exemple #5
0
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()
Exemple #6
0
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
Exemple #7
0
def test_many_subcommand():
    """Ensure a configuration without a ``subparser`` entry fails"""
    test_config = ApplicationConfiguration(
        application_name="test_config1",
        application_version="1.0",
        internals=Internals(),
        post_processor=None,
        subcommands=[
            SubCommand(
                name="subcommand1",
                description="subcommand1",
                version_added="v0.0",
            ),
        ],
        entries=[
            SettingsEntry(
                name="sb1",
                short_description="Subcommands",
                subcommand_value=True,
                value=SettingsEntryValue(default="welcome"),
                version_added="v0.0",
            ),
            SettingsEntry(
                name="sb2",
                short_description="Subcommands",
                subcommand_value=True,
                value=SettingsEntryValue(default="welcome"),
                version_added="v0.0",
            ),
        ],
    )
    with pytest.raises(ValueError,
                       match="Multiple entries with subparser value defined"):
        Configurator(params=[],
                     application_configuration=test_config).configure()
Exemple #8
0
def test_cmdline_source_not_set():
    """Ensure a configuration without a ``subparser`` entry fails"""
    test_config = ApplicationConfiguration(
        application_name="test_config1",
        application_version="1.0",
        internals=Internals(),
        post_processor=NavigatorPostProcessor(),
        subcommands=[
            SubCommand(
                name="subcommand1",
                description="subcommand1",
                version_added="v0.0",
            ),
        ],
        entries=[
            SettingsEntry(
                name="cmdline",
                short_description="cmdline",
                value=SettingsEntryValue(),
                version_added="v0.0",
            ),
        ],
    )
    configurator = Configurator(params=[],
                                application_configuration=test_config)
    configurator._post_process()
    assert "Completed post processing for cmdline" in configurator._messages[
        0][1]
    assert not configurator._exit_messages
Exemple #9
0
def test_custom_nargs_for_positional():
    """Ensure a ``nargs`` for a positional are carried forward"""
    test_config = ApplicationConfiguration(
        application_name="test_config1",
        application_version="1.0",
        internals=Internals(),
        post_processor=None,
        subcommands=[
            SubCommand(
                name="subcommand1",
                description="subcommand1",
                version_added="v0.0",
            ),
        ],
        entries=[
            SettingsEntry(
                name="sb1",
                short_description="Subcommands",
                subcommand_value=True,
                value=SettingsEntryValue(default="welcome"),
                version_added="v0.0",
            ),
            SettingsEntry(
                name="e1",
                cli_parameters=CliParameters(positional=True, nargs=3),
                short_description="ex1",
                value=SettingsEntryValue(),
                subcommands=["subcommand1"],
                version_added="v0.0",
            ),
        ],
    )
    parser = Parser(test_config)
    assert parser.parser._actions[2].choices["subcommand1"]._actions[
        2].nargs == 3
Exemple #10
0
def test_no_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=[],
    )
    with pytest.raises(ValueError,
                       match="No entry with subparser value defined"):
        Configurator(params=[],
                     application_configuration=test_config).configure()
Exemple #11
0
def test_no_subcommand():
    """Ensure a configuration without no ``subparser`` entry fails"""
    test_config = ApplicationConfiguration(
        application_name="test_config1",
        application_version="1.0",
        internals=Internals(),
        post_processor=None,
        subcommands=[
            SubCommand(
                name="subcommand1",
                description="subcommand1",
                version_added="v0.0",
            ),
        ],
        entries=[],
    )
    with pytest.raises(ValueError,
                       match="No entry with subparser value defined"):
        Configurator(params=[],
                     application_configuration=test_config).configure()
Exemple #12
0
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 == []
Exemple #13
0
def test_subcommand_not_found():
    """Ensure an error for invalid subcommand/action"""
    test_config = ApplicationConfiguration(
        application_name="test_config1",
        application_version="1.0",
        internals=Internals(action_packages=["ansible_navigator.actions"]),
        post_processor=NavigatorPostProcessor(),
        subcommands=[
            SubCommand(
                name="__test_action",
                description="test_action",
                version_added="v0.0",
            ),
        ],
        entries=[
            SettingsEntry(
                name="app",
                short_description="test_app",
                value=SettingsEntryValue(current="__test_action"),
                version_added="v0.0",
            ),
            SettingsEntry(
                name="mode",
                short_description="mode",
                value=SettingsEntryValue(),
                version_added="v0.0",
            ),
        ],
    )
    test_config.internals.initializing = True
    configurator = Configurator(params=[],
                                application_configuration=test_config)
    configurator._post_process()
    exit_msg = "Unable to find an action for '__test_action', tried: 'ansible_navigator.actions'"
    assert exit_msg in [
        exit_msg.message for exit_msg in configurator._exit_messages
    ]