Add dedent that sets line endings

This commit is contained in:
Daniel A. Wozniak 2018-08-22 12:21:29 -07:00 committed by rallytime
parent 0cda22e7a9
commit 6399d035a4
No known key found for this signature in database
GPG key ID: E8F1A4B90D0DEA19
2 changed files with 65 additions and 59 deletions

View file

@ -30,7 +30,8 @@ from tests.support.helpers import (
with_tempdir,
with_tempfile,
Webserver,
destructiveTest
destructiveTest,
dedent,
)
from tests.support.mixins import SaltReturnAssertsMixin
@ -2508,64 +2509,57 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
class BlockreplaceTest(ModuleCase, SaltReturnAssertsMixin):
marker_start = '# start'
marker_end = '# end'
content = os.linesep.join([
'Line 1 of block',
'Line 2 of block',
''
])
without_block = os.linesep.join([
'Hello world!',
'',
'# comment here',
''
])
with_non_matching_block = os.linesep.join([
'Hello world!',
'',
'# start',
'No match here',
'# end',
'# comment here',
''
])
with_non_matching_block_and_marker_end_not_after_newline = os.linesep.join([
'Hello world!',
'',
'# start',
'No match here# end',
'# comment here',
''
])
with_matching_block = os.linesep.join([
'Hello world!',
'',
'# start',
'Line 1 of block',
'Line 2 of block',
'# end',
'# comment here',
''
])
with_matching_block_and_extra_newline = os.linesep.join([
'Hello world!',
'',
'# start',
'Line 1 of block',
'Line 2 of block',
'',
'# end',
'# comment here',
''
])
with_matching_block_and_marker_end_not_after_newline = os.linesep.join([
'Hello world!',
'',
'# start',
'Line 1 of block',
'Line 2 of block# end',
'# comment here',
''
])
content = dedent(six.text_type('''\
Line 1 of block
Line 2 of block
'''))
without_block = dedent(six.text_type('''\
Hello world!
# comment here
'''))
with_non_matching_block = dedent(six.text_type('''\
Hello world!
# start
No match here
# end
# comment here
'''))
with_non_matching_block_and_marker_end_not_after_newline = dedent(six.text_type('''\
Hello world!
# start
No match here# end
# comment here
'''))
with_matching_block = dedent(six.text_type('''\
Hello world!
# start
Line 1 of block
Line 2 of block
# end
# comment here
'''))
with_matching_block_and_extra_newline = dedent(six.text_type('''\
Hello world!
# start
Line 1 of block
Line 2 of block
# end
# comment here
'''))
with_matching_block_and_marker_end_not_after_newline = dedent(six.text_type('''\
Hello world!
# start
Line 1 of block
Line 2 of block# end
# comment here
'''))
content_explicit_posix_newlines = ('Line 1 of block\n'
'Line 2 of block\n')
content_explicit_windows_newlines = ('Line 1 of block\r\n'

View file

@ -27,6 +27,7 @@ import string
import subprocess
import sys
import tempfile
import textwrap
import threading
import time
import tornado.ioloop
@ -1604,3 +1605,14 @@ def this_user():
if salt.utils.platform.is_windows():
return salt.utils.win_functions.get_current_user(with_domain=False)
return pwd.getpwuid(os.getuid())[0]
def dedent(text, linesep=os.linesep):
'''
A wrapper around textwrap.dedent that also sets line endings.
'''
if isinstance(text, six.text_type):
linesep = salt.utils.stringutils.to_unicode(linesep)
else:
linesep = salt.utils.stringutils.to_str(linesep)
return linesep.join(textwrap.dedent(text).splitlines())