fixes saltstack/salt#65501 file.comment ignore_missing not working with multiline char

This commit is contained in:
nicholasmhughes 2023-11-14 16:25:13 -05:00 committed by Pedro Algarvio
parent 6531c36679
commit c5fbfa1fe7
3 changed files with 16 additions and 2 deletions

1
changelog/65501.fixed.md Normal file
View file

@ -0,0 +1 @@
Fix file.comment ignore_missing not working with multiline char

View file

@ -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

View file

@ -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",
]
@ -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"