~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to testsweet.py

  • Committer: Martin Pool
  • Date: 2005-08-26 00:10:48 UTC
  • Revision ID: mbp@sourcefrog.net-20050826001048-b84148d3ef567d0d
- fix bzr.dev branch url in tutorial
  thanks to madduck

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
* utilities to run external commands and check their return code
31
31
  and/or output
32
32
 
33
 
Test cases should normally subclass TestBase.  The test runner should
 
33
Test cases should normally subclass testsweet.TestCase.  The test runner should
34
34
call runsuite().
35
35
 
36
36
This is meant to become independent of bzr, though that's not quite
37
37
true yet.
38
38
"""  
39
39
 
40
 
 
41
 
from unittest import TestResult, TestCase
42
 
 
 
40
import unittest
 
41
import sys
 
42
 
 
43
# XXX: Don't need this anymore now we depend on python2.4
43
44
def _need_subprocess():
44
45
    sys.stderr.write("sorry, this test suite requires the subprocess module\n"
45
46
                     "this is shipped with python2.4 and available separately for 2.3\n")
49
50
    pass
50
51
 
51
52
 
52
 
 
53
53
class TestSkipped(Exception):
54
54
    """Indicates that a test was intentionally skipped, rather than failing."""
55
55
    # XXX: Not used yet
56
56
 
57
57
 
58
 
class TestBase(TestCase):
59
 
    """Base class for bzr test cases.
60
 
 
61
 
    Just defines some useful helper functions; doesn't actually test
62
 
    anything.
 
58
class TestCase(unittest.TestCase):
 
59
    """Base class for bzr unit tests.
 
60
    
 
61
    Tests that need access to disk resources should subclass 
 
62
    FunctionalTestCase not TestCase.
63
63
    """
64
64
    
65
65
    # TODO: Special methods to invoke bzr, so that we can run it
68
68
    OVERRIDE_PYTHON = None # to run with alternative python 'python'
69
69
    BZRPATH = 'bzr'
70
70
 
71
 
    _log_buf = ""
72
 
 
73
 
 
74
71
    def setUp(self):
75
 
        super(TestBase, self).setUp()
 
72
        super(TestCase, self).setUp()
 
73
        # setup a temporary log for the test 
 
74
        import time
 
75
        import os
 
76
        import tempfile
 
77
        self.TEST_LOG = tempfile.NamedTemporaryFile(mode='wt', bufsize=0)
 
78
        # save stdout & stderr so there's no leakage from code-under-test
 
79
        self.real_stdout = sys.stdout
 
80
        self.real_stderr = sys.stderr
 
81
        sys.stdout = sys.stderr = self.TEST_LOG
76
82
        self.log("%s setup" % self.id())
77
83
 
78
 
 
79
84
    def tearDown(self):
80
 
        super(TestBase, self).tearDown()
 
85
        sys.stdout = self.real_stdout
 
86
        sys.stderr = self.real_stderr
81
87
        self.log("%s teardown" % self.id())
82
88
        self.log('')
83
 
 
 
89
        super(TestCase, self).tearDown()
 
90
 
 
91
    def log(self, msg):
 
92
        """Log a message to a progress file"""
 
93
        print >>self.TEST_LOG, msg
 
94
 
 
95
    def check_inventory_shape(self, inv, shape):
 
96
        """
 
97
        Compare an inventory to a list of expected names.
 
98
 
 
99
        Fail if they are not precisely equal.
 
100
        """
 
101
        extras = []
 
102
        shape = list(shape)             # copy
 
103
        for path, ie in inv.entries():
 
104
            name = path.replace('\\', '/')
 
105
            if ie.kind == 'dir':
 
106
                name = name + '/'
 
107
            if name in shape:
 
108
                shape.remove(name)
 
109
            else:
 
110
                extras.append(name)
 
111
        if shape:
 
112
            self.fail("expected paths not found in inventory: %r" % shape)
 
113
        if extras:
 
114
            self.fail("unexpected paths found in inventory: %r" % extras)
 
115
     
 
116
    def _get_log(self):
 
117
        """Get the log the test case used. This can only be called once,
 
118
        after which an exception will be raised.
 
119
        """
 
120
        self.TEST_LOG.flush()
 
