~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Martin Pool
  • Date: 2005-06-22 07:30:45 UTC
  • Revision ID: mbp@sourcefrog.net-20050622073045-80d336b7e0b859aa
- redirect stdout/stderr while running tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
import logging
19
 
import unittest
20
 
import tempfile
21
 
import os
22
 
import sys
23
 
 
24
 
from testsweet import run_suite
25
 
import bzrlib.commands
26
 
 
27
 
import bzrlib.trace
28
 
import bzrlib.fetch
29
 
 
30
 
MODULES_TO_TEST = []
31
 
MODULES_TO_DOCTEST = []
32
 
 
33
 
from logging import debug, warning, error
34
 
 
35
 
 
36
 
class TestCase(unittest.TestCase):
37
 
    """Base class for bzr unit tests.
 
18
from unittest import TestResult, TestCase
 
19
 
 
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
 
 
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
    """
38
39
    
39
 
    Tests that need access to disk resources should subclass 
40
 
    FunctionalTestCase not TestCase.
41
 
 
42
 
    Error and debug log messages are redirected from their usual
43
 
    location into a temporary file, the contents of which can be
44
 
    retrieved by _get_log().
45
 
       
46
 
    There are also convenience functions to invoke bzr's command-line
47
 
    routine, and to build and check bzr trees."""
48
 
 
 
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'
49
44
    BZRPATH = 'bzr'
50
 
 
51
 
    def setUp(self):
52
 
        # this replaces the default testsweet.TestCase; we don't want logging changed
53
 
        unittest.TestCase.setUp(self)
54
 
        bzrlib.trace.disable_default_logging()
55
 
        self._enable_file_logging()
56
 
 
57
 
 
58
 
    def _enable_file_logging(self):
59
 
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
60
 
 
61
 
        self._log_file = os.fdopen(fileno, 'w+')
62
 
 
63
 
        hdlr = logging.StreamHandler(self._log_file)
64
 
        hdlr.setLevel(logging.DEBUG)
65
 
        hdlr.setFormatter(logging.Formatter('%(levelname)4.4s  %(message)s'))
66
 
        logging.getLogger('').addHandler(hdlr)
67
 
        logging.getLogger('').setLevel(logging.DEBUG)
68
 
        self._log_hdlr = hdlr
69
 
        debug('opened log file %s', name)
70
 
        
71
 
        self._log_file_name = name
72
 
 
73
 
        
74
 
    def tearDown(self):
75
 
        logging.getLogger('').removeHandler(self._log_hdlr)
76
 
        bzrlib.trace.enable_default_logging()
77
 
        logging.debug('%s teardown', self.id())
78
 
        self._log_file.close()
79
 
        unittest.TestCase.tearDown(self)
80
 
 
81
 
 
82
 
    def log(self, *args):
83
 
        logging.debug(*args)
84
 
 
85
 
    def _get_log(self):
86
 
        """Return as a string the log for this test"""
87
 
        return open(self._log_file_name).read()
88
 
 
89
 
    def run_bzr(self, *args, **kwargs):
90
 
        """Invoke bzr, as if it were run from the command line.
91
 
 
92
 
        This should be the main method for tests that want to exercise the
93
 
        overall behavior of the bzr application (rather than a unit test
94
 
        or a functional test of the library.)
95
 
 
96
 
        Much of the old code runs bzr by forking a new copy of Python, but
97
 
        that is slower, harder to debug, and generally not necessary.
98
 
        """
99
 
        retcode = kwargs.get('retcode', 0)
100
 
        result = self.apply_redirected(None, None, None,
101
 
                                       bzrlib.commands.run_bzr, args)
102
 
        self.assertEquals(result, retcode)
103
 
        
104
 
    def check_inventory_shape(self, inv, shape):
105
 
        """
106
 
        Compare an inventory to a list of expected names.
107
 
 
108
 
        Fail if they are not precisely equal.
109
 
        """
110
 
        extras = []
111
 
        shape = list(shape)             # copy
112
 
        for path, ie in inv.entries():
113
 
            name = path.replace('\\', '/')
114
 
            if ie.kind == 'dir':
115
 
                name = name + '/'
116
 
            if name in shape:
117
 
                shape.remove(name)
118
 
            else:
119
 
                extras.append(name)
120
 
        if shape:
121
 
            self.fail("expected paths not found in inventory: %r" % shape)
122
 
        if extras:
123
 
            self.fail("unexpected paths found in inventory: %r" % extras)
124
 
 
125
 
BzrTestBase = TestCase
126
 
 
127
 
     
128
 
class FunctionalTestCase(TestCase):
129
 
    """Base class for tests that perform function testing - running bzr,
130
 
    using files on disk, and similar activities.
131
 
 
132
 
    InTempDir is an old alias for FunctionalTestCase.
133
 
    """
134
 
 
135
 
    TEST_ROOT = None
136
 
    _TEST_NAME = 'test'
137
 
    OVERRIDE_PYTHON = 'python'
138
 
 
139
 
    def check_file_contents(self, filename, expect):
140
 
        self.log("check contents of file %s" % filename)
141
 
        contents = file(filename, 'r').read()
142
 
        if contents != expect:
143
 
            self.log("expected: %r" % expect)
144
 
            self.log("actually: %r" % contents)
145
 
            self.fail("contents of %s not as expected")
146
 
 
147
 
    def _make_test_root(self):
148
 
        import os
149
 
        import shutil
150
 
        import tempfile
151
 
        
152
 
        if FunctionalTestCase.TEST_ROOT is not None:
153
 
            return
154
 
        FunctionalTestCase.TEST_ROOT = os.path.abspath(
155
 
                                 tempfile.mkdtemp(suffix='.tmp',
156
 
                                                  prefix=self._TEST_NAME + '-',
157
 
                                                  dir=os.curdir))
158
45
    
159
 
        # make a fake bzr directory there to prevent any tests propagating
160
 
        # up onto the source directory's real branch
161
 
        os.mkdir(os.path.join(FunctionalTestCase.TEST_ROOT, '.bzr'))
162
 
 
163
 
    def setUp(self):
164
 
        super(FunctionalTestCase, self).setUp()
165
 
        import os
166
 
        self._make_test_root()
167
 
        self._currentdir = os.getcwdu()
168
 
        self.test_dir = os.path.join(self.TEST_ROOT, self.id())
169
 
        os.mkdir(self.test_dir)
170
 
        os.chdir(self.test_dir)
171
 
        
172
 
    def tearDown(self):
173
 
        import os
174
 
        os.chdir(self._currentdir)
175
 
        super(FunctionalTestCase, self).tearDown()
176
 
 
177
 
    def _formcmd(self, cmd):
 
46
 
 
47
    def formcmd(self, cmd):
178
48
        if isinstance(cmd, basestring):
179
49
            cmd = cmd.split()
 
50
 
180
51
        if cmd[0] == 'bzr':
181
52
            cmd[0] = self.BZRPATH
182
53
            if self.OVERRIDE_PYTHON:
183
54
                cmd.insert(0, self.OVERRIDE_PYTHON)
 
55
 
184
56
        self.log('$ %r' % cmd)
 
57
 
185
58
        return cmd
186
59
 
 
60
 
187
61
    def runcmd(self, cmd, retcode=0):
188
62
        """Run one command and check the return code.
189
63
 
192
66
        If a single string is based, it is split into words.
193
67
        For commands that are not simple space-separated words, please
194
68
        pass a list instead."""
195
 
        try:
196
 
            import shutil
197
 
            from subprocess import call
198
 
        except ImportError, e:
199
 
            _need_subprocess()
200
 
            raise
201
 
        cmd = self._formcmd(cmd)
 
69
        cmd = self.formcmd(cmd)
 
70
 
202
71
        self.log('$ ' + ' '.join(cmd))
203
 
        actual_retcode = call(cmd, stdout=self._log_file, stderr=self._log_file)
 
72
        actual_retcode = call(cmd, stdout=self.TEST_LOG, stderr=self.TEST_LOG)
 
73
 
204
74
        if retcode != actual_retcode:
205
75
            raise CommandFailed("test failed: %r returned %d, expected %d"
206
76
                                % (cmd, actual_retcode, retcode))
207
77
 
 
78
 
208
79
    def backtick(self, cmd, retcode=0):
209
 
        """Run a command and return its output"""
210
 
        try:
211
 
            import shutil
212
 
            from subprocess import Popen, PIPE
213
 
        except ImportError, e:
214
 
            _need_subprocess()
215
 
            raise
216
 
 
217
 
        cmd = self._formcmd(cmd)
218
 
        child = Popen(cmd, stdout=PIPE, stderr=self._log_file)
 
80
        cmd = self.formcmd(cmd)
 
81
        child = Popen(cmd, stdout=PIPE, stderr=self.TEST_LOG)
219
82
        outd, errd = child.communicate()
220
83
        self.log(outd)
221
84
        actual_retcode = child.wait()
230
93
 
231
94
 
232
95
 
233
 
    def build_tree(self, shape):
234
 
        """Build a test tree according to a pattern.
