/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: Martin Pool
  • Date: 2007-10-08 07:29:57 UTC
  • mfrom: (2894 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2895.
  • Revision ID: mbp@sourcefrog.net-20071008072957-uhm1gl1mqcsdc377
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
from bzrlib import symbol_versioning
79
79
from bzrlib.symbol_versioning import (
80
80
    deprecated_method,
81
 
    zero_eighteen,
82
81
    zero_ninetyone,
 
82
    zero_ninetytwo,
83
83
    )
84
84
import bzrlib.trace
85
85
from bzrlib.transport import get_transport
161
161
class ExtendedTestResult(unittest._TextTestResult):
162
162
    """Accepts, reports and accumulates the results of running tests.
163
163
 
164
 
    Compared to this unittest version this class adds support for
 
164
    Compared to the unittest version this class adds support for
165
165
    profiling, benchmarking, stopping as soon as a test fails,  and
166
166
    skipping tests.  There are further-specialized subclasses for
167
167
    different types of display.
342
342
        except KeyboardInterrupt:
343
343
            raise
344
344
        except:
345
 
            self.addError(test, test.__exc_info())
 
345
            self.addError(test, test._exc_info())
346
346
        else:
347
347
            # seems best to treat this as success from point-of-view of unittest
348
348
            # -- it actually does nothing so it barely matters :)
1153
1153
            'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
1154
1154
            'HOME': os.getcwd(),
1155
1155
            'APPDATA': None,  # bzr now use Win32 API and don't rely on APPDATA
 
1156
            'BZR_EDITOR': None, # test_msgeditor manipulates this variable
1156
1157
            'BZR_EMAIL': None,
1157
1158
            'BZREMAIL': None, # may still be present in the environment
1158
1159
            'EMAIL': None,
1289
1290
        else:
1290
1291
            return "DELETED log file to reduce memory footprint"
1291
1292
 
1292
 
    @deprecated_method(zero_eighteen)
1293
 
    def capture(self, cmd, retcode=0):
1294
 
        """Shortcut that splits cmd into words, runs, and returns stdout"""
1295
 
        return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
1296
 
 
1297
1293
    def requireFeature(self, feature):
1298
1294
        """This test requires a specific feature is available.
1299
1295
 
1302
1298
        if not feature.available():
1303
1299
            raise UnavailableFeature(feature)
1304
1300
 
1305
 
    @deprecated_method(zero_eighteen)
1306
 
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
1307
 
                         working_dir=None):
1308
 
        """Invoke bzr and return (stdout, stderr).
1309
 
 
1310
 
        Don't call this method, just use run_bzr() which is equivalent.
1311
 
 
1312
 
        :param argv: Arguments to invoke bzr.  This may be either a 
1313
 
            single string, in which case it is split by shlex into words, 
1314
 
            or a list of arguments.
1315
 
        :param retcode: Expected return code, or None for don't-care.
1316
 
        :param encoding: Encoding for sys.stdout and sys.stderr
1317
 
        :param stdin: A string to be used as stdin for the command.
1318
 
        :param working_dir: Change to this directory before running
1319
 
        """
1320
 
        return self._run_bzr_autosplit(argv, retcode=retcode,
1321
 
                encoding=encoding, stdin=stdin, working_dir=working_dir,
1322
 
                )
1323
 
 
1324
1301
    def _run_bzr_autosplit(self, args, retcode, encoding, stdin,
1325
1302
            working_dir):
1326
1303
        """Run bazaar command line, splitting up a string command line."""
1327
1304
        if isinstance(args, basestring):
1328
 
            args = list(shlex.split(args))
 
1305
            # shlex don't understand unicode strings,
 
1306
            # so args should be plain string (bialix 20070906)
 
1307
            args = list(shlex.split(str(args)))
1329
1308
        return self._run_bzr_core(args, retcode=retcode,
1330
1309
                encoding=encoding, stdin=stdin, working_dir=working_dir,
1331
1310
                )
1356
1335
        try:
1357
1336
            result = self.apply_redirected(ui.ui_factory.stdin,
1358
1337
                stdout, stderr,
1359
 
                bzrlib.commands.run_bzr_catch_errors,
 
1338
                bzrlib.commands.run_bzr_catch_user_errors,
1360
1339
                args)
1361
1340
        finally:
1362
1341
            logger.removeHandler(handler)
1375
1354
                              message='Unexpected return code')
1376
1355
        return out, err
1377
1356
 
1378
 
    def run_bzr(self, *args, **kwargs):
 
1357
    def run_bzr(self, args, retcode=0, encoding=None, stdin=None,
 
1358
                working_dir=None, error_regexes=[], output_encoding=None):
1379
1359
        """Invoke bzr, as if it were run from the command line.
1380
1360
 
1381
1361
        The argument list should not include the bzr program name - the
1389
1369
        2- A single string, eg "add a".  This is the most convenient 
1390
1370
        for hardcoded commands.
1391
1371
 
1392
 
        3- Several varargs parameters, eg run_bzr("add", "a").  
1393
 
        This is not recommended for new code.
1394
 
 
1395
1372
        This runs bzr through the interface that catches and reports
1396
1373
        errors, and with logging set to something approximating the
1397
1374
        default, so that error reporting can be checked.
1410
1387
        :keyword error_regexes: A list of expected error messages.  If
1411
1388
            specified they must be seen in the error output of the command.
1412
1389
        """
1413
 
        retcode = kwargs.pop('retcode', 0)
1414
 
        encoding = kwargs.pop('encoding', None)
