Fixed issue with file.line on Windows running Python 2.

The target file was being read as binary, but written as text, causing file.line to append an extra linesep on every line.

Added integration test to ensure file.line produces the expected content.
This commit is contained in:
Gabe Van Engel 2017-11-12 18:07:50 -08:00
parent 8f89c99fa5
commit ef7b6bbb81
No known key found for this signature in database
GPG key ID: 424CAF57744AFEDA
2 changed files with 10 additions and 1 deletions

View file

@ -1861,7 +1861,9 @@ def line(path, content, match=None, mode=None, location=None,
if __opts__['test'] is False:
fh_ = None
try:
fh_ = salt.utils.atomicfile.atomic_open(path, 'w')
# Make sure we match the file mode from salt.utils.fopen
mode = 'wb' if six.PY2 and salt.utils.is_windows() else 'w'
fh_ = salt.utils.atomicfile.atomic_open(path, mode)
fh_.write(body)
finally:
if fh_:

View file

@ -323,6 +323,13 @@ class FileModuleTest(integration.ModuleCase):
mode='insert', after='Hello')
self.assertIn('Hello' + os.linesep + '+Goodbye', ret)
def test_file_line_content(self):
self.minion_run('file.line', self.myfile, 'Goodbye',
mode='insert', after='Hello')
with salt.utils.fopen(self.myfile, 'r') as fp:
content = fp.read()
self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)
if __name__ == '__main__':
from integration import run_tests
run_tests(FileModuleTest)