Skip to content

Commit

Permalink
npm - handle json decode exception (#1625) (#1636)
Browse files Browse the repository at this point in the history
* 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 a9c6465)

Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
patchback[bot] and Akasurde committed Jan 14, 2021
1 parent 3911b83 commit 4c9c8e0
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 16 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1614_npm.yml
@@ -0,0 +1,2 @@
bugfixes:
- npm - handle json decode exception while parsing command line output (https://github.com/ansible-collections/community.general/issues/1614).
36 changes: 20 additions & 16 deletions plugins/modules/packaging/language/npm.py
Expand Up @@ -7,39 +7,39 @@
__metaclass__ = type


DOCUMENTATION = '''
DOCUMENTATION = r'''
---
module: npm
short_description: Manage node.js packages with npm
description:
- Manage node.js packages with Node Package Manager (npm)
- Manage node.js packages with Node Package Manager (npm).
author: "Chris Hoffman (@chrishoffman)"
options:
name:
description:
- The name of a node.js library to install
- The name of a node.js library to install.
type: str
required: false
path:
description:
- The base path where to install the node.js libraries
- The base path where to install the node.js libraries.
type: path
required: false
version:
description:
- The version to be installed
- The version to be installed.
type: str
required: false
global:
description:
- Install the node.js library globally
- Install the node.js library globally.
required: false
default: no
type: bool
executable:
description:
- 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
required: false
ignore_scripts:
Expand All @@ -55,12 +55,12 @@
default: no
ci:
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
default: no
production:
description:
- Install dependencies in production mode, excluding devDependencies
- Install dependencies in production mode, excluding devDependencies.
required: false
type: bool
default: no
Expand All @@ -71,7 +71,7 @@
type: str
state:
description:
- The state of the node.js library
- The state of the node.js library.
required: false
type: str
default: present
Expand All @@ -80,7 +80,7 @@
- npm installed in bin path (recommended /usr/local/bin)
'''

EXAMPLES = '''
EXAMPLES = r'''
- name: Install "coffee-script" node.js package.
community.general.npm:
name: coffee-script
Expand Down Expand Up @@ -124,12 +124,12 @@
state: present
'''

import json
import os
import re

from ansible.module_utils.basic import AnsibleModule

import json
from ansible.module_utils._text import to_native


class Npm(object):
Expand All @@ -155,7 +155,7 @@ def __init__(self, module, **kwargs):
else:
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):
cmd = self.executable + args

Expand All @@ -167,7 +167,7 @@ def _exec(self, args, run_in_check_mode=False, check_rc=True):
cmd.append('--ignore-scripts')
if self.unsafe_perm:
cmd.append('--unsafe-perm')
if self.name:
if self.name and add_package_name:
cmd.append(self.name_version)
if self.registry:
cmd.append('--registry')
Expand All @@ -191,7 +191,11 @@ def list(self):

installed = 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:
for dep in data['dependencies']:
if 'missing' in data['dependencies'][dep] and data['dependencies'][dep]['missing']:
Expand Down
70 changes: 70 additions & 0 deletions tests/unit/plugins/modules/packaging/language/test_npm.py
@@ -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),
])

0 comments on commit 4c9c8e0

Please sign in to comment.