Esempio n. 1
0
def test_merging():
    a = Config.parse([echo_step])
    b = Config.parse([complex_step])
    c = a.merge_with(b)
    assert len(c.steps) == 2
    for step in a.steps.keys() & b.steps.keys():
        assert step in c.steps
Esempio n. 2
0
def lint(yaml) -> LintResult:
    lr = LintResult()

    data = read_yaml(yaml)
    validator = get_validator()
    errors = sorted(
        validator.iter_errors(data),
        key=lambda error: (relevance(error), repr(error.path)),
    )
    for error in errors:
        simplified_schema_path = [
            el for el in list(error.relative_schema_path)[:-1]
            if el not in ('properties', 'items')
        ]
        obj_path = [str(el) for el in error.path]
        lr.add_error(
            '  {validator} validation on {schema_path}: {message} ({path})'.
            format(
                validator=style(error.validator.title(), bold=True),
                schema_path=style('.'.join(simplified_schema_path), bold=True),
                message=style(error.message, fg='red'),
                path=style('.'.join(obj_path), bold=True),
            ))

    if len(errors) > 0:
        return lr

    try:
        config = Config.parse(data)
    except ValidationError as err:  # Could happen before we get to linting things
        lr.add_error(str(err), exception=err)
    else:
        config.lint(lr, context={})
    return lr
Esempio n. 3
0
def parse(yaml, validate=True):
    """
    Parse the given YAML data into a `Config` object, optionally validating it first.

    :param yaml: YAML data (either a string, a stream, or pre-parsed Python dict/list)
    :type yaml: list|dict|str|file
    :param validate: Whether to validate the data before attempting to parse it.
    :type validate: bool
    :return: Config object
    :rtype: valohai_yaml.objs.Config
    """
    data = read_yaml(yaml)
    if validate:  # pragma: no branch
        from .validation import validate
        validate(data, raise_exc=True)
    return Config.parse(data)
Esempio n. 4
0
def test_merging_conflict():
    a = Config.parse([complex_step])
    b = Config.parse([complex_step_alt])
    c = a.merge_with(b)
    expected = Config.parse([complex_steps_merged])
    assert c.serialize() == expected.serialize()
Esempio n. 5
0
def test_get_step_from_empty_config():
    config = Config.parse([])
    assert not config.get_step_by(name='greeting')
Esempio n. 6
0
def test_get_step_by_simple_name():
    config = Config.parse([echo_step, list_step])
    assert list_step['step'] == config.get_step_by(name='list files').serialize()
Esempio n. 7
0
def test_get_step_by_non_existing_attribute():
    config = Config.parse([echo_step, list_step])
    assert not config.get_step_by(gorilla='greeting')
Esempio n. 8
0
def test_get_step_by_nothing_returns_none():
    config = Config.parse([echo_step, list_step])
    assert not config.get_step_by()
Esempio n. 9
0
def test_get_step_by_name_and_command():
    config = Config.parse([echo_step, list_step])
    assert not config.get_step_by(name='greeting', command='echo HELLO MORDOR')
    assert not config.get_step_by(name='farewell', command='echo HELLO WORLD')
    assert echo_step['step'] == config.get_step_by(name='greeting', command='echo HELLO WORLD').serialize()
Esempio n. 10
0
def test_get_step_by_too_big_index():
    config = Config.parse([echo_step, list_step])
    assert not config.get_step_by(index=2)
Esempio n. 11
0
def test_get_step_by_index():
    config = Config.parse([echo_step, list_step])
    assert list_step['step'] == config.get_step_by(index=1).serialize()
Esempio n. 12
0
def test_get_step_by_command():
    config = Config.parse([echo_step, list_step])
    assert echo_step['step'] == config.get_step_by(command='echo HELLO WORLD').serialize()
    assert list_step['step'] == config.get_step_by(command='ls').serialize()
Esempio n. 13
0
def test_get_step_by_name_doesnt_exist():
    config = Config.parse([echo_step, list_step])
    assert not config.get_step_by(name='not found')