~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/arch/tests/framework.py

  • Committer: Robert Collins
  • Date: 2005-09-13 12:39:26 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050913123926-b72242bdacc1ae52
create the output directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# arch-tag: da321d26-9379-469d-b966-1cca8e5822ae
 
2
# Copyright (C) 2004 David Allouche <david@allouche.net>
 
3
#               2004 Canonical Ltd.
 
4
#
 
5
#    This program is free software; you can redistribute it and/or modify
 
6
#    it under the terms of the GNU General Public License as published by
 
7
#    the Free Software Foundation; either version 2 of the License, or
 
8
#    (at your option) any later version.
 
9
#
 
10
#    This program is distributed in the hope that it will be useful,
 
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#    GNU General Public License for more details.
 
14
#
 
15
#    You should have received a copy of the GNU General Public License
 
16
#    along with this program; if not, write to the Free Software
 
17
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
"""Testing framework
 
20
"""
 
21
 
 
22
import os
 
23
import tempfile
 
24
import shutil
 
25
import unittest
 
26
from arch.util import DirName
 
27
 
 
28
 
 
29
class Sandbox(object):
 
30
 
 
31
    def __init__(self):
 
32
        self._observers = list()
 
33
        self._set_up = False
 
34
 
 
35
    def subscribe(self, observer):
 
36
        if observer in self._observers: return
 
37
        self._observers.append(observer)
 
38
 
 
39
    def setUp(self):
 
40
        self._set_up = True
 
41
        self.home_dir = os.environ.get('HOME')
 
42
        self.tmp_dir = DirName(tempfile.mkdtemp(prefix='pyarch-')).realpath()
 
43
        self.saved_editor = None
 
44
        self.here = os.getcwd()
 
45
        os.environ['HOME'] = self.tmp_dir
 
46
        if os.environ.has_key('EDITOR'):
 
47
            self.saved_editor = os.environ['EDITOR']
 
48
            del(os.environ['EDITOR'])
 
49
        for obs in self._observers:
 
50
            obs.notify_setup(self)
 
51
 
 
52
    def tearDown(self):
 
53
        if not self._set_up: return
 
54
        os.environ['HOME'] = self.home_dir
 
55
        shutil.rmtree(self.tmp_dir, ignore_errors=True)
 
56
        if self.saved_editor is not None:
 
57
            os.environ['EDITOR'] = self.saved_editor
 
58
        os.chdir(self.here)
 
59
 
 
60
 
 
61
class TestParams(object):
 
62
 
 
63
    def __init__(self, sandbox=None):
 
64
        if sandbox: sandbox.subscribe(self)
 
65
        self.my_id = "John Doe <jdoe@example.com>"
 
66
        self.arch_name = 'jdoe@example.com--example--9999'
 
67
        self.arch_dir_base = DirName(r'pyarch\(sp)tests')
 
68
 
 
69
    def notify_setup(self, sandbox):
 
70
        self.arch_dir = sandbox.tmp_dir / self.arch_dir_base
 
71
        os.mkdir(self.arch_dir)
 
72
        self.working_dir = DirName(self.arch_dir/'workingtree')
 
73
        self.nested_dir = DirName(self.working_dir/'nestedtree')
 
74
        self.working_dir_newline = DirName(self.arch_dir/'working\ntree')
 
75
        self.other_working_dir = DirName(self.arch_dir/'othertree')
 
76
 
 
77
    def set_my_id(self):
 
78
        import arch
 
79
        arch.set_my_id(self.my_id)
 
80
 
 
81
    def create_archive(self):
 
82
        import arch
 
83
        name = self.arch_name
 
84
        self.archive = arch.make_archive(name, self.arch_dir/name)
 
85
 
 
86
    def _get_version(self):
 
87
        import arch
 
88
        return arch.Version(self.arch_name+'/cat--brn--1.0')
 
89
    version = property(_get_version)
 
90
 
 
91
    def _get_other_version(self):
 
92
        import arch
 
93
        return arch.Version(self.arch_name+'/cat--other--0')
 
