From ff8e03fe5542a9d7930a5720450c59bb9e9eaaf3 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Thu, 15 Aug 2019 12:18:08 +0200 Subject: preserve directives, optional push root level scalar to own line You need to set YAML().scalar_after_indicator = False to get a single scalar document starting on its own line (instead of after the directives end indicator: --- abc") fixes issue #304 *When this change indeed resolves your problem, please **Close** this issue*. *(You can do so using the WorkFlow pull-down (close to the top right of this page))* --- _test/roundtrip.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ _test/test_issues.py | 11 +++++++++++ 2 files changed, 60 insertions(+) (limited to '_test') diff --git a/_test/roundtrip.py b/_test/roundtrip.py index ee430a3..090b9e0 100644 --- a/_test/roundtrip.py +++ b/_test/roundtrip.py @@ -151,6 +151,55 @@ def round_trip( return data +def na_round_trip( + inp, + outp=None, + extra=None, + intermediate=None, + indent=None, + top_level_colon_align=None, + prefix_colon=None, + preserve_quotes=None, + explicit_start=None, + explicit_end=None, + version=None, + dump_data=None, +): + """ + inp: input string to parse + outp: expected output (equals input if not specified) + """ + if outp is None: + outp = inp + if version is not None: + version = version + doutp = dedent(outp) + if extra is not None: + doutp += extra + yaml = YAML() + yaml.preserve_quotes = preserve_quotes + yaml.scalar_after_indicator = False # newline after every directives end + data = yaml.load(inp) + if dump_data: + print('data', data) + if intermediate is not None: + if isinstance(intermediate, dict): + for k, v in intermediate.items(): + if data[k] != v: + print('{0!r} <> {1!r}'.format(data[k], v)) + raise ValueError + yaml.indent = indent + yaml.top_level_colon_align = top_level_colon_align + yaml.prefix_colon = prefix_colon + yaml.explicit_start = explicit_start + yaml.explicit_end = explicit_end + res = yaml.dump(data, compare=doutp) + #if res != doutp: + # diff(doutp, res, 'input string') + #print('\nroundtrip data:\n', res, sep="") + #assert res == doutp + + def YAML(**kw): import ruamel.yaml # NOQA diff --git a/_test/test_issues.py b/_test/test_issues.py index 3692f61..9b301a9 100644 --- a/_test/test_issues.py +++ b/_test/test_issues.py @@ -8,6 +8,7 @@ import pytest # NOQA from roundtrip import ( round_trip, + na_round_trip, round_trip_load, round_trip_dump, dedent, @@ -850,6 +851,16 @@ class TestIssues: match='while scanning a directive'): yaml.load(inp) + def test_issue_304(self): + inp = """ + %YAML 1.2 + %TAG ! tag:example.com,2019: + --- + !foo null + ... + """ + d = na_round_trip(inp) # NOQA + # @pytest.mark.xfail(strict=True, reason='bla bla', raises=AssertionError) # def test_issue_ xxx(self): # inp = """ -- cgit v1.2.1