From 734f084407db7fac82779dd50bca0f9eb64d86ee Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Wed, 18 Sep 2019 18:07:09 -0700 Subject: [PATCH] Porting PR #51988 to 2019.2.1 --- salt/states/file.py | 22 ++++++++-------------- tests/unit/states/test_file.py | 8 +++++--- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/salt/states/file.py b/salt/states/file.py index 4e451c58f812..1b1657b7e371 100644 --- a/salt/states/file.py +++ b/salt/states/file.py @@ -4904,15 +4904,9 @@ def comment(name, regex, char='#', backup='.bak'): comment_regex = char + unanchor_regex - # Check if the line is already commented - if __salt__['file.search'](name, comment_regex, multiline=True): - commented = True - else: - commented = False - # Make sure the pattern appears in the file before continuing - if commented or not __salt__['file.search'](name, regex, multiline=True): - if __salt__['file.search'](name, unanchor_regex, multiline=True): + if not __salt__['file.search'](name, regex, multiline=True): + if __salt__['file.search'](name, comment_regex, multiline=True): ret['comment'] = 'Pattern already commented' ret['result'] = True return ret @@ -5010,18 +5004,18 @@ def uncomment(name, regex, char='#', backup='.bak'): # Make sure the pattern appears in the file if __salt__['file.search']( + name, + '{0}[ \t]*{1}'.format(char, regex.lstrip('^')), + multiline=True): + # Line exists and is commented + pass + elif __salt__['file.search']( name, '^[ \t]*{0}'.format(regex.lstrip('^')), multiline=True): ret['comment'] = 'Pattern already uncommented' ret['result'] = True return ret - elif __salt__['file.search']( - name, - '{0}[ \t]*{1}'.format(char, regex.lstrip('^')), - multiline=True): - # Line exists and is commented - pass else: return _error(ret, '{0}: Pattern not found'.format(regex)) diff --git a/tests/unit/states/test_file.py b/tests/unit/states/test_file.py index 72cc1fe72867..6451f93121c1 100644 --- a/tests/unit/states/test_file.py +++ b/tests/unit/states/test_file.py @@ -1168,7 +1168,7 @@ def test_comment(self): with patch.object(os.path, 'isabs', mock_t): with patch.dict(filestate.__salt__, - {'file.search': MagicMock(side_effect=[True, True, True, False, False])}): + {'file.search': MagicMock(side_effect=[False, True, False, False])}): comt = ('Pattern already commented') ret.update({'comment': comt, 'result': True}) self.assertDictEqual(filestate.comment(name, regex), ret) @@ -1178,7 +1178,7 @@ def test_comment(self): self.assertDictEqual(filestate.comment(name, regex), ret) with patch.dict(filestate.__salt__, - {'file.search': MagicMock(side_effect=[False, True, False, True, True]), + {'file.search': MagicMock(side_effect=[True, True, True]), 'file.comment': mock_t, 'file.comment_line': mock_t}): with patch.dict(filestate.__opts__, {'test': True}): @@ -1216,7 +1216,9 @@ def test_uncomment(self): mock_t = MagicMock(return_value=True) mock_f = MagicMock(return_value=False) - mock = MagicMock(side_effect=[True, False, False, False, True, False, + mock = MagicMock(side_effect=[False, True, + False, False, + True, True, True]) with patch.object(os.path, 'isabs', mock_f): comt = ('Specified file {0} is not an absolute path'.format(name))