diff options
Diffstat (limited to 'test/units/parsing/yaml/test_dumper.py')
-rw-r--r-- | test/units/parsing/yaml/test_dumper.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/test/units/parsing/yaml/test_dumper.py b/test/units/parsing/yaml/test_dumper.py index 8af1eeed..cbf5b456 100644 --- a/test/units/parsing/yaml/test_dumper.py +++ b/test/units/parsing/yaml/test_dumper.py @@ -19,6 +19,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import io +import yaml from jinja2.exceptions import UndefinedError @@ -26,6 +27,7 @@ from units.compat import unittest from ansible.parsing import vault from ansible.parsing.yaml import dumper, objects from ansible.parsing.yaml.loader import AnsibleLoader +from ansible.module_utils.six import PY2 from ansible.template import AnsibleUndefined from units.mock.yaml_helper import YamlTestUtils @@ -74,6 +76,20 @@ class TestAnsibleDumper(unittest.TestCase, YamlTestUtils): data_from_yaml = loader.get_single_data() result = b_text + if PY2: + # https://pyyaml.org/wiki/PyYAMLDocumentation#string-conversion-python-2-only + # pyyaml on Python 2 can return either unicode or bytes when given byte strings. + # We normalize that to always return unicode on Python2 as that's right most of the + # time. However, this means byte strings can round trip through yaml on Python3 but + # not on Python2. To make this code work the same on Python2 and Python3 (we want + # the Python3 behaviour) we need to change the methods in Ansible to: + # (1) Let byte strings pass through yaml without being converted on Python2 + # (2) Convert byte strings to text strings before being given to pyyaml (Without this, + # strings would end up as byte strings most of the time which would mostly be wrong) + # In practice, we mostly read bytes in from files and then pass that to pyyaml, for which + # the present behavior is correct. + # This is a workaround for the current behavior. + result = u'tr\xe9ma' self.assertEqual(result, data_from_yaml) @@ -89,7 +105,10 @@ class TestAnsibleDumper(unittest.TestCase, YamlTestUtils): self.assertEqual(u_text, data_from_yaml) def test_vars_with_sources(self): - self._dump_string(VarsWithSources(), dumper=self.dumper) + try: + self._dump_string(VarsWithSources(), dumper=self.dumper) + except yaml.representer.RepresenterError: + self.fail("Dump VarsWithSources raised RepresenterError unexpectedly!") def test_undefined(self): undefined_object = AnsibleUndefined() |