File info_installed-works-without-status-attr-now.patch of Package salt.28025

From 3f8e937d938f19dd40fde527497f7775bbffe353 Mon Sep 17 00:00:00 2001
From: Alexander Graul <agraul@suse.com>
Date: Tue, 25 Jan 2022 17:12:47 +0100
Subject: [PATCH] info_installed works without status attr now

If 'status' was excluded via attr, info_installed was no longer able to
detect if a package was installed or not. Now info_installed adds the
'status' for the 'lowpkg.info' request again.
---
 salt/modules/aptpkg.py                    |  9 +++++++++
 tests/pytests/unit/modules/test_aptpkg.py | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/salt/modules/aptpkg.py b/salt/modules/aptpkg.py
index 3c3fbf4970..0d378355ab 100644
--- a/salt/modules/aptpkg.py
+++ b/salt/modules/aptpkg.py
@@ -3056,6 +3056,15 @@ def info_installed(*names, **kwargs):
     failhard = kwargs.pop("failhard", True)
     kwargs.pop("errors", None)  # Only for compatibility with RPM
     attr = kwargs.pop("attr", None)  # Package attributes to return
+
+    # status is needed to see if a package is installed. So we have to add it,
+    # even if it's excluded via attr parameter. Otherwise all packages are
+    # returned.
+    if attr:
+        attr_list = set(attr.split(","))
+        attr_list.add("status")
+        attr = ",".join(attr_list)
+
     all_versions = kwargs.pop(
         "all_versions", False
     )  # This is for backward compatible structure only
diff --git a/tests/pytests/unit/modules/test_aptpkg.py b/tests/pytests/unit/modules/test_aptpkg.py
index 51b7ffbe4d..8c64c8c9c1 100644
--- a/tests/pytests/unit/modules/test_aptpkg.py
+++ b/tests/pytests/unit/modules/test_aptpkg.py
@@ -361,6 +361,24 @@ def test_info_installed_attr(lowpkg_info_var):
         assert ret["wget"] == expected_pkg
 
 
+def test_info_installed_attr_without_status(lowpkg_info_var):
+    """
+    Test info_installed 'attr' for inclusion of 'status' attribute.
+
+    Since info_installed should only return installed packages, we need to
+    call __salt__['lowpkg.info'] with the 'status' attribute even if the user
+    is not asking for it in 'attr'. Otherwise info_installed would not be able
+    to check if the package is installed and would return everything.
+
+    :return:
+    """
+    mock = MagicMock(return_value=lowpkg_info_var)
+    with patch.dict(aptpkg.__salt__, {"lowpkg.info": mock}):
+        aptpkg.info_installed("wget", attr="version")
+        assert "status" in mock.call_args.kwargs["attr"]
+        assert "version" in mock.call_args.kwargs["attr"]
+
+
 def test_info_installed_all_versions(lowpkg_info_var):
     """
     Test info_installed 'all_versions'.
-- 
2.34.1


openSUSE Build Service is sponsored by