Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/59329.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add 'show_changes' arg for file.append and file.prepend states to hide output
22 changes: 22 additions & 0 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6918,6 +6918,7 @@ def append(
defaults=None,
context=None,
ignore_whitespace=True,
show_changes=True,
):
"""
Ensure that some text appears at the end of a file.
Expand Down Expand Up @@ -7009,6 +7010,12 @@ def append(
appending content, one space or multiple tabs are the same for salt.
Set this option to ``False`` if you want to change this behavior.

show_changes
.. versionadded:: 3008.0

Output a unified diff of the old file and the new file.
Set this option to ``False`` to disable this.

Multi-line example:

.. code-block:: yaml
Expand Down Expand Up @@ -7148,6 +7155,8 @@ def append(
if slines != nlines:
if not __utils__["files.is_text"](name):
ret["changes"]["diff"] = "Replace binary file"
elif not show_changes:
ret["changes"]["diff"] = "<show_changes=False>"
else:
# Changes happened, add them
ret["changes"]["diff"] = "\n".join(difflib.unified_diff(slines, nlines))
Expand All @@ -7170,6 +7179,8 @@ def append(
if slines != nlines:
if not __utils__["files.is_text"](name):
ret["changes"]["diff"] = "Replace binary file"
elif not show_changes:
ret["changes"]["diff"] = "<show_changes=False>"
else:
# Changes happened, add them
ret["changes"]["diff"] = "\n".join(difflib.unified_diff(slines, nlines))
Expand All @@ -7191,6 +7202,7 @@ def prepend(
defaults=None,
context=None,
header=None,
show_changes=True,
):
"""
Ensure that some text appears at the beginning of a file
Expand Down Expand Up @@ -7286,6 +7298,12 @@ def prepend(
Forces the text to be prepended. If it exists in the file but not at
the beginning, then it prepends a duplicate.

show_changes
.. versionadded:: 3008.0

Output a unified diff of the old file and the new file.
Set this option to ``False`` to disable this.

Multi-line example:

.. code-block:: yaml
Expand Down Expand Up @@ -7441,6 +7459,8 @@ def prepend(
if slines != nlines:
if not __utils__["files.is_text"](name):
ret["changes"]["diff"] = "Replace binary file"
elif not show_changes:
ret["changes"]["diff"] = "<show_changes=False>"
else:
# Changes happened, add them
ret["changes"]["diff"] = "".join(difflib.unified_diff(slines, nlines))
Expand Down Expand Up @@ -7480,6 +7500,8 @@ def prepend(
if slines != nlines:
if not __utils__["files.is_text"](name):
ret["changes"]["diff"] = "Replace binary file"
elif not show_changes:
ret["changes"]["diff"] = "<show_changes=False>"
else:
# Changes happened, add them
ret["changes"]["diff"] = "".join(difflib.unified_diff(slines, nlines))
Expand Down
23 changes: 23 additions & 0 deletions tests/pytests/functional/states/file/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,26 @@ def test_file_append_check_cmd(modules, state_tree, tmp_path):
for state_run in ret:
assert state_run.result is False
assert state_run.comment == "check_cmd determined the state failed"


@pytest.mark.parametrize("show_changes", (None, True, False))
def test_append_show_changes(file, show_changes, tmp_path):
"""
Test show_changes argument for file.append
"""

name = tmp_path / "testfile-show_changes"
name.write_text("#salty!")
if show_changes is None:
ret = file.append(name=str(name), text="cheese")
else:
ret = file.append(name=str(name), text="cheese", show_changes=show_changes)

assert ret.result is True
assert name.exists()

if show_changes in [True, None]:
assert "diff" in ret.changes
assert "cheese" in ret.changes["diff"]
else:
assert ret.changes["diff"] == "<show_changes=False>"
23 changes: 23 additions & 0 deletions tests/pytests/functional/states/file/test_prepend.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,26 @@ def test_prepend_issue_27401_makedirs(file, tmp_path):
assert name.is_file()
assert name.read_text() == "cheese\n"
assert name.parent.is_dir()


@pytest.mark.parametrize("show_changes", (None, True, False))
def test_prepend_show_changes(file, show_changes, tmp_path):
"""
Test show_changes argument for file.prepend
"""

name = tmp_path / "testfile-prepend-show_changes"
name.write_text("#salty!")
if show_changes is None:
ret = file.prepend(name=str(name), text="cheese")
else:
ret = file.prepend(name=str(name), text="cheese", show_changes=show_changes)

assert ret.result is True
assert name.exists()

if show_changes in [True, None]:
assert "diff" in ret.changes
assert "cheese" in ret.changes["diff"]
else:
assert ret.changes["diff"] == "<show_changes=False>"
Loading