Add the option to skip updating the head for a hg latest state (#37002)

- Updated hg state tests
This commit is contained in:
Daniel Hobley 2016-10-14 17:07:33 +02:00 committed by Nicole Thomas
parent c3ae83f533
commit 0aa6ecfb7f
2 changed files with 80 additions and 3 deletions

View File

@ -47,7 +47,8 @@ def latest(name,
user=None,
identity=None,
force=False,
opts=False):
opts=False,
update_head=True):
'''
Make sure the repository is cloned to the given directory and is up to date
@ -78,6 +79,9 @@ def latest(name,
opts
Include additional arguments and options to the hg command line
update_head
Should we update the head if new changes are found? Defaults to True
'''
ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
@ -89,7 +93,7 @@ def latest(name,
os.path.isdir('{0}/.hg'.format(target)))
if is_repository:
ret = _update_repo(ret, name, target, clean, user, identity, rev, opts)
ret = _update_repo(ret, name, target, clean, user, identity, rev, opts, update_head)
else:
if os.path.isdir(target):
fail = _handle_existing(ret, target, force)
@ -108,7 +112,7 @@ def latest(name,
return ret
def _update_repo(ret, name, target, clean, user, identity, rev, opts):
def _update_repo(ret, name, target, clean, user, identity, rev, opts, update_head):
'''
Update the repo to a given revision. Using clean passes -C to the hg up
'''
@ -138,6 +142,14 @@ def _update_repo(ret, name, target, clean, user, identity, rev, opts):
ret['comment'] = err
return ret
if update_head is False:
changes = 'no changes found' not in pull_out
if changes:
ret['comment'] = 'Update is probably required but update_head=False so we will skip updating.'
else:
ret['comment'] = 'No changes found and update_head=False so will skip updating.'
return ret
if rev:
try:
__salt__['hg.update'](target, rev, force=clean, user=user)

View File

@ -22,6 +22,7 @@ from salttesting.mock import (
ensure_in_syspath('../../')
hg.__opts__ = {}
hg.__salt__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
@ -56,6 +57,70 @@ class HgTestCase(TestCase):
self.assertDictEqual(hg.latest("salt", target="c:\\salt"),
ret)
def test_latest_update_changes(self):
'''
Test to make sure we don't update even if we have changes
'''
ret = {'changes': {}, 'comment': '',
'name': 'salt', 'result': True}
revision_mock = MagicMock(return_value='abcdef')
pull_mock = MagicMock(return_value='Blah.')
update_mock = MagicMock()
with patch.dict(hg.__salt__, {'hg.revision': revision_mock,
'hg.pull': pull_mock,
'hg.update': update_mock}):
mock = MagicMock(side_effect=[True, True])
with patch.object(os.path, 'isdir', mock):
mock = MagicMock(return_value=True)
with patch.dict(hg.__opts__, {'test': False}):
with patch.object(hg, '_clone_repo', mock):
self.assertDictEqual(hg.latest("salt", target="c:\\salt", update_head=True), ret)
assert update_mock.called
def test_latest_no_update_changes(self):
'''
Test to make sure we don't update even if we have changes
'''
ret = {'changes': {}, 'comment': 'Update is probably required but update_head=False so we will skip updating.',
'name': 'salt', 'result': True}
revision_mock = MagicMock(return_value='abcdef')
pull_mock = MagicMock(return_value='Blah.')
update_mock = MagicMock()
with patch.dict(hg.__salt__, {'hg.revision': revision_mock,
'hg.pull': pull_mock,
'hg.update': update_mock}):
mock = MagicMock(side_effect=[True, True])
with patch.object(os.path, 'isdir', mock):
mock = MagicMock(return_value=True)
with patch.dict(hg.__opts__, {'test': False}):
with patch.object(hg, '_clone_repo', mock):
self.assertDictEqual(hg.latest("salt", target="c:\\salt", update_head=False), ret)
assert not update_mock.called
def test_latest_no_update_no_changes(self):
'''
Test to Make sure the repository is cloned to
the given directory and is up to date
'''
ret = {'changes': {}, 'comment': 'No changes found and update_head=False so will skip updating.',
'name': 'salt', 'result': True}
revision_mock = MagicMock(return_value='abcdef')
pull_mock = MagicMock(return_value='Blah no changes found.')
update_mock = MagicMock()
with patch.dict(hg.__salt__, {'hg.revision': revision_mock,
'hg.pull': pull_mock,
'hg.update': update_mock}):
mock = MagicMock(side_effect=[True, True])
with patch.object(os.path, 'isdir', mock):
mock = MagicMock(return_value=True)
with patch.dict(hg.__opts__, {'test': False}):
with patch.object(hg, '_clone_repo', mock):
self.assertDictEqual(hg.latest("salt", target="c:\\salt", update_head=False), ret)
assert not update_mock.called
if __name__ == '__main__':
from integration import run_tests