1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

npm - fix updating version specific modules (#2830) (#2839)

* npm - fix updating version specific modules

if a version specific module is used, the comparison will be used with the version and not only by name

* Update plugins/modules/packaging/language/npm.py

Co-authored-by: Ajpantuso <ajpantuso@gmail.com>

* Update changelogs/fragments/2830-npm-version-update.yml

Co-authored-by: Ajpantuso <ajpantuso@gmail.com>

* Update changelogs/fragments/2830-npm-version-update.yml

Co-authored-by: Amin Vakil <info@aminvakil.com>

* Update changelogs/fragments/2830-npm-version-update.yml

Co-authored-by: Amin Vakil <info@aminvakil.com>

Co-authored-by: Ajpantuso <ajpantuso@gmail.com>
Co-authored-by: Amin Vakil <info@aminvakil.com>
(cherry picked from commit 1ed4394c5e)

Co-authored-by: Shahar Mor <shaharmor1@gmail.com>
This commit is contained in:
patchback[bot] 2021-06-18 22:50:24 +02:00 committed by GitHub
parent 2b435a591d
commit 0296c200c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 6 deletions

View file

@ -0,0 +1,4 @@
bugfixes:
- "npm - when the ``version`` option is used the comparison of installed vs missing will
use name@version instead of just name, allowing version specific updates
(https://github.com/ansible-collections/community.general/issues/2021)."

View file

@ -181,7 +181,7 @@ class Npm(object):
cmd.append('--ignore-scripts')
if self.unsafe_perm:
cmd.append('--unsafe-perm')
if self.name and add_package_name:
if self.name_version and add_package_name:
cmd.append(self.name_version)
if self.registry:
cmd.append('--registry')
@ -215,14 +215,17 @@ class Npm(object):
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']:
for dep, props in data['dependencies'].items():
dep_version = dep + '@' + str(props['version'])
if 'missing' in props and props['missing']:
missing.append(dep)
elif 'invalid' in data['dependencies'][dep] and data['dependencies'][dep]['invalid']:
elif 'invalid' in props and props['invalid']:
missing.append(dep)
else:
installed.append(dep)
if self.name and self.name not in installed:
installed.append(dep_version)
if self.name_version and self.name_version not in installed:
missing.append(self.name)
# Named dependency not installed
else:

View file

@ -47,6 +47,66 @@ class NPMModuleTestCase(ModuleTestCase):
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),
call(['/testbin/npm', 'install', '--global', 'coffee-script'], check_rc=True, cwd=None),
])
def test_present_version(self):
set_module_args({
'name': 'coffee-script',
'global': 'true',
'state': 'present',
'version': '2.5.1'
})
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),
call(['/testbin/npm', 'install', '--global', 'coffee-script@2.5.1'], check_rc=True, cwd=None),
])
def test_present_version_update(self):
set_module_args({
'name': 'coffee-script',
'global': 'true',
'state': 'present',
'version': '2.5.1'
})
self.module_main_command.side_effect = [
(0, '{"dependencies": {"coffee-script": {"version" : "2.5.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),
call(['/testbin/npm', 'install', '--global', 'coffee-script@2.5.1'], check_rc=True, cwd=None),
])
def test_present_version_exists(self):
set_module_args({
'name': 'coffee-script',
'global': 'true',
'state': 'present',
'version': '2.5.1'
})
self.module_main_command.side_effect = [
(0, '{"dependencies": {"coffee-script": {"version" : "2.5.1"}}}', ''),
(0, '{}', ''),
]
result = self.module_main(AnsibleExitJson)
self.assertFalse(result['changed'])
self.module_main_command.assert_has_calls([
call(['/testbin/npm', 'list', '--json', '--long', '--global'], check_rc=False, cwd=None),
])
@ -58,7 +118,7 @@ class NPMModuleTestCase(ModuleTestCase):
'state': 'absent'
})
self.module_main_command.side_effect = [
(0, '{"dependencies": {"coffee-script": {}}}', ''),
(0, '{"dependencies": {"coffee-script": {"version" : "2.5.1"}}}', ''),
(0, '{}', ''),
]
@ -66,5 +126,46 @@ class NPMModuleTestCase(ModuleTestCase):
self.assertTrue(result['changed'])
self.module_main_command.assert_has_calls([
call(['/testbin/npm', 'list', '--json', '--long', '--global'], check_rc=False, cwd=None),
call(['/testbin/npm', 'uninstall', '--global', 'coffee-script'], check_rc=True, cwd=None),
])
def test_absent_version(self):
set_module_args({
'name': 'coffee-script',
'global': 'true',
'state': 'absent',
'version': '2.5.1'
})
self.module_main_command.side_effect = [
(0, '{"dependencies": {"coffee-script": {"version" : "2.5.1"}}}', ''),
(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),
call(['/testbin/npm', 'uninstall', '--global', 'coffee-script'], check_rc=True, cwd=None),
])
def test_absent_version_different(self):
set_module_args({
'name': 'coffee-script',
'global': 'true',
'state': 'absent',
'version': '2.5.1'
})
self.module_main_command.side_effect = [
(0, '{"dependencies": {"coffee-script": {"version" : "2.5.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),
call(['/testbin/npm', 'uninstall', '--global', 'coffee-script'], check_rc=True, cwd=None),
])