コード例 #1
0
def test_many_subcommand():
    """Ensure a configuration without a ``subparser`` 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()
コード例 #2
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
コード例 #3
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()
コード例 #4
0
def _sample_settings():
    return ApplicationConfiguration(
        application_name="app",
        application_version="1.0",
        internals=Internals(settings_file_path="/test/path"),
        post_processor=NavigatorPostProcessor(),
        subcommands=[
            SubCommand(
                name="subcommand_1",
                description="subcommand_1",
                version_added="v0.0",
            ),
        ],
        entries=[],
    )
コード例 #5
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()
コード例 #6
0
def test_import_error():
    """Ensure an error for invalid action_package"""
    test_config = ApplicationConfiguration(
        application_name="test_config1",
        application_version="1.0",
        internals=Internals(action_packages=["__ansible_navigator.__actions"]),
        post_processor=NavigatorPostProcessor(),
        subcommands=[
            SubCommand(
                name="subcommand1",
                description="subcommand1",
                version_added="v0.0",
            ),
        ],
        entries=[
            SettingsEntry(
                name="app",
                short_description="test_app",
                value=SettingsEntryValue(current="subcommand1"),
                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()
    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)
    exit_msg = "Unable to find an action for 'subcommand1', tried: '__ansible_navigator.__actions'"
    assert exit_msg in [
        exit_msg.message for exit_msg in configurator._exit_messages
    ]
コード例 #7
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 == []
コード例 #8
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
    ]
コード例 #9
0
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
コード例 #10
0
def test_apply_cli_subset_none():
    """Ensure subset none works for apply CLI"""
    test_config = ApplicationConfiguration(
        application_name="test_application",
        application_version="1.0",
        internals=Internals(initializing=True),
        post_processor=None,
        subcommands=[
            SubCommand(
                name="list",
                description="list",
                version_added="v0.0",
            ),
            SubCommand(
                name="run",
                description="run",
                version_added="v0.0",
            ),
        ],
        entries=[
            SettingsEntry(
                name="subcommand",
                short_description="Subcommands",
                subcommand_value=True,
                value=SettingsEntryValue(default="run"),
                version_added="v0.0",
            ),
            SettingsEntry(
                name="z",
                apply_to_subsequent_cli=C.NONE,
                cli_parameters=CliParameters(short="-z"),
                short_description="the z parameter",
                value=SettingsEntryValue(),
                version_added="v0.0",
            ),
            SettingsEntry(
                name="execution_environment",
                apply_to_subsequent_cli=C.NONE,
                cli_parameters=CliParameters(short="-e"),
                short_description="the e parameter",
                value=SettingsEntryValue(),
                version_added="v0.0",
            ),
        ],
    )
    configurator = Configurator(
        params=["list", "-z", "zebra"],
        application_configuration=test_config,
    )
    _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]