Skip to content

Commit

Permalink
Adding tests for hg and correcting hg for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phargogh committed May 9, 2016
1 parent 3e773b8 commit 8c0ef1d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 9 deletions.
29 changes: 20 additions & 9 deletions paver/hg.py
Expand Up @@ -23,9 +23,9 @@ def clone(url, dest_folder, rev=None):
None"""
rev_string = ''
if rev:
rev_string = '-r %s' % rev
rev_string = ' -r %s' % rev

sh('hg clone {rev} {url} {dest}'.format(
sh('hg clone{rev} {url} {dest}'.format(
rev=rev_string, url=url, dest=dest_folder))


Expand All @@ -44,9 +44,15 @@ def pull(repo_path, rev=None, url=None):
None"""
rev_string = ''
if rev:
rev_string = '-r %s' % rev
rev_string = ' -r %s' % rev

sh('hg pull {rev} -R {repo}'.format(rev=rev_string, repo=repo_path))
url_string = ''
if url:
url_string = ' ' + url

sh('hg pull{rev} -R {repo}{url}'.format(rev=rev_string,
repo=repo_path,
url=url_string))


def latest_tag(repo_path, relative_to='tip'):
Expand Down Expand Up @@ -80,9 +86,9 @@ def update(repo_path, rev='tip', clean=False):
None"""
clean_string = ''
if clean:
clean_string = '--clean'
clean_string = ' --clean'

sh('hg update -r {rev} -R {repo} {clean}'.format(
sh('hg update -r {rev} -R {repo}{clean}'.format(
rev=rev, repo=repo_path, clean=clean_string))


Expand All @@ -101,13 +107,18 @@ def branches(repo_path, closed=False):

closed_string = ''
if closed:
closed_string = '--closed'
closed_string = ' --closed'

stdout_string = sh('hg branches -R {repo} {closed}'.format(
stdout_string = sh('hg branches -R {repo}{closed}'.format(
repo=repo_path, closed=closed_string), capture=True)

# Branch list comes out in the format:
# <branchname> <revnum>:<sha1>
branches = [string.split()[0] for string in stdout_string.split('\n')]
try:
branches = [line.split()[0] for line in stdout_string.split('\n')
if len(line) > 0]
except IndexError as e:
print 'std', stdout_string
print 'e', string

return current_branch, branches
82 changes: 82 additions & 0 deletions paver/tests/test_hg.py
@@ -0,0 +1,82 @@
from mock import patch
from paver import hg


@patch('paver.hg.sh')
def test_simple_clone(sh):
hg.clone("ssh://foo/foo", "bar")
assert sh.called
assert sh.call_args[0][0] == "hg clone ssh://foo/foo bar"


@patch('paver.hg.sh')
def test_simple_clone_to_rev(sh):
hg.clone('ssh://foo/bar', 'baz', 'bam')
assert sh.called
assert sh.call_args[0][0] == 'hg clone -r bam ssh://foo/bar baz'


@patch('paver.hg.sh')
def test_pull_simple(sh):
hg.pull('foo')
assert sh.called
assert sh.call_args[0][0] == 'hg pull -R foo'


@patch('paver.hg.sh')
def test_pull_rev(sh):
hg.pull('foo', 'bar')
assert sh.called
assert sh.call_args[0][0] == 'hg pull -r bar -R foo'


@patch('paver.hg.sh')
def test_pull_rev_url(sh):
hg.pull('foo', 'bar', 'baz')
assert sh.called
assert sh.call_args[0][0] == 'hg pull -r bar -R foo baz', sh.call_args[0][0]


def test_latest_tag():
with patch('paver.hg.sh') as sh:
sh.return_value = 'example'

returned_value = hg.latest_tag('foo')
assert returned_value == 'example', returned_value


@patch('paver.hg.sh')
def test_update_simple(sh):
hg.update('foo')
assert sh.called
assert sh.call_args[0][0] == 'hg update -r tip -R foo'


@patch('paver.hg.sh')
def test_update_to_rev(sh):
hg.update('foo', 'bar')
assert sh.called
assert sh.call_args[0][0] == 'hg update -r bar -R foo'


@patch('paver.hg.sh')
def test_update_to_rev_and_clean(sh):
hg.update('foo', 'bar', True)
assert sh.called
assert sh.call_args[0][0] == 'hg update -r bar -R foo --clean'


@patch('paver.hg.sh')
def test_branches_with_closed(sh):
sh.side_effect = [
'tag1', ('branch1 123:9871230987\n'
'branch2 456:1957091982\n')]
current_branch, branches = hg.branches('foo', True)

assert sh.called
assert sh.call_args_list[0][0][0] == 'hg branch -R foo'
assert sh.call_args_list[1][0][0] == 'hg branches -R foo --closed'

assert current_branch == 'tag1'
assert branches == ['branch1', 'branch2']

0 comments on commit 8c0ef1d

Please sign in to comment.