diff --git a/changelogs/fragments/1307-macports-fix-status-check.yml b/changelogs/fragments/1307-macports-fix-status-check.yml new file mode 100644 index 0000000000..878e66ca39 --- /dev/null +++ b/changelogs/fragments/1307-macports-fix-status-check.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - macports - fix failure to install a package whose name is contained within an already installed package's name or variant (https://github.com/ansible-collections/community.general/issues/1307). diff --git a/plugins/modules/packaging/os/macports.py b/plugins/modules/packaging/os/macports.py index 5fc51e966d..a865a8f339 100644 --- a/plugins/modules/packaging/os/macports.py +++ b/plugins/modules/packaging/os/macports.py @@ -148,17 +148,18 @@ def query_port(module, port_path, name, state="present"): if state == "present": - rc, out, err = module.run_command("%s installed | grep -q ^.*%s" % (shlex_quote(port_path), shlex_quote(name)), use_unsafe_shell=True) - if rc == 0: + rc, out, err = module.run_command([port_path, "-q", "installed", name]) + + if rc == 0 and out.strip().startswith(name + " "): return True return False elif state == "active": - rc, out, err = module.run_command("%s installed %s | grep -q active" % (shlex_quote(port_path), shlex_quote(name)), use_unsafe_shell=True) + rc, out, err = module.run_command([port_path, "-q", "installed", name]) - if rc == 0: + if rc == 0 and "(active)" in out: return True return False diff --git a/tests/unit/plugins/modules/packaging/os/test_macports.py b/tests/unit/plugins/modules/packaging/os/test_macports.py new file mode 100644 index 0000000000..ef7c522f0f --- /dev/null +++ b/tests/unit/plugins/modules/packaging/os/test_macports.py @@ -0,0 +1,34 @@ +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from ansible.module_utils import basic +from ansible_collections.community.general.plugins.modules.packaging.os import macports + +import pytest + +TESTED_MODULE = macports.__name__ + +QUERY_PORT_TEST_CASES = [ + pytest.param('', False, False, id='Not installed'), + pytest.param(' git @2.29.2_0+credential_osxkeychain+diff_highlight+doc+pcre+perl5_28', True, False, id='Installed but not active'), + pytest.param(' git @2.29.2_0+credential_osxkeychain+diff_highlight+doc+pcre+perl5_28 (active)', True, True, id='Installed and active'), + pytest.param(''' git @2.29.2_0+credential_osxkeychain+diff_highlight+doc+pcre+perl5_28 + git @2.28.1_0+credential_osxkeychain+diff_highlight+doc+pcre+perl5_28 +''', True, False, id='2 versions installed, neither active'), + pytest.param(''' git @2.29.2_0+credential_osxkeychain+diff_highlight+doc+pcre+perl5_28 (active) + git @2.28.1_0+credential_osxkeychain+diff_highlight+doc+pcre+perl5_28 +''', True, True, id='2 versions installed, one active'), +] + + +@pytest.mark.parametrize("run_cmd_return_val, present_expected, active_expected", QUERY_PORT_TEST_CASES) +def test_macports_query_port(mocker, run_cmd_return_val, present_expected, active_expected): + module = mocker.Mock() + run_command = mocker.Mock() + run_command.return_value = (0, run_cmd_return_val, '') + module.run_command = run_command + + assert macports.query_port(module, 'port', 'git', state="present") == present_expected + assert macports.query_port(module, 'port', 'git', state="active") == active_expected diff --git a/tests/unit/plugins/modules/packaging/os/test_redhat_subscription.py b/tests/unit/plugins/modules/packaging/os/test_redhat_subscription.py index 2448ce67ed..ef6f28b812 100644 --- a/tests/unit/plugins/modules/packaging/os/test_redhat_subscription.py +++ b/tests/unit/plugins/modules/packaging/os/test_redhat_subscription.py @@ -18,7 +18,7 @@ TESTED_MODULE = redhat_subscription.__name__ @pytest.fixture def patch_redhat_subscription(mocker): """ - Function used for mocking some parts of redhat_subscribtion module + Function used for mocking some parts of redhat_subscription module """ mocker.patch('ansible_collections.community.general.plugins.modules.packaging.os.redhat_subscription.RegistrationBase.REDHAT_REPO') mocker.patch('ansible_collections.community.general.plugins.modules.packaging.os.redhat_subscription.isfile', return_value=False) @@ -817,7 +817,7 @@ TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES] @pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, ids=TEST_CASES_IDS, indirect=['patch_ansible_module']) @pytest.mark.usefixtures('patch_ansible_module') -def test_redhat_subscribtion(mocker, capfd, patch_redhat_subscription, testcase): +def test_redhat_subscription(mocker, capfd, patch_redhat_subscription, testcase): """ Run unit tests for test cases listen in TEST_CASES """ @@ -1173,7 +1173,7 @@ SYSPURPOSE_TEST_CASES_IDS = [item[1]['id'] for item in SYSPURPOSE_TEST_CASES] @pytest.mark.parametrize('patch_ansible_module, testcase', SYSPURPOSE_TEST_CASES, ids=SYSPURPOSE_TEST_CASES_IDS, indirect=['patch_ansible_module']) @pytest.mark.usefixtures('patch_ansible_module') -def test_redhat_subscribtion_syspurpose(mocker, capfd, patch_redhat_subscription, patch_ansible_module, testcase, tmpdir): +def test_redhat_subscription_syspurpose(mocker, capfd, patch_redhat_subscription, patch_ansible_module, testcase, tmpdir): """ Run unit tests for test cases listen in SYSPURPOSE_TEST_CASES (syspurpose specific cases) """