Fix issue 49043

This commit is contained in:
Daniel A Wozniak 2018-09-26 06:26:42 +00:00 committed by Daniel A. Wozniak
parent 413868329c
commit 2a901e960d
No known key found for this signature in database
GPG Key ID: 166B9D2C06C82D61
6 changed files with 46 additions and 6 deletions

View File

@ -1118,7 +1118,7 @@ def _get_template_texts(source_list=None,
tmplines = None
with salt.utils.files.fopen(rndrd_templ_fn, 'rb') as fp_:
tmplines = fp_.read()
tmplines = tmplines.decode(__salt_system_encoding__)
tmplines = salt.utils.stringutils.to_unicode(tmplines)
tmplines = tmplines.splitlines(True)
if not tmplines:
msg = 'Failed to read rendered template file {0} ({1})'

View File

@ -791,10 +791,10 @@ def backup_minion(path, bkroot):
def get_encoding(path):
'''
Detect a file's encoding using the following:
- Check for ascii
- Check for Byte Order Marks (BOM)
- Check for UTF-8 Markers
- Check System Encoding
- Check for ascii
Args:
@ -867,10 +867,6 @@ def get_encoding(path):
except os.error:
raise CommandExecutionError('Failed to open file')
# Check for ASCII first
if check_ascii(data):
return 'ASCII'
# Check for Unicode BOM
encoding = check_bom(data)
if encoding:
@ -884,4 +880,8 @@ def get_encoding(path):
if check_system_encoding(data):
return __salt_system_encoding__
# Check for ASCII first
if check_ascii(data):
return 'ASCII'
raise CommandExecutionError('Could not detect file encoding')

View File

@ -558,6 +558,8 @@ def get_diff(a, b, *args, **kwargs):
with unicode on PY2.
'''
encoding = ('utf-8', 'latin-1', __salt_system_encoding__)
if 'lineterm' not in kwargs:
kwargs['lineterm'] = os.linesep
# Late import to avoid circular import
import salt.utils.data
return ''.join(

View File

@ -0,0 +1 @@
{{unicode_string}}

View File

@ -0,0 +1,16 @@
somefile-exists:
file:
- managed
- name: {{ pillar['name'] }}
somefile-blockreplace:
file:
- blockreplace
- append_if_not_found: true
- name: {{ pillar['name'] }}
- template: jinja
- source: salt://issue-49043
- require:
- file: somefile-exists
- context:
unicode_string: "\xe4\xf6\xfc"

View File

@ -3747,6 +3747,27 @@ class BlockreplaceTest(ModuleCase, SaltReturnAssertsMixin):
self._read(name),
self.with_matching_block_and_marker_end_not_after_newline)
@with_tempfile()
def test_issue_49043(self, name):
ret = self.run_function(
'state.sls',
mods='issue-49043',
pillar={'name': name},
)
log.error("ret = %s", repr(ret))
diff = dedent('''\
---
+++
@@ -0,0 +1,3 @@
+#-- start managed zone --
+äöü
+#-- end managed zone --
''')
job = 'file_|-somefile-blockreplace_|-{}_|-blockreplace'.format(name)
self.assertEqual(
ret[job]['changes']['diff'],
diff)
class RemoteFileTest(ModuleCase, SaltReturnAssertsMixin):
'''