From c4d30d6c7273045d4c7200651999386d68f5542b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Wed, 29 Apr 2026 13:53:46 +0100 Subject: [PATCH 1/4] Add 'show_changes' to file.append and file.prepend Co-authored-by: Adam Bolte --- salt/states/file.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/salt/states/file.py b/salt/states/file.py index aab52142b07..9b672e5f039 100644 --- a/salt/states/file.py +++ b/salt/states/file.py @@ -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. @@ -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 @@ -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"] = "" else: # Changes happened, add them ret["changes"]["diff"] = "\n".join(difflib.unified_diff(slines, nlines)) @@ -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"] = "" else: # Changes happened, add them ret["changes"]["diff"] = "\n".join(difflib.unified_diff(slines, nlines)) @@ -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 @@ -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 @@ -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"] = "" else: # Changes happened, add them ret["changes"]["diff"] = "".join(difflib.unified_diff(slines, nlines)) @@ -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"] = "" else: # Changes happened, add them ret["changes"]["diff"] = "".join(difflib.unified_diff(slines, nlines)) From 63a1a4b7fc674ad2cee1fd53ee0f4b0ec1319721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Wed, 29 Apr 2026 15:20:04 +0100 Subject: [PATCH 2/4] Add show_changes tests for file.append and file.prepend --- .../functional/states/file/test_append.py | 23 +++++++++++++++++++ .../functional/states/file/test_prepend.py | 23 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/tests/pytests/functional/states/file/test_append.py b/tests/pytests/functional/states/file/test_append.py index 1d5d8cd9bc2..aba3e07f39c 100644 --- a/tests/pytests/functional/states/file/test_append.py +++ b/tests/pytests/functional/states/file/test_append.py @@ -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"] == "" diff --git a/tests/pytests/functional/states/file/test_prepend.py b/tests/pytests/functional/states/file/test_prepend.py index 55e7548b382..219af9a8ecb 100644 --- a/tests/pytests/functional/states/file/test_prepend.py +++ b/tests/pytests/functional/states/file/test_prepend.py @@ -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"] == "" From e6f06afff3b53968bb9eba28e0d98b664db20da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Wed, 29 Apr 2026 15:46:32 +0100 Subject: [PATCH 3/4] Add changelog file --- changelog/59329.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/59329.added.md diff --git a/changelog/59329.added.md b/changelog/59329.added.md new file mode 100644 index 00000000000..09b36b2dd9c --- /dev/null +++ b/changelog/59329.added.md @@ -0,0 +1 @@ +Add 'show_changes' arg for file.append and file.prepend states to hide output From 3f986f1e2654ea4f6b67ace5b05434014ec83f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Mon, 4 May 2026 12:39:00 +0100 Subject: [PATCH 4/4] Fix issue in docstring for file.append and file.prepend --- salt/states/file.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/states/file.py b/salt/states/file.py index 9b672e5f039..d0f8d026014 100644 --- a/salt/states/file.py +++ b/salt/states/file.py @@ -7014,7 +7014,7 @@ def append( .. versionadded:: 3008.0 Output a unified diff of the old file and the new file. - Set this option to ``False` to disable this. + Set this option to ``False`` to disable this. Multi-line example: @@ -7302,7 +7302,7 @@ def prepend( .. versionadded:: 3008.0 Output a unified diff of the old file and the new file. - Set this option to ``False` to disable this. + Set this option to ``False`` to disable this. Multi-line example: