~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2009-10-27 21:54:26 UTC
  • mfrom: (4771 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4833.
  • Revision ID: jelmer@samba.org-20091027215426-72164bkd4mq9dsd4
merge bzr.dev.

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
61
59
 
62
60
 
 
61
def _fs_enc_to_unicode(s):
 
62
    return s.decode(osutils._fs_enc)
 
63
 
 
64
 
63
65
def _utf8_to_unicode(s):
64
66
    return s.decode('UTF-8')
65
67
 
82
84
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
83
85
                               _native_to_unicode=_utf8_to_unicode)))
84
86
 
85
 
    if test__walkdirs_win32.win32_readdir_feature.available():
 
87
    if test__walkdirs_win32.Win32ReadDirFeature.available():
86
88
        try:
87
89
            from bzrlib import _walkdirs_win32
 
90
            # TODO: check on windows, it may be that we need to use/add
 
91
            # safe_unicode instead of _fs_enc_to_unicode
88
92
            scenarios.append(
89
93
                ('win32',
90
94
                 dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
91
 
                      _native_to_unicode=_already_unicode)))
 
95
                      _native_to_unicode=_fs_enc_to_unicode)))
92
96
        except ImportError:
93
97
            pass
94
98
    return scenarios
122
126
 
123
127
class TestRename(tests.TestCaseInTempDir):
124
128
 
125
 
    def create_file(self, filename, content):
126
 
        f = open(filename, 'wb')
127
 
        try:
128
 
            f.write(content)
129
 
        finally:
130
 
            f.close()
131
 
 
132
 
    def _fancy_rename(self, a, b):
133
 
        osutils.fancy_rename(a, b, rename_func=os.rename,
134
 
                             unlink_func=os.unlink)
135
 
 
136
129
    def test_fancy_rename(self):
137
130
        # This should work everywhere
138
 
        self.create_file('a', 'something in a\n')
139
 
        self._fancy_rename('a', 'b')
 
131
        def rename(a, b):
 
132
            osutils.fancy_rename(a, b,
 
133
                    rename_func=os.rename,
 
134
                    unlink_func=os.unlink)
 
135
 
 
136
        open('a', 'wb').write('something in a\n')
 
137
        rename('a', 'b')
140
138
        self.failIfExists('a')
141
139
        self.failUnlessExists('b')
142
140
        self.check_file_contents('b', 'something in a\n')
143
141
 
144
 
        self.create_file('a', 'new something in a\n')
145
 
        self._fancy_rename('b', 'a')
 
142
        open('a', 'wb').write('new something in a\n')
 
143
        rename('b', 'a')
146
144
 
147
145
        self.check_file_contents('a', 'something in a\n')
148
146
 
149
 
    def test_fancy_rename_fails_source_missing(self):
150
 
        # An exception should be raised, and the target should be left in place
151
 
        self.create_file('target', 'data in target\n')
152
 
        self.assertRaises((IOError, OSError), self._fancy_rename,
153
 
                          'missingsource', 'target')
154
 
        self.failUnlessExists('target')
155
 
        self.check_file_contents('target', 'data in target\n')
156
 
 
157
 
    def test_fancy_rename_fails_if_source_and_target_missing(self):
158
 
        self.assertRaises((IOError, OSError), self._fancy_rename,
159
 
                          'missingsource', 'missingtarget')
160
 
 
161
147
    def test_rename(self):
162
148
        # Rename should be semi-atomic on all platforms
163
 
        self.create_file('a', 'something in a\n')
 
149
        open('a', 'wb').write('something in a\n')
164
150
        osutils.rename('a', 'b')
165
151
        self.failIfExists('a')
166
152
        self.failUnlessExists('b')
167
153
        self.check_file_contents('b', 'something in a\n')
168
154
 
169
 
        self.create_file('a', 'new something in a\n')
 
155
        open('a', 'wb').write('new something in a\n')
170
156
        osutils.rename('b', 'a')
171
157
 
172
158
        self.check_file_contents('a', 'something in a\n')
