mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #48156 from garethgreenaway/48113_ensure_writing_strings_with_file_dot_line
[2018.3] Unicode fixes for file.line
This commit is contained in:
commit
85ebcbd3f2
2 changed files with 29 additions and 1 deletions
|
@ -2039,7 +2039,12 @@ def line(path, content=None, match=None, mode=None, location=None,
|
|||
fh_ = None
|
||||
try:
|
||||
# Make sure we match the file mode from salt.utils.files.fopen
|
||||
mode = 'wb' if six.PY2 and salt.utils.platform.is_windows() else 'w'
|
||||
if six.PY2 and salt.utils.platform.is_windows():
|
||||
mode = 'wb'
|
||||
body = salt.utils.stringutils.to_bytes(body)
|
||||
else:
|
||||
mode = 'w'
|
||||
body = salt.utils.stringutils.to_str(body)
|
||||
fh_ = salt.utils.atomicfile.atomic_open(path, mode)
|
||||
fh_.write(body)
|
||||
finally:
|
||||
|
|
|
@ -1063,6 +1063,29 @@ class FilemodLineTests(TestCase, LoaderModuleMockMixin):
|
|||
self.assertEqual(atomic_opener().write.call_args_list[0][0][0],
|
||||
file_modified)
|
||||
|
||||
@patch('os.path.realpath', MagicMock())
|
||||
@patch('os.path.isfile', MagicMock(return_value=True))
|
||||
@patch('os.stat', MagicMock())
|
||||
def test_line_insert_multi_line_content_after_unicode(self):
|
||||
'''
|
||||
Test for file.line for insertion after specific line with Unicode
|
||||
|
||||
See issue #48113
|
||||
:return:
|
||||
'''
|
||||
file_content = ("This is a line\nThis is another line")
|
||||
file_modified = salt.utils.stringutils.to_str("This is a line\nThis is another line\nThis is a line with unicode Ŷ")
|
||||
cfg_content = "This is a line with unicode Ŷ"
|
||||
for after_line in ['This is another line']:
|
||||
files_fopen = mock_open(read_data=file_content)
|
||||
with patch('salt.utils.files.fopen', files_fopen):
|
||||
atomic_opener = mock_open()
|
||||
with patch('salt.utils.atomicfile.atomic_open', atomic_opener):
|
||||
filemod.line('foo', content=cfg_content, after=after_line, mode='insert', indent=False)
|
||||
self.assertEqual(len(atomic_opener().write.call_args_list), 1)
|
||||
self.assertEqual(atomic_opener().write.call_args_list[0][0][0],
|
||||
file_modified)
|
||||
|
||||
@patch('os.path.realpath', MagicMock())
|
||||
@patch('os.path.isfile', MagicMock(return_value=True))
|
||||
@patch('os.stat', MagicMock())
|
||||
|
|
Loading…
Add table
Reference in a new issue