mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
* Provide a user friendly message by handling json decode
exception rather than providing a stacktrace
Fixes: #1614
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
(cherry picked from commit a9c64655de
)
Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
3911b83145
commit
4c9c8e0514
3 changed files with 92 additions and 16 deletions
2
changelogs/fragments/1614_npm.yml
Normal file
2
changelogs/fragments/1614_npm.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- npm - handle json decode exception while parsing command line output (https://github.com/ansible-collections/community.general/issues/1614).
|
|
@ -7,39 +7,39 @@ from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = r'''
|
||||||
---
|
---
|
||||||
module: npm
|
module: npm
|
||||||
short_description: Manage node.js packages with npm
|
short_description: Manage node.js packages with npm
|
||||||
description:
|
description:
|
||||||
- Manage node.js packages with Node Package Manager (npm)
|
- Manage node.js packages with Node Package Manager (npm).
|
||||||
author: "Chris Hoffman (@chrishoffman)"
|
author: "Chris Hoffman (@chrishoffman)"
|
||||||
options:
|
options:
|
||||||
name:
|
name:
|
||||||
description:
|
description:
|
||||||
- The name of a node.js library to install
|
- The name of a node.js library to install.
|
||||||
type: str
|
type: str
|
||||||
required: false
|
required: false
|
||||||
path:
|
path:
|
||||||
description:
|
description:
|
||||||
- The base path where to install the node.js libraries
|
- The base path where to install the node.js libraries.
|
||||||
type: path
|
type: path
|
||||||
required: false
|
required: false
|
||||||
version:
|
version:
|
||||||
description:
|
description:
|
||||||
- The version to be installed
|
- The version to be installed.
|
||||||
type: str
|
type: str
|
||||||
required: false
|
required: false
|
||||||
global:
|
global:
|
||||||
description:
|
description:
|
||||||
- Install the node.js library globally
|
- Install the node.js library globally.
|
||||||
required: false
|
required: false
|
||||||
default: no
|
default: no
|
||||||
type: bool
|
type: bool
|
||||||
executable:
|
executable:
|
||||||
description:
|
description:
|
||||||
- The executable location for npm.
|
- The executable location for npm.
|
||||||
- This is useful if you are using a version manager, such as nvm
|
- This is useful if you are using a version manager, such as nvm.
|
||||||
type: path
|
type: path
|
||||||
required: false
|
required: false
|
||||||
ignore_scripts:
|
ignore_scripts:
|
||||||
|
@ -55,12 +55,12 @@ options:
|
||||||
default: no
|
default: no
|
||||||
ci:
|
ci:
|
||||||
description:
|
description:
|
||||||
- Install packages based on package-lock file, same as running npm ci
|
- Install packages based on package-lock file, same as running C(npm ci).
|
||||||
type: bool
|
type: bool
|
||||||
default: no
|
default: no
|
||||||
production:
|
production:
|
||||||
description:
|
description:
|
||||||
- Install dependencies in production mode, excluding devDependencies
|
- Install dependencies in production mode, excluding devDependencies.
|
||||||
required: false
|
required: false
|
||||||
type: bool
|
type: bool
|
||||||
default: no
|
default: no
|
||||||
|
@ -71,7 +71,7 @@ options:
|
||||||
type: str
|
type: str
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- The state of the node.js library
|
- The state of the node.js library.
|
||||||
required: false
|
required: false
|
||||||
type: str
|
type: str
|
||||||
default: present
|
default: present
|
||||||
|
@ -80,7 +80,7 @@ requirements:
|
||||||
- npm installed in bin path (recommended /usr/local/bin)
|
- npm installed in bin path (recommended /usr/local/bin)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = r'''
|
||||||
- name: Install "coffee-script" node.js package.
|
- name: Install "coffee-script" node.js package.
|
||||||
community.general.npm:
|
community.general.npm:
|
||||||
name: coffee-script
|
name: coffee-script
|
||||||
|
@ -124,12 +124,12 @@ EXAMPLES = '''
|
||||||
state: present
|
state: present
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils._text import to_native
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
class Npm(object):
|
class Npm(object):
|
||||||
|
@ -155,7 +155,7 @@ class Npm(object):
|
||||||
else:
|
else:
|
||||||
self.name_version = self.name
|
self.name_version = self.name
|
||||||
|
|
||||||
def _exec(self, args, run_in_check_mode=False, check_rc=True):
|
def _exec(self, args, run_in_check_mode=False, check_rc=True, add_package_name=True):
|
||||||
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
|
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
|
||||||
cmd = self.executable + args
|
cmd = self.executable + args
|
||||||
|
|
||||||
|
@ -167,7 +167,7 @@ class Npm(object):
|
||||||
cmd.append('--ignore-scripts')
|
cmd.append('--ignore-scripts')
|
||||||
if self.unsafe_perm:
|
if self.unsafe_perm:
|
||||||
cmd.append('--unsafe-perm')
|
cmd.append('--unsafe-perm')
|
||||||
if self.name:
|
if self.name and add_package_name:
|
||||||
cmd.append(self.name_version)
|
cmd.append(self.name_version)
|
||||||
if self.registry:
|
if self.registry:
|
||||||
cmd.append('--registry')
|
cmd.append('--registry')
|
||||||
|
@ -191,7 +191,11 @@ class Npm(object):
|
||||||
|
|
||||||
installed = list()
|
installed = list()
|
||||||
missing = list()
|
missing = list()
|
||||||
data = json.loads(self._exec(cmd, True, False))
|
data = {}
|
||||||
|
try:
|
||||||
|
data = json.loads(self._exec(cmd, True, False, False) or '{}')
|
||||||
|
except (getattr(json, 'JSONDecodeError', ValueError)) as e:
|
||||||
|
self.module.fail_json(msg="Failed to parse NPM output with error %s" % to_native(e))
|
||||||
if 'dependencies' in data:
|
if 'dependencies' in data:
|
||||||
for dep in data['dependencies']:
|
for dep in data['dependencies']:
|
||||||
if 'missing' in data['dependencies'][dep] and data['dependencies'][dep]['missing']:
|
if 'missing' in data['dependencies'][dep] and data['dependencies'][dep]['missing']:
|
||||||
|
|
70
tests/unit/plugins/modules/packaging/language/test_npm.py
Normal file
70
tests/unit/plugins/modules/packaging/language/test_npm.py
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
#
|
||||||
|
# Copyright: (c) 2021, Abhijeet Kasurde <akasurde@redhat.com>
|
||||||
|
# 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_collections.community.general.tests.unit.compat.mock import call, patch
|
||||||
|
from ansible_collections.community.general.plugins.modules.packaging.language import npm
|
||||||
|
from ansible_collections.community.general.tests.unit.plugins.modules.utils import (
|
||||||
|
AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args)
|
||||||
|
|
||||||
|
|
||||||
|
class NPMModuleTestCase(ModuleTestCase):
|
||||||
|
module = npm
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(NPMModuleTestCase, self).setUp()
|
||||||
|
ansible_module_path = "ansible_collections.community.general.plugins.modules.packaging.language.npm.AnsibleModule"
|
||||||
|
self.mock_run_command = patch('%s.run_command' % ansible_module_path)
|
||||||
|
self.module_main_command = self.mock_run_command.start()
|
||||||
|
self.mock_get_bin_path = patch('%s.get_bin_path' % ansible_module_path)
|
||||||
|
self.get_bin_path = self.mock_get_bin_path.start()
|
||||||
|
self.get_bin_path.return_value = '/testbin/npm'
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.mock_run_command.stop()
|
||||||
|
self.mock_get_bin_path.stop()
|
||||||
|
super(NPMModuleTestCase, self).tearDown()
|
||||||
|
|
||||||
|
def module_main(self, exit_exc):
|
||||||
|
with self.assertRaises(exit_exc) as exc:
|
||||||
|
self.module.main()
|
||||||
|
return exc.exception.args[0]
|
||||||
|
|
||||||
|
def test_present(self):
|
||||||
|
set_module_args({
|
||||||
|
'name': 'coffee-script',
|
||||||
|
'global': 'true',
|
||||||
|
'state': 'present'
|
||||||
|
})
|
||||||
|
self.module_main_command.side_effect = [
|
||||||
|
(0, '{}', ''),
|
||||||
|
(0, '{}', ''),
|
||||||
|
]
|
||||||
|
|
||||||
|
result = self.module_main(AnsibleExitJson)
|
||||||
|
|
||||||
|
self.assertTrue(result['changed'])
|
||||||
|
self.module_main_command.assert_has_calls([
|
||||||
|
call(['/testbin/npm', 'list', '--json', '--long', '--global'], check_rc=False, cwd=None),
|
||||||
|
])
|
||||||
|
|
||||||
|
def test_absent(self):
|
||||||
|
set_module_args({
|
||||||
|
'name': 'coffee-script',
|
||||||
|
'global': 'true',
|
||||||
|
'state': 'absent'
|
||||||
|
})
|
||||||
|
self.module_main_command.side_effect = [
|
||||||
|
(0, '{"dependencies": {"coffee-script": {}}}', ''),
|
||||||
|
(0, '{}', ''),
|
||||||
|
]
|
||||||
|
|
||||||
|
result = self.module_main(AnsibleExitJson)
|
||||||
|
|
||||||
|
self.assertTrue(result['changed'])
|
||||||
|
self.module_main_command.assert_has_calls([
|
||||||
|
call(['/testbin/npm', 'uninstall', '--global', 'coffee-script'], check_rc=True, cwd=None),
|
||||||
|
])
|
Loading…
Reference in a new issue