/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: Vincent Ladeuil
  • Date: 2007-06-20 14:25:06 UTC
  • mfrom: (2540 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2646.
  • Revision ID: v.ladeuil+lp@free.fr-20070620142506-txsb1v8538kpsafw
merge bzr.dev @ 2540

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
# general style of bzrlib.  Please continue that consistency when adding e.g.
27
27
# new assertFoo() methods.
28
28
 
 
29
import atexit
29
30
import codecs
30
31
from cStringIO import StringIO
31
32
import difflib
42
43
import tempfile
43
44
import unittest
44
45
import time
 
46
import warnings
45
47
 
46
48
 
47
49
from bzrlib import (
501
503
                 stream=sys.stderr,
502
504
                 descriptions=0,
503
505
                 verbosity=1,
504
 
                 keep_output=False,
505
506
                 bench_history=None,
506
507
                 use_numbered_dirs=False,
507
508
                 list_only=False
509
510
        self.stream = unittest._WritelnDecorator(stream)
510
511
        self.descriptions = descriptions
511
512
        self.verbosity = verbosity
512
 
        self.keep_output = keep_output
513
513
        self._bench_history = bench_history
514
514
        self.use_numbered_dirs = use_numbered_dirs
515
515
        self.list_only = list_only
576
576
            for feature, count in sorted(result.unsupported.items()):
577
577
                self.stream.writeln("Missing feature '%s' skipped %d tests." %
578
578
                    (feature, count))
579
 
        result.report_cleaning_up()
580
 
        # This is still a little bogus, 
581
 
        # but only a little. Folk not using our testrunner will
582
 
        # have to delete their temp directories themselves.
583
 
        test_root = TestCaseWithMemoryTransport.TEST_ROOT
584
 
        if result.wasSuccessful() or not self.keep_output:
585
 
            if test_root is not None:
586
 
                # If LANG=C we probably have created some bogus paths
587
 
                # which rmtree(unicode) will fail to delete
588
 
                # so make sure we are using rmtree(str) to delete everything
589
 
                # except on win32, where rmtree(str) will fail
590
 
                # since it doesn't have the property of byte-stream paths
591
 
                # (they are either ascii or mbcs)
592
 
                if sys.platform == 'win32':
593
 
                    # make sure we are using the unicode win32 api
594
 
                    test_root = unicode(test_root)
595
 
                else:
596
 
                    test_root = test_root.encode(
597
 
                        sys.getfilesystemencoding())
598
 
                _rmtree_temp_dir(test_root)
599
 
        else:
600
 
            note("Failed tests working directories are in '%s'\n", test_root)
601
 
        TestCaseWithMemoryTransport.TEST_ROOT = None
602
579
        result.finished()
603
580
        return result
604
581
 
1603
1580
    file defaults for the transport in tests, nor does it obey the command line
1604
1581
    override, so tests that accidentally write to the common directory should
1605
1582
    be rare.
 
1583
 
 
1584
    :cvar TEST_ROOT: Directory containing all temporary directories, plus
 
1585
    a .bzr directory that stops us ascending higher into the filesystem.
1606
1586
    """
1607
1587
 
1608
1588
    TEST_ROOT = None
1609
1589
    _TEST_NAME = 'test'
1610
1590
 
1611
 
 
1612
1591
    def __init__(self, methodName='runTest'):
1613
1592
        # allow test parameterisation after test construction and before test
1614
1593
        # execution. Variables that the parameteriser sets need to be 
1765
1744
    def _make_test_root(self):
1766
1745
        if TestCaseWithMemoryTransport.TEST_ROOT is not None:
1767
1746
            return
1768
 
        i = 0
1769
 
        while True:
1770
 
            root = u'test%04d.tmp' % i
1771
 
            try:
1772
 
                os.mkdir(root)
1773
 
            except OSError, e:
1774
 
                if e.errno == errno.EEXIST:
1775
 
                    i += 1
1776
 
                    continue
1777
 
                else:
1778
 
                    raise
1779
 
            # successfully created
1780
 
            TestCaseWithMemoryTransport.TEST_ROOT = osutils.abspath(root)
1781
 
            break
 
1747
        root = tempfile.mkdtemp(prefix='testbzr-', suffix='.tmp')
 
1748
        TestCaseWithMemoryTransport.TEST_ROOT = root
 
1749
        
1782
1750
        # make a fake bzr directory there to prevent any tests propagating
1783
1751
        # up onto the source directory's real branch
1784
 
        bzrdir.BzrDir.create_standalone_workingtree(
1785
 
            TestCaseWithMemoryTransport.TEST_ROOT)
 
1752
        bzrdir.BzrDir.create_standalone_workingtree(root)
 
1753
 
 
1754
        # The same directory is used by all tests, and we're not specifically
 
1755
        # told when all tests are finished.  This will do.
 
1756
        atexit.register(_rmtree_temp_dir, root)
1786
1757
 
1787
1758
    def makeAndChdirToTestDir(self):
1788
1759
        """Create a temporary directories for this one test.
1862
1833
    All test cases create their own directory within that.  If the
1863
1834
    tests complete successfully, the directory is removed.
1864
1835
 
1865
 
    InTempDir is an old alias for FunctionalTestCase.
 
1836
    :ivar test_base_dir: The path of the top-level directory for this 
 
1837
    test, which contains a home directory and a work directory.
 
1838
 
 
1839
    :ivar test_home_dir: An initially empty directory under test_base_dir
 
1840
    which is used as $HOME for this test.
 
1841
 
 
1842
    :ivar test_dir: A directory under test_base_dir used as the current
 
1843
    directory when the test proper is run.
1866
1844
    """
1867
1845
 
1868
1846
    OVERRIDE_PYTHON = 'python'
1882
1860
        For TestCaseInTempDir we create a temporary directory based on the test
1883
1861
        name and then create two subdirs - test and home under it.
1884
1862
        """
1885
 
        if self.use_numbered_dirs:  # strongly recommended on Windows
1886
 
                                    # due the path length limitation (260 ch.)
1887
 
            candidate_dir = '%s/%dK/%05d' % (self.TEST_ROOT,
1888
 
                                             int(self.number/1000),
1889
 
                                             self.number)
1890
 
            os.makedirs(candidate_dir)
1891
 
            self.test_home_dir = candidate_dir + '/home'
1892
 
            os.mkdir(self.test_home_dir)
1893
 
            self.test_dir = candidate_dir + '/work'
1894
 
            os.mkdir(self.test_dir)
1895
 
            os.chdir(self.test_dir)
1896
 
            # put name of test inside
1897
 
            f = file(candidate_dir + '/name', 'w')
 
1863
        # create a directory within the top level test directory
 
1864
        candidate_dir = tempfile.mkdtemp(dir=self.TEST_ROOT)
 
1865
        # now create test and home directories within this dir
 
1866
        self.test_base_dir = candidate_dir
 
1867
        self.test_home_dir = self.test_base_dir + '/home'
 
1868
        os.mkdir(self.test_home_dir)
 
1869
        self.test_dir = self.test_base_dir + '/work'
 
1870
        os.mkdir(self.test_dir)
 
1871
        os.chdir(self.test_dir)
 
1872
        # put name of test inside
 
1873
        f = file(self.test_base_dir + '/name', 'w')
 
1874
        try:
1898
1875
            f.write(self.id())
 
1876
        finally:
1899
1877
            f.close()
1900
 
            return
1901
 
        # Else NAMED DIRS
1902
 
        # shorten the name, to avoid test failures due to path length
1903
 
        short_id = self.id().replace('bzrlib.tests.', '') \
1904
 
                   .replace('__main__.', '')[-100:]
1905
 
        # it's possible the same test class is run several times for
1906
 
        # parameterized tests, so make sure the names don't collide.  
1907
 
        i = 0
1908
 
        while True:
1909
 
            if i > 0:
1910
 
                candidate_dir = '%s/%s.%d' % (self.TEST_ROOT, short_id, i)
1911
 
            else:
1912
 
                candidate_dir = '%s/%s' % (self.TEST_ROOT, short_id)
1913
 
            if os.path.exists(candidate_dir):
1914
 
                i = i + 1
1915
 
                continue
1916
 
            else:
1917
 
                os.mkdir(candidate_dir)
1918
 
                self.test_home_dir = candidate_dir + '/home'
1919
 
                os.mkdir(self.test_home_dir)
1920
 
                self.test_dir = candidate_dir + '/work'
1921
 
                os.mkdir(self.test_dir)
1922
 
                os.chdir(self.test_dir)
1923
 
                break
 
1878
        self.addCleanup(self.deleteTestDir)
 
1879
 
 
1880
    def deleteTestDir(self):
 
1881
        _rmtree_temp_dir(self.test_base_dir)
1924
1882
 
1925
1883
    def build_tree(self, shape, line_endings='binary', transport=None):
1926
1884
        """Build a test tree according to a pattern.
2162
2120
 
2163
2121
 
2164
2122
def run_suite(suite, name='test', verbose=False, pattern=".*",
2165
 
              stop_on_failure=False, keep_output=False,
 
2123
              stop_on_failure=False,
2166
2124
              transport=None, lsprof_timed=None, bench_history=None,
2167
2125
              matching_tests_first=None,
2168
2126
              numbered_dirs=None,
2182
2140
    runner = TextTestRunner(stream=sys.stdout,
2183
2141
                            descriptions=0,
2184
2142
                            verbosity=verbosity,
2185
 
                            keep_output=keep_output,
2186
2143
                            bench_history=bench_history,
2187
2144
                            use_numbered_dirs=use_numbered_dirs,
2188
2145
                            list_only=list_only,
2217
2174
 
2218
2175
 
2219
2176
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
2220
 
             keep_output=False,
2221
2177
             transport=None,
2222
2178
             test_suite_factory=None,
2223
2179
             lsprof_timed=None,
2245
2201
        else:
2246
2202
            suite = test_suite_factory()
2247
2203
        return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
2248
 
                     stop_on_failure=stop_on_failure, keep_output=keep_output,
 
2204
                     stop_on_failure=stop_on_failure,
2249
2205
                     transport=transport,
2250
2206
                     lsprof_timed=lsprof_timed,
2251
2207
                     bench_history=bench_history,
2284
2240
                   'bzrlib.tests.test_counted_lock',
2285
2241
                   'bzrlib.tests.test_decorators',
2286
2242
                   'bzrlib.tests.test_delta',
 
2243
                   'bzrlib.tests.test_deprecated_graph',
2287
2244
                   'bzrlib.tests.test_diff',
2288
2245
                   'bzrlib.tests.test_dirstate',
2289
2246
                   'bzrlib.tests.test_errors',
2303
2260
                   'bzrlib.tests.test_https_ca_bundle',
2304
2261
                   'bzrlib.tests.test_identitymap',
2305
2262
                   'bzrlib.tests.test_ignores',
 
2263
                   'bzrlib.tests.test_info',
2306
2264
                   'bzrlib.tests.test_inv',
2307
2265
                   'bzrlib.tests.test_knit',
2308
2266
                   'bzrlib.tests.test_lazy_import',
2310
2268
                   'bzrlib.tests.test_lockdir',
2311
2269
                   'bzrlib.tests.test_lockable_files',
2312
2270
                   'bzrlib.tests.test_log',
 
2271
                   'bzrlib.tests.test_lsprof',
2313
2272
                   'bzrlib.tests.test_memorytree',
2314
2273
                   'bzrlib.tests.test_merge',
2315
2274
                   'bzrlib.tests.test_merge3',
2421
2380
 
2422
2381
 
2423
2382
def _rmtree_temp_dir(dirname):
 
2383
    # If LANG=C we probably have created some bogus paths
 
2384
    # which rmtree(unicode) will fail to delete
 
2385
    # so make sure we are using rmtree(str) to delete everything
 
2386
    # except on win32, where rmtree(str) will fail
 
2387
    # since it doesn't have the property of byte-stream paths
 
2388
    # (they are either ascii or mbcs)
 
2389
    if sys.platform == 'win32':
 
2390
        # make sure we are using the unicode win32 api
 
2391
        dirname = unicode(dirname)
 
2392
    else:
 
2393
        dirname = dirname.encode(sys.getfilesystemencoding())
2424
2394
    try:
2425
2395
        osutils.rmtree(dirname)
2426
2396
    except OSError, e: