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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for the osutils wrapper."""
19
19
from cStringIO import StringIO
28
28
from bzrlib import (
34
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
35
from bzrlib.osutils import (
37
is_inside_or_parent_of_any,
34
41
from bzrlib.tests import (
42
probe_unicode_in_user_encoding,
49
from bzrlib.tests.file_utils import (
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):
52
from bzrlib.tests.test__walkdirs_win32 import WalkdirsWin32Feature
55
class TestOSUtils(TestCaseInTempDir):
111
57
def test_contains_whitespace(self):
112
58
self.failUnless(osutils.contains_whitespace(u' '))
358
271
def test_format_date(self):
359
272
self.assertRaises(errors.UnsupportedTimezoneFormat,
360
273
osutils.format_date, 0, timezone='foo')
361
self.assertIsInstance(osutils.format_date(0), str)
362
self.assertIsInstance(osutils.format_local_date(0), unicode)
363
# Testing for the actual value of the local weekday without
364
# duplicating the code from format_date is difficult.
365
# Instead blackbox.test_locale should check for localized
366
# dates once they do occur in output strings.
368
def test_local_time_offset(self):
369
"""Test that local_time_offset() returns a sane value."""
370
offset = osutils.local_time_offset()
371
self.assertTrue(isinstance(offset, int))
372
# Test that the offset is no more than a eighteen hours in
374
# Time zone handling is system specific, so it is difficult to
375
# do more specific tests, but a value outside of this range is
377
eighteen_hours = 18 * 3600
378
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
380
def test_local_time_offset_with_timestamp(self):
381
"""Test that local_time_offset() works with a timestamp."""
382
offset = osutils.local_time_offset(1000000000.1234567)
383
self.assertTrue(isinstance(offset, int))
384
eighteen_hours = 18 * 3600
385
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
388
class TestLinks(tests.TestCaseInTempDir):
390
275
def test_dereference_path(self):
391
self.requireFeature(tests.SymlinkFeature)
276
self.requireFeature(SymlinkFeature)
392
277
cwd = osutils.realpath('.')
394
279
bar_path = osutils.pathjoin(cwd, 'bar')
435
320
osutils.make_readonly('dangling')
436
321
osutils.make_writable('dangling')
323
def test_kind_marker(self):
324
self.assertEqual("", osutils.kind_marker("file"))
325
self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
326
self.assertEqual("@", osutils.kind_marker("symlink"))
327
self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
438
329
def test_host_os_dereferences_symlinks(self):
439
330
osutils.host_os_dereferences_symlinks()
442
class TestCanonicalRelPath(tests.TestCaseInTempDir):
444
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
446
def test_canonical_relpath_simple(self):
447
f = file('MixedCaseName', 'w')
449
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
450
real_base_dir = osutils.realpath(self.test_base_dir)
451
actual = osutils.canonical_relpath(real_base_dir, 'mixedcasename')
452
self.failUnlessEqual('work/MixedCaseName', actual)
454
def test_canonical_relpath_missing_tail(self):
455
os.mkdir('MixedCaseParent')
456
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
457
real_base_dir = osutils.realpath(self.test_base_dir)
458
actual = osutils.canonical_relpath(real_base_dir,
459
'mixedcaseparent/nochild')
460
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
463
class TestPumpFile(tests.TestCase):
333
class TestPumpFile(TestCase):
464
334
"""Test pumpfile method."""
467
tests.TestCase.setUp(self)
468
336
# create a test datablock
469
337
self.block_size = 512
470
338
pattern = '0123456789ABCDEF'
477
345
# make sure test data is larger than max read size
478
346
self.assertTrue(self.test_data_len > self.block_size)
480
from_file = file_utils.FakeReadFile(self.test_data)
348
from_file = FakeReadFile(self.test_data)
481
349
to_file = StringIO()
483
351
# read (max / 2) bytes and verify read size wasn't affected
484
352
num_bytes_to_read = self.block_size / 2
485
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
353
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
486
354
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
487
355
self.assertEqual(from_file.get_read_count(), 1)
489
357
# read (max) bytes and verify read size wasn't affected
490
358
num_bytes_to_read = self.block_size
491
359
from_file.reset_read_count()
492
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
360
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
493
361
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
494
362
self.assertEqual(from_file.get_read_count(), 1)
496
364
# read (max + 1) bytes and verify read size was limited
497
365
num_bytes_to_read = self.block_size + 1
498
366
from_file.reset_read_count()
499
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
367
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
500
368
self.assertEqual(from_file.get_max_read_size(), self.block_size)
501
369
self.assertEqual(from_file.get_read_count(), 2)
503
371
# finish reading the rest of the data
504
372
num_bytes_to_read = self.test_data_len - to_file.tell()
505
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
373
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
507
375
# report error if the data wasn't equal (we only report the size due
508
376
# to the length of the data)
573
440
message = "Data not equal. Expected %d bytes, received %d."
574
441
self.fail(message % (len(response_data), self.test_data_len))
576
def test_report_activity(self):
578
def log_activity(length, direction):
579
activity.append((length, direction))
580
from_file = StringIO(self.test_data)
582
osutils.pumpfile(from_file, to_file, buff_size=500,
583
report_activity=log_activity, direction='read')
584
self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
585
(36, 'read')], activity)
587
from_file = StringIO(self.test_data)
590
osutils.pumpfile(from_file, to_file, buff_size=500,
591
report_activity=log_activity, direction='write')
592
self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
593
(36, 'write')], activity)
595
# And with a limited amount of data
596
from_file = StringIO(self.test_data)
599
osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
600
report_activity=log_activity, direction='read')
601
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
605
class TestPumpStringFile(tests.TestCase):
607
def test_empty(self):
609
osutils.pump_string_file("", output)
610
self.assertEqual("", output.getvalue())
612
def test_more_than_segment_size(self):
614
osutils.pump_string_file("123456789", output, 2)
615
self.assertEqual("123456789", output.getvalue())
617
def test_segment_size(self):
619
osutils.pump_string_file("12", output, 2)
620
self.assertEqual("12", output.getvalue())
622
def test_segment_size_multiple(self):
624
osutils.pump_string_file("1234", output, 2)
625
self.assertEqual("1234", output.getvalue())
628
class TestRelpath(tests.TestCase):
630
def test_simple_relpath(self):
631
cwd = osutils.getcwd()
632
subdir = cwd + '/subdir'
633
self.assertEqual('subdir', osutils.relpath(cwd, subdir))
635
def test_deep_relpath(self):
636
cwd = osutils.getcwd()
637
subdir = cwd + '/sub/subsubdir'
638
self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
640
def test_not_relative(self):
641
self.assertRaises(errors.PathNotChild,
642
osutils.relpath, 'C:/path', 'H:/path')
643
self.assertRaises(errors.PathNotChild,
644
osutils.relpath, 'C:/', 'H:/path')
647
class TestSafeUnicode(tests.TestCase):
443
class TestSafeUnicode(TestCase):
649
445
def test_from_ascii_string(self):
650
446
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
752
548
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
754
550
def test_pathjoin(self):
755
self.assertEqual('path/to/foo',
756
osutils._win32_pathjoin('path', 'to', 'foo'))
757
self.assertEqual('C:/foo',
758
osutils._win32_pathjoin('path\\to', 'C:\\foo'))
759
self.assertEqual('C:/foo',
760
osutils._win32_pathjoin('path/to', 'C:/foo'))
761
self.assertEqual('path/to/foo',
762
osutils._win32_pathjoin('path/to/', 'foo'))
763
self.assertEqual('/foo',
764
osutils._win32_pathjoin('C:/path/to/', '/foo'))
765
self.assertEqual('/foo',
766
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
551
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
552
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
553
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
554
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
555
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
556
self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
768
558
def test_normpath(self):
769
self.assertEqual('path/to/foo',
770
osutils._win32_normpath(r'path\\from\..\to\.\foo'))
771
self.assertEqual('path/to/foo',
772
osutils._win32_normpath('path//from/../to/./foo'))
559
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
560
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
774
562
def test_getcwd(self):
775
563
cwd = osutils._win32_getcwd()
891
681
self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
894
class TestParentDirectories(tests.TestCaseInTempDir):
895
"""Test osutils.parent_directories()"""
897
def test_parent_directories(self):
898
self.assertEqual([], osutils.parent_directories('a'))
899
self.assertEqual(['a'], osutils.parent_directories('a/b'))
900
self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
903
class TestMacFuncsDirs(tests.TestCaseInTempDir):
684
class TestMacFuncsDirs(TestCaseInTempDir):
904
685
"""Test mac special functions that require directories."""
906
687
def test_getcwd(self):
907
self.requireFeature(tests.UnicodeFilenameFeature)
908
os.mkdir(u'B\xe5gfors')
688
# On Mac, this will actually create Ba\u030agfors
689
# but chdir will still work, because it accepts both paths
691
os.mkdir(u'B\xe5gfors')
693
raise TestSkipped("Unable to create Unicode filename")
909
695
os.chdir(u'B\xe5gfors')
910
696
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
912
698
def test_getcwd_nonnorm(self):
913
self.requireFeature(tests.UnicodeFilenameFeature)
914
699
# Test that _mac_getcwd() will normalize this path
915
os.mkdir(u'Ba\u030agfors')
701
os.mkdir(u'Ba\u030agfors')
703
raise TestSkipped("Unable to create Unicode filename")
916
705
os.chdir(u'Ba\u030agfors')
917
706
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
920
class TestChunksToLines(tests.TestCase):
922
def test_smoketest(self):
923
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
924
osutils.chunks_to_lines(['foo\nbar', '\nbaz\n']))
925
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
926
osutils.chunks_to_lines(['foo\n', 'bar\n', 'baz\n']))
928
def test_osutils_binding(self):
929
from bzrlib.tests import test__chunks_to_lines
930
if test__chunks_to_lines.CompiledChunksToLinesFeature.available():
931
from bzrlib._chunks_to_lines_pyx import chunks_to_lines
933
from bzrlib._chunks_to_lines_py import chunks_to_lines
934
self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
937
class TestSplitLines(tests.TestCase):
709
class TestSplitLines(TestCase):
939
711
def test_split_unicode(self):
940
712
self.assertEqual([u'foo\n', u'bar\xae'],
991
758
result.append((dirdetail, dirblock))
993
760
self.assertTrue(found_bzrdir)
994
self.assertExpectedBlocks(expected_dirblocks, result)
761
self.assertEqual(expected_dirblocks,
762
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
995
763
# you can search a subdir only, with a supplied prefix.
997
765
for dirblock in osutils.walkdirs('./1dir', '1dir'):
998
766
result.append(dirblock)
999
self.assertExpectedBlocks(expected_dirblocks[1:], result)
1001
def test_walkdirs_os_error(self):
1002
# <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1003
# Pyrex readdir didn't raise useful messages if it had an error
1004
# reading the directory
1005
if sys.platform == 'win32':
1006
raise tests.TestNotApplicable(
1007
"readdir IOError not tested on win32")
1008
os.mkdir("test-unreadable")
1009
os.chmod("test-unreadable", 0000)
1010
# must chmod it back so that it can be removed
1011
self.addCleanup(os.chmod, "test-unreadable", 0700)
1012
# The error is not raised until the generator is actually evaluated.
1013
# (It would be ok if it happened earlier but at the moment it
1015
e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
1016
self.assertEquals('./test-unreadable', e.filename)
1017
self.assertEquals(errno.EACCES, e.errno)
1018
# Ensure the message contains the file name
1019
self.assertContainsRe(str(e), "\./test-unreadable")
767
self.assertEqual(expected_dirblocks[1:],
768
[(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1021
770
def test__walkdirs_utf8(self):
1072
822
new_dirblock.append((info[0], info[1], info[2], info[4]))
1073
823
dirblock[:] = new_dirblock
825
def test__walkdirs_utf8_selection(self):
826
# Just trigger the function once, to make sure it has selected a real
828
list(osutils._walkdirs_utf8('.'))
829
if WalkdirsWin32Feature.available():
830
# If the compiled form is available, make sure it is used
831
from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
832
self.assertIs(_walkdirs_utf8_win32_find_file,
833
osutils._real_walkdirs_utf8)
834
elif sys.platform == 'win32':
835
self.assertIs(osutils._walkdirs_unicode_to_utf8,
836
osutils._real_walkdirs_utf8)
837
elif osutils._fs_enc.upper() in ('UTF-8', 'ASCII', 'ANSI_X3.4-1968'): # ascii
838
self.assertIs(osutils._walkdirs_fs_utf8,
839
osutils._real_walkdirs_utf8)
841
self.assertIs(osutils._walkdirs_unicode_to_utf8,
842
osutils._real_walkdirs_utf8)
1075
844
def _save_platform_info(self):
1076
845
cur_winver = win32utils.winver
1077
846
cur_fs_enc = osutils._fs_enc
1078
cur_dir_reader = osutils._selected_dir_reader
847
cur_real_walkdirs_utf8 = osutils._real_walkdirs_utf8
1080
849
win32utils.winver = cur_winver
1081
850
osutils._fs_enc = cur_fs_enc
1082
osutils._selected_dir_reader = cur_dir_reader
851
osutils._real_walkdirs_utf8 = cur_real_walkdirs_utf8
1083
852
self.addCleanup(restore)
1085
def assertDirReaderIs(self, expected):
854
def assertWalkdirsUtf8Is(self, expected):
1086
855
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
1087
856
# Force it to redetect
1088
osutils._selected_dir_reader = None
857
osutils._real_walkdirs_utf8 = None
1089
858
# Nothing to list, but should still trigger the selection logic
1090
859
self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
1091
self.assertIsInstance(osutils._selected_dir_reader, expected)
860
self.assertIs(expected, osutils._real_walkdirs_utf8)
1093
862
def test_force_walkdirs_utf8_fs_utf8(self):
1094
self.requireFeature(UTF8DirReaderFeature)
1095
863
self._save_platform_info()
1096
864
win32utils.winver = None # Avoid the win32 detection code
1097
865
osutils._fs_enc = 'UTF-8'
1098
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
866
self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1100
868
def test_force_walkdirs_utf8_fs_ascii(self):
1101
self.requireFeature(UTF8DirReaderFeature)
1102
869
self._save_platform_info()
1103
870
win32utils.winver = None # Avoid the win32 detection code
1104
871
osutils._fs_enc = 'US-ASCII'
1105
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
872
self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1107
874
def test_force_walkdirs_utf8_fs_ANSI(self):
1108
self.requireFeature(UTF8DirReaderFeature)
1109
875
self._save_platform_info()
1110
876
win32utils.winver = None # Avoid the win32 detection code
1111
877
osutils._fs_enc = 'ANSI_X3.4-1968'
1112
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
878
self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
1114
880
def test_force_walkdirs_utf8_fs_latin1(self):
1115
881
self._save_platform_info()
1116
882
win32utils.winver = None # Avoid the win32 detection code
1117
883
osutils._fs_enc = 'latin1'
1118
self.assertDirReaderIs(osutils.UnicodeDirReader)
884
self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
1120
886
def test_force_walkdirs_utf8_nt(self):
1121
# Disabled because the thunk of the whole walkdirs api is disabled.
1122
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
887
self.requireFeature(WalkdirsWin32Feature)
1123
888
self._save_platform_info()
1124
889
win32utils.winver = 'Windows NT'
1125
from bzrlib._walkdirs_win32 import Win32ReadDir
1126
self.assertDirReaderIs(Win32ReadDir)
890
from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
891
self.assertWalkdirsUtf8Is(_walkdirs_utf8_win32_find_file)
1128
def test_force_walkdirs_utf8_98(self):
1129
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
893
def test_force_walkdirs_utf8_nt(self):
894
self.requireFeature(WalkdirsWin32Feature)
1130
895
self._save_platform_info()
1131
896
win32utils.winver = 'Windows 98'
1132
self.assertDirReaderIs(osutils.UnicodeDirReader)
897
self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
1134
899
def test_unicode_walkdirs(self):
1135
900
"""Walkdirs should always return unicode paths."""
1136
self.requireFeature(tests.UnicodeFilenameFeature)
1137
901
name0 = u'0file-\xb6'
1138
902
name1 = u'1dir-\u062c\u0648'
1139
903
name2 = u'2file-\u0633'
1558
1329
self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1561
class TestSizeShaFile(tests.TestCaseInTempDir):
1563
def test_sha_empty(self):
1564
self.build_tree_contents([('foo', '')])
1565
expected_sha = osutils.sha_string('')
1567
self.addCleanup(f.close)
1568
size, sha = osutils.size_sha_file(f)
1569
self.assertEqual(0, size)
1570
self.assertEqual(expected_sha, sha)
1572
def test_sha_mixed_endings(self):
1573
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1574
self.build_tree_contents([('foo', text)])
1575
expected_sha = osutils.sha_string(text)
1577
self.addCleanup(f.close)
1578
size, sha = osutils.size_sha_file(f)
1579
self.assertEqual(38, size)
1580
self.assertEqual(expected_sha, sha)
1583
class TestShaFileByName(tests.TestCaseInTempDir):
1585
def test_sha_empty(self):
1586
self.build_tree_contents([('foo', '')])
1587
expected_sha = osutils.sha_string('')
1588
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1590
def test_sha_mixed_endings(self):
1591
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1592
self.build_tree_contents([('foo', text)])
1593
expected_sha = osutils.sha_string(text)
1594
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1597
class TestResourceLoading(tests.TestCaseInTempDir):
1332
class TestLocalTimeOffset(TestCase):
1334
def test_local_time_offset(self):
1335
"""Test that local_time_offset() returns a sane value."""
1336
offset = osutils.local_time_offset()
1337
self.assertTrue(isinstance(offset, int))
1338
# Test that the offset is no more than a eighteen hours in
1340
# Time zone handling is system specific, so it is difficult to
1341
# do more specific tests, but a value outside of this range is
1343
eighteen_hours = 18 * 3600
1344
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1346
def test_local_time_offset_with_timestamp(self):
1347
"""Test that local_time_offset() works with a timestamp."""
1348
offset = osutils.local_time_offset(1000000000.1234567)
1349
self.assertTrue(isinstance(offset, int))
1350
eighteen_hours = 18 * 3600
1351
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1354
class TestShaFileByName(TestCaseInTempDir):
1356
def test_sha_empty(self):
1357
self.build_tree_contents([('foo', '')])
1358
expected_sha = osutils.sha_string('')
1359
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1361
def test_sha_mixed_endings(self):
1362
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1363
self.build_tree_contents([('foo', text)])
1364
expected_sha = osutils.sha_string(text)
1365
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1369
r'''# Copyright (C) 2005, 2006 Canonical Ltd
1371
# This program is free software; you can redistribute it and/or modify
1372
# it under the terms of the GNU General Public License as published by
1373
# the Free Software Foundation; either version 2 of the License, or
1374
# (at your option) any later version.
1376
# This program is distributed in the hope that it will be useful,
1377
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1378
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1379
# GNU General Public License for more details.
1381
# You should have received a copy of the GNU General Public License
1382
# along with this program; if not, write to the Free Software
1383
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1386
# NOTE: If update these, please also update the help for global-options in
1387
# bzrlib/help_topics/__init__.py
1390
"""Set of flags that enable different debug behaviour.
1392
These are set with eg ``-Dlock`` on the bzr command line.
1396
* auth - show authentication sections used
1397
* error - show stack traces for all top level exceptions
1398
* evil - capture call sites that do expensive or badly-scaling operations.
1399
* fetch - trace history copying between repositories
1400
* graph - trace graph traversal information
1401
* hashcache - log every time a working file is read to determine its hash
1402
* hooks - trace hook execution
1403
* hpss - trace smart protocol requests and responses
1404
* http - trace http connections, requests and responses
1405
* index - trace major index operations
1406
* knit - trace knit operations
1407
* lock - trace when lockdir locks are taken or released
1408
* merge - emit information for debugging merges
1409
* pack - emit information about pack operations
1415
class TestResourceLoading(TestCaseInTempDir):
1599
1417
def test_resource_string(self):
1600
1418
# test resource in bzrlib
1601
1419
text = osutils.resource_string('bzrlib', 'debug.py')
1602
self.assertContainsRe(text, "debug_flags = set()")
1420
self.assertEquals(_debug_text, text)
1603
1421
# test resource under bzrlib
1604
1422
text = osutils.resource_string('bzrlib.ui', 'text.py')
1605
1423
self.assertContainsRe(text, "class TextUIFactory")
1609
1427
# test unknown resource
1610
1428
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1613
class TestReCompile(tests.TestCase):
1615
def test_re_compile_checked(self):
1616
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1617
self.assertTrue(r.match('aaaa'))
1618
self.assertTrue(r.match('aAaA'))
1620
def test_re_compile_checked_error(self):
1621
# like https://bugs.launchpad.net/bzr/+bug/251352
1622
err = self.assertRaises(
1623
errors.BzrCommandError,
1624
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1626
"Invalid regular expression in test case: '*': "
1627
"nothing to repeat",
1631
class TestDirReader(tests.TestCaseInTempDir):
1634
_dir_reader_class = None
1635
_native_to_unicode = None
1638
tests.TestCaseInTempDir.setUp(self)
1640
# Save platform specific info and reset it
1641
cur_dir_reader = osutils._selected_dir_reader
1644
osutils._selected_dir_reader = cur_dir_reader
1645
self.addCleanup(restore)
1647
osutils._selected_dir_reader = self._dir_reader_class()
1649
def _get_ascii_tree(self):
1657
expected_dirblocks = [
1659
[('0file', '0file', 'file'),
1660
('1dir', '1dir', 'directory'),
1661
('2file', '2file', 'file'),
1664
(('1dir', './1dir'),
1665
[('1dir/0file', '0file', 'file'),
1666
('1dir/1dir', '1dir', 'directory'),
1669
(('1dir/1dir', './1dir/1dir'),
1674
return tree, expected_dirblocks
1676
def test_walk_cur_dir(self):
1677
tree, expected_dirblocks = self._get_ascii_tree()
1678
self.build_tree(tree)
1679
result = list(osutils._walkdirs_utf8('.'))
1680
# Filter out stat and abspath
1681
self.assertEqual(expected_dirblocks,
1682
[(dirinfo, [line[0:3] for line in block])
1683
for dirinfo, block in result])
1685
def test_walk_sub_dir(self):
1686
tree, expected_dirblocks = self._get_ascii_tree()
1687
self.build_tree(tree)
1688
# you can search a subdir only, with a supplied prefix.
1689
result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1690
# Filter out stat and abspath
1691
self.assertEqual(expected_dirblocks[1:],
1692
[(dirinfo, [line[0:3] for line in block])
1693
for dirinfo, block in result])
1695
def _get_unicode_tree(self):
1696
name0u = u'0file-\xb6'
1697
name1u = u'1dir-\u062c\u0648'
1698
name2u = u'2file-\u0633'
1702
name1u + '/' + name0u,
1703
name1u + '/' + name1u + '/',
1706
name0 = name0u.encode('UTF-8')
1707
name1 = name1u.encode('UTF-8')
1708
name2 = name2u.encode('UTF-8')
1709
expected_dirblocks = [
1711
[(name0, name0, 'file', './' + name0u),
1712
(name1, name1, 'directory', './' + name1u),
1713
(name2, name2, 'file', './' + name2u),
1716
((name1, './' + name1u),
1717
[(name1 + '/' + name0, name0, 'file', './' + name1u
1719
(name1 + '/' + name1, name1, 'directory', './' + name1u
1723
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1728
return tree, expected_dirblocks
1730
def _filter_out(self, raw_dirblocks):
1731
"""Filter out a walkdirs_utf8 result.
1733
stat field is removed, all native paths are converted to unicode
1735
filtered_dirblocks = []
1736
for dirinfo, block in raw_dirblocks:
1737
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1740
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1741
filtered_dirblocks.append((dirinfo, details))
1742
return filtered_dirblocks
1744
def test_walk_unicode_tree(self):
1745
self.requireFeature(tests.UnicodeFilenameFeature)
1746
tree, expected_dirblocks = self._get_unicode_tree()
1747
self.build_tree(tree)
1748
result = list(osutils._walkdirs_utf8('.'))
1749
self.assertEqual(expected_dirblocks, self._filter_out(result))
1751
def test_symlink(self):
1752
self.requireFeature(tests.SymlinkFeature)
1753
self.requireFeature(tests.UnicodeFilenameFeature)
1754
target = u'target\N{Euro Sign}'
1755
link_name = u'l\N{Euro Sign}nk'
1756
os.symlink(target, link_name)
1757
target_utf8 = target.encode('UTF-8')
1758
link_name_utf8 = link_name.encode('UTF-8')
1759
expected_dirblocks = [
1761
[(link_name_utf8, link_name_utf8,
1762
'symlink', './' + link_name),],
1764
result = list(osutils._walkdirs_utf8('.'))
1765
self.assertEqual(expected_dirblocks, self._filter_out(result))
1768
class TestReadLink(tests.TestCaseInTempDir):
1769
"""Exposes os.readlink() problems and the osutils solution.
1771
The only guarantee offered by os.readlink(), starting with 2.6, is that a
1772
unicode string will be returned if a unicode string is passed.
1774
But prior python versions failed to properly encode the passed unicode
1777
_test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
1780
super(tests.TestCaseInTempDir, self).setUp()
1781
self.link = u'l\N{Euro Sign}ink'
1782
self.target = u'targe\N{Euro Sign}t'
1783
os.symlink(self.target, self.link)
1785
def test_os_readlink_link_encoding(self):
1786
if sys.version_info < (2, 6):
1787
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1789
self.assertEquals(self.target, os.readlink(self.link))
1791
def test_os_readlink_link_decoding(self):
1792
self.assertEquals(self.target.encode(osutils._fs_enc),
1793
os.readlink(self.link.encode(osutils._fs_enc)))
1796
class TestConcurrency(tests.TestCase):
1798
def test_local_concurrency(self):
1799
concurrency = osutils.local_concurrency()
1800
self.assertIsInstance(concurrency, int)