/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Robert Collins
  • Date: 2006-08-08 23:19:29 UTC
  • mfrom: (1884 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1912.
  • Revision ID: robertc@robertcollins.net-20060808231929-4e3e298190214b3a
current status

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import codecs
30
30
from cStringIO import StringIO
31
31
import difflib
 
32
import doctest
32
33
import errno
33
34
import logging
34
35
import os
69
70
from bzrlib.transport.local import LocalRelpathServer
70
71
from bzrlib.transport.readonly import ReadonlyServer
71
72
from bzrlib.trace import mutter
72
 
from bzrlib.tests.TestUtil import TestLoader, TestSuite
 
73
from bzrlib.tests import TestUtil
 
74
from bzrlib.tests.TestUtil import (
 
75
                          TestSuite,
 
76
                          TestLoader,
 
77
                          )
73
78
from bzrlib.tests.treeshape import build_tree_contents
74
79
import bzrlib.urlutils as urlutils
75
80
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
214
219
            self.stream.write('E')
215
220
        elif self.dots:
216
221
            self.pb.update(self._ellipsise_unimportant_words('ERROR', 13), self.testsRun, None)
 
222
            self.pb.note(self._ellipsise_unimportant_words(
 
223
                            test.id() + ': ERROR',
 
224
                            osutils.terminal_width()))
217
225
        self.stream.flush()
218
226
        if self.stop_early:
219
227
            self.stop()
227
235
            self.stream.write('F')
228
236
        elif self.dots:
229
237
            self.pb.update(self._ellipsise_unimportant_words('FAIL', 13), self.testsRun, None)
 
238
            self.pb.note(self._ellipsise_unimportant_words(
 
239
                            test.id() + ': FAIL',
 
240
                            osutils.terminal_width()))
230
241
        self.stream.flush()
231
242
        if self.stop_early:
232
243
            self.stop()
257
268
        self.stream.flush()
258
269
        # seems best to treat this as success from point-of-view of unittest
259
270
        # -- it actually does nothing so it barely matters :)
260
 
        unittest.TestResult.addSuccess(self, test)
 
271
        try:
 
272
            test.tearDown()
 
273
        except KeyboardInterrupt:
 
274
            raise
 
275
        except:
 
276
            self.addError(test, test.__exc_info())
 
277
        else:
 
278
            unittest.TestResult.addSuccess(self, test)
261
279
 
262
280
    def printErrorList(self, flavour, errors):
263
281
        for test, err in errors:
334
352
                # If LANG=C we probably have created some bogus paths
335
353
                # which rmtree(unicode) will fail to delete
336
354
                # so make sure we are using rmtree(str) to delete everything
337
 
                osutils.rmtree(test_root.encode(
338
 
                    sys.getfilesystemencoding()))
 
355
                # except on win32, where rmtree(str) will fail
 
356
                # since it doesn't have the property of byte-stream paths
 
357
                # (they are either ascii or mbcs)
 
358
                if sys.platform == 'win32':
 
359
                    # make sure we are using the unicode win32 api
 
360
                    test_root = unicode(test_root)
 
361
                else:
 
362
                    test_root = test_root.encode(
 
363
                        sys.getfilesystemencoding())
 
364
                osutils.rmtree(test_root)
339
365
        else:
340
366
            if self.pb is not None:
341
367
                self.pb.note("Failed tests working directories are in '%s'\n",
484
510
            raise AssertionError('pattern "%s" not found in "%s"'
485
511
                    % (needle_re, haystack))
486
512
 
 
513
    def assertNotContainsRe(self, haystack, needle_re):
 
514
        """Assert that a does not match a regular expression"""
 
515
        if re.search(needle_re, haystack):
 
516
            raise AssertionError('pattern "%s" found in "%s"'
 
517
                    % (needle_re, haystack))
 
518
 
487
519
    def assertSubset(self, sublist, superlist):
488
520
        """Assert that every entry in sublist is present in superlist."""
489
521
        missing = []
533
565
 
534
566
        Read contents into memory, close, and delete.
535
567
        """
 
568
        if self._log_file is None:
 
569
            return
536
570
        bzrlib.trace.disable_test_log(self._log_nonce)
537
571
        self._log_file.seek(0)
538
572
        self._log_contents = self._log_file.read()
670
704
        self.log('run bzr: %r', argv)
671
705
        # FIXME: don't call into logging here
672
706
        handler = logging.StreamHandler(stderr)
673
 
        handler.setFormatter(bzrlib.trace.QuietFormatter())
674
707
        handler.setLevel(logging.INFO)
675
708
        logger = logging.getLogger('')
676
709
        logger.addHandler(handler)
721
754
            encoding = bzrlib.user_encoding
722
755
        return self.run_bzr(*args, **kwargs)[0].decode(encoding)
723
756
 
724
 
    def run_bzr_external(self, *args, **kwargs):
 
757
    def run_bzr_error(self, error_regexes, *args, **kwargs):
 
758
        """Run bzr, and check that stderr contains the supplied regexes
 
759
        
 
760
        :param error_regexes: Sequence of regular expressions which 
 
761
            must each be found in the error output. The relative ordering
 
762
            is not enforced.
 
763
        :param args: command-line arguments for bzr
 
764
        :param kwargs: Keyword arguments which are interpreted by run_bzr
 
765
            This function changes the default value of retcode to be 3,
 
766
            since in most cases this is run when you expect bzr to fail.
 
767
        :return: (out, err) The actual output of running the command (in case you
 
768
                 want to do more inspection)
 
769
 
 
770
        Examples of use:
 
771
            # Make sure that commit is failing because there is nothing to do
 
772
            self.run_bzr_error(['no changes to commit'],
 
773
                               'commit', '-m', 'my commit comment')
 
774
            # Make sure --strict is handling an unknown file, rather than
 
775
            # giving us the 'nothing to do' error
 
776
            self.build_tree(['unknown'])
 
777
            self.run_bzr_error(['Commit refused because there are unknown files'],
 
778
                               'commit', '--strict', '-m', 'my commit comment')
 
779
        """
 
780
        kwargs.setdefault('retcode', 3)
 
781
        out, err = self.run_bzr(*args, **kwargs)
 
782
        for regex in error_regexes:
 
783
            self.assertContainsRe(err, regex)
 
784
        return out, err
 
785
 
 
786
    def run_bzr_subprocess(self, *args, **kwargs):
 
787
        """Run bzr in a subprocess for testing.
 
788
 
 
789
        This starts a new Python interpreter and runs bzr in there. 
 
790
        This should only be used for tests that have a justifiable need for
 
791
        this isolation: e.g. they are testing startup time, or signal
 
792
        handling, or early startup code, etc.  Subprocess code can't be 
 
793
        profiled or debugged so easily.
 
794
 
 
795
        :param retcode: The status code that is expected.  Defaults to 0.  If
 
796
        None is supplied, the status code is not checked.
 
797
        """
725
798
        bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
726
 
        if len(args) == 1:
727
 
            args = shlex.split(args[0])
728
799
        args = list(args)
729
 
        process = Popen([bzr_path]+args, stdout=PIPE, stderr=PIPE)
 
800
        process = Popen([sys.executable, bzr_path]+args, stdout=PIPE, 
 
801
                         stderr=PIPE)
730
802
        out = process.stdout.read()
731
803
        err = process.stderr.read()
732
804
        retcode = process.wait()
733
 
        supplied_retcode = kwargs.get('retcode')
 
805
        supplied_retcode = kwargs.get('retcode', 0)
734
806
        if supplied_retcode is not None:
735
807
            assert supplied_retcode == retcode
736
 
        else:
737
 
            assert retcode == 0
738
808
        return [out, err]
739
809
 
740
810
    def check_inventory_shape(self, inv, shape):
1117
1187
 
1118
1188
 
1119
1189
def filter_suite_by_re(suite, pattern):
1120
 
    result = TestSuite()
 
1190
    result = TestUtil.TestSuite()
1121
1191
    filter_re = re.compile(pattern)
1122
1192
    for test in iter_suite_tests(suite):
1123
1193
        if filter_re.search(test.id()):
1178
1248
    This function can be replaced if you need to change the default test
1179
1249
    suite on a global basis, but it is not encouraged.
1180
1250
    """
1181
 
    from doctest import DocTestSuite
1182
 
 
1183
 
    global MODULES_TO_DOCTEST
1184
 
 
1185
 
    testmod_names = [ \
 
1251
    testmod_names = [
1186
1252
                   'bzrlib.tests.test_ancestry',
1187
1253
                   'bzrlib.tests.test_api',
1188
1254
                   'bzrlib.tests.test_bad_files',
1194
1260
                   'bzrlib.tests.test_commit_merge',
1195
1261
                   'bzrlib.tests.test_config',
1196
1262
                   'bzrlib.tests.test_conflicts',
 
1263
                   'bzrlib.tests.test_delta',
1197
1264
                   'bzrlib.tests.test_decorators',
1198
1265
                   'bzrlib.tests.test_diff',
1199
1266
                   'bzrlib.tests.test_doc_generate',
1204
1271
                   'bzrlib.tests.test_graph',
1205
1272
                   'bzrlib.tests.test_hashcache',
1206
1273
                   'bzrlib.tests.test_http',
 
1274
                   'bzrlib.tests.test_http_response',
1207
1275
                   'bzrlib.tests.test_identitymap',
 
1276
                   'bzrlib.tests.test_ignores',
1208
1277
                   'bzrlib.tests.test_inv',
1209
1278
                   'bzrlib.tests.test_knit',
1210
1279
                   'bzrlib.tests.test_lockdir',
1228
1297
                   'bzrlib.tests.test_revision',
1229
1298
                   'bzrlib.tests.test_revisionnamespaces',
1230
1299
                   'bzrlib.tests.test_revprops',
 
1300
                   'bzrlib.tests.test_revisiontree',
1231
1301
                   'bzrlib.tests.test_rio',
1232
1302
                   'bzrlib.tests.test_sampler',
1233
1303
                   'bzrlib.tests.test_selftest',
1257
1327
                   'bzrlib.tests.test_xml',
1258
1328
                   ]
1259
1329
    test_transport_implementations = [
1260
 
        'bzrlib.tests.test_transport_implementations']
1261
 
 
1262
 
    suite = TestSuite()
 
1330
        'bzrlib.tests.test_transport_implementations',
 
1331
        'bzrlib.tests.test_read_bundle',
 
1332
        ]
 
1333
    suite = TestUtil.TestSuite()
1263
1334
    loader = TestUtil.TestLoader()
 
1335
    suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
1264
1336
    from bzrlib.transport import TransportTestProviderAdapter
1265
1337
    adapter = TransportTestProviderAdapter()
1266
1338
    adapt_modules(test_transport_implementations, adapter, loader, suite)
1267
 
    suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
1268
1339
    for package in packages_to_test():
1269
1340
        suite.addTest(package.test_suite())
1270
1341
    for m in MODULES_TO_TEST:
1271
1342
        suite.addTest(loader.loadTestsFromModule(m))
1272
 
    for m in (MODULES_TO_DOCTEST):
1273
 
        suite.addTest(DocTestSuite(m))
 
1343
    for m in MODULES_TO_DOCTEST:
 
1344
        suite.addTest(doctest.DocTestSuite(m))
1274
1345
    for name, plugin in bzrlib.plugin.all_plugins().items():
1275
1346
        if getattr(plugin, 'test_suite', None) is not None:
1276
1347
            suite.addTest(plugin.test_suite())