review/tests/util.py @ e29d33ec47b1

cli: add support for deleting items
author Steve Losh <steve@stevelosh.com>
date Sat, 19 Jun 2010 02:03:37 -0400
parents bc5a004bfcca
children 40e13965af72
from __future__ import with_statement

"""Utilities for writing unit tests for hg-review."""

import os, shutil
import sample_data
from mercurial import cmdutil, commands, hg, ui
from .. import api, cli

_ui = ui.ui()
_ui.setconfig('extensions', 'progress', '!')
def review(init=False, comment=False, signoff=False, check=False, yes=False,
    no=False, force=False, message='', rev='.', remote_path='', lines='',
    files=None, unified='5', web=False, verbose=False, debug=False, mdown=False,
    seen=False, yeses='', no_nos=False, delete=False):

    if not files:
        files = []

    _ui.pushbuffer()
    if debug:
        _ui.debugflag = True
    elif verbose:
        _ui.verbose = True
    cli.review(_ui, get_sandbox_repo(), *files,
        **dict(
            init=init, comment=comment, signoff=signoff, check=check, yes=yes,
            no=no, force=force, message=message, rev=rev, remote_path=remote_path,
            lines=lines, unified=unified, web=web, mdown=mdown, seen=seen,
            yeses=yeses, no_nos=no_nos, delete=delete
        )
    )
    _ui.verbose, _ui.debugflag = False, False
    output = _ui.popbuffer()

    print output
    return output


sandbox_path = os.path.join(os.path.realpath('.'), 'sandbox')
sandbox_repo_path = os.path.join(sandbox_path, 'original')
sandbox_clone_path = os.path.join(sandbox_path, 'clone')

def setup_sandbox():
    os.mkdir(sandbox_path)
    os.chdir(sandbox_path)

    os.mkdir(sandbox_repo_path)
    os.chdir(sandbox_repo_path)

    commands.init(_ui)

    sandbox_hg_path = os.path.join(sandbox_repo_path, '.hg')
    with open(os.path.join(sandbox_hg_path, 'hgrc'), 'w') as hgrc:
        hgrc.write('[extensions]\nprogress=!')

    sandbox = get_sandbox_repo()

    opts = { 'addremove': True, 'date': None, 'user': 'Review Tester',
             'logfile': None, 'message': "Sandbox commit.", }
    for state in sample_data.log:
        for filename in state:
            dirname, key = None, filename

            # Support one-level-deep directories in the sample data.
            if '/' in filename:
                dirname, _, filename = filename.partition('/')
                if not os.path.exists(dirname):
                    os.mkdir(dirname)
                os.chdir(dirname)

            with open(filename, 'w') as f:
                f.write(state[key])

            if dirname:
                os.chdir('..')
        commands.commit(_ui, sandbox, **opts)

def setup_reviewed_sandbox():
    setup_sandbox()
    sandbox = get_sandbox_repo()

    rpath = os.path.join(sandbox.root, api.DEFAULT_DATASTORE_DIRNAME)
    review(init=True, remote_path=rpath)

    review_hg_path = os.path.join(rpath, '.hg')
    with open(os.path.join(review_hg_path, 'hgrc'), 'w') as hgrc:
        hgrc.write('[extensions]\nprogress=!')

    opts = { 'addremove': True, 'date': None, 'user': 'Review Tester',
             'logfile': None, 'message': "Add the code review.", }
    commands.commit(_ui, sandbox, **opts)

def teardown_sandbox():
    os.chdir(os.path.realpath(os.path.join(sandbox_path, os.pardir)))
    shutil.rmtree(sandbox_path)


def get_sandbox_repo():
    return hg.repository(_ui, sandbox_repo_path)

def get_sandbox_clone():
    return hg.repository(_ui, sandbox_clone_path)

def clone_sandbox_repo():
    hg.clone(cmdutil.remoteui(_ui, {}), sandbox_repo_path, sandbox_clone_path)

def get_datastore_repo(path=api.DEFAULT_DATASTORE_DIRNAME):
    return hg.repository(_ui, path)

def get_ui():
    return _ui