From 14d43b10c1361499734551bd81c44fbcd1881785 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Fri, 11 Mar 2022 06:55:54 +0100 Subject: [PATCH] npm: add support for `production` flag when using `ci` (#4299) (#4339) * npm - add '--production` support to 'npm ci' flag * add changelog fragement for 4299 * Add backticks Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein (cherry picked from commit 43af053d73b838c372d342bee485170cea0770ff) Co-authored-by: Daniel Miller --- .../4299-npm-add-production-with-ci-flag.yml | 2 + plugins/modules/packaging/language/npm.py | 2 +- .../modules/packaging/language/test_npm.py | 72 +++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/4299-npm-add-production-with-ci-flag.yml diff --git a/changelogs/fragments/4299-npm-add-production-with-ci-flag.yml b/changelogs/fragments/4299-npm-add-production-with-ci-flag.yml new file mode 100644 index 0000000000..1165e6736d --- /dev/null +++ b/changelogs/fragments/4299-npm-add-production-with-ci-flag.yml @@ -0,0 +1,2 @@ +minor_changes: + - npm - add ability to use ``production`` flag when ``ci`` is set (https://github.com/ansible-collections/community.general/pull/4299). diff --git a/plugins/modules/packaging/language/npm.py b/plugins/modules/packaging/language/npm.py index de316d397f..1c97035e67 100644 --- a/plugins/modules/packaging/language/npm.py +++ b/plugins/modules/packaging/language/npm.py @@ -175,7 +175,7 @@ class Npm(object): if self.glbl: cmd.append('--global') - if self.production and ('install' in cmd or 'update' in cmd): + if self.production and ('install' in cmd or 'update' in cmd or 'ci' in cmd): cmd.append('--production') if self.ignore_scripts: cmd.append('--ignore-scripts') diff --git a/tests/unit/plugins/modules/packaging/language/test_npm.py b/tests/unit/plugins/modules/packaging/language/test_npm.py index 89de549915..6e30a5d41d 100644 --- a/tests/unit/plugins/modules/packaging/language/test_npm.py +++ b/tests/unit/plugins/modules/packaging/language/test_npm.py @@ -188,3 +188,75 @@ class NPMModuleTestCase(ModuleTestCase): 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_present_package_json(self): + set_module_args({ + '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', 'install', '--global'], check_rc=True, cwd=None), + ]) + + def test_present_package_json_production(self): + set_module_args({ + 'production': 'true', + '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', 'install', '--global', '--production'], check_rc=True, cwd=None), + ]) + + def test_present_package_json_ci(self): + set_module_args({ + 'ci': 'true', + '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', 'ci', '--global'], check_rc=True, cwd=None), + ]) + + def test_present_package_json_ci_production(self): + set_module_args({ + 'ci': 'true', + 'production': 'true', + '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', 'ci', '--global', '--production'], check_rc=True, cwd=None), + ])