/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: 2009-12-09 05:47:32 UTC
  • mfrom: (4879 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4880.
  • Revision ID: mbp@sourcefrog.net-20091209054732-7414e9uma23mfv6x
trivial merge of trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
533
533
    def report_test_start(self, test):
534
534
        self.count += 1
535
535
        name = self._shortened_test_description(test)
536
 
        # width needs space for 6 char status, plus 1 for slash, plus an
537
 
        # 11-char time string, plus a trailing blank
538
 
        # when NUMBERED_DIRS: plus 5 chars on test number, plus 1 char on space
539
 
        self.stream.write(self._ellipsize_to_right(name,
540
 
                          osutils.terminal_width()-18))
 
536
        width = osutils.terminal_width()
 
537
        if width is not None:
 
538
            # width needs space for 6 char status, plus 1 for slash, plus an
 
539
            # 11-char time string, plus a trailing blank
 
540
            # when NUMBERED_DIRS: plus 5 chars on test number, plus 1 char on
 
541
            # space
 
542
            self.stream.write(self._ellipsize_to_right(name, width-18))
 
543
        else:
 
544
            self.stream.write(name)
541
545
        self.stream.flush()
542
546
 
543
547
    def _error_summary(self, err):
1115
1119
        :raises AssertionError: If the expected and actual stat values differ
1116
1120
            other than by atime.
1117
1121
        """
1118
 
        self.assertEqual(expected.st_size, actual.st_size)
1119
 
        self.assertEqual(expected.st_mtime, actual.st_mtime)
1120
 
        self.assertEqual(expected.st_ctime, actual.st_ctime)
1121
 
        self.assertEqual(expected.st_dev, actual.st_dev)
1122
 
        self.assertEqual(expected.st_ino, actual.st_ino)
1123
 
        self.assertEqual(expected.st_mode, actual.st_mode)
 
1122
        self.assertEqual(expected.st_size, actual.st_size,
 
1123
                         'st_size did not match')
 
1124
        self.assertEqual(expected.st_mtime, actual.st_mtime,
 
1125
                         'st_mtime did not match')
 
1126
        self.assertEqual(expected.st_ctime, actual.st_ctime,
 
1127
                         'st_ctime did not match')
 
1128
        if sys.platform != 'win32':
 
1129
            # On Win32 both 'dev' and 'ino' cannot be trusted. In python2.4 it
 
1130
            # is 'dev' that varies, in python 2.5 (6?) it is st_ino that is
 
1131
            # odd. Regardless we shouldn't actually try to assert anything
 
1132
            # about their values
 
1133
            self.assertEqual(expected.st_dev, actual.st_dev,
 
1134
                             'st_dev did not match')
 
1135
            self.assertEqual(expected.st_ino, actual.st_ino,
 
1136
                             'st_ino did not match')
 
1137
        self.assertEqual(expected.st_mode, actual.st_mode,
 
1138
                         'st_mode did not match')
1124
1139
 
1125
1140
    def assertLength(self, length, obj_with_len):
1126
1141
        """Assert that obj_with_len is of length length."""
1513
1528
            'BZR_PROGRESS_BAR': None,
1514
1529
            'BZR_LOG': None,
1515
1530
            'BZR_PLUGIN_PATH': None,
 
1531
            'BZR_CONCURRENCY': None,
1516
1532
            # Make sure that any text ui tests are consistent regardless of
1517
1533
            # the environment the test case is run in; you may want tests that
1518
1534
            # test other combinations.  'dumb' is a reasonable guess for tests
1520
1536
            'TERM': 'dumb',
1521
1537
            'LINES': '25',
1522
1538
            'COLUMNS': '80',
 
1539
            'BZR_COLUMNS': '80',
1523
1540
            # SSH Agent
1524
1541
            'SSH_AUTH_SOCK': None,
1525
1542
            # Proxies
1825
1842
            os.chdir(working_dir)
1826
1843
 
1827
1844
        try:
1828
 
            result = self.apply_redirected(ui.ui_factory.stdin,
1829
 
                stdout, stderr,
1830
 
                bzrlib.commands.run_bzr_catch_user_errors,
1831
 
                args)
 
1845
            try:
 
1846
                result = self.apply_redirected(ui.ui_factory.stdin,
 
1847
                    stdout, stderr,
 
1848
                    bzrlib.commands.run_bzr_catch_user_errors,
 
1849
                    args)
 
1850
            except KeyboardInterrupt:
 
1851
                # Reraise KeyboardInterrupt with contents of redirected stdout
 
1852
                # and stderr as arguments, for tests which are interested in
 
1853
                # stdout and stderr and are expecting the exception.
 
1854
                out = stdout.getvalue()
 
1855
                err = stderr.getvalue()
 
1856
                if out:
 
1857
                    self.log('output:\n%r', out)
 
1858
                if err:
 
1859
                    self.log('errors:\n%r', err)
 
1860
                raise KeyboardInterrupt(out, err)
1832
1861
        finally:
1833
1862
            logger.removeHandler(handler)
1834
1863
            ui.ui_factory = old_ui_factory
2382
2411
            # recreate a new one or all the followng tests will fail.
2383
2412
            # If you need to inspect its content uncomment the following line
2384
2413
            # import pdb; pdb.set_trace()
2385
 
            _rmtree_temp_dir(root + '/.bzr')
 
2414
            _rmtree_temp_dir(root + '/.bzr', test_id=self.id())
2386
2415
            self._create_safety_net()
2387
2416
            raise AssertionError('%s/.bzr should not be modified' % root)
2388
2417
 
2464
2493
        return branchbuilder.BranchBuilder(branch=branch)
2465
2494
 
2466
2495
    def overrideEnvironmentForTesting(self):
2467
 
        os.environ['HOME'] = self.test_home_dir
2468
 
        os.environ['BZR_HOME'] = self.test_home_dir
 
2496
        test_home_dir = self.test_home_dir
 
2497
        if isinstance(test_home_dir, unicode):
 
2498
            test_home_dir = test_home_dir.encode(sys.getfilesystemencoding())
 
2499
        os.environ['HOME'] = test_home_dir
 
2500
        os.environ['BZR_HOME'] = test_home_dir
2469
2501
 
2470
2502
    def setUp(self):
2471
2503
        super(TestCaseWithMemoryTransport, self).setUp()
2577
2609
 
2578
2610
    def deleteTestDir(self):
2579
2611
        os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
2580
 
        _rmtree_temp_dir(self.test_base_dir)
 
2612
        _rmtree_temp_dir(self.test_base_dir, test_id=self.id())
2581
2613
 
2582
2614
    def build_tree(self, shape, line_endings='binary', transport=None):
2583
2615
        """Build a test tree according to a pattern.
3321
3353
        if not os.path.isfile(bzr_path):
3322
3354
            # We are probably installed. Assume sys.argv is the right file
3323
3355
            bzr_path = sys.argv[0]
 
3356
        bzr_path = [bzr_path]
 
3357
        if sys.platform == "win32":
 
3358
            # if we're on windows, we can't execute the bzr script directly
 
3359
            bzr_path = [sys.executable] + bzr_path
3324
3360
        fd, test_list_file_name = tempfile.mkstemp()
3325
3361
        test_list_file = os.fdopen(fd, 'wb', 1)
3326
3362
        for test in process_tests:
3327
3363
            test_list_file.write(test.id() + '\n')
3328
3364
        test_list_file.close()
3329
3365
        try:
3330
 
            argv = [bzr_path, 'selftest', '--load-list', test_list_file_name,
 
3366
            argv = bzr_path + ['selftest', '--load-list', test_list_file_name,
3331
3367
                '--subunit']
3332
3368
            if '--no-plugins' in sys.argv:
3333
3369
                argv.append('--no-plugins')
4101
4137
    return new_test
4102
4138
 
4103
4139
 
4104
 
def _rmtree_temp_dir(dirname):
 
4140
def _rmtree_temp_dir(dirname, test_id=None):
4105
4141
    # If LANG=C we probably have created some bogus paths
4106
4142
    # which rmtree(unicode) will fail to delete
4107
4143
    # so make sure we are using rmtree(str) to delete everything
4119
4155
        # We don't want to fail here because some useful display will be lost
4120
4156
        # otherwise. Polluting the tmp dir is bad, but not giving all the
4121
4157
        # possible info to the test runner is even worse.
 
4158
        if test_id != None:
 
4159
            ui.ui_factory.clear_term()
 
4160
            sys.stderr.write('\nWhile running: %s\n' % (test_id,))
4122
4161
        sys.stderr.write('Unable to remove testing dir %s\n%s'
4123
4162
                         % (os.path.basename(dirname), e))
4124
4163
 
4208
4247
UnicodeFilenameFeature = _UnicodeFilenameFeature()
4209
4248
 
4210
4249
 
 
4250
class ModuleAvailableFeature(Feature):
 
4251
    """This is a feature than describes a module we want to be available.
 
4252
 
 
4253
    Declare the name of the module in __init__(), and then after probing, the
 
4254
    module will be available as 'self.module'.
 
4255
 
 
4256
    :ivar module: The module if it is available, else None.
 
4257
    """
 
4258
 
 
4259
    def __init__(self, module_name):
 
4260
        super(ModuleAvailableFeature, self).__init__()
 
4261
        self.module_name = module_name
 
4262
 
 
4263
    def _probe(self):
 
4264
        try:
 
4265
            self._module = __import__(self.module_name, {}, {}, [''])
 
4266
            return True
 
4267
        except ImportError:
 
4268
            return False
 
4269
 
 
4270
    @property
 
4271
    def module(self):
 
4272
        if self.available(): # Make sure the probe has been done
 
4273
            return self._module
 
4274
        return None
 
4275
    
 
4276
    def feature_name(self):
 
4277
        return self.module_name
 
4278
 
 
4279
 
 
4280
 
4211
4281
def probe_unicode_in_user_encoding():
4212
4282
    """Try to encode several unicode strings to use in unicode-aware tests.
4213
4283
    Return first successfull match.
4321
4391
            # Windows doesn't have os.kill, and we catch the SIGBREAK signal.
4322
4392
            # We trigger SIGBREAK via a Console api so we need ctypes to
4323
4393
            # access the function
4324
 
            if not have_ctypes:
 
4394
            try:
 
4395
                import ctypes
 
4396
            except OSError:
4325
4397
                return False
4326
4398
        return True
4327
4399