Skip to content

Commit

Permalink
fixes saltstack#62044 add ignore_missing to file.comment state
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasmhughes committed May 10, 2022
1 parent d5bfa7b commit 987830a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/62044.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ignore_missing parameter to file.comment state
17 changes: 15 additions & 2 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6004,8 +6004,11 @@ def blockreplace(
return ret


def comment(name, regex, char="#", backup=".bak"):
def comment(name, regex, char="#", backup=".bak", ignore_missing=False):
"""
.. versionadded:: 0.9.5
.. versionchanged:: 3005
Comment out specified lines in a file.
name
Expand All @@ -6030,6 +6033,12 @@ def comment(name, regex, char="#", backup=".bak"):
after the first invocation.
Set to False/None to not keep a backup.
ignore_missing
Ignore a failure to find the regex in the file. This is useful for
scenarios where a line must only be commented if it is found in the
file.
.. versionadded:: 3005
Usage:
Expand All @@ -6039,7 +6048,6 @@ def comment(name, regex, char="#", backup=".bak"):
file.comment:
- regex: ^bind 127.0.0.1
.. versionadded:: 0.9.5
"""
name = os.path.expanduser(name)

Expand All @@ -6062,6 +6070,10 @@ def comment(name, regex, char="#", backup=".bak"):
ret["comment"] = "Pattern already commented"
ret["result"] = True
return ret
elif ignore_missing:
ret["comment"] = "Pattern not found and ignore_missing set to True"
ret["result"] = True
return ret
else:
return _error(ret, "{}: Pattern not found".format(unanchor_regex))

Expand All @@ -6070,6 +6082,7 @@ def comment(name, regex, char="#", backup=".bak"):
ret["comment"] = "File {} is set to be updated".format(name)
ret["result"] = None
return ret

with salt.utils.files.fopen(name, "rb") as fp_:
slines = fp_.read()
slines = slines.decode(__salt_system_encoding__)
Expand Down
10 changes: 9 additions & 1 deletion tests/pytests/unit/states/file/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ def test_comment():
with patch.object(os.path, "isabs", mock_t):
with patch.dict(
filestate.__salt__,
{"file.search": MagicMock(side_effect=[False, True, False, False])},
{
"file.search": MagicMock(
side_effect=[False, True, False, False, False, False]
)
},
):
comt = "Pattern already commented"
ret.update({"comment": comt, "result": True})
assert filestate.comment(name, regex) == ret

comt = "Pattern not found and ignore_missing set to True"
ret.update({"comment": comt, "result": True})
assert filestate.comment(name, regex, ignore_missing=True) == ret

comt = "{}: Pattern not found".format(regex)
ret.update({"comment": comt, "result": False})
assert filestate.comment(name, regex) == ret
Expand Down

0 comments on commit 987830a

Please sign in to comment.