121
        log = open(self.TEST_LOG.name, 'rt').read()
 
122
        self.TEST_LOG.close()
 
123
        return log
 
124
 
 
125
 
 
126
class FunctionalTestCase(TestCase):
 
127
    """Base class for tests that perform function testing - running bzr,
 
128
    using files on disk, and similar activities.
 
129
 
 
130
    InTempDir is an old alias for FunctionalTestCase.
 
131
    """
 
132
 
 
133
    TEST_ROOT = None
 
134
    _TEST_NAME = 'test'
 
135
 
 
136
    def check_file_contents(self, filename, expect):
 
137
        self.log("check contents of file %s" % filename)
 
138
        contents = file(filename, 'r').read()
 
139
        if contents != expect:
 
140
            self.log("expected: %r" % expect)
 
141
            self.log("actually: %r" % contents)
 
142
            self.fail("contents of %s not as expected")
 
143
 
 
144
    def _make_test_root(self):
 
145
        import os
 
146
        import shutil
 
147
        import tempfile
 
148
        
 
149
        if FunctionalTestCase.TEST_ROOT is not None:
 
150
            return
 
151
        FunctionalTestCase.TEST_ROOT = os.path.abspath(
 
152
                                 tempfile.mkdtemp(suffix='.tmp',
 
153
                                                  prefix=self._TEST_NAME + '-',
 
154
                                                  dir=os.curdir))
 
155
    
 
156
        # make a fake bzr directory there to prevent any tests propagating
 
157
        # up onto the source directory's real branch
 
158
        os.mkdir(os.path.join(FunctionalTestCase.TEST_ROOT, '.bzr'))
 
159
 
 
160
    def setUp(self):
 
161
        super(FunctionalTestCase, self).setUp()
 
162
        import os
 
163
        self._make_test_root()
 
164
        self._currentdir = os.getcwdu()
 
165
        self.test_dir = os.path.join(self.TEST_ROOT, self.id())
 
166
        os.mkdir(self.test_dir)
 
167
        os.chdir(self.test_dir)
 
168
        
 
169
    def tearDown(self):
 
170
        import os
 
171
        os.chdir(self._currentdir)
 
172
        super(FunctionalTestCase, self).tearDown()
84
173
 
85
174
    def formcmd(self, cmd):
86
175
        if isinstance(cmd, basestring):
87
176
            cmd = cmd.split()
88
 
 
89
177
        if cmd[0] == 'bzr':
90
178
            cmd[0] = self.BZRPATH
91
179
            if self.OVERRIDE_PYTHON:
92
180
                cmd.insert(0, self.OVERRIDE_PYTHON)
93
 
 
94
181
        self.log('$ %r' % cmd)
95
 
 
96
182
        return cmd
97
183
 
98
 
 
99
184
    def runcmd(self, cmd, retcode=0):
100
185
        """Run one command and check the return code.
101
186
 
110
195
        except ImportError, e:
111
196
            _need_subprocess()
112
197
            raise
113
 
 
114
 
 
115
198
        cmd = self.formcmd(cmd)
116
 
 
117
199
        self.log('$ ' + ' '.join(cmd))
118
200
        actual_retcode = call(cmd, stdout=self.TEST_LOG, stderr=self.TEST_LOG)
119
 
 
120
201
        if retcode != actual_retcode:
121
202
            raise CommandFailed("test failed: %r returned %d, expected %d"
122
203
                                % (cmd, actual_retcode, retcode))
123
204
 
124
 
 
125
205
    def backtick(self, cmd, retcode=0):
126
206
        """Run a command and return its output"""
127
207
        try:
165
245
                f = file(name, 'wt')
166
246
                print >>f, "contents of", name
167
247
                f.close()
168
 
 
169
 
 
170
 
    def log(self, msg):
171
 
        """Log a message to a progress file"""
172
 
        self._log_buf = self._log_buf + str(msg) + '\n'
173
 
        print >>self.TEST_LOG, msg
174
 
 
175
 
 
176
 
    def check_inventory_shape(self, inv, shape):
177
 
        """
178
 
        Compare an inventory to a list of expected names.
179
 
 
180
 
        Fail if they are not precisely equal.
