示例#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
示例#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
示例#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)
示例#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()
示例#5
0
def test_get_step_from_empty_config():
    config = Config.parse([])
    assert not config.get_step_by(name='greeting')
示例#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()
示例#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')
示例#8
0
def test_get_step_by_nothing_returns_none():
    config = Config.parse([echo_step, list_step])
    assert not config.get_step_by()
示例#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()
示例#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)
示例#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()
示例#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()
示例#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')