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

Started helpers for intermediate level integration tests

The idea is to provide what's needed for efficient
Mercurial tests at lower level than the .t system
(text-based tests), yet still more integration than
pure unit.

By not spawning hg processes, it should be faster
than text-based tests. Calling in the inner API will
allow tests that are closer in style to main developments
being tested, while simplifying assertions.

This version is meant for python2 only for the time being,
because that's what is currently to be tested for Heptapod.

We'll convert it for py3 for the hgitaly project, or make
it agnostic and publish it independently in some or other
way.
parent 61bb90d71e41
No related branches found
No related tags found
No related merge requests found
# Copyright 2019 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""Helpers for automatic tests.
These allow both high level operation on testing repos, and lower level
calls and introspections, making it possible to test more exhaustively inner
code paths that with `.t` tests, which are really functional tests.
"""
import os
from mercurial import (
cmdutil,
commands,
hg,
node,
ui as uimod,
)
# re-exports for stability
NULL_REVISION = node.nullrev
NULL_ID = node.nullid
def make_ui(base_ui):
# let's make sure we aren't polluted by surrounding settings
os.environ['HGRCPATH'] = ''
if base_ui is None:
return uimod.ui.load()
return base_ui.copy()
class LocalRepoWrapper(object):
def __init__(self, repo):
self.repo = repo
@classmethod
def init(cls, path, base_ui=None):
path = str(path)
init = cmdutil.findcmd('init', commands.table)[1][0]
ui = make_ui(base_ui)
init(ui, dest=path)
return cls(hg.repository(ui, path))
@classmethod
def load(cls, path, base_ui=None):
path = str(path)
ui = make_ui(base_ui)
return cls(hg.repository(ui, path))
def write_commit(self, rpath, content=None, message=None):
"""Write content at rpath and commit in one call.
This is meant to allow fast and efficient preparation of
testing repositories. To do so, it goes a bit lower level
than the actual commit command, so is not suitable to test specific
commit options, especially if through extensions.
This leaves the working directoy updated at the new commit.
:param rpath: relative path from repository root. If existing,
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``
:returns: binary node for the resulting commit.
"""
rpath = str(rpath)
repo = self.repo
path = os.path.join(repo.root, rpath)
if content is None:
content = "some content that should be random"
if message is None:
message = content
flags = 'wb' if isinstance(content, bytes) else 'w'
with open(path, flags) as fobj:
fobj.write(content)
def commitfun(ui, repo, message, match, opts):
return self.repo.commit(message,
opts.get('user'),
opts.get('date'),
match=match,
editor=False,
extra=None)
return cmdutil.commit(repo.ui, repo, commitfun, (path, ),
dict(addremove=True,
message=message))
# Copyright 2019 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from ..testhelpers import (
LocalRepoWrapper,
NULL_ID,
NULL_REVISION,
)
def test_init_write_commit(tmpdir):
wrapper = LocalRepoWrapper.init(tmpdir)
node = wrapper.write_commit('foo', content='Foo', message='Foo committed')
ctx = wrapper.repo[node]
assert ctx.description() == 'Foo committed'
parents = ctx.parents()
assert len(parents) == 1
assert parents[0].rev() == NULL_REVISION
assert parents[0].node() == NULL_ID
del wrapper, ctx
reloaded = LocalRepoWrapper.load(tmpdir)
rl_ctx = reloaded.repo[node]
assert rl_ctx.description() == 'Foo committed'
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