Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back-port #49291 and #49331 to 2018.3.3 #49468

Merged
merged 5 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 54 additions & 60 deletions tests/integration/states/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
with_tempdir,
with_tempfile,
Webserver,
destructiveTest
destructiveTest,
dedent,
)
from tests.support.mixins import SaltReturnAssertsMixin

Expand Down Expand Up @@ -2096,7 +2097,7 @@ def test_issue_8343_accumulated_require_in(self, base_dir):
'#-- end salt managed zoneend --',
'']

self.assertEqual([line.encode('utf-8') for line in expected], contents)
self.assertEqual([salt.utils.stringutils.to_str(line) for line in expected], contents)

@with_tempdir()
def test_issue_11003_immutable_lazy_proxy_sum(self, base_dir):
Expand Down Expand Up @@ -2508,64 +2509,57 @@ def test_issue_48557(self, tempdir):
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'
Expand Down
15 changes: 15 additions & 0 deletions tests/support/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import subprocess
import sys
import tempfile
import textwrap
import threading
import time
import tornado.ioloop
Expand Down Expand Up @@ -1604,3 +1605,17 @@ 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.
'''
linesep = salt.utils.to_unicode(linesep)
unicode_text = textwrap.dedent(salt.utils.to_unicode(text))
clean_text = linesep.join(unicode_text.splitlines())
if unicode_text.endswith(u'\n'):
clean_text += linesep
if not isinstance(text, six.text_type):
return salt.utils.to_bytes(clean_text)
return clean_text