diff --git a/changelogs/fragments/9061-cpanm-version.yml b/changelogs/fragments/9061-cpanm-version.yml new file mode 100644 index 0000000000..af91cac1c0 --- /dev/null +++ b/changelogs/fragments/9061-cpanm-version.yml @@ -0,0 +1,2 @@ +minor_changes: + - cpanm - add return value ``cpanm_version`` (https://github.com/ansible-collections/community.general/pull/9061). diff --git a/plugins/modules/cpanm.py b/plugins/modules/cpanm.py index 25489170dd..04b3b06b7f 100644 --- a/plugins/modules/cpanm.py +++ b/plugins/modules/cpanm.py @@ -142,7 +142,19 @@ EXAMPLES = """ version: '1.0' """ +RETURN = """ +--- +cpanm_version: + description: Version of CPANMinus. + type: str + returned: always + sample: "1.7047" + version_added: 10.0.0 +""" + + import os +import re from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper @@ -175,6 +187,7 @@ class CPANMinus(ModuleHelper): mirror_only=cmd_runner_fmt.as_bool("--mirror-only"), installdeps=cmd_runner_fmt.as_bool("--installdeps"), pkg_spec=cmd_runner_fmt.as_list(), + cpanm_version=cmd_runner_fmt.as_fixed("--version"), ) use_old_vardict = False @@ -191,6 +204,14 @@ class CPANMinus(ModuleHelper): self.runner = CmdRunner(self.module, self.command, self.command_args_formats, check_rc=True) self.vars.binary = self.runner.binary + with self.runner("cpanm_version") as ctx: + rc, out, err = ctx.run() + line = out.split('\n')[0] + match = re.search(r"version\s+([\d\.]+)\s+", line) + if not match: + self.do_raise("Failed to determine version number. First line of output: {0}".format(line)) + self.vars.cpanm_version = match.group(1) + def _is_package_installed(self, name, locallib, version): def process(rc, out, err): return rc == 0 diff --git a/tests/unit/plugins/modules/test_cpanm.yaml b/tests/unit/plugins/modules/test_cpanm.yaml index ad081254d6..ff4bd9c0f7 100644 --- a/tests/unit/plugins/modules/test_cpanm.yaml +++ b/tests/unit/plugins/modules/test_cpanm.yaml @@ -10,15 +10,23 @@ mode: compatibility output: changed: true + cpanm_version: "1.7047" mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: &env-def-true {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/perl, -le, 'use Dancer;'] environ: &env-def-false {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false} rc: 2 out: "" err: "error, not installed" - command: [/testbin/cpanm, Dancer] - environ: &env-def-true {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + environ: *env-def-true rc: 0 out: "" err: "" @@ -30,6 +38,13 @@ changed: false mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/perl, -le, 'use Dancer;'] environ: *env-def-false rc: 0 @@ -42,6 +57,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, Dancer] environ: *env-def-true rc: 0 @@ -55,6 +77,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, MIYAGAWA/Plack-0.99_05.tar.gz] environ: *env-def-true rc: 0 @@ -67,6 +96,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, MIYAGAWA/Plack-0.99_05.tar.gz] environ: *env-def-true rc: 0 @@ -81,6 +117,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, --local-lib, /srv/webapps/my_app/extlib, Dancer] environ: *env-def-true rc: 0 @@ -94,6 +137,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, /srv/webapps/my_app/src/] environ: *env-def-true rc: 0 @@ -109,6 +159,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, --notest, --local-lib, /srv/webapps/my_app/extlib, Dancer] environ: *env-def-true rc: 0 @@ -123,6 +180,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, --mirror, "http://cpan.cpantesters.org/", Dancer] environ: *env-def-true rc: 0 @@ -146,6 +210,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, Dancer~1.0] environ: *env-def-true rc: 0 @@ -160,6 +231,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, Dancer~1.5] environ: *env-def-true rc: 0 @@ -174,6 +252,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, Dancer@1.7] environ: *env-def-true rc: 0 @@ -188,7 +273,14 @@ failed: true msg: parameter 'version' must not be used when installing from a file mocks: - run_command: [] + run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - id: install_specific_version_from_directory_error input: from_path: ~/ @@ -198,7 +290,14 @@ failed: true msg: parameter 'version' must not be used when installing from a directory mocks: - run_command: [] + run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - id: install_specific_version_from_git_url_explicit input: name: "git://github.com/plack/Plack.git" @@ -208,6 +307,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, "git://github.com/plack/Plack.git@1.7"] environ: *env-def-true rc: 0 @@ -222,6 +328,13 @@ changed: true mocks: run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: "" - command: [/testbin/cpanm, "git://github.com/plack/Plack.git@2.5"] environ: *env-def-true rc: 0 @@ -236,4 +349,11 @@ failed: true msg: operator '~' not allowed in version parameter when installing from git repository mocks: - run_command: [] + run_command: + - command: [/testbin/cpanm, --version] + environ: *env-def-true + rc: 0 + out: | + cpanm (App::cpanminus) version 1.7047 (/usr/local/bin/cpanm) + perl version 5.041005 (/usr/local/bin/perl) + err: ""