Skip to content
Snippets Groups Projects
Commit 3b02a4ed authored by Georges Racinet's avatar Georges Racinet
Browse files

testhelpers: write_commit parent kwarg

parent 9e636c1c
Branches
Tags
No related merge requests found
......@@ -56,7 +56,7 @@
ui = make_ui(base_ui, config=config)
return cls(hg.repository(ui, path))
def write_commit(self, rpath, content=None, message=None):
def write_commit(self, rpath, content=None, message=None, parent=None):
"""Write content at rpath and commit in one call.
This is meant to allow fast and efficient preparation of
......@@ -72,6 +72,10 @@
If not specified, will be replaced by random content.
:param message: message commit. If not specified, defaults to
``content``
:param parent: binary node value. If specified, the repository is
updated to it first. Useful to produce branching
histories. This is single valued, because the purpose
of this method is not to produce merge commits.
:returns: binary node for the resulting commit.
"""
rpath = str(rpath)
......@@ -81,6 +85,9 @@
content = "some content that should be random"
if message is None:
message = content
if parent is not None:
self.update_bin(parent)
flags = 'wb' if isinstance(content, bytes) else 'w'
with open(path, flags) as fobj:
fobj.write(content)
......@@ -95,3 +102,15 @@
return cmdutil.commit(repo.ui, repo, commitfun, (path, ),
dict(addremove=True,
message=message))
def update_bin(self, bin_node):
"""Update to a revision specified by its node in binary form.
This is separated in order to avoid ambiguities
"""
# maybe we'll do something lower level later
self.update(node.hex(bin_node))
def update(self, rev):
repo = self.repo
cmdutil.findcmd('update', commands.table)[1][0](repo.ui, repo, rev)
......@@ -67,7 +67,6 @@
def test_init_config_extension(tmpdir):
from mercurial import ui as uimod
ui = uimod.ui.load()
ui.setconfig('foo', 'bar', 'yes', source='tests')
ui.setconfig('extensions', 'heptapod', HGEXT_HEPTA_SOURCE, source='tests')
......@@ -80,3 +79,31 @@
assert wrapper.repo.ui.configbool('foo', 'bar')
exts = dict(extensions.extensions(wrapper.repo.ui))
assert_is_hepta_ext(exts.get('heptapod'))
def test_update(tmpdir):
wrapper = LocalRepoWrapper.init(tmpdir)
wrapper.write_commit('foo', content='Foo 0')
node1 = wrapper.write_commit('foo', content='Foo 1')
foo = tmpdir.join('foo')
assert foo.read() == 'Foo 1'
wrapper.update('0')
assert foo.read() == 'Foo 0'
wrapper.update_bin(NULL_ID)
assert not foo.isfile()
wrapper.update_bin(node1)
assert foo.read() == 'Foo 1'
def test_write_commit_branching(tmpdir):
"""Demonstrate the use of write_commit with parent."""
wrapper = LocalRepoWrapper.init(tmpdir)
node0 = wrapper.write_commit('foo', content='Foo 0')
wrapper.write_commit('foo', content='Foo 1')
nodebr = wrapper.write_commit('foo', content='Foo branch', parent=node0)
ctxbr = wrapper.repo[nodebr]
assert [c.node() for c in ctxbr.parents()] == [node0]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment