From 70d578a1af1c33029dc239568c86170992f32979 Mon Sep 17 00:00:00 2001 From: Ch3LL Date: Thu, 9 May 2019 15:18:32 -0400 Subject: [PATCH 1/4] Set default_flow_style=None in yaml.dump calls --- salt/serializers/yaml.py | 1 + salt/serializers/yamlex.py | 1 + salt/utils/yamldumper.py | 2 ++ tests/unit/serializers/test_serializers.py | 21 ++++++++++++ tests/unit/utils/test_yamldumper.py | 38 ++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 tests/unit/utils/test_yamldumper.py diff --git a/salt/serializers/yaml.py b/salt/serializers/yaml.py index c95b40e48fbf..2154e5dcabfb 100644 --- a/salt/serializers/yaml.py +++ b/salt/serializers/yaml.py @@ -72,6 +72,7 @@ def serialize(obj, **options): ''' options.setdefault('Dumper', Dumper) + options.setdefault('default_flow_style', None) try: response = yaml.dump(obj, **options) if response.endswith('\n...\n'): diff --git a/salt/serializers/yamlex.py b/salt/serializers/yamlex.py index 2e4ba6fb2b60..e42634f58044 100644 --- a/salt/serializers/yamlex.py +++ b/salt/serializers/yamlex.py @@ -182,6 +182,7 @@ def serialize(obj, **options): ''' options.setdefault('Dumper', Dumper) + options.setdefault('default_flow_style', None) try: response = yaml.dump(obj, **options) if response.endswith('\n...\n'): diff --git a/salt/utils/yamldumper.py b/salt/utils/yamldumper.py index 9892c87d231e..3692ea324aa3 100644 --- a/salt/utils/yamldumper.py +++ b/salt/utils/yamldumper.py @@ -115,6 +115,7 @@ def dump(data, stream=None, **kwargs): ''' if 'allow_unicode' not in kwargs: kwargs['allow_unicode'] = True + kwargs.setdefault('default_flow_style', None) return yaml.dump(data, stream, **kwargs) @@ -126,4 +127,5 @@ def safe_dump(data, stream=None, **kwargs): ''' if 'allow_unicode' not in kwargs: kwargs['allow_unicode'] = True + kwargs.setdefault('default_flow_style', None) return yaml.dump(data, stream, Dumper=SafeOrderedDumper, **kwargs) diff --git a/tests/unit/serializers/test_serializers.py b/tests/unit/serializers/test_serializers.py index 8ebedcdd3fa2..84bb4724bbe9 100644 --- a/tests/unit/serializers/test_serializers.py +++ b/tests/unit/serializers/test_serializers.py @@ -68,9 +68,24 @@ def test_serialize_sls(self): serialized = yamlex.serialize(data) assert serialized == '{foo: bar}', serialized + serialized = yamlex.serialize(data, default_flow_style=False) + assert serialized == 'foo: bar', serialized + deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized + serialized = yaml.serialize(data) + assert serialized == '{foo: bar}', serialized + + deserialized = yaml.deserialize(serialized) + assert deserialized == data, deserialized + + serialized = yaml.serialize(data, default_flow_style=False) + assert serialized == 'foo: bar', serialized + + deserialized = yaml.deserialize(serialized) + assert deserialized == data, deserialized + @skipIf(not yamlex.available, SKIP_MESSAGE % 'sls') def test_serialize_complex_sls(self): data = OrderedDict([ @@ -84,6 +99,12 @@ def test_serialize_complex_sls(self): deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized + serialized = yaml.serialize(data) + assert serialized == '{bar: 2, baz: true, foo: 1}', serialized + + deserialized = yaml.deserialize(serialized) + assert deserialized == data, deserialized + @skipIf(not yaml.available, SKIP_MESSAGE % 'yaml') @skipIf(not yamlex.available, SKIP_MESSAGE % 'sls') def test_compare_sls_vs_yaml(self): diff --git a/tests/unit/utils/test_yamldumper.py b/tests/unit/utils/test_yamldumper.py new file mode 100644 index 000000000000..22e77e3168b5 --- /dev/null +++ b/tests/unit/utils/test_yamldumper.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +''' + Unit tests for salt.utils.yamldumper +''' + +# Import python libs +from __future__ import absolute_import, print_function, unicode_literals + +# Import Salt Libs +import salt.utils.yamldumper + +# Import Salt Testing Libs +from tests.support.unit import TestCase, skipIf +from tests.support.mock import patch, NO_MOCK, NO_MOCK_REASON, mock_open + + +@skipIf(NO_MOCK, NO_MOCK_REASON) +class YamlDumperTestCase(TestCase): + ''' + TestCase for salt.utils.yamlloader module + ''' + def test_yaml_dump(self): + ''' + Test yaml.dump a dict + ''' + data = {'foo': 'bar'} + assert salt.utils.yamldumper.dump(data) == '{!!python/unicode \'foo\': !!python/unicode \'bar\'}\n' + + assert salt.utils.yamldumper.dump(data, default_flow_style=False) == '!!python/unicode \'foo\': !!python/unicode \'bar\'\n' + + def test_yaml_safe_dump(self): + ''' + Test yaml.safe_dump a dict + ''' + data = {'foo': 'bar'} + assert salt.utils.yamldumper.safe_dump(data) == '{foo: bar}\n' + + assert salt.utils.yamldumper.safe_dump(data, default_flow_style=False) == 'foo: bar\n' From bf648e57a8d890e50234e91287d602602dfd1e90 Mon Sep 17 00:00:00 2001 From: Megan Wilhite Date: Thu, 9 May 2019 15:39:12 -0400 Subject: [PATCH 2/4] Update tests/unit/utils/test_yamldumper.py Co-Authored-By: Wayne Werner --- tests/unit/utils/test_yamldumper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_yamldumper.py b/tests/unit/utils/test_yamldumper.py index 22e77e3168b5..d46715505824 100644 --- a/tests/unit/utils/test_yamldumper.py +++ b/tests/unit/utils/test_yamldumper.py @@ -11,7 +11,7 @@ # Import Salt Testing Libs from tests.support.unit import TestCase, skipIf -from tests.support.mock import patch, NO_MOCK, NO_MOCK_REASON, mock_open +from tests.support.mock import NO_MOCK, NO_MOCK_REASON @skipIf(NO_MOCK, NO_MOCK_REASON) From 20bc9544830f329c07c8f80e2f38a7fc9fdb3245 Mon Sep 17 00:00:00 2001 From: Ch3LL Date: Thu, 9 May 2019 15:42:52 -0400 Subject: [PATCH 3/4] Update docs for yamldumper test --- tests/unit/utils/test_yamldumper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_yamldumper.py b/tests/unit/utils/test_yamldumper.py index d46715505824..b39aa0210129 100644 --- a/tests/unit/utils/test_yamldumper.py +++ b/tests/unit/utils/test_yamldumper.py @@ -17,7 +17,7 @@ @skipIf(NO_MOCK, NO_MOCK_REASON) class YamlDumperTestCase(TestCase): ''' - TestCase for salt.utils.yamlloader module + TestCase for salt.utils.yamldumper module ''' def test_yaml_dump(self): ''' From 0d0e354627441daf33ea8c5702c3977de992cc7a Mon Sep 17 00:00:00 2001 From: Ch3LL Date: Fri, 10 May 2019 14:52:58 -0400 Subject: [PATCH 4/4] Fix yamldumper test for both py2/py3 --- tests/unit/utils/test_yamldumper.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/unit/utils/test_yamldumper.py b/tests/unit/utils/test_yamldumper.py index b39aa0210129..7056d90fec20 100644 --- a/tests/unit/utils/test_yamldumper.py +++ b/tests/unit/utils/test_yamldumper.py @@ -7,6 +7,7 @@ from __future__ import absolute_import, print_function, unicode_literals # Import Salt Libs +import salt.ext.six import salt.utils.yamldumper # Import Salt Testing Libs @@ -24,9 +25,15 @@ def test_yaml_dump(self): Test yaml.dump a dict ''' data = {'foo': 'bar'} - assert salt.utils.yamldumper.dump(data) == '{!!python/unicode \'foo\': !!python/unicode \'bar\'}\n' - assert salt.utils.yamldumper.dump(data, default_flow_style=False) == '!!python/unicode \'foo\': !!python/unicode \'bar\'\n' + if salt.ext.six.PY2: + exp_yaml = '{!!python/unicode \'foo\': !!python/unicode \'bar\'}\n' + else: + exp_yaml = '{foo: bar}\n' + + assert salt.utils.yamldumper.dump(data) == exp_yaml + + assert salt.utils.yamldumper.dump(data, default_flow_style=False) == exp_yaml.replace('{', '').replace('}', '') def test_yaml_safe_dump(self): '''