コード例 #1
0
def check_file(path, validate=False, fix=False):
    output_file = '{}.output'.format(path)
    error_file = '{}.error'.format(path)

    with open(path) as fh:
        contents = fh.read()

        if os.path.exists(error_file):
            with open(error_file) as fh:
                name, pos = fh.readline().split(':')
                pos = int(pos)

            with pytest.raises(safeyaml.ParserErr) as excinfo:
                safeyaml.parse(contents)

            error = excinfo.value
            assert error.name() == name
            assert error.pos == pos
            return

        options = safeyaml.Options(
            fix_unquoted=fix,
            fix_nodent=fix,
            fix_nospace=fix,
        )

        output = io.StringIO()
        obj = safeyaml.parse(contents, output=output, options=options)
        output = output.getvalue()

        if validate:
            try:
                ref_obj = yaml.load(contents)
            except:
                raise Exception("input isn't valid YAML: {}".format(contents))

            assert obj == ref_obj

            try:
                parsed_output = yaml.load(output)
            except Exception as e:
                raise Exception("output isn't valid YAML: {}".format(output))

            assert parsed_output == ref_obj

        if fix:
            with open(output_file) as fh:
                expected_output = fh.read()
                assert output == expected_output
コード例 #2
0
def test_smoke(code, ref_obj):
    obj = safeyaml.parse(code)
    assert obj == ref_obj