diff --git a/changelog/59329.added.md b/changelog/59329.added.md new file mode 100644 index 000000000000..09b36b2dd9c3 --- /dev/null +++ b/changelog/59329.added.md @@ -0,0 +1 @@ +Add 'show_changes' arg for file.append and file.prepend states to hide output diff --git a/salt/states/file.py b/salt/states/file.py index aab52142b073..d0f8d0260141 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)) diff --git a/tests/pytests/functional/states/file/test_append.py b/tests/pytests/functional/states/file/test_append.py index 1d5d8cd9bc2d..aba3e07f39c9 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 55e7548b382e..219af9a8ecbe 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"] == ""