Esempio n. 1
0
def test_set_type_invalid_fallback(capfd, args):
    args.VARIABLE = "PATH"
    args.type = "numeric"
    args.description = None

    configuration = get_variable_information_manager(args.VARIABLE,
                                                     read_only=True)
    assert (configuration[args.VARIABLE] is None)

    command(args)

    stdout, stderr = capfd.readouterr()
    assert ("Set type for 'PATH'." in stdout)
    assert (not stderr)

    configuration = get_variable_information_manager(args.VARIABLE,
                                                     read_only=True)
    assert (configuration[args.VARIABLE]["type"] == "numeric")

    # Normally, the commands now might fail for the user because PATH clearly
    # contains a non-numeric value. However, `epc set` should have a fallback
    # routine.
    args.type = "path"
    command(args)

    stdout, stderr = capfd.readouterr()
    assert ("Set type for 'PATH'." in stdout)
    assert (not stderr)

    configuration = get_variable_information_manager(args.VARIABLE,
                                                     read_only=True)
    assert (configuration[args.VARIABLE]["type"] == "path")
Esempio n. 2
0
def command(args):
    try:
        env_var, _ = args.environment[args.VARIABLE]
    except Exception:
        # The environment variable object failed to instantiate. This commonly
        # happens if the type conflicts with the value in the environment.
        # To allow the user to continue, we can always fall back using the
        # default heuristics.
        env_var = create_environment_variable(
            args.VARIABLE, args.environment.current_environment)
    varinfo = env_var.extended_attributes

    conf_to_apply = dict()

    if args.type is not None:
        print("Set type for '{0}'.".format(args.VARIABLE))
        conf_to_apply["type"] = args.type

    if args.description is not None:
        print("Set description for '{0}'.".format(args.VARIABLE))
        conf_to_apply["description"] = args.description

    if conf_to_apply:
        varinfo.apply(conf_to_apply)

        varinfo_manager = get_variable_information_manager(args.VARIABLE,
                                                           read_only=False)
        if all(not x for x in conf_to_apply.values()):
            # If all the configuration is gone (reset to empty) for the
            # variable, delete the entire record.
            del varinfo_manager[args.VARIABLE]
        else:
            # Save the changes with the hardcoded "local" source tag.
            varinfo_manager.set(args.VARIABLE, varinfo, "local")
Esempio n. 3
0
def test_set_type(capfd, args):
    args.VARIABLE = "PATH"
    args.type = "numeric"
    args.description = None

    configuration = get_variable_information_manager(args.VARIABLE,
                                                     read_only=True)
    assert (configuration[args.VARIABLE] is None)

    command(args)

    stdout, stderr = capfd.readouterr()
    assert ("Set type for 'PATH'." in stdout)
    assert (not stderr)

    configuration = get_variable_information_manager(args.VARIABLE,
                                                     read_only=True)
    assert (configuration[args.VARIABLE]["type"] == "numeric")
Esempio n. 4
0
def test_set_multiple(capfd, args):
    args.VARIABLE = "PATH"
    args.type = "string"
    args.description = "Multiple tested."

    configuration = get_variable_information_manager(args.VARIABLE,
                                                     read_only=True)
    assert (configuration[args.VARIABLE] is None)

    command(args)

    stdout, stderr = capfd.readouterr()
    assert ("Set type for 'PATH'." in stdout)
    assert ("Set description for 'PATH'." in stdout)
    assert (not stderr)

    configuration = get_variable_information_manager(args.VARIABLE,
                                                     read_only=True)
    assert (configuration[args.VARIABLE]["type"] == "string")
    assert (configuration[args.VARIABLE]["description"] == "Multiple tested.")
Esempio n. 5
0
def test_get_description_local(capfd, args):
    manager = get_variable_information_manager("DUMMY_VAR", read_only=False)
    manager.set("DUMMY_VAR", MockExtendedData(), "test-info")

    args.VARIABLE = "DUMMY_VAR"
    command(args)

    stdout, stderr = capfd.readouterr()
    assert("Type: 'path'" in stdout)
    assert("Description:\n\ttest" in stdout)
    assert("Source: test-info" in stdout)
    assert(not stderr)
Esempio n. 6
0
def test_delete(capfd, args):
    args.VARIABLE = "PATH"
    args.type = "foo"
    args.description = "Delete test."

    command(args)
    capfd.readouterr()

    # Reset the configuration to empty.
    args.type = ""
    args.description = ""

    command(args)
    stdout, stderr = capfd.readouterr()
    assert ("Set type for 'PATH'." in stdout)
    assert ("Set description for 'PATH'." in stdout)
    assert (not stderr)

    configuration = get_variable_information_manager(args.VARIABLE,
                                                     read_only=True)
    assert (configuration[args.VARIABLE] is None)
Esempio n. 7
0
def __create_global_shell_and_env():
    """Creates a valid :py:class:`.shell.Shell` and
    :py:class:`.environment.Environment` instance for the current running
    system, shell, and configuration, to be used as the basis of injected state
    by the subcommands.

    Returns
    -------
    shell : .shell.Shell
        The current Shell backend implementation.
    environment : .environment.Environment
        The handler for environment variable access.
    """
    return get_shell_and_env_always(
        os.environ,
        assemble_standard_type_heuristics_pipeline(
            varcfg_user_loader=lambda varname:
                get_variable_information_manager(varname, read_only=True),
            varcfg_description_loader=lambda varname:
                get_community_variable_information_manager(varname,
                                                           read_only=True)
            )
    )