Merge pull request #20563 from thatch45/RobertFach-feature-20340-gem-sources

Merge #20382
This commit is contained in:
Nicole Thomas 2015-02-10 10:57:59 -07:00
commit d196ad2f6c
2 changed files with 107 additions and 0 deletions

View File

@ -147,3 +147,73 @@ def removed(name, ruby=None, user=None, gem_bin=None):
ret['result'] = False
ret['comment'] = 'Could not remove gem.'
return ret
def sources_add(name, ruby=None, user=None):
'''
Make sure that a gem source is added.
name
The URL of the gem source to be added
ruby: None
For RVM or rbenv installations: the ruby version and gemset to target.
user: None
The user under which to run the ``gem`` command
.. versionadded:: 0.17.0
'''
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
if name in __salt__['gem.sources_list'](ruby, runas=user):
ret['result'] = True
ret['comment'] = 'Gem source is already added.'
return ret
if __opts__['test']:
ret['comment'] = 'The gem source {0} would have been removed.'.format(name)
return ret
if __salt__['gem.sources_add'](source_uri=name, ruby=ruby, runas=user):
ret['result'] = True
ret['changes'][name] = 'Installed'
ret['comment'] = 'Gem source was successfully added.'
else:
ret['result'] = False
ret['comment'] = 'Could not add gem source.'
return ret
def sources_remove(name, ruby=None, user=None):
'''
Make sure that a gem source is removed.
name
The URL of the gem source to be removed
ruby: None
For RVM or rbenv installations: the ruby version and gemset to target.
user: None
The user under which to run the ``gem`` command
.. versionadded:: 0.17.0
'''
ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}
if name not in __salt__['gem.sources_list'](ruby, runas=user):
ret['result'] = True
ret['comment'] = 'Gem source is already removed.'
return ret
if __opts__['test']:
ret['comment'] = 'The gem source would have been removed'
return ret
if __salt__['gem.sources_remove'](source_uri=name, ruby=ruby, runas=user):
ret['result'] = True
ret['changes'][name] = 'Removed'
ret['comment'] = 'Gem source was successfully removed.'
else:
ret['result'] = False
ret['comment'] = 'Could not remove gem source.'
return ret

View File

@ -69,6 +69,43 @@ class TestGemState(TestCase):
gem_uninstall_fails.assert_called_once_with(
'bar', None, runas=None, gem_bin=None)
def test_sources_add(self):
gem_sources = ['http://foo', 'http://bar']
gem_sources_list = MagicMock(return_value=gem_sources)
gem_sources_add_succeeds = MagicMock(return_value=True)
gem_sources_add_fails = MagicMock(return_value=False)
with patch.dict(gem.__salt__, {'gem.sources_list': gem_sources_list}):
with patch.dict(gem.__salt__, {'gem.sources_add': gem_sources_add_succeeds}):
ret = gem.sources_add('http://foo')
self.assertEqual(True, ret['result'])
ret = gem.sources_add('http://fui')
self.assertEqual(True, ret['result'])
gem_sources_add_succeeds.assert_called_once_with(
source_uri='http://fui', ruby=None, runas=None)
with patch.dict(gem.__salt__, {'gem.sources_add': gem_sources_add_fails}):
ret = gem.sources_add('http://fui')
self.assertEqual(False, ret['result'])
gem_sources_add_fails.assert_called_once_with(
source_uri='http://fui', ruby=None, runas=None)
def test_sources_remove(self):
gem_sources = ['http://foo', 'http://bar']
gem_sources_list = MagicMock(return_value=gem_sources)
gem_sources_remove_succeeds = MagicMock(return_value=True)
gem_sources_remove_fails = MagicMock(return_value=False)
with patch.dict(gem.__salt__, {'gem.sources_list': gem_sources_list}):
with patch.dict(gem.__salt__, {'gem.sources_remove': gem_sources_remove_succeeds}):
ret = gem.sources_remove('http://fui')
self.assertEqual(True, ret['result'])
ret = gem.sources_remove('http://foo')
self.assertEqual(True, ret['result'])
gem_sources_remove_succeeds.assert_called_once_with(
source_uri='http://foo', ruby=None, runas=None)
with patch.dict(gem.__salt__, {'gem.sources_remove': gem_sources_remove_fails}):
ret = gem.sources_remove('http://bar')
self.assertEqual(False, ret['result'])
gem_sources_remove_fails.assert_called_once_with(
source_uri='http://bar', ruby=None, runas=None)
if __name__ == '__main__':
from integration import run_tests