-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #62121 file.comment state picks up comments after config in the…
… line
- Loading branch information
1 parent
5a18c14
commit 03f913c
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix broken file.comment functionality introduced in #62045 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
Tests for file.comment state function | ||
""" | ||
import re | ||
|
||
import pytest | ||
import salt.utils.files | ||
from tests.support.helpers import dedent | ||
|
||
pytestmark = [ | ||
pytest.mark.windows_whitelisted, | ||
] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def file(states): | ||
return states.file | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def source(): | ||
with pytest.helpers.temp_file( | ||
name="file.txt", | ||
contents=dedent( | ||
""" | ||
things = stuff | ||
port = 5432 # (change requires restart) | ||
# commented = something | ||
moar = things | ||
""" | ||
), | ||
) as source: | ||
yield source | ||
|
||
|
||
def test_issue_62121(file, source): | ||
""" | ||
Test file.comment when the comment character is | ||
later in the line, after the text | ||
""" | ||
regex = r"^port\s*=.+" | ||
reg_cmp = re.compile(regex, re.MULTILINE) | ||
cmt_regex = r"^#port\s*=.+" | ||
cmt_cmp = re.compile(cmt_regex, re.MULTILINE) | ||
|
||
with salt.utils.files.fopen(source) as _fp: | ||
assert reg_cmp.findall(_fp.read()) | ||
|
||
file.comment(name=str(source), regex=regex) | ||
|
||
with salt.utils.files.fopen(source) as _fp: | ||
assert not reg_cmp.findall(_fp.read()) | ||
_fp.seek(0) | ||
assert cmt_cmp.findall(_fp.read()) |