354
253
self.assertEqual("@", osutils.kind_marker("symlink"))
355
254
self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
357
def test_host_os_dereferences_symlinks(self):
358
osutils.host_os_dereferences_symlinks()
361
class TestCanonicalRelPath(TestCaseInTempDir):
363
_test_needs_features = [CaseInsCasePresFilenameFeature]
365
def test_canonical_relpath_simple(self):
366
f = file('MixedCaseName', 'w')
368
self.failUnlessEqual(
369
canonical_relpath(self.test_base_dir, 'mixedcasename'),
370
'work/MixedCaseName')
372
def test_canonical_relpath_missing_tail(self):
373
os.mkdir('MixedCaseParent')
374
self.failUnlessEqual(
375
canonical_relpath(self.test_base_dir, 'mixedcaseparent/nochild'),
376
'work/MixedCaseParent/nochild')
379
class TestPumpFile(TestCase):
380
"""Test pumpfile method."""
382
# create a test datablock
383
self.block_size = 512
384
pattern = '0123456789ABCDEF'
385
self.test_data = pattern * (3 * self.block_size / len(pattern))
386
self.test_data_len = len(self.test_data)
388
def test_bracket_block_size(self):
389
"""Read data in blocks with the requested read size bracketing the
391
# make sure test data is larger than max read size
392
self.assertTrue(self.test_data_len > self.block_size)
394
from_file = FakeReadFile(self.test_data)
397
# read (max / 2) bytes and verify read size wasn't affected
398
num_bytes_to_read = self.block_size / 2
399
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
400
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
401
self.assertEqual(from_file.get_read_count(), 1)
403
# read (max) bytes and verify read size wasn't affected
404
num_bytes_to_read = self.block_size
405
from_file.reset_read_count()
406
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
407
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
408
self.assertEqual(from_file.get_read_count(), 1)
410
# read (max + 1) bytes and verify read size was limited
411
num_bytes_to_read = self.block_size + 1
412
from_file.reset_read_count()
413
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
414
self.assertEqual(from_file.get_max_read_size(), self.block_size)
415
self.assertEqual(from_file.get_read_count(), 2)
417
# finish reading the rest of the data
418
num_bytes_to_read = self.test_data_len - to_file.tell()
419
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
421
# report error if the data wasn't equal (we only report the size due
422
# to the length of the data)
423
response_data = to_file.getvalue()
424
if response_data != self.test_data:
425
message = "Data not equal. Expected %d bytes, received %d."
426
self.fail(message % (len(response_data), self.test_data_len))
428
def test_specified_size(self):
429
"""Request a transfer larger than the maximum block size and verify
430
that the maximum read doesn't exceed the block_size."""
431
# make sure test data is larger than max read size
432
self.assertTrue(self.test_data_len > self.block_size)
434
# retrieve data in blocks
435
from_file = FakeReadFile(self.test_data)
437
pumpfile(from_file, to_file, self.test_data_len, self.block_size)
439
# verify read size was equal to the maximum read size
440
self.assertTrue(from_file.get_max_read_size() > 0)
441
self.assertEqual(from_file.get_max_read_size(), self.block_size)
442
self.assertEqual(from_file.get_read_count(), 3)
444
# report error if the data wasn't equal (we only report the size due
445
# to the length of the data)
446
response_data = to_file.getvalue()
447
if response_data != self.test_data:
448
message = "Data not equal. Expected %d bytes, received %d."
449
self.fail(message % (len(response_data), self.test_data_len))
451
def test_to_eof(self):
452
"""Read to end-of-file and verify that the reads are not larger than
453
the maximum read size."""
454
# make sure test data is larger than max read size
455
self.assertTrue(self.test_data_len > self.block_size)
457
# retrieve data to EOF
458
from_file = FakeReadFile(self.test_data)
460
pumpfile(from_file, to_file, -1, self.block_size)
462
# verify read size was equal to the maximum read size
463
self.assertEqual(from_file.get_max_read_size(), self.block_size)
464
self.assertEqual(from_file.get_read_count(), 4)
466
# report error if the data wasn't equal (we only report the size due
467
# to the length of the data)
468
response_data = to_file.getvalue()
469
if response_data != self.test_data:
470
message = "Data not equal. Expected %d bytes, received %d."
471
self.fail(message % (len(response_data), self.test_data_len))
473
def test_defaults(self):
474
"""Verifies that the default arguments will read to EOF -- this
475
test verifies that any existing usages of pumpfile will not be broken
476
with this new version."""
477
# retrieve data using default (old) pumpfile method
478
from_file = FakeReadFile(self.test_data)
480
pumpfile(from_file, to_file)
482
# report error if the data wasn't equal (we only report the size due
483
# to the length of the data)
484
response_data = to_file.getvalue()
485
if response_data != self.test_data:
486
message = "Data not equal. Expected %d bytes, received %d."
487
self.fail(message % (len(response_data), self.test_data_len))
489
def test_report_activity(self):
491
def log_activity(length, direction):
492
activity.append((length, direction))
493
from_file = StringIO(self.test_data)
495
pumpfile(from_file, to_file, buff_size=500,
496
report_activity=log_activity, direction='read')
497
self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
498
(36, 'read')], activity)
500
from_file = StringIO(self.test_data)
503
pumpfile(from_file, to_file, buff_size=500,
504
report_activity=log_activity, direction='write')
505
self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
506
(36, 'write')], activity)
508
# And with a limited amount of data
509
from_file = StringIO(self.test_data)
512
pumpfile(from_file, to_file, buff_size=500, read_length=1028,
513
report_activity=log_activity, direction='read')
514
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
518
class TestPumpStringFile(TestCase):
520
def test_empty(self):
522
pump_string_file("", output)
523
self.assertEqual("", output.getvalue())
525
def test_more_than_segment_size(self):
527
pump_string_file("123456789", output, 2)
528
self.assertEqual("123456789", output.getvalue())
530
def test_segment_size(self):
532
pump_string_file("12", output, 2)
533
self.assertEqual("12", output.getvalue())
535
def test_segment_size_multiple(self):
537
pump_string_file("1234", output, 2)
538
self.assertEqual("1234", output.getvalue())
541
257
class TestSafeUnicode(TestCase):
937
625
new_dirblock.append((info[0], info[1], info[2], info[4]))
938
626
dirblock[:] = new_dirblock
940
def _save_platform_info(self):
941
cur_winver = win32utils.winver
942
cur_fs_enc = osutils._fs_enc
943
cur_dir_reader = osutils._selected_dir_reader
945
win32utils.winver = cur_winver
946
osutils._fs_enc = cur_fs_enc
947
osutils._selected_dir_reader = cur_dir_reader
948
self.addCleanup(restore)
950
def assertReadFSDirIs(self, expected):
951
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
952
# Force it to redetect
953
osutils._selected_dir_reader = None
954
# Nothing to list, but should still trigger the selection logic
955
self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
956
self.assertIsInstance(osutils._selected_dir_reader, expected)
958
def test_force_walkdirs_utf8_fs_utf8(self):
959
self.requireFeature(UTF8DirReaderFeature)
960
self._save_platform_info()
961
win32utils.winver = None # Avoid the win32 detection code
962
osutils._fs_enc = 'UTF-8'
963
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
965
def test_force_walkdirs_utf8_fs_ascii(self):
966
self.requireFeature(UTF8DirReaderFeature)
967
self._save_platform_info()
968
win32utils.winver = None # Avoid the win32 detection code
969
osutils._fs_enc = 'US-ASCII'
970
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
972
def test_force_walkdirs_utf8_fs_ANSI(self):
973
self.requireFeature(UTF8DirReaderFeature)
974
self._save_platform_info()
975
win32utils.winver = None # Avoid the win32 detection code
976
osutils._fs_enc = 'ANSI_X3.4-1968'
977
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
979
def test_force_walkdirs_utf8_fs_latin1(self):
980
self._save_platform_info()
981
win32utils.winver = None # Avoid the win32 detection code
982
osutils._fs_enc = 'latin1'
983
self.assertReadFSDirIs(osutils.UnicodeDirReader)
985
def test_force_walkdirs_utf8_nt(self):
986
# Disabled because the thunk of the whole walkdirs api is disabled.
987
self.requireFeature(Win32ReadDirFeature)
988
self._save_platform_info()
989
win32utils.winver = 'Windows NT'
990
from bzrlib._walkdirs_win32 import Win32ReadDir
991
self.assertReadFSDirIs(Win32ReadDir)
993
def test_force_walkdirs_utf8_98(self):
994
self.requireFeature(Win32ReadDirFeature)
995
self._save_platform_info()
996
win32utils.winver = 'Windows 98'
997
self.assertReadFSDirIs(osutils.UnicodeDirReader)
999
628
def test_unicode_walkdirs(self):
1000
629
"""Walkdirs should always return unicode paths."""
1001
630
name0 = u'0file-\xb6'
1155
result = list(osutils._walkdirs_utf8('.'))
1156
self._filter_out_stat(result)
1157
self.assertEqual(expected_dirblocks, result)
1159
def test__walkdirs_utf8_win32readdir(self):
1160
self.requireFeature(Win32ReadDirFeature)
1161
self.requireFeature(tests.UnicodeFilenameFeature)
1162
from bzrlib._walkdirs_win32 import Win32ReadDir
1163
self._save_platform_info()
1164
osutils._selected_dir_reader = Win32ReadDir()
1165
name0u = u'0file-\xb6'
1166
name1u = u'1dir-\u062c\u0648'
1167
name2u = u'2file-\u0633'
1171
name1u + '/' + name0u,
1172
name1u + '/' + name1u + '/',
1175
self.build_tree(tree)
1176
name0 = name0u.encode('utf8')
1177
name1 = name1u.encode('utf8')
1178
name2 = name2u.encode('utf8')
1180
# All of the abspaths should be in unicode, all of the relative paths
1182
expected_dirblocks = [
1184
[(name0, name0, 'file', './' + name0u),
1185
(name1, name1, 'directory', './' + name1u),
1186
(name2, name2, 'file', './' + name2u),
1189
((name1, './' + name1u),
1190
[(name1 + '/' + name0, name0, 'file', './' + name1u
1192
(name1 + '/' + name1, name1, 'directory', './' + name1u
1196
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1201
result = list(osutils._walkdirs_utf8(u'.'))
1202
self._filter_out_stat(result)
1203
self.assertEqual(expected_dirblocks, result)
1205
def assertStatIsCorrect(self, path, win32stat):
1206
os_stat = os.stat(path)
1207
self.assertEqual(os_stat.st_size, win32stat.st_size)
1208
self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
1209
self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
1210
self.assertAlmostEqual(os_stat.st_atime, win32stat.st_atime, places=4)
1211
self.assertEqual(os_stat.st_dev, win32stat.st_dev)
1212
self.assertEqual(os_stat.st_ino, win32stat.st_ino)
1213
self.assertEqual(os_stat.st_mode, win32stat.st_mode)
1215
def test__walkdirs_utf_win32_find_file_stat_file(self):
1216
"""make sure our Stat values are valid"""
1217
self.requireFeature(Win32ReadDirFeature)
1218
self.requireFeature(tests.UnicodeFilenameFeature)
1219
from bzrlib._walkdirs_win32 import Win32ReadDir
1220
name0u = u'0file-\xb6'
1221
name0 = name0u.encode('utf8')
1222
self.build_tree([name0u])
1223
# I hate to sleep() here, but I'm trying to make the ctime different
1226
f = open(name0u, 'ab')
1228
f.write('just a small update')
1232
result = Win32ReadDir().read_dir('', u'.')
1234
self.assertEqual((name0, name0, 'file'), entry[:3])
1235
self.assertEqual(u'./' + name0u, entry[4])
1236
self.assertStatIsCorrect(entry[4], entry[3])
1237
self.assertNotEqual(entry[3].st_mtime, entry[3].st_ctime)
1239
def test__walkdirs_utf_win32_find_file_stat_directory(self):
1240
"""make sure our Stat values are valid"""
1241
self.requireFeature(Win32ReadDirFeature)
1242
self.requireFeature(tests.UnicodeFilenameFeature)
1243
from bzrlib._walkdirs_win32 import Win32ReadDir
1244
name0u = u'0dir-\u062c\u0648'
1245
name0 = name0u.encode('utf8')
1246
self.build_tree([name0u + '/'])
1248
result = Win32ReadDir().read_dir('', u'.')
1250
self.assertEqual((name0, name0, 'directory'), entry[:3])
1251
self.assertEqual(u'./' + name0u, entry[4])
1252
self.assertStatIsCorrect(entry[4], entry[3])
780
result = list(osutils._walkdirs_unicode_to_utf8('.'))
781
self._filter_out_stat(result)
782
self.assertEqual(expected_dirblocks, result)
1254
784
def assertPathCompare(self, path_less, path_greater):
1255
785
"""check that path_less and path_greater compare correctly."""
1455
996
self.assertTrue(isinstance(offset, int))
1456
997
eighteen_hours = 18 * 3600
1457
998
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1460
class TestShaFileByName(TestCaseInTempDir):
1462
def test_sha_empty(self):
1463
self.build_tree_contents([('foo', '')])
1464
expected_sha = osutils.sha_string('')
1465
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1467
def test_sha_mixed_endings(self):
1468
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1469
self.build_tree_contents([('foo', text)])
1470
expected_sha = osutils.sha_string(text)
1471
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1474
class TestResourceLoading(TestCaseInTempDir):
1476
def test_resource_string(self):
1477
# test resource in bzrlib
1478
text = osutils.resource_string('bzrlib', 'debug.py')
1479
self.assertContainsRe(text, "debug_flags = set()")
1480
# test resource under bzrlib
1481
text = osutils.resource_string('bzrlib.ui', 'text.py')
1482
self.assertContainsRe(text, "class TextUIFactory")
1483
# test unsupported package
1484
self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1486
# test unknown resource
1487
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')