380
366
        # Instead blackbox.test_locale should check for localized
381
367
        # dates once they do occur in output strings.
382
368
 
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
369
    def test_local_time_offset(self):
392
370
        """Test that local_time_offset() returns a sane value."""
393
371
        offset = osutils.local_time_offset()
989
967
 
990
968
    def test_osutils_binding(self):
991
969
        from bzrlib.tests import test__chunks_to_lines
992
 
        if test__chunks_to_lines.compiled_chunkstolines_feature.available():
 
970
        if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
993
971
            from bzrlib._chunks_to_lines_pyx import chunks_to_lines
994
972
        else:
995
973
            from bzrlib._chunks_to_lines_py import chunks_to_lines
1181
1159
 
1182
1160
    def test_force_walkdirs_utf8_nt(self):
1183
1161
        # Disabled because the thunk of the whole walkdirs api is disabled.
1184
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1162
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1185
1163
        self._save_platform_info()
1186
1164
        win32utils.winver = 'Windows NT'
1187
1165
        from bzrlib._walkdirs_win32 import Win32ReadDir
1188
1166
        self.assertDirReaderIs(Win32ReadDir)
1189
1167
 
1190
1168
    def test_force_walkdirs_utf8_98(self):
1191
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1169
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1192
1170
        self._save_platform_info()
1193
1171
        win32utils.winver = 'Windows 98'
1194
1172
        self.assertDirReaderIs(osutils.UnicodeDirReader)
1345
1323
        self.assertEqual(expected_dirblocks, result)
1346
1324
 
1347
1325
    def test__walkdirs_utf8_win32readdir(self):
1348
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1326
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1349
1327
        self.requireFeature(tests.UnicodeFilenameFeature)
1350
1328
        from bzrlib._walkdirs_win32 import Win32ReadDir
1351
1329
        self._save_platform_info()
1402
1380
 
1403
1381
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1404
1382
        """make sure our Stat values are valid"""
1405
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1383
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1406
1384
        self.requireFeature(tests.UnicodeFilenameFeature)
1407
1385
        from bzrlib._walkdirs_win32 import Win32ReadDir
1408
1386
        name0u = u'0file-\xb6'
1426
1404
 
1427
1405
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1428
1406
        """make sure our Stat values are valid"""
1429
 
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
1407
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1430
1408
        self.requireFeature(tests.UnicodeFilenameFeature)
1431
1409
        from bzrlib._walkdirs_win32 import Win32ReadDir
1432
1410
        name0u = u'0dir-\u062c\u0648'
1635
1613
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1636
1614
        self.build_tree_contents([('foo', text)])
1637
1615
        expected_sha = osutils.sha_string(text)
1638
 
        f = open('foo', 'rb')
 
1616
        f = open('foo')
1639
1617
        self.addCleanup(f.close)
1640
1618
        size, sha = osutils.size_sha_file(f)
1641
1619
        self.assertEqual(38, size)
1857
1835
 
1858
1836
class TestConcurrency(tests.TestCase):
1859
1837
 
1860
 
    def setUp(self):
1861
 
        super(TestConcurrency, self).setUp()
1862
 
        orig = osutils._cached_local_concurrency
1863
 
        def restore():
1864
 
            osutils._cached_local_concurrency = orig
1865
 
        self.addCleanup(restore)
1866
 
 
1867
1838
    def test_local_concurrency(self):
1868
1839
        concurrency = osutils.local_concurrency()
1869
1840
        self.assertIsInstance(concurrency, int)
1870
1841
 
1871
 
    def test_local_concurrency_environment_variable(self):
1872
 
        os.environ['BZR_CONCURRENCY'] = '2'
1873
 
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1874
 
        os.environ['BZR_CONCURRENCY'] = '3'
1875
 
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1876
 
        os.environ['BZR_CONCURRENCY'] = 'foo'
1877
 
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1878
 
 
1879
 
    def test_option_concurrency(self):
1880
 
        os.environ['BZR_CONCURRENCY'] = '1'
