Improve salt.utils.build_whitepace_splited_regex(). Fixes #2379.

* Although we're building a regex which should ignore white space and new lines, the built regex should always match start and end of string.
This commit is contained in:
Pedro Algarvio 2012-11-08 19:33:51 +00:00
parent 98d0e2d5aa
commit b7c763dec1
2 changed files with 36 additions and 6 deletions

View File

@ -562,12 +562,8 @@ def build_whitepace_splited_regex(text):
regex = r''
for line in text.splitlines():
parts = [re.escape(s) for s in __build_parts(line)]
regex += r'(?:(?:[^])?(?:[\s]+)?|(?:[\s]+)?)'
regex += r'{0}'.format(
r'(?:(?:[^])?(?:[\s]+)?|(?:[\s]+)?)'.join(parts)
)
regex += r'(?:(?:[^])?(?:[\s]+)?|(?:[\s]+)?)'
return regex
regex += r'(?:[\s]+)?{0}(?:[\s]+)?'.format(r'(?:[\s]+)?'.join(parts))
return r'^{0}$'.format(regex)
def format_call(fun, data):

View File

@ -526,6 +526,40 @@ class FileTest(integration.ModuleCase):
if os.path.isfile(tmp_file):
os.remove(tmp_file)
def test_issue_2379_file_append(self):
# Get a path to the temporary file
tmp_file = os.path.join(integration.TMP, 'issue-2379-file-append.txt')
# Write some data to it
open(tmp_file, 'w').write(
'hello\nworld\n' + # Some junk
'#PermitRootLogin yes\n' + # Commented text
'# PermitRootLogin yes\n' # Commented text with space
)
# create the sls template
template_lines = [
"{0}:".format(tmp_file),
" file.append:",
" - text: PermitRootLogin yes"
]
template = '\n'.join(template_lines)
try:
ret = self.run_function('state.template_str', [template])
self.assertTrue(isinstance(ret, dict)), ret
self.assertNotEqual(ret, {})
for part in ret.itervalues():
self.assertTrue(part['result'])
self.assertEqual(
part['comment'], 'Appended 1 lines'
)
except AssertionError:
shutil.copy(tmp_file, tmp_file + '.bak')
raise
finally:
if os.path.isfile(tmp_file):
os.remove(tmp_file)
if __name__ == '__main__':
from integration import run_tests