diff --git a/changelogs/fragments/4052-fix-detection-of-installed-cargo-packages-with-hyphens.yaml b/changelogs/fragments/4052-fix-detection-of-installed-cargo-packages-with-hyphens.yaml new file mode 100644 index 0000000000..e17195ac6d --- /dev/null +++ b/changelogs/fragments/4052-fix-detection-of-installed-cargo-packages-with-hyphens.yaml @@ -0,0 +1,3 @@ +bugfixes: + - cargo - fix incorrectly reported changed status for packages with a name containing a hyphen (https://github.com/ansible-collections/community.general/issues/4044, https://github.com/ansible-collections/community.general/pull/4052). + - cargo - fix detection of outdated packages when ``state=latest`` (https://github.com/ansible-collections/community.general/pull/4052). diff --git a/plugins/modules/packaging/language/cargo.py b/plugins/modules/packaging/language/cargo.py index b7251887aa..d449f1020e 100644 --- a/plugins/modules/packaging/language/cargo.py +++ b/plugins/modules/packaging/language/cargo.py @@ -112,7 +112,7 @@ class Cargo(object): cmd = ["install", "--list"] data, dummy = self._exec(cmd, True, False, False) - package_regex = re.compile(r"^(\w+) v(.+):$") + package_regex = re.compile(r"^([\w\-]+) v(.+):$") installed = {} for line in data.splitlines(): package_info = package_regex.match(line) @@ -136,11 +136,11 @@ class Cargo(object): installed_version = self.get_installed().get(name) cmd = ["search", name, "--limit", "1"] - data = self._exec(cmd, True, False, False) + data, dummy = self._exec(cmd, True, False, False) match = re.search(r'"(.+)"', data) if match: - latest_version = match[1] + latest_version = match.group(1) return installed_version != latest_version diff --git a/tests/integration/targets/cargo/tasks/test_version.yml b/tests/integration/targets/cargo/tasks/test_version.yml index a59bd72e0a..1db9d786dc 100644 --- a/tests/integration/targets/cargo/tasks/test_version.yml +++ b/tests/integration/targets/cargo/tasks/test_version.yml @@ -5,21 +5,42 @@ version: 0.1.0 register: install_helloworld_010 +- name: Install application helloworld-yliu 0.1.0 (idempotent) + community.general.cargo: + name: helloworld-yliu + version: 0.1.0 + register: install_helloworld_010_idem + - name: Upgrade helloworld-yliu 0.1.0 community.general.cargo: name: helloworld-yliu state: latest register: upgrade_helloworld_010 +- name: Upgrade helloworld-yliu 0.1.0 (idempotent) + community.general.cargo: + name: helloworld-yliu + state: latest + register: upgrade_helloworld_010_idem + - name: Downgrade helloworld-yliu 0.1.0 community.general.cargo: name: helloworld-yliu version: 0.1.0 register: downgrade_helloworld_010 -- name: Check assertions helloworld-yliu 0.1.0 +- name: Downgrade helloworld-yliu 0.1.0 (idempotent) + community.general.cargo: + name: helloworld-yliu + version: 0.1.0 + register: downgrade_helloworld_010_idem + +- name: Check assertions helloworld-yliu assert: that: - install_helloworld_010 is changed + - install_helloworld_010_idem is not changed - upgrade_helloworld_010 is changed + - upgrade_helloworld_010_idem is not changed - downgrade_helloworld_010 is changed + - downgrade_helloworld_010_idem is not changed