1881
 
        self.run_bzr('rocks --concurrency 42')
1882
 
        # Command line overrides envrionment variable
1883
 
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1884
 
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1885
 
 
1886
1842
 
1887
1843
class TestFailedToLoadExtension(tests.TestCase):
1888
1844
 
1924
1880
            r"bzr: warning: some compiled extensions could not be loaded; "
1925
1881
            "see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1926
1882
            )
1927
 
 
1928
 
 
1929
 
class TestTerminalWidth(tests.TestCase):
1930
 
 
1931
 
    def replace_stdout(self, new):
1932
 
        orig_stdout = sys.stdout
1933
 
        def restore():
1934
 
            sys.stdout = orig_stdout
1935
 
        self.addCleanup(restore)
1936
 
        sys.stdout = new
1937
 
 
1938
 
    def replace__terminal_size(self, new):
1939
 
        orig__terminal_size = osutils._terminal_size
1940
 
        def restore():
1941
 
            osutils._terminal_size = orig__terminal_size
1942
 
        self.addCleanup(restore)
1943
 
        osutils._terminal_size = new
1944
 
 
1945
 
    def set_fake_tty(self):
1946
 
 
1947
 
        class I_am_a_tty(object):
1948
 
            def isatty(self):
1949
 
                return True
1950
 
 
1951
 
        self.replace_stdout(I_am_a_tty())
1952
 
 
1953
 
    def test_default_values(self):
1954
 
        self.assertEqual(80, osutils.default_terminal_width)
1955
 
 
1956
 
    def test_defaults_to_BZR_COLUMNS(self):
1957
 
        # BZR_COLUMNS is set by the test framework
1958
 
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1959
 
        os.environ['BZR_COLUMNS'] = '12'
1960
 
        self.assertEqual(12, osutils.terminal_width())
1961
 
 
1962
 
    def test_falls_back_to_COLUMNS(self):
1963
 
        del os.environ['BZR_COLUMNS']
1964
 
        self.assertNotEqual('42', os.environ['COLUMNS'])
1965
 
        self.set_fake_tty()
1966
 
        os.environ['COLUMNS'] = '42'
1967
 
        self.assertEqual(42, osutils.terminal_width())
1968
 
 
1969
 
    def test_tty_default_without_columns(self):
1970
 
        del os.environ['BZR_COLUMNS']
1971
 
        del os.environ['COLUMNS']
1972
 
 
1973
 
        def terminal_size(w, h):
1974
 
            return 42, 42
1975
 
 
1976
 
        self.set_fake_tty()
1977
 
        # We need to override the osutils definition as it depends on the
1978
 
        # running environment that we can't control (PQM running without a
1979
 
        # controlling terminal is one example).
1980
 
        self.replace__terminal_size(terminal_size)
1981
 
        self.assertEqual(42, osutils.terminal_width())
1982
 
 
1983
 
    def test_non_tty_default_without_columns(self):
1984
 
        del os.environ['BZR_COLUMNS']
1985
 
        del os.environ['COLUMNS']
1986
 
        self.replace_stdout(None)
1987
 
        self.assertEqual(None, osutils.terminal_width())
1988
 
 
1989
 
    def test_no_TIOCGWINSZ(self):
1990
 
        self.requireFeature(term_ios_feature)
1991
 
        termios = term_ios_feature.module
1992
 
        # bug 63539 is about a termios without TIOCGWINSZ attribute
1993
 
        try:
1994
 
            orig = termios.TIOCGWINSZ
1995
 
        except AttributeError:
1996
 
            # We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
1997
 
            pass
1998
 
        else:
1999
 
            def restore():
2000
 
                termios.TIOCGWINSZ = orig
2001
 
            self.addCleanup(restore)
2002
 
            del termios.TIOCGWINSZ
2003
 
        del os.environ['BZR_COLUMNS']
2004
 
        del os.environ['COLUMNS']
2005
 
        # Whatever the result is, if we don't raise an exception, it's ok.
2006
 
        osutils.terminal_width()