From 4a6fac0934c90ba0cae5dc0a56e747c5d757a79f Mon Sep 17 00:00:00 2001 From: nicholasmhughes Date: Fri, 22 Mar 2024 20:56:16 -0400 Subject: [PATCH] fixes saltstack/salt#66260 aptpkg.remove unable to locate package for non-existent package --- changelog/66260.fixed.md | 1 + salt/modules/aptpkg.py | 12 ++++++++++-- tests/pytests/functional/modules/test_aptpkg.py | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 changelog/66260.fixed.md diff --git a/changelog/66260.fixed.md b/changelog/66260.fixed.md new file mode 100644 index 00000000000..73d07f17857 --- /dev/null +++ b/changelog/66260.fixed.md @@ -0,0 +1 @@ +Fixed `aptpkg.remove` "unable to locate package" error for non-existent package diff --git a/salt/modules/aptpkg.py b/salt/modules/aptpkg.py index ff5991c11c1..85380b96318 100644 --- a/salt/modules/aptpkg.py +++ b/salt/modules/aptpkg.py @@ -1067,9 +1067,17 @@ def _uninstall(action="remove", name=None, pkgs=None, **kwargs): old = list_pkgs() old_removed = list_pkgs(removed=True) - targets = salt.utils.pkg.match_wildcard(old, pkg_params) + targets = [ + _pkg for _pkg in salt.utils.pkg.match_wildcard(old, pkg_params) if _pkg in old + ] if action == "purge": - targets.update(salt.utils.pkg.match_wildcard(old_removed, pkg_params)) + targets.extend( + [ + _pkg + for _pkg in salt.utils.pkg.match_wildcard(old_removed, pkg_params) + if _pkg in old_removed + ] + ) if not targets: return {} cmd = ["apt-get", "-q", "-y", action] diff --git a/tests/pytests/functional/modules/test_aptpkg.py b/tests/pytests/functional/modules/test_aptpkg.py index a9063e48b8b..161e3c7827e 100644 --- a/tests/pytests/functional/modules/test_aptpkg.py +++ b/tests/pytests/functional/modules/test_aptpkg.py @@ -398,3 +398,9 @@ def test_aptpkg_remove_wildcard(): assert ret["nginx-light"]["old"] assert not ret["nginx-doc"]["new"] assert ret["nginx-doc"]["old"] + + +@pytest.mark.skip_if_not_root +def test_aptpkg_remove_unknown_package(): + ret = aptpkg.remove(name="thispackageistotallynotthere") + assert not ret