~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

[merge] from robert and fix up tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
import bzrlib.osutils as osutils
34
34
from bzrlib.selftest import TestUtil
35
35
from bzrlib.selftest.TestUtil import TestLoader, TestSuite
36
 
 
 
36
from bzrlib.selftest.treeshape import build_tree_contents
37
37
 
38
38
MODULES_TO_TEST = []
39
39
MODULES_TO_DOCTEST = []
121
121
 
122
122
 
123
123
class TextTestRunner(unittest.TextTestRunner):
 
124
    stop_on_failure = False
124
125
 
125
126
    def _makeResult(self):
126
127
        result = _MyResult(self.stream, self.descriptions, self.verbosity)
127
 
        return EarlyStoppingTestResultAdapter(result)
 
128
        if self.stop_on_failure:
 
129
            result = EarlyStoppingTestResultAdapter(result)
 
130
        return result
128
131
 
129
132
 
130
133
def iter_suite_tests(suite):
166
169
 
167
170
    def setUp(self):
168
171
        unittest.TestCase.setUp(self)
 
172
        self.oldenv = os.environ.get('HOME', None)
 
173
        os.environ['HOME'] = os.getcwd()
 
174
        self.bzr_email = os.environ.get('BZREMAIL')
 
175
        if self.bzr_email is not None:
 
176
            del os.environ['BZREMAIL']
 
177
        self.email = os.environ.get('EMAIL')
 
178
        if self.email is not None:
 
179
            del os.environ['EMAIL']
169
180
        bzrlib.trace.disable_default_logging()
170
181
        self._enable_file_logging()
171
182
 
172
183
    def _ndiff_strings(self, a, b):
173
 
        """Return ndiff between two strings containing lines."""
 
184
        """Return ndiff between two strings containing lines.
 
185
        
 
186
        A trailing newline is added if missing to make the strings
 
187
        print properly."""
 
188
        if b and b[-1] != '\n':
 
189
            b += '\n'
 
190
        if a and a[-1] != '\n':
 
191
            a += '\n'
174
192
        difflines = difflib.ndiff(a.splitlines(True),
175
193
                                  b.splitlines(True),
176
194
                                  linejunk=lambda x: False,
188
206
            return
189
207
        raise AssertionError("texts not equal:\n" + 
190
208
                             self._ndiff_strings(a, b))      
 
209
 
 
210
    def assertContainsRe(self, haystack, needle_re):
 
211
        """Assert that a contains something matching a regular expression."""
 
212
        if not re.search(needle_re, haystack):
 
213
            raise AssertionError('pattern "%s" not found in "%s"'
 
214
                    % (needle_re, haystack))
191
215
        
192
216
    def _enable_file_logging(self):
193
217
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
205
229
        self._log_file_name = name
206
230
 
207
231
    def tearDown(self):
 
232
        os.environ['HOME'] = self.oldenv
 
233
        if os.environ.get('BZREMAIL') is not None:
 
234
            del os.environ['BZREMAIL']
 
235
        if self.bzr_email is not None:
 
236
            os.environ['BZREMAIL'] = self.bzr_email
 
237
        if os.environ.get('EMAIL') is not None:
 
238
            del os.environ['EMAIL']
 
239
        if self.email is not None:
 
240
            os.environ['EMAIL'] = self.email
208
241
        logging.getLogger('').removeHandler(self._log_hdlr)
209
242
        bzrlib.trace.enable_default_logging()
210
243
        logging.debug('%s teardown', self.id())
385
418
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
386
419
 
387
420
    def setUp(self):
388
 
        super(TestCaseInTempDir, self).setUp()
389
421
        self._make_test_root()
390
422
        self._currentdir = os.getcwdu()
391
423
        short_id = self.id().replace('bzrlib.selftest.', '') \
393
425
        self.test_dir = os.path.join(self.TEST_ROOT, short_id)
394
426
        os.mkdir(self.test_dir)
395
427
        os.chdir(self.test_dir)
 
428
        super(TestCaseInTempDir, self).setUp()
396
429
        
397
430
    def tearDown(self):
398
431
        os.chdir(self._currentdir)
416
449
                print >>f, "contents of", name
417
450
                f.close()
418
451
 
 
452
    def build_tree_contents(self, shape):
 
453
        bzrlib.selftest.build_tree_contents(shape)
 
454
 
419
455
    def failUnlessExists(self, path):
420
456
        """Fail unless path, which may be abs or relative, exists."""
421
457
        self.failUnless(osutils.lexists(path))
427
463
        logging.info('an info message')
428
464
        warning('something looks dodgy...')
429
465
        logging.debug('hello, test is running')
430
 
        ##assert 0
 
466
        ## assert 0
431
467
 
432
468
 
433
469
def filter_suite_by_re(suite, pattern):
439
475
    return result
440
476
 
441
477
 
442
 
def run_suite(suite, name='test', verbose=False, pattern=".*"):
 
478
def run_suite(suite, name='test', verbose=False, pattern=".*",
 
479
              stop_on_failure=False):
443
480
    TestCaseInTempDir._TEST_NAME = name
444
481
    if verbose:
445
482
        verbosity = 2
448
485
    runner = TextTestRunner(stream=sys.stdout,
449
486
                            descriptions=0,
450
487
                            verbosity=verbosity)
 
488
    runner.stop_on_failure=stop_on_failure
451
489
    if pattern != '.*':
452
490
        suite = filter_suite_by_re(suite, pattern)
453
491
    result = runner.run(suite)
462
500
    return result.wasSuccessful()
463
501
 
464
502
 
465
 
def selftest(verbose=False, pattern=".*"):
 
503
def selftest(verbose=False, pattern=".*", stop_on_failure=True):
466
504
    """Run the whole test suite under the enhanced runner"""
467
 
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
 
505
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
 
506
                     stop_on_failure=stop_on_failure)
468
507
 
469
508
 
470
509
def test_suite():
477
516
 
478
517
    testmod_names = \
479
518
                  ['bzrlib.selftest.MetaTestLog',
 
519
                   'bzrlib.selftest.testgpg',
480
520
                   'bzrlib.selftest.testidentitymap',
481
521
                   'bzrlib.selftest.testinv',
482
522
                   'bzrlib.selftest.test_ancestry',
514
554
                   'bzrlib.selftest.testtestament',
515
555
                   'bzrlib.selftest.testannotate',
516
556
                   'bzrlib.selftest.testrevprops',
 
557
                   'bzrlib.selftest.testoptions',
 
558
                   'bzrlib.selftest.testhttp',
 
559
                   'bzrlib.selftest.testnonascii',
517
560
                   ]
518
561
 
519
562
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
520
 
              bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
 
563
              bzrlib.osutils, bzrlib.commands, bzrlib.merge3,
 
564
              bzrlib.errors,
 
565
              ):
521
566
        if m not in MODULES_TO_DOCTEST:
522
567
            MODULES_TO_DOCTEST.append(m)
523
568