~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: John Arbash Meinel
  • Date: 2009-11-30 17:25:22 UTC
  • mto: This revision was merged to the branch mainline in revision 4849.
  • Revision ID: john@arbash-meinel.com-20091130172522-m373yel3msmjppey
Implement KnownGraph.add_node() for the pyrex version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
 
54
54
UTF8DirReaderFeature = _UTF8DirReaderFeature()
55
55
 
56
 
term_ios_feature = tests.ModuleAvailableFeature('termios')
57
 
 
58
56
 
59
57
def _already_unicode(s):
60
58
    return s
82
80
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
83
81
                               _native_to_unicode=_utf8_to_unicode)))
84
82
 
85
 
    if test__walkdirs_win32.win32_readdir_feature.available():
 
83
    if test__walkdirs_win32.Win32ReadDirFeature.available():
86
84
        try:
87
85
            from bzrlib import _walkdirs_win32
88
86
            scenarios.append(
380
378
        # Instead blackbox.test_locale should check for localized
381
379
        # dates once they do occur in output strings.
382
380
 
383
 
    def test_format_date_with_offset_in_original_timezone(self):
384
 
        self.assertEqual("Thu 1970-01-01 00:00:00 +0000",
385
 
            osutils.format_date_with_offset_in_original_timezone(0))
386
 
        self.assertEqual("Fri 1970-01-02 03:46:40 +0000",
387
 
            osutils.format_date_with_offset_in_original_timezone(100000))
388
 
        self.assertEqual("Fri 1970-01-02 05:46:40 +0200",
389
 
            osutils.format_date_with_offset_in_original_timezone(100000, 7200))
390
 
 
391
381
    def test_local_time_offset(self):
392
382
        """Test that local_time_offset() returns a sane value."""
393
383
        offset = osutils.local_time_offset()
989
979
 
990
980
    def test_osutils_binding(self):
991
981
        from bzrlib.tests import test__chunks_to_lines
992
 
        if test__chunks_to_lines.compiled_chunkstolines_feature.available():
 
982
        if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
993
983
            from bzrlib._chunks_to_lines_pyx import chunks_to_lines
994
984
        else:
995
985
            from bzrlib._chunks_to_lines_py import chunks_to_lines
1135
1125
            dirblock[:] = new_dirblock
1136
1126
 
1137
1127
    def _save_platform_info(self):
1138
 
        self.overrideAttr(win32utils, 'winver')
1139
 
        self.overrideAttr(osutils, '_fs_enc')
1140
 
        self.overrideAttr(osutils, '_selected_dir_reader')
 
1128
        cur_winver = win32utils.winver
 
1129
        cur_fs_enc = osutils._fs_enc
 
1130
        cur_dir_reader = osutils._selected_dir_reader
 
1131
        def restore():
 
1132
            win32utils.winver = cur_winver
 
1133
            osutils._fs_enc = cur_fs_enc
 
1134
            osutils._selected_dir_reader = cur_dir_reader
 
1135
        self.addCleanup(restore)
1141
1136
 
1142
1137
    def assertDirReaderIs(self, expected):
1143
1138
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
1176
1171
 
1177
1172
    def test_force_walkdirs_utf8_nt(self):
1178
1173
        # Disabled because the thunk of the whole walkdirs api is disabled.
1179
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1174
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1180
1175
        self._save_platform_info()
1181
1176
        win32utils.winver = 'Windows NT'
1182
1177
        from bzrlib._walkdirs_win32 import Win32ReadDir
1183
1178
        self.assertDirReaderIs(Win32ReadDir)
1184
1179
 
1185
1180
    def test_force_walkdirs_utf8_98(self):
1186
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1181
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1187
1182
        self._save_platform_info()
1188
1183
        win32utils.winver = 'Windows 98'
1189
1184
        self.assertDirReaderIs(osutils.UnicodeDirReader)
1340
1335
        self.assertEqual(expected_dirblocks, result)
1341
1336
 
1342
1337
    def test__walkdirs_utf8_win32readdir(self):
1343
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1338
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1344
1339
        self.requireFeature(tests.UnicodeFilenameFeature)
1345
1340
        from bzrlib._walkdirs_win32 import Win32ReadDir
1346
1341
        self._save_platform_info()
1397
1392
 
1398
1393
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1399
1394
        """make sure our Stat values are valid"""
1400
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1395
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1401
1396
        self.requireFeature(tests.UnicodeFilenameFeature)
1402
1397
        from bzrlib._walkdirs_win32 import Win32ReadDir
1403
1398
        name0u = u'0file-\xb6'
1421
1416
 
1422
1417
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1423
1418
        """make sure our Stat values are valid"""
1424
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1419
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1425
1420
        self.requireFeature(tests.UnicodeFilenameFeature)
1426
1421
        from bzrlib._walkdirs_win32 import Win32ReadDir
1427
1422
        name0u = u'0dir-\u062c\u0648'
1576
1571
        def cleanup():
1577
1572
            if 'BZR_TEST_ENV_VAR' in os.environ:
1578
1573
                del os.environ['BZR_TEST_ENV_VAR']
 
1574
 
1579
1575
        self.addCleanup(cleanup)
1580
1576
 
1581
1577
    def test_set(self):
1692
1688
 
1693
1689
    def setUp(self):
1694
1690
        tests.TestCaseInTempDir.setUp(self)
1695
 
        self.overrideAttr(osutils,
1696
 
                          '_selected_dir_reader', self._dir_reader_class())
 
1691
 
 
1692
        # Save platform specific info and reset it
 
1693
        cur_dir_reader = osutils._selected_dir_reader
 
1694
 
 
1695
        def restore():
 
1696
            osutils._selected_dir_reader = cur_dir_reader
 
1697
        self.addCleanup(restore)
 
1698
 
 
1699
        osutils._selected_dir_reader = self._dir_reader_class()
1697
1700
 
1698
1701
    def _get_ascii_tree(self):
1699
1702
        tree = [
1844
1847
 
1845
1848
class TestConcurrency(tests.TestCase):
1846
1849
 
1847
 
    def setUp(self):
1848
 
        super(TestConcurrency, self).setUp()
1849
 
        self.overrideAttr(osutils, '_cached_local_concurrency')
1850
 
 
1851
1850
    def test_local_concurrency(self):
1852
1851
        concurrency = osutils.local_concurrency()
1853
1852
        self.assertIsInstance(concurrency, int)
1854
1853
 
1855
 
    def test_local_concurrency_environment_variable(self):
1856
 
        os.environ['BZR_CONCURRENCY'] = '2'
1857
 
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1858
 
        os.environ['BZR_CONCURRENCY'] = '3'
1859
 
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1860
 
        os.environ['BZR_CONCURRENCY'] = 'foo'
1861
 
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1862
 
 
1863
 
    def test_option_concurrency(self):
1864
 
        os.environ['BZR_CONCURRENCY'] = '1'
1865
 
        self.run_bzr('rocks --concurrency 42')
1866
 
        # Command line overrides envrionment variable
1867
 
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1868
 
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1869
 
 
1870
1854
 
1871
1855
class TestFailedToLoadExtension(tests.TestCase):
1872
1856
 
1879
1863
 
1880
1864
    def setUp(self):
1881
1865
        super(TestFailedToLoadExtension, self).setUp()
1882
 
        self.overrideAttr(osutils, '_extension_load_failures', [])
 
1866
        self.saved_failures = osutils._extension_load_failures[:]
 
1867
        del osutils._extension_load_failures[:]
 
1868
        self.addCleanup(self.restore_failures)
 
1869
 
 
1870
    def restore_failures(self):
 
1871
        osutils._extension_load_failures = self.saved_failures
1883
1872
 
1884
1873
    def test_failure_to_load(self):
1885
1874
        self._try_loading()
1903
1892
            r"bzr: warning: some compiled extensions could not be loaded; "
1904
1893
            "see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1905
1894
            )
1906
 
 
1907
 
 
1908
 
class TestTerminalWidth(tests.TestCase):
1909
 
 
1910
 
    def replace_stdout(self, new):
1911
 
        self.overrideAttr(sys, 'stdout', new)
1912
 
 
1913
 
    def replace__terminal_size(self, new):
1914
 
        self.overrideAttr(osutils, '_terminal_size', new)
1915
 
 
1916
 
    def set_fake_tty(self):
1917
 
 
1918
 
        class I_am_a_tty(object):
1919
 
            def isatty(self):
1920
 
                return True
1921
 
 
1922
 
        self.replace_stdout(I_am_a_tty())
1923
 
 
1924
 
    def test_default_values(self):
1925
 
        self.assertEqual(80, osutils.default_terminal_width)
1926
 
 
1927
 
    def test_defaults_to_BZR_COLUMNS(self):
1928
 
        # BZR_COLUMNS is set by the test framework
1929
 
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1930
 
        os.environ['BZR_COLUMNS'] = '12'
1931
 
        self.assertEqual(12, osutils.terminal_width())
1932
 
 
1933
 
    def test_falls_back_to_COLUMNS(self):
1934
 
        del os.environ['BZR_COLUMNS']
1935
 
        self.assertNotEqual('42', os.environ['COLUMNS'])
1936
 
        self.set_fake_tty()
1937
 
        os.environ['COLUMNS'] = '42'
1938
 
        self.assertEqual(42, osutils.terminal_width())
1939
 
 
1940
 
    def test_tty_default_without_columns(self):
1941
 
        del os.environ['BZR_COLUMNS']
1942
 
        del os.environ['COLUMNS']
1943
 
 
1944
 
        def terminal_size(w, h):
1945
 
            return 42, 42
1946
 
 
1947
 
        self.set_fake_tty()
1948
 
        # We need to override the osutils definition as it depends on the
1949
 
        # running environment that we can't control (PQM running without a
1950
 
        # controlling terminal is one example).
1951
 
        self.replace__terminal_size(terminal_size)
1952
 
        self.assertEqual(42, osutils.terminal_width())
1953
 
 
1954
 
    def test_non_tty_default_without_columns(self):
1955
 
        del os.environ['BZR_COLUMNS']
1956
 
        del os.environ['COLUMNS']
1957
 
        self.replace_stdout(None)
1958
 
        self.assertEqual(None, osutils.terminal_width())
1959
 
 
1960
 
    def test_no_TIOCGWINSZ(self):
1961
 
        self.requireFeature(term_ios_feature)
1962
 
        termios = term_ios_feature.module
1963
 
        # bug 63539 is about a termios without TIOCGWINSZ attribute
1964
 
        try:
1965
 
            orig = termios.TIOCGWINSZ
1966
 
        except AttributeError:
1967
 
            # We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
1968
 
            pass
1969
 
        else:
1970
 
            self.overrideAttr(termios, 'TIOCGWINSZ')
1971
 
            del termios.TIOCGWINSZ
1972
 
        del os.environ['BZR_COLUMNS']
1973
 
        del os.environ['COLUMNS']
1974
 
        # Whatever the result is, if we don't raise an exception, it's ok.
1975
 
        osutils.terminal_width()