94
    other_version = property(_get_other_version)
 
95
 
 
96
    def create_archive_and_mirror(self):
 
97
        self.create_archive()
 
98
        master = self.archive
 
99
        mirror_name = master.name + '-MIRROR'
 
100
        location = self.arch_dir/mirror_name
 
101
        self.mirror = master.make_mirror(mirror_name, location)
 
102
 
 
103
    def _create_tree_helper(self, dirname, version):
 
104
        import arch
 
105
        os.mkdir(dirname)
 
106
        if version is None:
 
107
            return arch.init_tree(self.working_dir)
 
108
        else:
 
109
            return arch.init_tree(self.working_dir, version)
 
110
 
 
111
    def create_working_tree(self, version=None):
 
112
        self.working_tree = self._create_tree_helper(self.working_dir, version)
 
113
 
 
114
    def create_other_tree(self, version=None):
 
115
        self.other_tree = (
 
116
            self._create_tree_helper(self.other_working_dir, version))
 
117
 
 
118
    def create_branch(self):
 
119
        self.version.branch.setup()
 
120
        assert self.version.branch.exists()
 
121
 
 
122
    def create_version(self):
 
123
        self.version.setup()
 
124
 
 
125
    def create_other_version(self):
 
126
        self.other_version.setup()
 
127
 
 
128
    def commit_summary(self, tree, summary):
 
129
        m = tree.log_message()
 
130
        m['Summary'] = m.description = summary
 
131
        tree.commit(m)
 
132
 
 
133
    def make_history(self, tree, history):
 
134
        for index, changes in enumerate(history):
 
135
            for name, content in changes.items():
 
136
                if name.startswith('%'): continue
 
137
                open(tree/name, 'w').write(content)
 
138
            for name in changes.get('%add', ()):
 
139
                tree.add_tag(name)
 
140
            for old, new in changes.get('%mv', ()):
 
141
                tree.move_file(old, new)
 
142
                tree.move_tag(old, new)
 
143
            if index == 0:
 
144
                tree.import_()
 
145
            else:
 
146
                self.commit_summary(tree, 'revision %d' % index)
 
147
 
 
148
 
 
149
class TestCase(unittest.TestCase):
 
150
 
 
151
    sandbox = None
 
152
    params = None
 
153
 
 
154
    def __getattribute__(self, name):
 
155
        try:
 
156
            return unittest.TestCase.__getattribute__(self, name)
 
157
        except AttributeError, exc:
 
158
            params = unittest.TestCase.__getattribute__(self, 'params')
 
159
            try:
 
160
                return getattr(params, name)
 
161
            except AttributeError:
 
162
                raise exc
 
163
 
 
164
    def setUp(self):
 
165
        self.sandbox = Sandbox()
 
166
        self.params = TestParams(self.sandbox)
 
167
        self.sandbox.setUp()
 
168
        self.extraSetup()
 
169
 
 
170
    def extraSetup(self):
 
171
        pass
 
172
 
 
173
    def tearDown(self):
 
174
        if self.sandbox:
 
175
            self.sandbox.tearDown()
 
176
 
 
177
 
 
178
def make_test_suite(globals_, classes, limit=()):
 
179
    if len(limit):
 
180
        limited_classes = limit
 
181
    else:
 
182
        limited_classes = classes
 
183
    suite = unittest.TestSuite()
 
184
    for cls_name in limited_classes:
 
185
        cls = globals_[cls_name]
 
186
        if hasattr(cls, 'tests'):
 
187
            suite.addTests(map(cls, cls.tests))
 
188
        else:
 
189
            suite.addTest(unittest.makeSuite(cls))
 
190
    return suite
 
191
 
 
192
 
 
193
def run_test_suite(argv, test_suite):
 
194
    suite = unittest.TestSuite()
 
195
    limit = argv[1:]
 
196
    suite.addTest(test_suite(limit))
 
197
    runner = unittest.TextTestRunner(verbosity=2)
 
198
    if not runner.run(suite).wasSuccessful(): return 1
 
199
    return 0