Repository: ansible-collections/community.general

Issue:
yarn module throws an error when running with --check flag
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
yarn module throws ValueError: need more than 0 values to unpack when running with `--check` flag.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
`yarn` module

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.9.6
  config file = /Users/ahmed/.dotfiles/ansible.cfg
  configured module search path = ['/Users/ahmed/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.9.6_1/libexec/lib/python3.8/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.8.2 (default, Mar 11 2020, 00:29:50) [Clang 11.0.0 (clang-1100.0.33.17)]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
ANSIBLE_NOCOWS(/Users/ahmed/.dotfiles/ansible.cfg) = True
DEFAULT_GATHER_SUBSET(/Users/ahmed/.dotfiles/ansible.cfg) = ['!all']
DEFAULT_MANAGED_STR(/Users/ahmed/.dotfiles/ansible.cfg) = vim: set nomodifiable : Ansible managed - DO NOT EDIT - see template source instead ({file}) or use `:set modifiable` to force.
INTERPRETER_PYTHON(/Users/ahmed/.dotfiles/ansible.cfg) = auto_silent
RETRY_FILES_SAVE_PATH(/Users/ahmed/.dotfiles/ansible.cfg) = /Users/ahmed/.ansible
```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
macOS 10.15.4

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->

<!--- Paste example playbooks or commands between quotes below -->
Running the example from the documentation https://docs.ansible.com/ansible/latest/modules/yarn_module.html#examples

```yaml
- name: Install "imagemin" node.js package globally.
  yarn:
    name: imagemin
    global: yes
```

Reported on https://github.com/ansible/ansible/issues/68827 by @ahmedelgabri
<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
Return null out and err values

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->
Error when running runpy on check mode

<!--- Paste verbatim command output between quotes -->
```paste below
TASK [yarn : Install "imagemin" node.js package globally.] **************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ValueError: need more than 0 values to unpack
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/Users/ahmed/.ansible/tmp/ansible-tmp-1586467962.020019-255476546078701/AnsiballZ_yarn.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/Users/ahmed/.ansible/tmp/ansible-tmp-1586467962.020019-255476546078701/AnsiballZ_yarn.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/Users/ahmed/.ansible/tmp/ansible-tmp-1586467962.020019-255476546078701/AnsiballZ_yarn.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible.modules.packaging.language.yarn', init_globals=None, run_name='__main__', alter_sys=True)\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py\", line 188, in run_module\n    fname, loader, pkg_name)\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py\", line 82, in _run_module_code\n    mod_name, mod_fname, mod_loader, pkg_name)\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py\", line 72, in _run_code\n    exec code in run_globals\n  File \"/var/folders/x3/_cx1vn0173b5s54dhcwmrgv80000gn/T/ansible_yarn_payload_1Yqsvo/ansible_yarn_payload.zip/ansible/modules/packaging/language/yarn.py\", line 389, in <module>\n  File \"/var/folders/x3/_cx1vn0173b5s54dhcwmrgv80000gn/T/ansible_yarn_payload_1Yqsvo/ansible_yarn_payload.zip/ansible/modules/packaging/language/yarn.py\", line 362, in main\nValueError: need more than 0 values to unpack\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
```



Discussion:

Resolving patch:
diff --git a/plugins/modules/packaging/language/yarn.py b/plugins/modules/packaging/language/yarn.py
--- a/plugins/modules/packaging/language/yarn.py
+++ b/plugins/modules/packaging/language/yarn.py
@@ -222,7 +222,7 @@ def _exec(self, args, run_in_check_mode=False, check_rc=True):
             rc, out, err = self.module.run_command(cmd, check_rc=check_rc, cwd=cwd)
             return out, err
 
-        return ''
+        return(None, None)
 
     def list(self):
         cmd = ['list', '--depth=0', '--json']


Test patch:
diff --git a/tests/integration/targets/yarn/tasks/run.yml b/tests/integration/targets/yarn/tasks/run.yml
--- a/tests/integration/targets/yarn/tasks/run.yml
+++ b/tests/integration/targets/yarn/tasks/run.yml
@@ -69,6 +69,24 @@
         that:
           - not (yarn_install is changed)
 
+    - name: 'Install all packages in check mode.'
+      yarn:
+        path: '{{ output_dir }}'
+        executable: '{{ yarn_bin_path }}/yarn'
+        state: present
+      environment:
+        PATH: '{{ node_bin_path }}:{{ ansible_env.PATH }}'
+      check_mode: true
+      register: yarn_install_check
+
+    - name: verify test yarn global installation in check mode
+      assert:
+        that:
+          - yarn_install_check.err is defined
+          - yarn_install_check.out is defined
+          - yarn_install_check.err is none
+          - yarn_install_check.out is none
+
     - name: 'Install package with explicit version (older version of package)'
       yarn:
         path: '{{ output_dir }}'