コード例 #1
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]
コード例 #2
0
def test_settings_entry(sample_settings, settings_file_dict):
    """Ensure a settings entry is properly constructed.

    :param sample_settings: A sample application configuration (settings)
    :param settings_file_dict: The expected settings as a dictionary
    """
    entry = SettingsEntry(
        name="se_1",
        choices=["choice_1", "choice_2"],
        cli_parameters=CliParameters(short="-se1"),
        short_description="description",
        value=SettingsEntryValue(
            current="current",
            default="default",
            source=Constants.ENVIRONMENT_VARIABLE,
        ),
        version_added="v0.0",
    )
    sample_settings.entries = [entry]
    configurator = Configurator(params=[], application_configuration=sample_settings)
    configurator._post_process()  # pylint: disable=protected-access
    presentable = to_presentable(sample_settings)
    # pylint: disable=not-an-iterable # https://github.com/PyCQA/pylint/issues/2296
    assert all(isinstance(p, PresentableSettingsEntry) for p in presentable)
    assert asdict(presentable[0]) == settings_file_dict
    entry_dict = {
        "choices": ["choice_1", "choice_2"],
        "current_settings_file": "/test/path",
        "current_value": "current",
        "default_value": "default",
        "default": False,
        "description": "description",
        "env_var": "APP_SE_1",
        "name": "Se 1",
        "settings_file_sample": {"app": {"se-1": "<------"}},
        "source": "Environment variable",
        "subcommands": ["subcommand_1"],
        "cli_parameters": {"long": "--se-1", "short": "-se1"},
        "version_added": "v0.0",
    }
    assert asdict(presentable[1]) == entry_dict