181
 
        """
182
 
        extras = []
183
 
        shape = list(shape)             # copy
184
 
        for path, ie in inv.entries():
185
 
            name = path.replace('\\', '/')
186
 
            if ie.kind == 'dir':
187
 
                name = name + '/'
188
 
            if name in shape:
189
 
                shape.remove(name)
190
 
            else:
191
 
                extras.append(name)
192
 
        if shape:
193
 
            self.fail("expected paths not found in inventory: %r" % shape)
194
 
        if extras:
195
 
            self.fail("unexpected paths found in inventory: %r" % extras)
196
 
 
197
 
 
198
 
    def check_file_contents(self, filename, expect):
199
 
        self.log("check contents of file %s" % filename)
200
 
        contents = file(filename, 'r').read()
201
 
        if contents != expect:
202
 
            self.log("expected: %r" % expect)
203
 
            self.log("actually: %r" % contents)
204
 
            self.fail("contents of %s not as expected")
205
 
            
206
 
 
207
 
 
208
 
class InTempDir(TestBase):
209
 
    """Base class for tests run in a temporary branch."""
210
 
    def setUp(self):
211
 
        import os
212
 
        self.test_dir = os.path.join(self.TEST_ROOT, self.__class__.__name__)
213
 
        os.mkdir(self.test_dir)
214
 
        os.chdir(self.test_dir)
215
 
        
216
 
    def tearDown(self):
217
 
        import os
218
 
        os.chdir(self.TEST_ROOT)
219
 
 
220
 
 
221
 
 
222
 
 
223
 
 
224
 
class _MyResult(TestResult):
 
248
                
 
249
InTempDir = FunctionalTestCase
 
250
 
 
251
 
 
252
class _MyResult(unittest._TextTestResult):
225
253
    """
226
254
    Custom TestResult.
227
255
 
228
256
    No special behaviour for now.
