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)

* 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>
This commit is contained in:
Shahar Mor 2021-06-18 23:08:46 +03:00 committed by GitHub
parent ee23c26150
commit 1ed4394c5e
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') cmd.append('--ignore-scripts')
if self.unsafe_perm: if self.unsafe_perm:
cmd.append('--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) cmd.append(self.name_version)
if self.registry: if self.registry:
cmd.append('--registry') cmd.append('--registry')
@ -215,14 +215,17 @@ class Npm(object):
except (getattr(json, 'JSONDecodeError', ValueError)) as e: except (getattr(json, 'JSONDecodeError', ValueError)) as e:
self.module.fail_json(msg="Failed to parse NPM output with error %s" % to_native(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, props in data['dependencies'].items():
if 'missing' in data['dependencies'][dep] and data['dependencies'][dep]['missing']: dep_version = dep + '@' + str(props['version'])
if 'missing' in props and props['missing']:
missing.append(dep) missing.append(dep)
elif 'invalid' in data['dependencies'][dep] and data['dependencies'][dep]['invalid']: elif 'invalid' in props and props['invalid']:
missing.append(dep) missing.append(dep)
else: else:
installed.append(dep) 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) missing.append(self.name)
# Named dependency not installed # Named dependency not installed
else: else:

View file

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