13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for the osutils wrapper."""
19
from cStringIO import StringIO
26
28
from bzrlib import (
31
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
32
34
from bzrlib.tests import (
40
class TestOSUtils(TestCaseInTempDir):
40
class _UTF8DirReaderFeature(tests.Feature):
44
from bzrlib import _readdir_pyx
45
self.reader = _readdir_pyx.UTF8DirReader
50
def feature_name(self):
51
return 'bzrlib._readdir_pyx'
53
UTF8DirReaderFeature = _UTF8DirReaderFeature()
56
def _already_unicode(s):
60
def _fs_enc_to_unicode(s):
61
return s.decode(osutils._fs_enc)
64
def _utf8_to_unicode(s):
65
return s.decode('UTF-8')
68
def dir_reader_scenarios():
69
# For each dir reader we define:
71
# - native_to_unicode: a function converting the native_abspath as returned
72
# by DirReader.read_dir to its unicode representation
74
# UnicodeDirReader is the fallback, it should be tested on all platforms.
75
scenarios = [('unicode',
76
dict(_dir_reader_class=osutils.UnicodeDirReader,
77
_native_to_unicode=_already_unicode))]
78
# Some DirReaders are platform specific and even there they may not be
80
if UTF8DirReaderFeature.available():
81
from bzrlib import _readdir_pyx
82
scenarios.append(('utf8',
83
dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
84
_native_to_unicode=_utf8_to_unicode)))
86
if test__walkdirs_win32.Win32ReadDirFeature.available():
88
from bzrlib import _walkdirs_win32
89
# TODO: check on windows, it may be that we need to use/add
90
# safe_unicode instead of _fs_enc_to_unicode
93
dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
94
_native_to_unicode=_fs_enc_to_unicode)))
100
def load_tests(basic_tests, module, loader):
101
suite = loader.suiteClass()
102
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
103
basic_tests, tests.condition_isinstance(TestDirReader))
104
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
105
suite.addTest(remaining_tests)
109
class TestContainsWhitespace(tests.TestCase):
42
111
def test_contains_whitespace(self):
43
112
self.failUnless(osutils.contains_whitespace(u' '))
218
343
self.assertFormatedDelta('1 second in the future', -1)
219
344
self.assertFormatedDelta('2 seconds in the future', -2)
346
def test_format_date(self):
347
self.assertRaises(errors.UnsupportedTimezoneFormat,
348
osutils.format_date, 0, timezone='foo')
349
self.assertIsInstance(osutils.format_date(0), str)
350
self.assertIsInstance(osutils.format_local_date(0), unicode)
351
# Testing for the actual value of the local weekday without
352
# duplicating the code from format_date is difficult.
353
# Instead blackbox.test_locale should check for localized
354
# dates once they do occur in output strings.
356
def test_local_time_offset(self):
357
"""Test that local_time_offset() returns a sane value."""
358
offset = osutils.local_time_offset()
359
self.assertTrue(isinstance(offset, int))
360
# Test that the offset is no more than a eighteen hours in
362
# Time zone handling is system specific, so it is difficult to
363
# do more specific tests, but a value outside of this range is
365
eighteen_hours = 18 * 3600
366
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
368
def test_local_time_offset_with_timestamp(self):
369
"""Test that local_time_offset() works with a timestamp."""
370
offset = osutils.local_time_offset(1000000000.1234567)
371
self.assertTrue(isinstance(offset, int))
372
eighteen_hours = 18 * 3600
373
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
376
class TestLinks(tests.TestCaseInTempDir):
221
378
def test_dereference_path(self):
222
if not osutils.has_symlinks():
223
raise TestSkipped('Symlinks are not supported on this platform')
379
self.requireFeature(tests.SymlinkFeature)
224
380
cwd = osutils.realpath('.')
226
382
bar_path = osutils.pathjoin(cwd, 'bar')
268
423
osutils.make_readonly('dangling')
269
424
osutils.make_writable('dangling')
272
def test_kind_marker(self):
273
self.assertEqual("", osutils.kind_marker("file"))
274
self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
275
self.assertEqual("@", osutils.kind_marker("symlink"))
276
self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
279
class TestSafeUnicode(TestCase):
426
def test_host_os_dereferences_symlinks(self):
427
osutils.host_os_dereferences_symlinks()
430
class TestCanonicalRelPath(tests.TestCaseInTempDir):
432
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
434
def test_canonical_relpath_simple(self):
435
f = file('MixedCaseName', 'w')
437
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
438
real_base_dir = osutils.realpath(self.test_base_dir)
439
actual = osutils.canonical_relpath(real_base_dir, 'mixedcasename')
440
self.failUnlessEqual('work/MixedCaseName', actual)
442
def test_canonical_relpath_missing_tail(self):
443
os.mkdir('MixedCaseParent')
444
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
445
real_base_dir = osutils.realpath(self.test_base_dir)
446
actual = osutils.canonical_relpath(real_base_dir,
447
'mixedcaseparent/nochild')
448
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
451
class TestPumpFile(tests.TestCase):
452
"""Test pumpfile method."""
455
tests.TestCase.setUp(self)
456
# create a test datablock
457
self.block_size = 512
458
pattern = '0123456789ABCDEF'
459
self.test_data = pattern * (3 * self.block_size / len(pattern))
460
self.test_data_len = len(self.test_data)
462
def test_bracket_block_size(self):
463
"""Read data in blocks with the requested read size bracketing the
465
# make sure test data is larger than max read size
466
self.assertTrue(self.test_data_len > self.block_size)
468
from_file = file_utils.FakeReadFile(self.test_data)
471
# read (max / 2) bytes and verify read size wasn't affected
472
num_bytes_to_read = self.block_size / 2
473
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
474
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
475
self.assertEqual(from_file.get_read_count(), 1)
477
# read (max) bytes and verify read size wasn't affected
478
num_bytes_to_read = self.block_size
479
from_file.reset_read_count()
480
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
481
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
482
self.assertEqual(from_file.get_read_count(), 1)
484
# read (max + 1) bytes and verify read size was limited
485
num_bytes_to_read = self.block_size + 1
486
from_file.reset_read_count()
487
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
488
self.assertEqual(from_file.get_max_read_size(), self.block_size)
489
self.assertEqual(from_file.get_read_count(), 2)
491
# finish reading the rest of the data
492
num_bytes_to_read = self.test_data_len - to_file.tell()
493
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
495
# report error if the data wasn't equal (we only report the size due
496
# to the length of the data)
497
response_data = to_file.getvalue()
498
if response_data != self.test_data:
499
message = "Data not equal. Expected %d bytes, received %d."
500
self.fail(message % (len(response_data), self.test_data_len))
502
def test_specified_size(self):
503
"""Request a transfer larger than the maximum block size and verify
504
that the maximum read doesn't exceed the block_size."""
505
# make sure test data is larger than max read size
506
self.assertTrue(self.test_data_len > self.block_size)
508
# retrieve data in blocks
509
from_file = file_utils.FakeReadFile(self.test_data)
511
osutils.pumpfile(from_file, to_file, self.test_data_len,
514
# verify read size was equal to the maximum read size
515
self.assertTrue(from_file.get_max_read_size() > 0)
516
self.assertEqual(from_file.get_max_read_size(), self.block_size)
517
self.assertEqual(from_file.get_read_count(), 3)
519
# report error if the data wasn't equal (we only report the size due
520
# to the length of the data)
521
response_data = to_file.getvalue()
522
if response_data != self.test_data:
523
message = "Data not equal. Expected %d bytes, received %d."
524
self.fail(message % (len(response_data), self.test_data_len))
526
def test_to_eof(self):
527
"""Read to end-of-file and verify that the reads are not larger than
528
the maximum read size."""
529
# make sure test data is larger than max read size
530
self.assertTrue(self.test_data_len > self.block_size)
532
# retrieve data to EOF
533
from_file = file_utils.FakeReadFile(self.test_data)
535
osutils.pumpfile(from_file, to_file, -1, self.block_size)
537
# verify read size was equal to the maximum read size
538
self.assertEqual(from_file.get_max_read_size(), self.block_size)
539
self.assertEqual(from_file.get_read_count(), 4)
541
# report error if the data wasn't equal (we only report the size due
542
# to the length of the data)
543
response_data = to_file.getvalue()
544
if response_data != self.test_data:
545
message = "Data not equal. Expected %d bytes, received %d."
546
self.fail(message % (len(response_data), self.test_data_len))
548
def test_defaults(self):
549
"""Verifies that the default arguments will read to EOF -- this
550
test verifies that any existing usages of pumpfile will not be broken
551
with this new version."""
552
# retrieve data using default (old) pumpfile method
553
from_file = file_utils.FakeReadFile(self.test_data)
555
osutils.pumpfile(from_file, to_file)
557
# report error if the data wasn't equal (we only report the size due
558
# to the length of the data)
559
response_data = to_file.getvalue()
560
if response_data != self.test_data:
561
message = "Data not equal. Expected %d bytes, received %d."
562
self.fail(message % (len(response_data), self.test_data_len))
564
def test_report_activity(self):
566
def log_activity(length, direction):
567
activity.append((length, direction))
568
from_file = StringIO(self.test_data)
570
osutils.pumpfile(from_file, to_file, buff_size=500,
571
report_activity=log_activity, direction='read')
572
self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
573
(36, 'read')], activity)
575
from_file = StringIO(self.test_data)
578
osutils.pumpfile(from_file, to_file, buff_size=500,
579
report_activity=log_activity, direction='write')
580
self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
581
(36, 'write')], activity)
583
# And with a limited amount of data
584
from_file = StringIO(self.test_data)
587
osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
588
report_activity=log_activity, direction='read')
589
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
593
class TestPumpStringFile(tests.TestCase):
595
def test_empty(self):
597
osutils.pump_string_file("", output)
598
self.assertEqual("", output.getvalue())
600
def test_more_than_segment_size(self):
602
osutils.pump_string_file("123456789", output, 2)
603
self.assertEqual("123456789", output.getvalue())
605
def test_segment_size(self):
607
osutils.pump_string_file("12", output, 2)
608
self.assertEqual("12", output.getvalue())
610
def test_segment_size_multiple(self):
612
osutils.pump_string_file("1234", output, 2)
613
self.assertEqual("1234", output.getvalue())
616
class TestSafeUnicode(tests.TestCase):
281
618
def test_from_ascii_string(self):
282
619
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
383
721
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
385
723
def test_pathjoin(self):
386
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
387
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
388
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
389
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
390
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
391
self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
724
self.assertEqual('path/to/foo',
725
osutils._win32_pathjoin('path', 'to', 'foo'))
726
self.assertEqual('C:/foo',
727
osutils._win32_pathjoin('path\\to', 'C:\\foo'))
728
self.assertEqual('C:/foo',
729
osutils._win32_pathjoin('path/to', 'C:/foo'))
730
self.assertEqual('path/to/foo',
731
osutils._win32_pathjoin('path/to/', 'foo'))
732
self.assertEqual('/foo',
733
osutils._win32_pathjoin('C:/path/to/', '/foo'))
734
self.assertEqual('/foo',
735
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
393
737
def test_normpath(self):
394
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
395
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
738
self.assertEqual('path/to/foo',
739
osutils._win32_normpath(r'path\\from\..\to\.\foo'))
740
self.assertEqual('path/to/foo',
741
osutils._win32_normpath('path//from/../to/./foo'))
397
743
def test_getcwd(self):
398
744
cwd = osutils._win32_getcwd()
506
860
self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
509
class TestMacFuncsDirs(TestCaseInTempDir):
863
class TestParentDirectories(tests.TestCaseInTempDir):
864
"""Test osutils.parent_directories()"""
866
def test_parent_directories(self):
867
self.assertEqual([], osutils.parent_directories('a'))
868
self.assertEqual(['a'], osutils.parent_directories('a/b'))
869
self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
872
class TestMacFuncsDirs(tests.TestCaseInTempDir):
510
873
"""Test mac special functions that require directories."""
512
875
def test_getcwd(self):
513
# On Mac, this will actually create Ba\u030agfors
514
# but chdir will still work, because it accepts both paths
516
os.mkdir(u'B\xe5gfors')
518
raise TestSkipped("Unable to create Unicode filename")
876
self.requireFeature(tests.UnicodeFilenameFeature)
877
os.mkdir(u'B\xe5gfors')
520
878
os.chdir(u'B\xe5gfors')
521
879
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
523
881
def test_getcwd_nonnorm(self):
882
self.requireFeature(tests.UnicodeFilenameFeature)
524
883
# Test that _mac_getcwd() will normalize this path
526
os.mkdir(u'Ba\u030agfors')
528
raise TestSkipped("Unable to create Unicode filename")
884
os.mkdir(u'Ba\u030agfors')
530
885
os.chdir(u'Ba\u030agfors')
531
886
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
534
class TestSplitLines(TestCase):
889
class TestChunksToLines(tests.TestCase):
891
def test_smoketest(self):
892
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
893
osutils.chunks_to_lines(['foo\nbar', '\nbaz\n']))
894
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
895
osutils.chunks_to_lines(['foo\n', 'bar\n', 'baz\n']))
897
def test_osutils_binding(self):
898
from bzrlib.tests import test__chunks_to_lines
899
if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
900
from bzrlib._chunks_to_lines_pyx import chunks_to_lines
902
from bzrlib._chunks_to_lines_py import chunks_to_lines
903
self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
906
class TestSplitLines(tests.TestCase):
536
908
def test_split_unicode(self):
537
909
self.assertEqual([u'foo\n', u'bar\xae'],
583
960
result.append((dirdetail, dirblock))
585
962
self.assertTrue(found_bzrdir)
586
self.assertEqual(expected_dirblocks,
587
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
963
self.assertExpectedBlocks(expected_dirblocks, result)
588
964
# you can search a subdir only, with a supplied prefix.
590
966
for dirblock in osutils.walkdirs('./1dir', '1dir'):
591
967
result.append(dirblock)
592
self.assertEqual(expected_dirblocks[1:],
593
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
968
self.assertExpectedBlocks(expected_dirblocks[1:], result)
970
def test_walkdirs_os_error(self):
971
# <https://bugs.edge.launchpad.net/bzr/+bug/338653>
972
# Pyrex readdir didn't raise useful messages if it had an error
973
# reading the directory
974
if sys.platform == 'win32':
975
raise tests.TestNotApplicable(
976
"readdir IOError not tested on win32")
977
os.mkdir("test-unreadable")
978
os.chmod("test-unreadable", 0000)
979
# must chmod it back so that it can be removed
980
self.addCleanup(os.chmod, "test-unreadable", 0700)
981
# The error is not raised until the generator is actually evaluated.
982
# (It would be ok if it happened earlier but at the moment it
984
e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
985
self.assertEquals('./test-unreadable', e.filename)
986
self.assertEquals(errno.EACCES, e.errno)
987
# Ensure the message contains the file name
988
self.assertContainsRe(str(e), "\./test-unreadable")
595
990
def test__walkdirs_utf8(self):
647
1041
new_dirblock.append((info[0], info[1], info[2], info[4]))
648
1042
dirblock[:] = new_dirblock
1044
def _save_platform_info(self):
1045
cur_winver = win32utils.winver
1046
cur_fs_enc = osutils._fs_enc
1047
cur_dir_reader = osutils._selected_dir_reader
1049
win32utils.winver = cur_winver
1050
osutils._fs_enc = cur_fs_enc
1051
osutils._selected_dir_reader = cur_dir_reader
1052
self.addCleanup(restore)
1054
def assertDirReaderIs(self, expected):
1055
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
1056
# Force it to redetect
1057
osutils._selected_dir_reader = None
1058
# Nothing to list, but should still trigger the selection logic
1059
self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
1060
self.assertIsInstance(osutils._selected_dir_reader, expected)
1062
def test_force_walkdirs_utf8_fs_utf8(self):
1063
self.requireFeature(UTF8DirReaderFeature)
1064
self._save_platform_info()
1065
win32utils.winver = None # Avoid the win32 detection code
1066
osutils._fs_enc = 'UTF-8'
1067
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1069
def test_force_walkdirs_utf8_fs_ascii(self):
1070
self.requireFeature(UTF8DirReaderFeature)
1071
self._save_platform_info()
1072
win32utils.winver = None # Avoid the win32 detection code
1073
osutils._fs_enc = 'US-ASCII'
1074
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1076
def test_force_walkdirs_utf8_fs_ANSI(self):
1077
self.requireFeature(UTF8DirReaderFeature)
1078
self._save_platform_info()
1079
win32utils.winver = None # Avoid the win32 detection code
1080
osutils._fs_enc = 'ANSI_X3.4-1968'
1081
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1083
def test_force_walkdirs_utf8_fs_latin1(self):
1084
self._save_platform_info()
1085
win32utils.winver = None # Avoid the win32 detection code
1086
osutils._fs_enc = 'latin1'
1087
self.assertDirReaderIs(osutils.UnicodeDirReader)
1089
def test_force_walkdirs_utf8_nt(self):
1090
# Disabled because the thunk of the whole walkdirs api is disabled.
1091
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1092
self._save_platform_info()
1093
win32utils.winver = 'Windows NT'
1094
from bzrlib._walkdirs_win32 import Win32ReadDir
1095
self.assertDirReaderIs(Win32ReadDir)
1097
def test_force_walkdirs_utf8_98(self):
1098
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1099
self._save_platform_info()
1100
win32utils.winver = 'Windows 98'
1101
self.assertDirReaderIs(osutils.UnicodeDirReader)
650
1103
def test_unicode_walkdirs(self):
651
1104
"""Walkdirs should always return unicode paths."""
1105
self.requireFeature(tests.UnicodeFilenameFeature)
652
1106
name0 = u'0file-\xb6'
653
1107
name1 = u'1dir-\u062c\u0648'
654
1108
name2 = u'2file-\u0633'
754
1201
result.append((dirdetail, new_dirblock))
755
1202
self.assertEqual(expected_dirblocks, result)
757
def test_unicode__walkdirs_unicode_to_utf8(self):
758
"""walkdirs_unicode_to_utf8 should be a safe fallback everywhere
1204
def test__walkdirs_utf8_with_unicode_fs(self):
1205
"""UnicodeDirReader should be a safe fallback everywhere
760
1207
The abspath portion should be in unicode
762
name0u = u'0file-\xb6'
763
name1u = u'1dir-\u062c\u0648'
764
name2u = u'2file-\u0633'
768
name1u + '/' + name0u,
769
name1u + '/' + name1u + '/',
1209
self.requireFeature(tests.UnicodeFilenameFeature)
1210
# Use the unicode reader. TODO: split into driver-and-driven unit
1212
self._save_platform_info()
1213
osutils._selected_dir_reader = osutils.UnicodeDirReader()
1214
name0u = u'0file-\xb6'
1215
name1u = u'1dir-\u062c\u0648'
1216
name2u = u'2file-\u0633'
1220
name1u + '/' + name0u,
1221
name1u + '/' + name1u + '/',
1224
self.build_tree(tree)
1225
name0 = name0u.encode('utf8')
1226
name1 = name1u.encode('utf8')
1227
name2 = name2u.encode('utf8')
1229
# All of the abspaths should be in unicode, all of the relative paths
1231
expected_dirblocks = [
1233
[(name0, name0, 'file', './' + name0u),
1234
(name1, name1, 'directory', './' + name1u),
1235
(name2, name2, 'file', './' + name2u),
1238
((name1, './' + name1u),
1239
[(name1 + '/' + name0, name0, 'file', './' + name1u
1241
(name1 + '/' + name1, name1, 'directory', './' + name1u
1245
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1250
result = list(osutils._walkdirs_utf8('.'))
1251
self._filter_out_stat(result)
1252
self.assertEqual(expected_dirblocks, result)
1254
def test__walkdirs_utf8_win32readdir(self):
1255
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1256
self.requireFeature(tests.UnicodeFilenameFeature)
1257
from bzrlib._walkdirs_win32 import Win32ReadDir
1258
self._save_platform_info()
1259
osutils._selected_dir_reader = Win32ReadDir()
1260
name0u = u'0file-\xb6'
1261
name1u = u'1dir-\u062c\u0648'
1262
name2u = u'2file-\u0633'
1266
name1u + '/' + name0u,
1267
name1u + '/' + name1u + '/',
1270
self.build_tree(tree)
1271
name0 = name0u.encode('utf8')
1272
name1 = name1u.encode('utf8')
1273
name2 = name2u.encode('utf8')
1275
# All of the abspaths should be in unicode, all of the relative paths
1277
expected_dirblocks = [
1279
[(name0, name0, 'file', './' + name0u),
1280
(name1, name1, 'directory', './' + name1u),
1281
(name2, name2, 'file', './' + name2u),
1284
((name1, './' + name1u),
1285
[(name1 + '/' + name0, name0, 'file', './' + name1u
1287
(name1 + '/' + name1, name1, 'directory', './' + name1u
1291
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1296
result = list(osutils._walkdirs_utf8(u'.'))
1297
self._filter_out_stat(result)
1298
self.assertEqual(expected_dirblocks, result)
1300
def assertStatIsCorrect(self, path, win32stat):
1301
os_stat = os.stat(path)
1302
self.assertEqual(os_stat.st_size, win32stat.st_size)
1303
self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
1304
self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
1305
self.assertAlmostEqual(os_stat.st_atime, win32stat.st_atime, places=4)
1306
self.assertEqual(os_stat.st_dev, win32stat.st_dev)
1307
self.assertEqual(os_stat.st_ino, win32stat.st_ino)
1308
self.assertEqual(os_stat.st_mode, win32stat.st_mode)
1310
def test__walkdirs_utf_win32_find_file_stat_file(self):
1311
"""make sure our Stat values are valid"""
1312
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1313
self.requireFeature(tests.UnicodeFilenameFeature)
1314
from bzrlib._walkdirs_win32 import Win32ReadDir
1315
name0u = u'0file-\xb6'
1316
name0 = name0u.encode('utf8')
1317
self.build_tree([name0u])
1318
# I hate to sleep() here, but I'm trying to make the ctime different
1321
f = open(name0u, 'ab')
773
self.build_tree(tree)
775
raise TestSkipped('Could not represent Unicode chars'
776
' in current encoding.')
1323
f.write('just a small update')
1327
result = Win32ReadDir().read_dir('', u'.')
1329
self.assertEqual((name0, name0, 'file'), entry[:3])
1330
self.assertEqual(u'./' + name0u, entry[4])
1331
self.assertStatIsCorrect(entry[4], entry[3])
1332
self.assertNotEqual(entry[3].st_mtime, entry[3].st_ctime)
1334
def test__walkdirs_utf_win32_find_file_stat_directory(self):
1335
"""make sure our Stat values are valid"""
1336
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1337
self.requireFeature(tests.UnicodeFilenameFeature)
1338
from bzrlib._walkdirs_win32 import Win32ReadDir
1339
name0u = u'0dir-\u062c\u0648'
777
1340
name0 = name0u.encode('utf8')
778
name1 = name1u.encode('utf8')
779
name2 = name2u.encode('utf8')
1341
self.build_tree([name0u + '/'])
781
# All of the abspaths should be in unicode, all of the relative paths
783
expected_dirblocks = [
785
[(name0, name0, 'file', './' + name0u),
786
(name1, name1, 'directory', './' + name1u),
787
(name2, name2, 'file', './' + name2u),
790
((name1, './' + name1u),
791
[(name1 + '/' + name0, name0, 'file', './' + name1u
793
(name1 + '/' + name1, name1, 'directory', './' + name1u
797
((name1 + '/' + name1, './' + name1u + '/' + name1u),
802
result = list(osutils._walkdirs_unicode_to_utf8('.'))
803
self._filter_out_stat(result)
804
self.assertEqual(expected_dirblocks, result)
1343
result = Win32ReadDir().read_dir('', u'.')
1345
self.assertEqual((name0, name0, 'directory'), entry[:3])
1346
self.assertEqual(u'./' + name0u, entry[4])
1347
self.assertStatIsCorrect(entry[4], entry[3])
806
1349
def assertPathCompare(self, path_less, path_greater):
807
1350
"""check that path_less and path_greater compare correctly."""
998
1527
self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1001
class TestLocalTimeOffset(TestCase):
1003
def test_local_time_offset(self):
1004
"""Test that local_time_offset() returns a sane value."""
1005
offset = osutils.local_time_offset()
1006
self.assertTrue(isinstance(offset, int))
1007
# Test that the offset is no more than a eighteen hours in
1009
# Time zone handling is system specific, so it is difficult to
1010
# do more specific tests, but a value outside of this range is
1012
eighteen_hours = 18 * 3600
1013
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1015
def test_local_time_offset_with_timestamp(self):
1016
"""Test that local_time_offset() works with a timestamp."""
1017
offset = osutils.local_time_offset(1000000000.1234567)
1018
self.assertTrue(isinstance(offset, int))
1019
eighteen_hours = 18 * 3600
1020
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1530
class TestSizeShaFile(tests.TestCaseInTempDir):
1532
def test_sha_empty(self):
1533
self.build_tree_contents([('foo', '')])
1534
expected_sha = osutils.sha_string('')
1536
self.addCleanup(f.close)
1537
size, sha = osutils.size_sha_file(f)
1538
self.assertEqual(0, size)
1539
self.assertEqual(expected_sha, sha)
1541
def test_sha_mixed_endings(self):
1542
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1543
self.build_tree_contents([('foo', text)])
1544
expected_sha = osutils.sha_string(text)
1546
self.addCleanup(f.close)
1547
size, sha = osutils.size_sha_file(f)
1548
self.assertEqual(38, size)
1549
self.assertEqual(expected_sha, sha)
1552
class TestShaFileByName(tests.TestCaseInTempDir):
1554
def test_sha_empty(self):
1555
self.build_tree_contents([('foo', '')])
1556
expected_sha = osutils.sha_string('')
1557
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1559
def test_sha_mixed_endings(self):
1560
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1561
self.build_tree_contents([('foo', text)])
1562
expected_sha = osutils.sha_string(text)
1563
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1566
class TestResourceLoading(tests.TestCaseInTempDir):
1568
def test_resource_string(self):
1569
# test resource in bzrlib
1570
text = osutils.resource_string('bzrlib', 'debug.py')
1571
self.assertContainsRe(text, "debug_flags = set()")
1572
# test resource under bzrlib
1573
text = osutils.resource_string('bzrlib.ui', 'text.py')
1574
self.assertContainsRe(text, "class TextUIFactory")
1575
# test unsupported package
1576
self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1578
# test unknown resource
1579
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1582
class TestReCompile(tests.TestCase):
1584
def test_re_compile_checked(self):
1585
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1586
self.assertTrue(r.match('aaaa'))
1587
self.assertTrue(r.match('aAaA'))
1589
def test_re_compile_checked_error(self):
1590
# like https://bugs.launchpad.net/bzr/+bug/251352
1591
err = self.assertRaises(
1592
errors.BzrCommandError,
1593
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1595
"Invalid regular expression in test case: '*': "
1596
"nothing to repeat",
1600
class TestDirReader(tests.TestCaseInTempDir):
1603
_dir_reader_class = None
1604
_native_to_unicode = None
1607
tests.TestCaseInTempDir.setUp(self)
1609
# Save platform specific info and reset it
1610
cur_dir_reader = osutils._selected_dir_reader
1613
osutils._selected_dir_reader = cur_dir_reader
1614
self.addCleanup(restore)
1616
osutils._selected_dir_reader = self._dir_reader_class()
1618
def _get_ascii_tree(self):
1626
expected_dirblocks = [
1628
[('0file', '0file', 'file'),
1629
('1dir', '1dir', 'directory'),
1630
('2file', '2file', 'file'),
1633
(('1dir', './1dir'),
1634
[('1dir/0file', '0file', 'file'),
1635
('1dir/1dir', '1dir', 'directory'),
1638
(('1dir/1dir', './1dir/1dir'),
1643
return tree, expected_dirblocks
1645
def test_walk_cur_dir(self):
1646
tree, expected_dirblocks = self._get_ascii_tree()
1647
self.build_tree(tree)
1648
result = list(osutils._walkdirs_utf8('.'))
1649
# Filter out stat and abspath
1650
self.assertEqual(expected_dirblocks,
1651
[(dirinfo, [line[0:3] for line in block])
1652
for dirinfo, block in result])
1654
def test_walk_sub_dir(self):
1655
tree, expected_dirblocks = self._get_ascii_tree()
1656
self.build_tree(tree)
1657
# you can search a subdir only, with a supplied prefix.
1658
result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1659
# Filter out stat and abspath
1660
self.assertEqual(expected_dirblocks[1:],
1661
[(dirinfo, [line[0:3] for line in block])
1662
for dirinfo, block in result])
1664
def _get_unicode_tree(self):
1665
name0u = u'0file-\xb6'
1666
name1u = u'1dir-\u062c\u0648'
1667
name2u = u'2file-\u0633'
1671
name1u + '/' + name0u,
1672
name1u + '/' + name1u + '/',
1675
name0 = name0u.encode('UTF-8')
1676
name1 = name1u.encode('UTF-8')
1677
name2 = name2u.encode('UTF-8')
1678
expected_dirblocks = [
1680
[(name0, name0, 'file', './' + name0u),
1681
(name1, name1, 'directory', './' + name1u),
1682
(name2, name2, 'file', './' + name2u),
1685
((name1, './' + name1u),
1686
[(name1 + '/' + name0, name0, 'file', './' + name1u
1688
(name1 + '/' + name1, name1, 'directory', './' + name1u
1692
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1697
return tree, expected_dirblocks
1699
def _filter_out(self, raw_dirblocks):
1700
"""Filter out a walkdirs_utf8 result.
1702
stat field is removed, all native paths are converted to unicode
1704
filtered_dirblocks = []
1705
for dirinfo, block in raw_dirblocks:
1706
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1709
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1710
filtered_dirblocks.append((dirinfo, details))
1711
return filtered_dirblocks
1713
def test_walk_unicode_tree(self):
1714
self.requireFeature(tests.UnicodeFilenameFeature)
1715
tree, expected_dirblocks = self._get_unicode_tree()
1716
self.build_tree(tree)
1717
result = list(osutils._walkdirs_utf8('.'))
1718
self.assertEqual(expected_dirblocks, self._filter_out(result))
1720
def test_symlink(self):
1721
self.requireFeature(tests.SymlinkFeature)
1722
self.requireFeature(tests.UnicodeFilenameFeature)
1723
target = u'target\N{Euro Sign}'
1724
link_name = u'l\N{Euro Sign}nk'
1725
os.symlink(target, link_name)
1726
target_utf8 = target.encode('UTF-8')
1727
link_name_utf8 = link_name.encode('UTF-8')
1728
expected_dirblocks = [
1730
[(link_name_utf8, link_name_utf8,
1731
'symlink', './' + link_name),],
1733
result = list(osutils._walkdirs_utf8('.'))
1734
self.assertEqual(expected_dirblocks, self._filter_out(result))
1737
class TestReadLink(tests.TestCaseInTempDir):
1738
"""Exposes os.readlink() problems and the osutils solution.
1740
The only guarantee offered by os.readlink(), starting with 2.6, is that a
1741
unicode string will be returned if a unicode string is passed.
1743
But prior python versions failed to properly encode the passed unicode
1746
_test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
1749
super(tests.TestCaseInTempDir, self).setUp()
1750
self.link = u'l\N{Euro Sign}ink'
1751
self.target = u'targe\N{Euro Sign}t'
1752
os.symlink(self.target, self.link)
1754
def test_os_readlink_link_encoding(self):
1755
if sys.version_info < (2, 6):
1756
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1758
self.assertEquals(self.target, os.readlink(self.link))
1760
def test_os_readlink_link_decoding(self):
1761
self.assertEquals(self.target.encode(osutils._fs_enc),
1762
os.readlink(self.link.encode(osutils._fs_enc)))