tests/util.py @ 16aa2def1d5b default tip

Update to work with Python 3

The Hamster Wheel of Backwards Incompatibility turns ever onward.  This probably
breaks support for Python 2, but at this point I don't care.  I just can't
summon up the willpower to do anything more than the most basic unfucking of
this thing that's been finished for 11 years but still breaks constantly because
of Mercurial's API churn and Python 3 setting the world on fire.

                                                                                    The
_  _ ____ _  _ ____ ___ ____ ____    _ _ _ _  _ ____ ____ _       ____ ____    ___  ____ ____ _  _ _ _ _ ____ ____ ___  ____    _ _  _ ____ ____ _  _ ___  ____ ___ _ ___  _ _    _ ___ _   _
|__| |__| |\/| [__   |  |___ |__/    | | | |__| |___ |___ |       |  | |___    |__] |__| |    |_/  | | | |__| |__/ |  \ [__     | |\ | |    |  | |\/| |__] |__|  |  | |__] | |    |  |   \_/
|  | |  | |  | ___]  |  |___ |  \    |_|_| |  | |___ |___ |___    |__| |       |__] |  | |___ | \_ |_|_| |  | |  \ |__/ ___]    | | \| |___ |__| |  | |    |  |  |  | |__] | |___ |  |    |
                                                                                turns forever
author Steve Losh <steve@stevelosh.com>
date Tue, 06 Apr 2021 12:55:48 -0400
parents 869146b8b9fb
children (none)
"""Utilities for writing unit tests for hg-prompt."""

import os, shutil, sys
from mercurial import cmdutil, commands, hg, ui

pkg_path = os.path.realpath(__file__)
sys.path =[os.path.split(os.path.split(pkg_path)[0])[0]] + sys.path
from prompt import prompt as _prompt

_ui = ui.ui()
def prompt(fs=''):
    _ui.pushbuffer()
    _prompt(_ui, get_sandbox_repo(), fs=fs)
    output = _ui.popbuffer()
    
    print output
    return output


sandbox_path = os.path.join(os.path.realpath('.'), 'sandbox')

def setup_sandbox():
    os.mkdir(sandbox_path)
    os.chdir(sandbox_path)
    
    commands.init(_ui)

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_path)

def get_sandbox_ui():
    return _ui


# Mercurial command wrappers

def hg_branch(branch='test'):
    commands.branch(_ui, get_sandbox_repo(), branch)

def hg_bookmark(bookmark='test'):
    commands.bookmark(_ui, get_sandbox_repo(), bookmark)

def hg_deactivate_bookmark():
    commands.bookmark(_ui, get_sandbox_repo(), inactive=True)

def hg_update(rev):
    opts = { 'rev': str(rev), }
    commands.update(_ui, get_sandbox_repo(), **opts)

def hg_merge(rev):
    opts = { 'rev': str(rev) }
    commands.merge(_ui, get_sandbox_repo(), **opts)

def hg_commit(filename='text.txt'):
    with open(os.path.join(sandbox_path, filename), 'a') as test_file:
        test_file.writelines(['test', '.'])
    
    opts = { 'addremove': True, 'date': None, 'user': 'Prompt Tester',
             'logfile': None, 'message': "Sandbox commit." }
    commands.commit(get_sandbox_ui(), get_sandbox_repo(), **opts)

def hg_log():
    opts = { 'template': '{rev} {desc}\n', 'rev': None, 'date': None, 'user': None }
    
    _ui.pushbuffer()
    commands.log(_ui, get_sandbox_repo(), **opts)
    output = _ui.popbuffer()
    
    return output