Repository: ansible-collections/community.general

Issue:
mas : Fix error "invalid literal for int() with base 10: 'No'" when no app is installed
##### SUMMARY
Module mas uses `mas-cli` to interact with Mac App Store. When no application is installed (eg when a new host is just installed), here is result when list of apps is requested by `mas.py`:

```
jpmat@host:⇒  mas list
No installed apps found
```

This case is not handled by `mas.py`. It gives error:
> invalid literal for int() with base 10: 'No'

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
Module mas

##### ANSIBLE VERSION
```
ansible 2.10.2
  config file = None
  configured module search path = ['/Users/jpmat/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Library/Python/3.8/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.8.2 (default, Oct  2 2020, 10:45:42) [Clang 12.0.0 (clang-1200.0.32.27)]
```

##### CONFIGURATION
```
/tmp/ansible_mas_payload_33piej0z/ansible_mas_payloaANSIBLE_SSH_CONTROL_PATH(env: ANSIBLE_SSH_CONTROL_PATH) = /Users/jpmat/.ssh/master-socket/%%r@%%h:%%p
```

##### OS / ENVIRONMENT
I found the bug with macOS 11.0.1. I've reproduced it with macOS 10.15.7.

```
mas version
1.7.1
```

##### STEPS TO REPRODUCE

```bash
bash$ mas list
No installed apps found
bash$ cd ~/.ansible/collections/ansible_collections/community/general
bash$ gsed -i '/^needs\/root/d' tests/integration/targets/mas/aliases
bash$ ansible-test integration --local -v unsupported/mas
```

##### EXPECTED RESULTS
MAS Integration test should be successful.


##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes -->
```
(...)
TASK [mas : Uninstall Rested to ensure consistent starting point] **********************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ValueError: invalid literal for int() with base 10: 'No'
fatal: [testhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/Users/jpmat/.ansible/tmp/ansible-tmp-1607013569.818048-10067-143554147524156/AnsiballZ_mas.py\", line 116, in <module>\n    _ansiballz_main()\n  File \"/Users/jpmat/.ansible/tmp/ansible-tmp-1607013569.818048-10067-143554147524156/AnsiballZ_mas.py\", line 108, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/Users/jpmat/.ansible/tmp/ansible-tmp-1607013569.818048-10067-143554147524156/AnsiballZ_mas.py\", line 54, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.mas', init_globals=None, run_name='__main__', alter_sys=True)\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/runpy.py\", line 206, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/runpy.py\", line 96, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/runpy.py\", line 86, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_mas_payload_33piej0z/ansible_mas_payload.zip/ansible_collections/community/general/plugins/modules/mas.py\", line 295, in <module>\n  File \"/tmp/ansible_mas_payload_33piej0z/ansible_mas_payload.zip/ansible_collections/community/general/plugins/modules/mas.py\", line 272, in main\n  File \"/tmp/ansible_mas_payload_33piej0z/ansible_mas_payload.zip/ansible_collections/community/general/plugins/modules/mas.py\", line 209, in is_installed\n  File \"/tmp/ansible_mas_payload_33piej0z/ansible_mas_payload.zip/ansible_collections/community/general/plugins/modules/mas.py\", line 202, in installed\n  File \"/tmp/ansible_mas_payload_33piej0z/ansible_mas_payload.zip/ansible_collections/community/general/plugins/modules/mas.py\", line 193, in get_current_state\nValueError: invalid literal for int() with base 10: 'No'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
(...)
```


Discussion:
I'm going to submit PR.
cc @lukasbestle @mheap
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/packaging/os/mas.py b/plugins/modules/packaging/os/mas.py
--- a/plugins/modules/packaging/os/mas.py
+++ b/plugins/modules/packaging/os/mas.py
@@ -183,6 +183,8 @@ def get_current_state(self, command):
 
         rc, raw_apps, err = self.run([command])
         rows = raw_apps.split("\n")
+        if rows[0] == "No installed apps found":
+            rows = []
         apps = []
         for r in rows:
             # Format: "123456789 App Name"


Test patch: