~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Martin Pool
  • Date: 2005-07-04 11:57:18 UTC
  • Revision ID: mbp@sourcefrog.net-20050704115718-b532986c0714e7a7
- don't write precursor field in new revision xml
- make parents more primary; remove more precursor code
- test commit of revision with parents

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