def test_cache_cleaned(self): ''' Test to verify that the npm cache is cleaned. ''' name = 'coffee-script' pkg_ret = {'name': name, 'result': False, 'comment': '', 'changes': {}} ret = {'name': None, 'result': False, 'comment': '', 'changes': {}} mock_list = MagicMock( return_value=['~/.npm', '~/.npm/{0}/'.format(name)]) mock_cache_clean_success = MagicMock(return_value=True) mock_cache_clean_failure = MagicMock(return_value=False) mock_err = MagicMock(side_effect=CommandExecutionError) with patch.dict(npm.__salt__, {'npm.cache_list': mock_err}): comt = ('Error looking up cached packages: ') ret.update({'comment': comt}) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__salt__, {'npm.cache_list': mock_err}): comt = ("Error looking up cached {0}: ".format(name)) pkg_ret.update({'comment': comt}) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) mock_data = { 'npm.cache_list': mock_list, 'npm.cache_clean': MagicMock() } with patch.dict(npm.__salt__, mock_data): non_cached_pkg = 'salt' comt = ('Package {0} is not in the cache'.format(non_cached_pkg)) pkg_ret.update({ 'name': non_cached_pkg, 'result': True, 'comment': comt }) self.assertDictEqual(npm.cache_cleaned(non_cached_pkg), pkg_ret) pkg_ret.update({'name': name}) with patch.dict(npm.__opts__, {'test': True}): comt = ('Cached packages set to be removed') ret.update({'result': None, 'comment': comt}) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {'test': True}): comt = ('Cached {0} set to be removed'.format(name)) pkg_ret.update({'result': None, 'comment': comt}) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) with patch.dict(npm.__opts__, {'test': False}): comt = ('Cached packages successfully removed') ret.update({ 'result': True, 'comment': comt, 'changes': { 'cache': 'Removed' } }) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {'test': False}): comt = ('Cached {0} successfully removed'.format(name)) pkg_ret.update({ 'result': True, 'comment': comt, 'changes': { name: 'Removed' } }) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) mock_data = { 'npm.cache_list': mock_list, 'npm.cache_clean': MagicMock(return_value=False) } with patch.dict(npm.__salt__, mock_data): with patch.dict(npm.__opts__, {'test': False}): comt = ('Error cleaning cached packages') ret.update({'result': False, 'comment': comt}) ret['changes'] = {} self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {'test': False}): comt = ('Error cleaning cached {0}'.format(name)) pkg_ret.update({'result': False, 'comment': comt}) pkg_ret['changes'] = {} self.assertDictEqual(npm.cache_cleaned(name), pkg_ret)
def test_cache_cleaned(self): """ Test to verify that the npm cache is cleaned. """ name = "coffee-script" pkg_ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": None, "result": False, "comment": "", "changes": {}} mock_list = MagicMock( return_value=["~/.npm", "~/.npm/{0}/".format(name)]) mock_cache_clean_success = MagicMock(return_value=True) mock_cache_clean_failure = MagicMock(return_value=False) mock_err = MagicMock(side_effect=CommandExecutionError) with patch.dict(npm.__salt__, {"npm.cache_list": mock_err}): comt = "Error looking up cached packages: " ret.update({"comment": comt}) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__salt__, {"npm.cache_list": mock_err}): comt = "Error looking up cached {0}: ".format(name) pkg_ret.update({"comment": comt}) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) mock_data = { "npm.cache_list": mock_list, "npm.cache_clean": MagicMock() } with patch.dict(npm.__salt__, mock_data): non_cached_pkg = "salt" comt = "Package {0} is not in the cache".format(non_cached_pkg) pkg_ret.update({ "name": non_cached_pkg, "result": True, "comment": comt }) self.assertDictEqual(npm.cache_cleaned(non_cached_pkg), pkg_ret) pkg_ret.update({"name": name}) with patch.dict(npm.__opts__, {"test": True}): comt = "Cached packages set to be removed" ret.update({"result": None, "comment": comt}) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {"test": True}): comt = "Cached {0} set to be removed".format(name) pkg_ret.update({"result": None, "comment": comt}) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) with patch.dict(npm.__opts__, {"test": False}): comt = "Cached packages successfully removed" ret.update({ "result": True, "comment": comt, "changes": { "cache": "Removed" } }) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {"test": False}): comt = "Cached {0} successfully removed".format(name) pkg_ret.update({ "result": True, "comment": comt, "changes": { name: "Removed" } }) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) mock_data = { "npm.cache_list": mock_list, "npm.cache_clean": MagicMock(return_value=False), } with patch.dict(npm.__salt__, mock_data): with patch.dict(npm.__opts__, {"test": False}): comt = "Error cleaning cached packages" ret.update({"result": False, "comment": comt}) ret["changes"] = {} self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {"test": False}): comt = "Error cleaning cached {0}".format(name) pkg_ret.update({"result": False, "comment": comt}) pkg_ret["changes"] = {} self.assertDictEqual(npm.cache_cleaned(name), pkg_ret)
def test_cache_cleaned(self): ''' Test to verify that the npm cache is cleaned. ''' name = 'coffee-script' pkg_ret = { 'name': name, 'result': False, 'comment': '', 'changes': {} } ret = { 'name': None, 'result': False, 'comment': '', 'changes': {} } mock_list = MagicMock(return_value=['~/.npm', '~/.npm/{0}/'.format(name)]) mock_cache_clean_success = MagicMock(return_value=True) mock_cache_clean_failure = MagicMock(return_value=False) mock_err = MagicMock(side_effect=CommandExecutionError) with patch.dict(npm.__salt__, {'npm.cache_list': mock_err}): comt = ('Error looking up cached packages: ') ret.update({'comment': comt}) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__salt__, {'npm.cache_list': mock_err}): comt = ("Error looking up cached {0}: ".format(name)) pkg_ret.update({'comment': comt}) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) mock_data = {'npm.cache_list': mock_list, 'npm.cache_clean': MagicMock()} with patch.dict(npm.__salt__, mock_data): non_cached_pkg = 'salt' comt = ('Package {0} is not in the cache'.format(non_cached_pkg)) pkg_ret.update({'name': non_cached_pkg, 'result': True, 'comment': comt}) self.assertDictEqual(npm.cache_cleaned(non_cached_pkg), pkg_ret) pkg_ret.update({'name': name}) with patch.dict(npm.__opts__, {'test': True}): comt = ('Cached packages set to be removed') ret.update({'result': None, 'comment': comt}) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {'test': True}): comt = ('Cached {0} set to be removed'.format(name)) pkg_ret.update({'result': None, 'comment': comt}) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) with patch.dict(npm.__opts__, {'test': False}): comt = ('Cached packages successfully removed') ret.update({'result': True, 'comment': comt, 'changes': {'cache': 'Removed'}}) self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {'test': False}): comt = ('Cached {0} successfully removed'.format(name)) pkg_ret.update({'result': True, 'comment': comt, 'changes': {name: 'Removed'}}) self.assertDictEqual(npm.cache_cleaned(name), pkg_ret) mock_data = {'npm.cache_list': mock_list, 'npm.cache_clean': MagicMock(return_value=False)} with patch.dict(npm.__salt__, mock_data): with patch.dict(npm.__opts__, {'test': False}): comt = ('Error cleaning cached packages') ret.update({'result': False, 'comment': comt}) ret['changes'] = {} self.assertDictEqual(npm.cache_cleaned(), ret) with patch.dict(npm.__opts__, {'test': False}): comt = ('Error cleaning cached {0}'.format(name)) pkg_ret.update({'result': False, 'comment': comt}) pkg_ret['changes'] = {} self.assertDictEqual(npm.cache_cleaned(name), pkg_ret)