1415
 
        stdin = kwargs.pop('stdin', None)
1416
 
        working_dir = kwargs.pop('working_dir', None)
1417
 
        error_regexes = kwargs.pop('error_regexes', [])
1418
 
 
1419
 
        if kwargs:
1420
 
            raise TypeError("run_bzr() got unexpected keyword arguments '%s'"
1421
 
                            % kwargs.keys())
1422
 
 
1423
 
        if len(args) == 1:
1424
 
            if isinstance(args[0], (list, basestring)):
1425
 
                args = args[0]
1426
 
        else:
1427
 
            symbol_versioning.warn(zero_eighteen % "passing varargs to run_bzr",
1428
 
                                   DeprecationWarning, stacklevel=3)
1429
 
 
1430
 
        out, err = self._run_bzr_autosplit(args=args,
 
1390
        out, err = self._run_bzr_autosplit(
 
1391
            args=args,
1431
1392
            retcode=retcode,
1432
 
            encoding=encoding, stdin=stdin, working_dir=working_dir,
 
1393
            encoding=encoding,
 
1394
            stdin=stdin,
 
1395
            working_dir=working_dir,
1433
1396
            )
1434
 
 
1435
1397
        for regex in error_regexes:
1436
1398
            self.assertContainsRe(err, regex)
1437
1399
        return out, err
1438
1400
 
1439
 
    def run_bzr_decode(self, *args, **kwargs):
1440
 
        if 'encoding' in kwargs:
1441
 
            encoding = kwargs['encoding']
1442
 
        else:
1443
 
            encoding = bzrlib.user_encoding
1444
 
        return self.run_bzr(*args, **kwargs)[0].decode(encoding)
1445
 
 
1446
1401
    def run_bzr_error(self, error_regexes, *args, **kwargs):
1447
1402
        """Run bzr, and check that stderr contains the supplied regexes
1448
1403
 
1880
1835
        base = self.get_vfs_only_server().get_url()
1881
1836
        return self._adjust_url(base, relpath)
1882
1837
 
 
1838
    def _create_safety_net(self):
 
1839
        """Make a fake bzr directory.
 
1840
 
 
1841
        This prevents any tests propagating up onto the TEST_ROOT directory's
 
1842
        real branch.
 
1843
        """
 
1844
        root = TestCaseWithMemoryTransport.TEST_ROOT
 
1845
        bzrdir.BzrDir.create_standalone_workingtree(root)
 
1846
 
 
1847
    def _check_safety_net(self):
 
1848
        """Check that the safety .bzr directory have not been touched.
 
1849
 
 
1850
        _make_test_root have created a .bzr directory to prevent tests from
 
1851
        propagating. This method ensures than a test did not leaked.
 
1852
        """
 
1853
        root = TestCaseWithMemoryTransport.TEST_ROOT
 
1854
        wt = workingtree.WorkingTree.open(root)
 
1855
        last_rev = wt.last_revision()
 
1856
        if last_rev != 'null:':
 
1857
            # The current test have modified the /bzr directory, we need to
 
1858
            # recreate a new one or all the followng tests will fail.
 
1859
            # If you need to inspect its content uncomment the following line
 
1860
            # import pdb; pdb.set_trace()
 
1861
            _rmtree_temp_dir(root + '/.bzr')
 
1862
            self._create_safety_net()
 
1863
            raise AssertionError('%s/.bzr should not be modified' % root)
 
1864
 
1883
1865
    def _make_test_root(self):
1884
 
        if TestCaseWithMemoryTransport.TEST_ROOT is not None:
1885
 
            return
1886
 
        root = osutils.mkdtemp(prefix='testbzr-', suffix='.tmp')
1887
 
        TestCaseWithMemoryTransport.TEST_ROOT = root
1888
 
        
1889
 
        # make a fake bzr directory there to prevent any tests propagating
1890
 
        # up onto the source directory's real branch
1891
 
        bzrdir.BzrDir.create_standalone_workingtree(root)
1892
 
 
1893
 
        # The same directory is used by all tests, and we're not specifically
1894
 
        # told when all tests are finished.  This will do.
1895
 
        atexit.register(_rmtree_temp_dir, root)
 
1866
        if TestCaseWithMemoryTransport.TEST_ROOT is None:
 
1867
            root = osutils.mkdtemp(prefix='testbzr-', suffix='.tmp')
 
1868
            TestCaseWithMemoryTransport.TEST_ROOT = root
 
1869
 
 
1870
            self._create_safety_net()
 
1871
 
 
1872
            # The same directory is used by all tests, and we're not
 
1873
            # specifically told when all tests are finished.  This will do.
 
1874
            atexit.register(_rmtree_temp_dir, root)
 
1875
 
 
1876
        self.addCleanup(self._check_safety_net)
1896
1877
 
1897
1878
    def makeAndChdirToTestDir(self):
1898
1879
        """Create a temporary directories for this one test.
2676
2657
        else:
2677
2658
            return uni_val, str_val
2678
2659
    return None, None
 
2660
 
 
2661
 
 
2662
def probe_bad_non_ascii(encoding):
 
2663
    """Try to find [bad] character with code [128..255]
 
2664
    that cannot be decoded to unicode in some encoding.
 
2665
    Return None if all non-ascii characters is valid
 
2666
    for given encoding.
 
2667
    """
 
2668
    for i in xrange(128, 256):
 
2669
        char = chr(i)
 
2670
        try:
 
2671
            char.decode(encoding)
 
2672
        except UnicodeDecodeError:
 
2673
            return char
 
2674
    return None