82
84
dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
83
85
_native_to_unicode=_utf8_to_unicode)))
85
if test__walkdirs_win32.win32_readdir_feature.available():
87
if test__walkdirs_win32.Win32ReadDirFeature.available():
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
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:
123
127
class TestRename(tests.TestCaseInTempDir):
125
def create_file(self, filename, content):
126
f = open(filename, 'wb')
132
def _fancy_rename(self, a, b):
133
osutils.fancy_rename(a, b, rename_func=os.rename,
134
unlink_func=os.unlink)
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')
132
osutils.fancy_rename(a, b,
133
rename_func=os.rename,
134
unlink_func=os.unlink)
136
open('a', 'wb').write('something in a\n')
140
138
self.failIfExists('a')
141
139
self.failUnlessExists('b')
142
140
self.check_file_contents('b', 'something in a\n')
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')
147
145
self.check_file_contents('a', 'something in a\n')
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')
157
def test_fancy_rename_fails_if_source_and_target_missing(self):
158
self.assertRaises((IOError, OSError), self._fancy_rename,
159
'missingsource', 'missingtarget')
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')
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')
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.
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))
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()
1135
1113
dirblock[:] = new_dirblock
1137
1115
def _save_platform_info(self):
1138
self.overrideAttr(win32utils, 'winver')
1139
self.overrideAttr(osutils, '_fs_enc')
1140
self.overrideAttr(osutils, '_selected_dir_reader')
1116
cur_winver = win32utils.winver
1117
cur_fs_enc = osutils._fs_enc
1118
cur_dir_reader = osutils._selected_dir_reader
1120
win32utils.winver = cur_winver
1121
osutils._fs_enc = cur_fs_enc
1122
osutils._selected_dir_reader = cur_dir_reader
1123
self.addCleanup(restore)
1142
1125
def assertDirReaderIs(self, expected):
1143
1126
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
1177
1160
def test_force_walkdirs_utf8_nt(self):
1178
1161
# Disabled because the thunk of the whole walkdirs api is disabled.
1179
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1162
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1180
1163
self._save_platform_info()
1181
1164
win32utils.winver = 'Windows NT'
1182
1165
from bzrlib._walkdirs_win32 import Win32ReadDir
1183
1166
self.assertDirReaderIs(Win32ReadDir)
1185
1168
def test_force_walkdirs_utf8_98(self):
1186
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1169
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1187
1170
self._save_platform_info()
1188
1171
win32utils.winver = 'Windows 98'
1189
1172
self.assertDirReaderIs(osutils.UnicodeDirReader)
1340
1323
self.assertEqual(expected_dirblocks, result)
1342
1325
def test__walkdirs_utf8_win32readdir(self):
1343
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1326
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1344
1327
self.requireFeature(tests.UnicodeFilenameFeature)
1345
1328
from bzrlib._walkdirs_win32 import Win32ReadDir
1346
1329
self._save_platform_info()
1422
1405
def test__walkdirs_utf_win32_find_file_stat_directory(self):
1423
1406
"""make sure our Stat values are valid"""
1424
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1407
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1425
1408
self.requireFeature(tests.UnicodeFilenameFeature)
1426
1409
from bzrlib._walkdirs_win32 import Win32ReadDir
1427
1410
name0u = u'0dir-\u062c\u0648'
1693
1677
def setUp(self):
1694
1678
tests.TestCaseInTempDir.setUp(self)
1695
self.overrideAttr(osutils,
1696
'_selected_dir_reader', self._dir_reader_class())
1680
# Save platform specific info and reset it
1681
cur_dir_reader = osutils._selected_dir_reader
1684
osutils._selected_dir_reader = cur_dir_reader
1685
self.addCleanup(restore)
1687
osutils._selected_dir_reader = self._dir_reader_class()
1698
1689
def _get_ascii_tree(self):
1845
1836
class TestConcurrency(tests.TestCase):
1848
super(TestConcurrency, self).setUp()
1849
self.overrideAttr(osutils, '_cached_local_concurrency')
1851
1838
def test_local_concurrency(self):
1852
1839
concurrency = osutils.local_concurrency()
1853
1840
self.assertIsInstance(concurrency, int)
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))
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))
1871
1843
class TestFailedToLoadExtension(tests.TestCase):
1880
1852
def setUp(self):
1881
1853
super(TestFailedToLoadExtension, self).setUp()
1882
self.overrideAttr(osutils, '_extension_load_failures', [])
1854
self.saved_failures = osutils._extension_load_failures[:]
1855
del osutils._extension_load_failures[:]
1856
self.addCleanup(self.restore_failures)
1858
def restore_failures(self):
1859
osutils._extension_load_failures = self.saved_failures
1884
1861
def test_failure_to_load(self):
1885
1862
self._try_loading()
1903
1880
r"bzr: warning: some compiled extensions could not be loaded; "
1904
1881
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1908
class TestTerminalWidth(tests.TestCase):
1910
def replace_stdout(self, new):
1911
self.overrideAttr(sys, 'stdout', new)
1913
def replace__terminal_size(self, new):
1914
self.overrideAttr(osutils, '_terminal_size', new)
1916
def set_fake_tty(self):
1918
class I_am_a_tty(object):
1922
self.replace_stdout(I_am_a_tty())
1924
def test_default_values(self):
1925
self.assertEqual(80, osutils.default_terminal_width)
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())
1933
def test_falls_back_to_COLUMNS(self):
1934
del os.environ['BZR_COLUMNS']
1935
self.assertNotEqual('42', os.environ['COLUMNS'])
1937
os.environ['COLUMNS'] = '42'
1938
self.assertEqual(42, osutils.terminal_width())
1940
def test_tty_default_without_columns(self):
1941
del os.environ['BZR_COLUMNS']
1942
del os.environ['COLUMNS']
1944
def terminal_size(w, h):
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())
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())
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
1965
orig = termios.TIOCGWINSZ
1966
except AttributeError:
1967
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
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()