235
 
 
236
 
        shape is a sequence of file specifications.  If the final
237
 
        character is '/', a directory is created.
238
 
 
239
 
        This doesn't add anything to a branch.
240
 
        """
241
 
        # XXX: It's OK to just create them using forward slashes on windows?
242
 
        import os
243
 
        for name in shape:
244
 
            assert isinstance(name, basestring)
245
 
            if name[-1] == '/':
246
 
                os.mkdir(name[:-1])
247
 
            else:
248
 
                f = file(name, 'wt')
249
 
                print >>f, "contents of", name
250
 
                f.close()
251
 
                
252
 
 
253
 
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
254
 
                         a_callable=None, *args, **kwargs):
255
 
        """Call callable with redirected std io pipes.
256
 
 
257
 
        Returns the return code."""
258
 
        from StringIO import StringIO
259
 
        if not callable(a_callable):
260
 
            raise ValueError("a_callable must be callable.")
261
 
        if stdin is None:
262
 
            stdin = StringIO("")
263
 
        if stdout is None:
264
 
            stdout = self._log_file
265
 
        if stderr is None:
266
 
            stderr = self._log_file
267
 
        real_stdin = sys.stdin
268
 
        real_stdout = sys.stdout
269
 
        real_stderr = sys.stderr
270
 
        result = None
271
 
        try:
272
 
            sys.stdout = stdout
273
 
            sys.stderr = stderr
274
 
            sys.stdin = stdin
275
 
            result = a_callable(*args, **kwargs)
276
 
        finally:
277
 
            sys.stdout = real_stdout
278
 
            sys.stderr = real_stderr
279
 
            sys.stdin = real_stdin
280
 
        return result
281
 
 
282
 
 
283
 
InTempDir = FunctionalTestCase
284
 
 
285
 
 
286
 
class MetaTestLog(TestCase):
287
 
    def test_logging(self):
288
 
        """Test logs are captured when a test fails."""
289
 
        logging.info('an info message')
290
 
        warning('something looks dodgy...')
291
 
        logging.debug('hello, test is running')
292
 
        ##assert 0
293
 
 
294
 
 
295
 
def selftest(verbose=False, pattern=".*"):
296
 
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
297
 
 
298
 
 
299
 
def test_suite():
300
 
    from bzrlib.selftest.TestUtil import TestLoader, TestSuite
301
 
    import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
302
 
    import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
 
96
 
 
97
    def log(self, msg):
 
98
        """Log a message to a progress file"""
 
99
        print >>self.TEST_LOG, msg
 
100
               
 
101
 
 
102
class InTempDir(TestBase):
 
103
    """Base class for tests run in a temporary branch."""
 
104
    def setUp(self):
 
105
        import os
 
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)
 
109
        
 
110
    def tearDown(self):
 
111
        import os
 
112
        os.chdir(self.TEST_ROOT)
 
113
 
 
114
 
 
115
 
 
116
 
 
117
 
 
118
class _MyResult(TestResult):
 
119
    """
 
120
    Custom TestResult.
 
121
 
 
122
    No special behaviour for now.
 
