~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# arch-tag: da321d26-9379-469d-b966-1cca8e5822ae
# Copyright (C) 2004 David Allouche <david@allouche.net>
#               2004 Canonical Ltd.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Testing framework
"""

import os
import tempfile
import shutil
import unittest
from arch.util import DirName


class Sandbox(object):

    def __init__(self):
        self._observers = list()
        self._set_up = False

    def subscribe(self, observer):
        if observer in self._observers: return
        self._observers.append(observer)

    def setUp(self):
        self._set_up = True
        self.home_dir = os.environ.get('HOME')
        self.tmp_dir = DirName(tempfile.mkdtemp(prefix='pyarch-')).realpath()
        self.saved_editor = None
        self.here = os.getcwd()
        os.environ['HOME'] = self.tmp_dir
        if os.environ.has_key('EDITOR'):
            self.saved_editor = os.environ['EDITOR']
            del(os.environ['EDITOR'])
        for obs in self._observers:
            obs.notify_setup(self)

    def tearDown(self):
        if not self._set_up: return
        os.environ['HOME'] = self.home_dir
        shutil.rmtree(self.tmp_dir, ignore_errors=True)
        if self.saved_editor is not None:
            os.environ['EDITOR'] = self.saved_editor
        os.chdir(self.here)


class TestParams(object):

    def __init__(self, sandbox=None):
        if sandbox: sandbox.subscribe(self)
        self.my_id = "John Doe <jdoe@example.com>"
        self.arch_name = 'jdoe@example.com--example--9999'
        self.arch_dir_base = DirName(r'pyarch\(sp)tests')

    def notify_setup(self, sandbox):
        self.arch_dir = sandbox.tmp_dir / self.arch_dir_base
        os.mkdir(self.arch_dir)
        self.working_dir = DirName(self.arch_dir/'workingtree')
        self.nested_dir = DirName(self.working_dir/'nestedtree')
        self.working_dir_newline = DirName(self.arch_dir/'working\ntree')
        self.other_working_dir = DirName(self.arch_dir/'othertree')

    def set_my_id(self):
        import arch
        arch.set_my_id(self.my_id)

    def create_archive(self):
        import arch
        name = self.arch_name
        self.archive = arch.make_archive(name, self.arch_dir/name)

    def _get_version(self):
        import arch
        return arch.Version(self.arch_name+'/cat--brn--1.0')
    version = property(_get_version)

    def _get_other_version(self):
        import arch
        return arch.Version(self.arch_name+'/cat--other--0')
    other_version = property(_get_other_version)

    def create_archive_and_mirror(self):
        self.create_archive()
        master = self.archive
        mirror_name = master.name + '-MIRROR'
        location = self.arch_dir/mirror_name
        self.mirror = master.make_mirror(mirror_name, location)

    def _create_tree_helper(self, dirname, version):
        import arch
        os.mkdir(dirname)
        if version is None:
            return arch.init_tree(self.working_dir)
        else:
            return arch.init_tree(self.working_dir, version)

    def create_working_tree(self, version=None):
        self.working_tree = self._create_tree_helper(self.working_dir, version)

    def create_other_tree(self, version=None):
        self.other_tree = (
            self._create_tree_helper(self.other_working_dir, version))

    def create_branch(self):
        self.version.branch.setup()
        assert self.version.branch.exists()

    def create_version(self):
        self.version.setup()

    def create_other_version(self):
        self.other_version.setup()

    def commit_summary(self, tree, summary):
        m = tree.log_message()
        m['Summary'] = m.description = summary
        tree.commit(m)

    def make_history(self, tree, history):
        for index, changes in enumerate(history):
            for name, content in changes.items():
                if name.startswith('%'): continue
                open(tree/name, 'w').write(content)
            for name in changes.get('%add', ()):
                tree.add_tag(name)
            for old, new in changes.get('%mv', ()):
                tree.move_file(old, new)
                tree.move_tag(old, new)
            if index == 0:
                tree.import_()
            else:
                self.commit_summary(tree, 'revision %d' % index)


class TestCase(unittest.TestCase):

    sandbox = None
    params = None

    def __getattribute__(self, name):
        try:
            return unittest.TestCase.__getattribute__(self, name)
        except AttributeError, exc:
            params = unittest.TestCase.__getattribute__(self, 'params')
            try:
                return getattr(params, name)
            except AttributeError:
                raise exc

    def setUp(self):
        self.sandbox = Sandbox()
        self.params = TestParams(self.sandbox)
        self.sandbox.setUp()
        self.extraSetup()

    def extraSetup(self):
        pass

    def tearDown(self):
        if self.sandbox:
            self.sandbox.tearDown()


def make_test_suite(globals_, classes, limit=()):
    if len(limit):
        limited_classes = limit
    else:
        limited_classes = classes
    suite = unittest.TestSuite()
    for cls_name in limited_classes:
        cls = globals_[cls_name]
        if hasattr(cls, 'tests'):
            suite.addTests(map(cls, cls.tests))
        else:
            suite.addTest(unittest.makeSuite(cls))
    return suite


def run_test_suite(argv, test_suite):
    suite = unittest.TestSuite()
    limit = argv[1:]
    suite.addTest(test_suite(limit))
    runner = unittest.TextTestRunner(verbosity=2)
    if not runner.run(suite).wasSuccessful(): return 1
    return 0