/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
608 by Martin Pool
- Split selftests out into a new module and start changing them
1
# Copyright (C) 2005 by Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
609 by Martin Pool
- cleanup test code
17
720 by Martin Pool
- start moving external tests into the testsuite framework
18
from unittest import TestResult, TestCase
719 by Martin Pool
- reorganize selftest code
19
727 by Martin Pool
- move more code to run external commands from testbzr to selftest
20
try:
21
    import shutil
22
    from subprocess import call, Popen, PIPE
23
except ImportError, e:
24
    sys.stderr.write("testbzr: sorry, this test suite requires the subprocess module\n"
25
                     "this is shipped with python2.4 and available separately for 2.3\n")
26
    raise
27
28
29
class CommandFailed(Exception):
30
    pass
31
721 by Martin Pool
- framework for running external commands from unittest suite
32
33
class TestBase(TestCase):
34
    """Base class for bzr test cases.
35
36
    Just defines some useful helper functions; doesn't actually test
37
    anything.
38
    """
727 by Martin Pool
- move more code to run external commands from testbzr to selftest
39
    
40
    # TODO: Special methods to invoke bzr, so that we can run it
41
    # through a specified Python intepreter
42
43
    OVERRIDE_PYTHON = None # to run with alternative python 'python'
44
    BZRPATH = 'bzr'
45
    
46
47
    def formcmd(self, cmd):
48
        if isinstance(cmd, basestring):
49
            cmd = cmd.split()
50
51
        if cmd[0] == 'bzr':
52
            cmd[0] = self.BZRPATH
53
            if self.OVERRIDE_PYTHON:
54
                cmd.insert(0, self.OVERRIDE_PYTHON)
55
56
        self.log('$ %r' % cmd)
57
58
        return cmd
59
60
61
    def runcmd(self, cmd, retcode=0):
62
        """Run one command and check the return code.
63
64
        Returns a tuple of (stdout,stderr) strings.
65
66
        If a single string is based, it is split into words.
67
        For commands that are not simple space-separated words, please
68
        pass a list instead."""
69
        cmd = self.formcmd(cmd)
70
71
        self.log('$ ' + ' '.join(cmd))
72
        actual_retcode = call(cmd, stdout=self.TEST_LOG, stderr=self.TEST_LOG)
73
74
        if retcode != actual_retcode:
75
            raise CommandFailed("test failed: %r returned %d, expected %d"
76
                                % (cmd, actual_retcode, retcode))
77
78
732 by Martin Pool
- move more tests into bzr selftest
79
    def backtick(self, cmd, retcode=0):
80
        cmd = self.formcmd(cmd)
81
        child = Popen(cmd, stdout=PIPE, stderr=self.TEST_LOG)
82
        outd, errd = child.communicate()
83
        self.log(outd)
84
        actual_retcode = child.wait()
85
86
        outd = outd.replace('\r', '')
87
88
        if retcode != actual_retcode:
89
            raise CommandFailed("test failed: %r returned %d, expected %d"
90
                                % (cmd, actual_retcode, retcode))
91
92
        return outd
93
94
95
721 by Martin Pool
- framework for running external commands from unittest suite
96
97
    def log(self, msg):
98
        """Log a message to a progress file"""
726 by Martin Pool
- more rearrangement of blackbox tests
99
        print >>self.TEST_LOG, msg
721 by Martin Pool
- framework for running external commands from unittest suite
100
               
101
732 by Martin Pool
- move more tests into bzr selftest
102
class InTempDir(TestBase):
103
    """Base class for tests run in a temporary branch."""
104
    def setUp(self):
105
        import os
735 by Martin Pool
- clean up code for running tests in selfcontained directories
106
        self.test_dir = os.path.join(self.TEST_ROOT, self.__class__.__name__)
107
        os.mkdir(self.test_dir)
108
        os.chdir(self.test_dir)
732 by Martin Pool
- move more tests into bzr selftest
109
        
110
    def tearDown(self):
111
        import os
735 by Martin Pool
- clean up code for running tests in selfcontained directories
112
        os.chdir(self.TEST_ROOT)
732 by Martin Pool
- move more tests into bzr selftest
113
114
115
721 by Martin Pool
- framework for running external commands from unittest suite
116
117
720 by Martin Pool
- start moving external tests into the testsuite framework
118
class _MyResult(TestResult):
721 by Martin Pool
- framework for running external commands from unittest suite
119
    """
120
    Custom TestResult.
121
122
    No special behaviour for now.
123
    """
745 by Martin Pool
- redirect stdout/stderr while running tests
124
    def __init__(self, out):
125
        self.out = out
126
        TestResult.__init__(self)
127
733 by Martin Pool
- show test names while running
128
    def startTest(self, test):
744 by Martin Pool
- show nicer descriptions while running tests
129
        # TODO: Maybe show test.shortDescription somewhere?
745 by Martin Pool
- redirect stdout/stderr while running tests
130
        print >>self.out, '%-60.60s' % test.id(),
733 by Martin Pool
- show test names while running
131
        TestResult.startTest(self, test)
132
133
    def stopTest(self, test):
134
        # print
135
        TestResult.stopTest(self, test)
136
137
138
    def addError(self, test, err):
745 by Martin Pool
- redirect stdout/stderr while running tests
139
        print >>self.out, 'ERROR'
733 by Martin Pool
- show test names while running
140
        TestResult.addError(self, test, err)
141
142
    def addFailure(self, test, err):
745 by Martin Pool
- redirect stdout/stderr while running tests
143
        print >>self.out, 'FAILURE'