123
    """
 
124
    def __init__(self, out):
 
125
        self.out = out
 
126
        TestResult.__init__(self)
 
127
 
 
128
    def startTest(self, test):
 
129
        # TODO: Maybe show test.shortDescription somewhere?
 
130
        print >>self.out, '%-60.60s' % test.id(),
 
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):
 
139
        print >>self.out, 'ERROR'
 
140
        TestResult.addError(self, test, err)
 
141
 
 
142
    def addFailure(self, test, err):
 
143
        print >>self.out, 'FAILURE'
 
144
        TestResult.addFailure(self, test, err)
 
145
 
 
146
    def addSuccess(self, test):
 
147
        print >>self.out, 'OK'
 
148
        TestResult.addSuccess(self, test)
 
149
 
 
150
 
 
151
 
 
152
def selftest():
 
153
    from unittest import TestLoader, TestSuite
 
154
    import bzrlib
 
155
    import bzrlib.selftest.whitebox
 
156
    import bzrlib.selftest.blackbox
 
157
    import bzrlib.selftest.versioning
303
158
    from doctest import DocTestSuite
304
159
    import os
305
160
    import shutil
306
161
    import time
307
162
    import sys
308
163
 
309
 
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
310
 
 
311
 
    testmod_names = \
312
 
                  ['bzrlib.selftest.MetaTestLog',
313
 
                   'bzrlib.selftest.testinv',
314
 
                   'bzrlib.selftest.testfetch',
315
 
                   'bzrlib.selftest.versioning',
316
 
                   'bzrlib.selftest.whitebox',
317
 
                   'bzrlib.selftest.testmerge3',
318
 
                   'bzrlib.selftest.testhashcache',
319
 
                   'bzrlib.selftest.teststatus',
320
 
                   'bzrlib.selftest.testlog',
321
 
                   'bzrlib.selftest.blackbox',
322
 
                   'bzrlib.selftest.testrevisionnamespaces',
323
 
                   'bzrlib.selftest.testbranch',
324
 
                   'bzrlib.selftest.testrevision',
325
 
                   'bzrlib.selftest.test_merge_core',
326
 
                   'bzrlib.selftest.test_smart_add',
327
 
                   'bzrlib.selftest.testdiff',
328
 
                   'bzrlib.fetch'
329
 
                   ]
330
 
 
331
 
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
332
 
              bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
333
 
        if m not in MODULES_TO_DOCTEST:
334
 
            MODULES_TO_DOCTEST.append(m)
335
 
 
336
 
    TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
337
 
    print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
 
164
    _setup_test_log()
 
165
    _setup_test_dir()
338
166
    print
 
167
 
339
168
    suite = TestSuite()
340
 
    suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
341
 
    for m in MODULES_TO_TEST:
342
 
         suite.addTest(TestLoader().loadTestsFromModule(m))
343
 
    for m in (MODULES_TO_DOCTEST):
 
169
    tl = TestLoader()
 
170
 
 
171
    for m in bzrlib.selftest.whitebox, \
 
172
            bzrlib.selftest.versioning:
 
173
        suite.addTest(tl.loadTestsFromModule(m))
 
174
 
 
175
    suite.addTest(bzrlib.selftest.blackbox.suite())
 
176
 
 
177
    for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
 
178
            bzrlib.commands:
344
179
        suite.addTest(DocTestSuite(m))
345
 
    for p in bzrlib.plugin.all_plugins:
346
 
        if hasattr(p, 'test_suite'):
347
 
            suite.addTest(p.test_suite())
348
 
    return suite
349
 
 
 
180
 
 
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
 
191
 
 
192
    _show_results(result)
 
193
 
 
194
    return result.wasSuccessful()
 
195
 
 
196
 
 
197
 
 
198
 
 
199
def _setup_test_log():
 
200
    import time
 
201
    import os
 
202
    
 
203
    log_filename = os.path.abspath('testbzr.log')
 
204
    TestBase.TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
 
205
 
 
206
    print >>TestBase.TEST_LOG, "bzr tests run at " + time.ctime()
 
207
    print '%-30s %s' % ('test log', log_filename)
 
208
 
 
209
 
 
210
def _setup_test_dir():
 
211
    import os
 
212
    import shutil
 
213
    
 
214
    TestBase.ORIG_DIR = os.getcwdu()
 
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)
 
223
 
 
224
    # make a fake bzr directory there to prevent any tests propagating
 
225
    # up onto the source directory's real branch
 
226
    os.mkdir(os.path.join(TestBase.TEST_ROOT, '.bzr'))
 
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
 
238
     print '%4d tests run' % result.testsRun
 
239
     print '%4d errors' % len(result.errors)
 
240
     print '%4d failures' % len(result.failures)
 
241
 
 
242
 
 
243
 
 
244
def _show_test_failure(kind, case, tb):
 
245
     print (kind + '! ').ljust(60, '-')
 
246
     print case
 
247
     desc = test.shortDescription()
 
248
     if desc:
 
249
         print '   (%s)' % desc
 
250
     print tb
 
251
     print ''.ljust(60, '-')
 
252