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
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
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)
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()
def test_get_step_from_empty_config(): config = Config.parse([]) assert not config.get_step_by(name='greeting')
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()
def test_get_step_by_non_existing_attribute(): config = Config.parse([echo_step, list_step]) assert not config.get_step_by(gorilla='greeting')
def test_get_step_by_nothing_returns_none(): config = Config.parse([echo_step, list_step]) assert not config.get_step_by()
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()
def test_get_step_by_too_big_index(): config = Config.parse([echo_step, list_step]) assert not config.get_step_by(index=2)
def test_get_step_by_index(): config = Config.parse([echo_step, list_step]) assert list_step['step'] == config.get_step_by(index=1).serialize()
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()
def test_get_step_by_name_doesnt_exist(): config = Config.parse([echo_step, list_step]) assert not config.get_step_by(name='not found')