229
257
    """
230
 
    def __init__(self, out):
231
 
        self.out = out
232
 
        TestResult.__init__(self)
233
258
 
234
259
    def startTest(self, test):
 
260
        unittest.TestResult.startTest(self, test)
235
261
        # TODO: Maybe show test.shortDescription somewhere?
236
262
        what = test.id()
237
263
        # python2.3 has the bad habit of just "runit" for doctests
238
264
        if what == 'runit':
239
265
            what = test.shortDescription()
240
 
        
241
 
        print >>self.out, '%-60.60s' % what,
242
 
        self.out.flush()
243
 
        TestResult.startTest(self, test)
244
 
 
245
 
    def stopTest(self, test):
246
 
        # print
247
 
        TestResult.stopTest(self, test)
248
 
 
 
266
        if self.showAll:
 
267
            self.stream.write('%-60.60s' % what)
 
268
        self.stream.flush()
249
269
 
250
270
    def addError(self, test, err):
251
 
        print >>self.out, 'ERROR'
252
 
        TestResult.addError(self, test, err)
253
 
        _show_test_failure('error', test, err, self.out)
 
271
        super(_MyResult, self).addError(test, err)
 
272
        self.stream.flush()
254
273
 
255
274
    def addFailure(self, test, err):
256
 
        print >>self.out, 'FAILURE'
257
 
        TestResult.addFailure(self, test, err)
258
 
        _show_test_failure('failure', test, err, self.out)
 
275
        super(_MyResult, self).addFailure(test, err)
 
276
        self.stream.flush()
259
277
 
260
278
    def addSuccess(self, test):
261
 
        print >>self.out, 'OK'
262
 
        TestResult.addSuccess(self, test)
263
 
 
264
 
 
265
 
 
266
 
def run_suite(suite, name="test"):
267
 
    import os
 
279
        if self.showAll:
 
280
            self.stream.writeln('OK')
 
281
        elif self.dots:
 
282
            self.stream.write('~')
 
283
        self.stream.flush()
 
284
        unittest.TestResult.addSuccess(self, test)
 
285
 
 
286
    def printErrorList(self, flavour, errors):
 
287
        for test, err in errors:
 
288
            self.stream.writeln(self.separator1)
 
289
            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
 
290
            self.stream.writeln(self.separator2)
 
291
            self.stream.writeln("%s" % err)
 
292
            if isinstance(test, TestCase):
 
293
                self.stream.writeln()
 
294
                self.stream.writeln('log from this test:')
 
295
                print >>self.stream, test._get_log()
 
296
 
 
297
 
 
298
class TextTestRunner(unittest.TextTestRunner):
 
299
 
 
300
    def _makeResult(self):
 
301
        return _MyResult(self.stream, self.descriptions, self.verbosity)
 
302
 
 
303
 
 
304
def run_suite(suite, name='test', verbose=False):
268
305
    import shutil
269
 
    import time
270
 
    import sys
271
 
    
272
 
    _setup_test_log(name)
273
 
    _setup_test_dir(name)
274
 
    print
275
 
 
276
 
    # save stdout & stderr so there's no leakage from code-under-test
277
 
    real_stdout = sys.stdout
278
 
    real_stderr = sys.stderr
279
 
    sys.stdout = sys.stderr = TestBase.TEST_LOG
280
 
    try:
281
 
        result = _MyResult(real_stdout)
282
 
        suite.run(result)
283
 
    finally:
284
 
        sys.stdout = real_stdout
285
 
        sys.stderr = real_stderr
286
 
 
287
 
    _show_results(result)
288
 
 
 
306
    FunctionalTestCase._TEST_NAME = name
 
307
    if verbose:
 
308
        verbosity = 2
 
309
    else:
 
310
        verbosity = 1
 
311
    runner = TextTestRunner(stream=sys.stdout,
 
312
                            descriptions=0,
 
313
                            verbosity=verbosity)
 
314
    result = runner.run(suite)
 
315
    # This is still a little bogus, 
 
316
    # but only a little. Folk not using our testrunner will
 
317
    # have to delete their temp directories themselves.
 
318
    if result.wasSuccessful():
 
319
        shutil.rmtree(FunctionalTestCase.TEST_ROOT) 
 
320
    else:
 
321
        print "Failed tests working directories are in '%s'\n" % FunctionalTestCase.TEST_ROOT
289
322
    return result.wasSuccessful()
290
 
 
291
 
 
292
 
 
293
 
def _setup_test_log(name):
294
 
    import time
295
 
    import os
296
 
    
297
 
    log_filename = os.path.abspath(name + '.log')
298
 
    TestBase.TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
299
 
 
300
 
    print >>TestBase.TEST_LOG, "tests run at " + time.ctime()
301
 
    print '%-30s %s' % ('test log', log_filename)
302
 
 
303
 
 
304
 
def _setup_test_dir(name):
305
 
    import os
306
 
    import shutil
307
 
    
308
 
    TestBase.ORIG_DIR = os.getcwdu()
309
 
    TestBase.TEST_ROOT = os.path.abspath(name + '.tmp')
310
 
 
311
 
    print '%-30s %s' % ('running tests in', TestBase.TEST_ROOT)
312
 
 
313
 
    if os.path.exists(TestBase.TEST_ROOT):
314
 
        shutil.rmtree(TestBase.TEST_ROOT)
315
 
    os.mkdir(TestBase.TEST_ROOT)
316
 
    os.chdir(TestBase.TEST_ROOT)
317
 
 
318
 
    # make a fake bzr directory there to prevent any tests propagating
319
 
    # up onto the source directory's real branch
320
 
    os.mkdir(os.path.join(TestBase.TEST_ROOT, '.bzr'))
321
 
 
322
 
    
323
 
 
324
 
def _show_results(result):
325
 
     print
326
 
     print '%4d tests run' % result.testsRun
327
 
     print '%4d errors' % len(result.errors)
328
 
     print '%4d failures' % len(result.failures)
329
 
 
330
 
 
331
 
 
332
 
def _show_test_failure(kind, case, exc_info, out):
333
 
    from traceback import print_exception
334
 
    
335
 
    print >>out, '-' * 60
336
 
    print >>out, case
337
 
    
338
 
    desc = case.shortDescription()
339
 
    if desc:
340
 
        print >>out, '   (%s)' % desc
341
 
         
342
 
    print_exception(exc_info[0], exc_info[1], exc_info[2], None, out)
343
 
        
344
 
    if isinstance(case, TestBase):
345
 
        print >>out
346
 
        print >>out, 'log from this test:'
347
 
        print >>out, case._log_buf
348
 
         
349
 
    print >>out, '-' * 60
350
 
    
351