Source

repoman / tests / support.py

# Copyright 2009-2012 Edlund A/S
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
'''
Create some test forests in the current directory.
'''

# Commit user and initial date.
user = 'Jane Doe <jane.doe@rlyeh.org>'
date = 0

# Text of file foo, core/stable version.
foo1 = ('this is the\r\nfoo file which\r\ncontains several\r\n\r\n' +
        'lines of text\r\nwith which one\r\nmight perform\r\n\r\n' +
        'a number of\r\ndifferent\r\ntest scenarios\r\n\r\n' +
        'the lines are nice\r\nand short so that\r\nI don\'t need to spend\r\n\r\n' +
        'so much time making\r\nthis stuff up\r\n')

# Text of file foo, core/dev version.
foo2 = ('this is the\r\nfoo file which\r\ncontains several\r\n\r\n' +
        'lines of text\r\nwith which one\r\nmight perform\r\n\r\n' +
        'a number of\r\ndifferent\r\ntest scenarios\r\n\r\n' +
        'here we have moved\r\non to the core/dev\r\nbranch which means\r\n\r\n' +
        'we added some lines\r\ninto the file at\r\nthis point\r\n\r\n' +
        'the lines are nice\r\nand short so that\r\nI don\'t need to spend\r\n\r\n' +
        'so much time making\r\nthis stuff up\r\n')

# Text of file bar, core/dev version.
bar = ('this file is unique\r\nto the initial core/dev\r\ncommit\r\n\r\n' +
        'some more lines to\r\nmake diff tests more\r\nflexible\r\n')

import os
import sys

from mercurial import ui
from mercurial import hg

def write_text(filename, text):
    with file(filename, 'wb') as f:
        f.write(text)

def write_lines(filename, lines):
    with file(filename, 'wb') as f:
        f.write('\r\n'.join(lines) + '\r\n')

def date_str(date):
    return '%i 0' % date

def add_files(repo, files):
    if isinstance(files, str):
        files = [files]
    for f in files:
        repo.dirstate.add(f)
    repo.dirstate.write()


def create_test_forest():
    '''Create a test forest called 'forest1' in the current directory.'''

    u = ui.ui()
    modules = 'a b c d'.split()
    os.mkdir('forest1')
    os.chdir('forest1')
    root = hg.repository(u, '.root', create=True)
    repo = hg.repository(u, '.repo', create=True)
    repos = {}
    repos['a'] = hg.repository(u, 'a', create=True)
    repos['b'] = hg.repository(u, 'b', create=True)
    repos['c'] = hg.repository(u, 'c', create=True)
    repos['d'] = hg.repository(u, 'd', create=True)

    for m in modules:
        write_text('%s/foo' % m, foo1)
        r = repos[m]
        add_files(r, 'foo')
        r.dirstate.setbranch('core/stable')
        r.commit('initial core/stable', user=user, date=date_str(date))
        write_text('%s/foo' % m, foo2)
        write_text('%s/bar' % m, bar)
        add_files(r, 'bar')
        r.dirstate.setbranch('core/dev')
        r.commit('initial core/dev', user=user, date=date_str(date + 30))

    write_lines('.repo/modules', modules)
    add_files(repo, 'modules')
    repo.commit('initial forest', user=user, date=date_str(date + 60))
    write_text('.repo/branch', 'core/dev')

    write_text('.root/forest', 'forest1')
    os.mkdir('.root/solutions')
    write_text('.root/solutions/sol1', 'a\r\nb\r\nc\r\n')
    write_text('.root/solutions/sol2', 'b\r\nc\r\nd\r\n')
    write_text('.root/solutions/sol3', 'a\r\nc\r\nd\r\n')
    write_text('.root/solutions/sol4', 'c\r\nd\r\n')
    add_files(root, 'forest solutions/sol1 solutions/sol2 solutions/sol3 solutions/sol4'.split())
    root.commit('initial forest', user=user, date=date_str(date + 60))
    os.mkdir('.root/branches')
    os.mkdir('.root/branches/core')
    nullstr = '0' * 40
    write_lines('.root/branches/core/stable.b', ['%s %s' % (nullstr, m) for m in modules])
    add_files(root, 'branches/core/stable.b'.split())
    root.commit('Created branch core/stable', user=user, date=date_str(date + 60))
    write_lines('.root/branches/core/dev.b', ['%s %s' % (repos[m]['tip'].hex(), m) for m in modules])
    add_files(root, 'branches/core/dev.b'.split())
    root.commit('Created branch core/dev', user=user, date=date_str(date + 60))
    os.chdir('..')