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

testhelpers: new commit() method

it will both be the base for new helpers, and directly usable as
demonstrated by the new test case.
parent e7f429bf44bd
No related branches found
No related tags found
No related merge requests found
......@@ -160,5 +160,69 @@
else:
self.update(parent.hex())
def commit(self, rel_paths,
message=None,
utc_timestamp=None,
branch=None,
topic=None, # TODO probably not to be upstreamed
user=None,
add_remove=False, return_ctx=True):
"""Commit the current state of working directory.
This method does not perform any update nor does it change the dirstate
before committing. See :meth:`prepare_wdir` for helpers about that.
:param rel_paths: any iterable of relative paths from the repository
root. Each can be specified as :class:`str` or :class:`bytes`
:param message: commit message. If not specified, a randomized value
is used.
:param user: full user name and email, as in ``ui.username`` config
option. Can be :class:`str` or :class:`bytes`
:param utc_timestamp: seconds since Epoch UTC. Good enough for
tests without ambiguity. Can be float (only
seconds will be kept). Defaults to
``time.time()``
:param return_ctx: if ``True``, returns a :class:`changectx` instance
and a binary node id otherwise, which can be more
straightforward and faster in some cases.
:returns: :class:`changectx` instance or binary node id for the
generated commit, according to ``return_ctx``.
"""
repo = self.repo
if utc_timestamp is None:
utc_timestamp = time.time()
if branch is not None:
repo.dirstate.setbranch(as_bytes(branch))
if topic is not None:
self.command(b'topics', as_bytes(topic))
if user is None:
user = repo.ui.config(b'ui', b'username')
if message is None:
message = as_bytes(
"random: {}\n\nparent: {}\n".format(
random.random(),
node.hex(repo.dirstate.p1()))
)
def commitfun(ui, repo, message, match, opts):
return repo.commit(message,
as_bytes(user),
(utc_timestamp, 0),
match=match,
editor=False,
extra=None,
)
new_node = cmdutil.commit(repo.ui, repo, commitfun,
(os.path.join(repo.root, as_bytes(rel_path))
for rel_path in rel_paths),
{b'addremove': add_remove,
b'message': as_bytes(message),
})
return repo[new_node] if return_ctx else new_node
def write_commit(self, rpath,
content=None, message=None,
......@@ -163,4 +227,3 @@
def write_commit(self, rpath,
content=None, message=None,
return_ctx=True,
parent=None,
......@@ -166,8 +229,5 @@
parent=None,
branch=None,
user=None,
utc_timestamp=None,
topic=None):
**commit_opts):
"""Write content at rpath and commit in one call.
This is meant to allow fast and efficient preparation of
......@@ -181,10 +241,8 @@
will be overwritten by `content`
:param content: what's to be written in ``rpath``.
If not specified, will be replaced by random content.
:param message: message commit. If not specified, defaults to
``content``
:param parent: binary node id or :class:`changectx` instance.
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.
......@@ -186,19 +244,10 @@
:param parent: binary node id or :class:`changectx` instance.
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.
:param user: full user name and email, as in ``ui.username`` config
option. Can be :class:`str` or :class:`bytes`
:param utc_timestamp: seconds since Epoch UTC. Good enough for
tests without ambiguity. Can be float (only
seconds will be kept). Defaults to
``time.time()``
:param return_ctx: if ``True``, returns a :class:`changectx` instance
and a binary node id otherwise, which can be more
straightforward and faster in some cases.
:returns: :class:`changectx` instance or binary node id for the
generated commit, according to ``return_ctx``.
:param commit_opts: additional kwargs as in :meth:`commit`
:returns: same as :meth:`commit`
"""
repo = self.repo
path = os.path.join(repo.root, as_bytes(rpath))
......@@ -211,12 +260,6 @@
if message is None:
message = content
if branch is not None:
self.repo.dirstate.setbranch(as_bytes(branch))
if topic is not None:
self.command(b'topics', as_bytes(topic))
with open(path, 'wb') as fobj:
fobj.write(content)
......@@ -220,21 +263,8 @@
with open(path, 'wb') as fobj:
fobj.write(content)
if utc_timestamp is None:
utc_timestamp = time.time()
def commitfun(ui, repo, message, match, opts):
return self.repo.commit(message,
as_bytes(user),
(int(utc_timestamp), 0),
match=match,
editor=False,
extra=None)
new_node = cmdutil.commit(repo.ui, repo, commitfun, (path, ),
{b'addremove': True,
b'message': as_bytes(message),
})
return repo[new_node] if return_ctx else new_node
return self.commit((path, ), message=message, add_remove=True,
**commit_opts)
def update_bin(self, bin_node, **opts):
"""Update to a revision specified by its node in binary form.
......
......@@ -134,6 +134,15 @@
assert main_ctx.description() == b'Done in dest'
def test_commit(tmpdir):
"""Demonstrates message auto generation and how to commit several files."""
wrapper = LocalRepoWrapper.init(tmpdir)
(tmpdir / 'foo').write('foo')
(tmpdir / 'bar').write('bar')
ctx0 = wrapper.commit(('foo', 'bar'), add_remove=True)
assert set(ctx0.files()) == {b'foo', b'bar'}
def test_write_commit_named_branch(tmpdir):
"""Demonstrate the use of write_commit with parent."""
wrapper = LocalRepoWrapper.init(tmpdir)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment