Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default_flow_style=None in yaml.dump calls #52957

Merged
merged 7 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions salt/serializers/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
1 change: 1 addition & 0 deletions salt/serializers/yamlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
2 changes: 2 additions & 0 deletions salt/utils/yamldumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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)
21 changes: 21 additions & 0 deletions tests/unit/serializers/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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):
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/utils/test_yamldumper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- 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.ext.six
import salt.utils.yamldumper

# Import Salt Testing Libs
from tests.support.unit import TestCase, skipIf
from tests.support.mock import NO_MOCK, NO_MOCK_REASON


@skipIf(NO_MOCK, NO_MOCK_REASON)
class YamlDumperTestCase(TestCase):
'''
TestCase for salt.utils.yamldumper module
'''
def test_yaml_dump(self):
'''
Test yaml.dump a dict
'''
data = {'foo': 'bar'}

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):
'''
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'