733 by Martin Pool
- show test names while running
144
        TestResult.addFailure(self, test, err)
145
146
    def addSuccess(self, test):
745 by Martin Pool
- redirect stdout/stderr while running tests
147
        print >>self.out, 'OK'
733 by Martin Pool
- show test names while running
148
        TestResult.addSuccess(self, test)
719 by Martin Pool
- reorganize selftest code
149
150
720 by Martin Pool
- start moving external tests into the testsuite framework
151
608 by Martin Pool
- Split selftests out into a new module and start changing them
152
def selftest():
721 by Martin Pool
- framework for running external commands from unittest suite
153
    from unittest import TestLoader, TestSuite
154
    import bzrlib
723 by Martin Pool
- move whitebox/blackbox modules into bzrlib.selftest subdirectory
155
    import bzrlib.selftest.whitebox
156
    import bzrlib.selftest.blackbox
743 by Martin Pool
- new simple versioning test cases
157
    import bzrlib.selftest.versioning
721 by Martin Pool
- framework for running external commands from unittest suite
158
    from doctest import DocTestSuite
159
    import os
160
    import shutil
161
    import time
745 by Martin Pool
- redirect stdout/stderr while running tests
162
    import sys
721 by Martin Pool
- framework for running external commands from unittest suite
163
164
    _setup_test_log()
165
    _setup_test_dir()
744 by Martin Pool
- show nicer descriptions while running tests
166
    print
721 by Martin Pool
- framework for running external commands from unittest suite
167
168
    suite = TestSuite()
169
    tl = TestLoader()
170
743 by Martin Pool
- new simple versioning test cases
171
    for m in bzrlib.selftest.whitebox, \
172
            bzrlib.selftest.versioning:
721 by Martin Pool
- framework for running external commands from unittest suite
173
        suite.addTest(tl.loadTestsFromModule(m))
174
726 by Martin Pool
- more rearrangement of blackbox tests
175
    suite.addTest(bzrlib.selftest.blackbox.suite())
176
721 by Martin Pool
- framework for running external commands from unittest suite
177
    for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
178
            bzrlib.commands:
179
        suite.addTest(DocTestSuite(m))
180
745 by Martin Pool
- redirect stdout/stderr while running tests
181
    # save stdout & stderr so there's no leakage from code-under-test
182
    real_stdout = sys.stdout
183
    real_stderr = sys.stderr
184
    sys.stdout = sys.stderr = TestBase.TEST_LOG
185
    try:
186
        result = _MyResult(real_stdout)
187
        suite.run(result)
188
    finally:
189
        sys.stdout = real_stdout
190
        sys.stderr = real_stderr
721 by Martin Pool
- framework for running external commands from unittest suite
191
192
    _show_results(result)
193
194
    return result.wasSuccessful()
195
196
745 by Martin Pool
- redirect stdout/stderr while running tests
197
198
721 by Martin Pool
- framework for running external commands from unittest suite
199
def _setup_test_log():
200
    import time
201
    import os
202
    
203
    log_filename = os.path.abspath('testbzr.log')
726 by Martin Pool
- more rearrangement of blackbox tests
204
    TestBase.TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
721 by Martin Pool
- framework for running external commands from unittest suite
205
726 by Martin Pool
- more rearrangement of blackbox tests
206
    print >>TestBase.TEST_LOG, "bzr tests run at " + time.ctime()
721 by Martin Pool
- framework for running external commands from unittest suite
207
    print '%-30s %s' % ('test log', log_filename)
208
209
210
def _setup_test_dir():
211
    import os
212
    import shutil
213
    
726 by Martin Pool
- more rearrangement of blackbox tests
214
    TestBase.ORIG_DIR = os.getcwdu()
735 by Martin Pool
- clean up code for running tests in selfcontained directories
215
    TestBase.TEST_ROOT = os.path.abspath("testbzr.tmp")
216
217
    print '%-30s %s' % ('running tests in', TestBase.TEST_ROOT)
218
219
    if os.path.exists(TestBase.TEST_ROOT):
220
        shutil.rmtree(TestBase.TEST_ROOT)
221
    os.mkdir(TestBase.TEST_ROOT)
222
    os.chdir(TestBase.TEST_ROOT)
726 by Martin Pool
- more rearrangement of blackbox tests
223
224
    # make a fake bzr directory there to prevent any tests propagating
225
    # up onto the source directory's real branch
735 by Martin Pool
- clean up code for running tests in selfcontained directories
226
    os.mkdir(os.path.join(TestBase.TEST_ROOT, '.bzr'))
721 by Martin Pool
- framework for running external commands from unittest suite
227
228
    
229
230
def _show_results(result):
231
     for case, tb in result.errors:
232
         _show_test_failure('ERROR', case, tb)
233
234
     for case, tb in result.failures:
235
         _show_test_failure('FAILURE', case, tb)
236
         
237
     print
719 by Martin Pool
- reorganize selftest code
238
     print '%4d tests run' % result.testsRun
239
     print '%4d errors' % len(result.errors)
240
     print '%4d failures' % len(result.failures)
241
721 by Martin Pool
- framework for running external commands from unittest suite
242
243
244
def _show_test_failure(kind, case, tb):
245
     print (kind + '! ').ljust(60, '-')
246
     print case
748 by Martin Pool
- Fix typo
247
     desc = case.shortDescription()
744 by Martin Pool
- show nicer descriptions while running tests
248
     if desc:
249
         print '   (%s)' % desc
721 by Martin Pool
- framework for running external commands from unittest suite
250
     print tb
251
     print ''.ljust(60, '-')
252