Skip to content

Commit

Permalink
fixes saltstack#65501 file.comment ignore_missing not working with mu…
Browse files Browse the repository at this point in the history
…ltiline char
  • Loading branch information
nicholasmhughes committed Nov 14, 2023
1 parent 6531c36 commit eb30dec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/65501.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix file.comment ignore_missing not working with multiline char
2 changes: 1 addition & 1 deletion salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6171,7 +6171,7 @@ def comment(name, regex, char="#", backup=".bak", ignore_missing=False):
# remove (?i)-like flags, ^ and $
unanchor_regex = re.sub(r"^(\(\?[iLmsux]\))?\^?(.*?)\$?$", r"\2", regex)

uncomment_regex = rf"^(?!\s*{char}).*" + unanchor_regex
uncomment_regex = rf"^(?!\s*{char})\s*" + unanchor_regex
comment_regex = char + unanchor_regex

# Make sure the pattern appears in the file before continuing
Expand Down
15 changes: 14 additions & 1 deletion tests/pytests/functional/states/file/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_issue_2401_file_comment(modules, tmp_path):
tmp_file.write_text("hello\nworld\n")
# create the sls template
template_lines = [
"{}:".format(tmp_file),
f"{tmp_file}:",
" file.comment:",
" - regex: ^world",
]
Expand All @@ -122,3 +122,16 @@ def test_issue_2401_file_comment(modules, tmp_path):
for state_run in ret:
assert state_run.result is True
assert "Pattern already commented" in state_run.comment


def test_issue_65501(file, tmp_path):
tmp_file = tmp_path / "issue-65501.txt"
tmp_file.write_text("first\n#PermitRootLogin prohibit-password\nlast")
ret = file.comment(
name=str(tmp_file),
regex="^PermitRootLogin[ \t]+.*$",
char="# NEXT LINE COMMENT SALTSTACK openssh-server_comment_permitrootlogin_sshd_config\n# ",
ignore_missing=True,
)
assert ret.result is True
assert ret.comment == "Pattern not found and ignore_missing set to True"

0 comments on commit eb30dec

Please sign in to comment.