Fix file.line line endings

This commit is contained in:
Daniel A. Wozniak 2018-08-09 02:47:40 +00:00
parent 4590494b50
commit baf291b4c8
No known key found for this signature in database
GPG Key ID: 166B9D2C06C82D61
2 changed files with 34 additions and 3 deletions

View File

@ -158,7 +158,12 @@ def atomic_open(filename, mode='w'):
'''
if mode in ('r', 'rb', 'r+', 'rb+', 'a', 'ab'):
raise TypeError('Read or append modes don\'t work with atomic_open')
ntf = tempfile.NamedTemporaryFile(mode, prefix='.___atomic_write',
dir=os.path.dirname(filename),
delete=False)
kwargs = {
'prefix': '.___atomic_write',
'dir': os.path.dirname(filename),
'delete': False,
}
if six.PY3:
kwargs['newline'] = ''
ntf = tempfile.NamedTemporaryFile(mode, **kwargs)
return _AtomicWFile(ntf, ntf.name, filename)

View File

@ -2404,6 +2404,32 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
self.assertEqual(pwd.getpwuid(temp_file_stats.st_uid).pw_name, user)
self.assertEqual(grp.getgrgid(temp_file_stats.st_gid).gr_name, group)
@with_tempdir()
def test_issue_48557(self, tempdir):
tempfile = os.path.join(tempdir, 'temp_file_issue_48557')
with open(tempfile, 'wb') as fp:
fp.write(os.linesep.join([
'test1',
'test2',
'test3',
'',
]).encode('utf-8'))
ret = self.run_state('file.line',
name=tempfile,
after='test2',
mode='insert',
content='test4')
self.assertSaltTrueReturn(ret)
with open(tempfile, 'rb') as fp:
content = fp.read()
self.assertEqual(content, os.linesep.join([
'test1',
'test2',
'test4',
'test3',
'',
]).encode('utf-8'))
class BlockreplaceTest(ModuleCase, SaltReturnAssertsMixin):
marker_start = '# start'