Skip to content

Commit

Permalink
Merge pull request #51988 from mbunkus/fix-file-comment
Browse files Browse the repository at this point in the history
Fix file comment
  • Loading branch information
twangboy authored Mar 6, 2019
2 parents a15d4bc + faf87cf commit dd813d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
22 changes: 8 additions & 14 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5392,15 +5392,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
Expand Down Expand Up @@ -5498,18 +5492,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))

Expand Down
8 changes: 5 additions & 3 deletions tests/unit/states/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,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)
Expand All @@ -1177,7 +1177,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}):
Expand Down Expand Up @@ -1215,7 +1215,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))
Expand Down

0 comments on commit dd813d8

Please sign in to comment.