98
def load_tests(basic_tests, module, loader):
99
suite = loader.suiteClass()
100
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
101
basic_tests, tests.condition_isinstance(TestDirReader))
102
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
103
suite.addTest(remaining_tests)
104
load_tests = load_tests_apply_scenarios
107
107
class TestContainsWhitespace(tests.TestCase):
109
109
def test_contains_whitespace(self):
110
self.failUnless(osutils.contains_whitespace(u' '))
111
self.failUnless(osutils.contains_whitespace(u'hello there'))
112
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
113
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
114
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
115
self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
110
self.assertTrue(osutils.contains_whitespace(u' '))
111
self.assertTrue(osutils.contains_whitespace(u'hello there'))
112
self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
113
self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
114
self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
115
self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
117
117
# \xa0 is "Non-breaking-space" which on some python locales thinks it
118
118
# is whitespace, but we do not.
119
self.failIf(osutils.contains_whitespace(u''))
120
self.failIf(osutils.contains_whitespace(u'hellothere'))
121
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
119
self.assertFalse(osutils.contains_whitespace(u''))
120
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
121
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
124
124
class TestRename(tests.TestCaseInTempDir):
137
137
def test_fancy_rename(self):
138
138
# This should work everywhere
139
self.create_file('a', 'something in a\n')
139
self.create_file('a', b'something in a\n')
140
140
self._fancy_rename('a', 'b')
141
self.failIfExists('a')
142
self.failUnlessExists('b')
143
self.check_file_contents('b', 'something in a\n')
141
self.assertPathDoesNotExist('a')
142
self.assertPathExists('b')
143
self.check_file_contents('b', b'something in a\n')
145
self.create_file('a', 'new something in a\n')
145
self.create_file('a', b'new something in a\n')
146
146
self._fancy_rename('b', 'a')
148
self.check_file_contents('a', 'something in a\n')
148
self.check_file_contents('a', b'something in a\n')
150
150
def test_fancy_rename_fails_source_missing(self):
151
151
# An exception should be raised, and the target should be left in place
152
self.create_file('target', 'data in target\n')
152
self.create_file('target', b'data in target\n')
153
153
self.assertRaises((IOError, OSError), self._fancy_rename,
154
154
'missingsource', 'target')
155
self.failUnlessExists('target')
156
self.check_file_contents('target', 'data in target\n')
155
self.assertPathExists('target')
156
self.check_file_contents('target', b'data in target\n')
158
158
def test_fancy_rename_fails_if_source_and_target_missing(self):
159
159
self.assertRaises((IOError, OSError), self._fancy_rename,
418
439
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
442
class TestFdatasync(tests.TestCaseInTempDir):
444
def do_fdatasync(self):
445
f = tempfile.NamedTemporaryFile()
446
osutils.fdatasync(f.fileno())
450
def raise_eopnotsupp(*args, **kwargs):
451
raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
454
def raise_enotsup(*args, **kwargs):
455
raise IOError(errno.ENOTSUP, os.strerror(errno.ENOTSUP))
457
def test_fdatasync_handles_system_function(self):
458
self.overrideAttr(os, "fdatasync")
461
def test_fdatasync_handles_no_fdatasync_no_fsync(self):
462
self.overrideAttr(os, "fdatasync")
463
self.overrideAttr(os, "fsync")
466
def test_fdatasync_handles_no_EOPNOTSUPP(self):
467
self.overrideAttr(errno, "EOPNOTSUPP")
470
def test_fdatasync_catches_ENOTSUP(self):
471
enotsup = getattr(errno, "ENOTSUP", None)
473
raise tests.TestNotApplicable("No ENOTSUP on this platform")
474
self.overrideAttr(os, "fdatasync", self.raise_enotsup)
477
def test_fdatasync_catches_EOPNOTSUPP(self):
478
enotsup = getattr(errno, "EOPNOTSUPP", None)
480
raise tests.TestNotApplicable("No EOPNOTSUPP on this platform")
481
self.overrideAttr(os, "fdatasync", self.raise_eopnotsupp)
421
485
class TestLinks(tests.TestCaseInTempDir):
423
487
def test_dereference_path(self):
424
self.requireFeature(tests.SymlinkFeature)
488
self.requireFeature(features.SymlinkFeature)
425
489
cwd = osutils.realpath('.')
427
491
bar_path = osutils.pathjoin(cwd, 'bar')
550
613
self.assertTrue(self.test_data_len > self.block_size)
552
615
from_file = file_utils.FakeReadFile(self.test_data)
555
# read (max / 2) bytes and verify read size wasn't affected
556
num_bytes_to_read = self.block_size / 2
557
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
618
# read (max // 2) bytes and verify read size wasn't affected
619
num_bytes_to_read = self.block_size // 2
620
osutils.pumpfile(from_file, to_file,
621
num_bytes_to_read, self.block_size)
558
622
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
559
623
self.assertEqual(from_file.get_read_count(), 1)
561
625
# read (max) bytes and verify read size wasn't affected
562
626
num_bytes_to_read = self.block_size
563
627
from_file.reset_read_count()
564
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
628
osutils.pumpfile(from_file, to_file,
629
num_bytes_to_read, self.block_size)
565
630
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
566
631
self.assertEqual(from_file.get_read_count(), 1)
568
633
# read (max + 1) bytes and verify read size was limited
569
634
num_bytes_to_read = self.block_size + 1
570
635
from_file.reset_read_count()
571
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
636
osutils.pumpfile(from_file, to_file,
637
num_bytes_to_read, self.block_size)
572
638
self.assertEqual(from_file.get_max_read_size(), self.block_size)
573
639
self.assertEqual(from_file.get_read_count(), 2)
575
641
# finish reading the rest of the data
576
642
num_bytes_to_read = self.test_data_len - to_file.tell()
577
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
643
osutils.pumpfile(from_file, to_file,
644
num_bytes_to_read, self.block_size)
579
646
# report error if the data wasn't equal (we only report the size due
580
647
# to the length of the data)
665
733
(36, 'write')], activity)
667
735
# And with a limited amount of data
668
from_file = StringIO(self.test_data)
736
from_file = BytesIO(self.test_data)
671
739
osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
672
740
report_activity=log_activity, direction='read')
673
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
742
[(500, 'read'), (500, 'read'), (28, 'read')], activity)
677
745
class TestPumpStringFile(tests.TestCase):
679
747
def test_empty(self):
681
osutils.pump_string_file("", output)
682
self.assertEqual("", output.getvalue())
749
osutils.pump_string_file(b"", output)
750
self.assertEqual(b"", output.getvalue())
684
752
def test_more_than_segment_size(self):
686
osutils.pump_string_file("123456789", output, 2)
687
self.assertEqual("123456789", output.getvalue())
754
osutils.pump_string_file(b"123456789", output, 2)
755
self.assertEqual(b"123456789", output.getvalue())
689
757
def test_segment_size(self):
691
osutils.pump_string_file("12", output, 2)
692
self.assertEqual("12", output.getvalue())
759
osutils.pump_string_file(b"12", output, 2)
760
self.assertEqual(b"12", output.getvalue())
694
762
def test_segment_size_multiple(self):
696
osutils.pump_string_file("1234", output, 2)
697
self.assertEqual("1234", output.getvalue())
764
osutils.pump_string_file(b"1234", output, 2)
765
self.assertEqual(b"1234", output.getvalue())
700
768
class TestRelpath(tests.TestCase):
728
796
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
730
798
def test_from_utf8_string(self):
731
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
799
self.assertEqual(u'foo\xae', osutils.safe_unicode(b'foo\xc2\xae'))
733
801
def test_bad_utf8_string(self):
734
802
self.assertRaises(errors.BzrBadParameterNotUnicode,
735
803
osutils.safe_unicode,
739
807
class TestSafeUtf8(tests.TestCase):
741
809
def test_from_ascii_string(self):
743
self.assertEqual('foobar', osutils.safe_utf8(f))
811
self.assertEqual(b'foobar', osutils.safe_utf8(f))
745
813
def test_from_unicode_string_ascii_contents(self):
746
self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
814
self.assertEqual(b'bargam', osutils.safe_utf8(u'bargam'))
748
816
def test_from_unicode_string_unicode_contents(self):
749
self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
817
self.assertEqual(b'bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
751
819
def test_from_utf8_string(self):
752
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
820
self.assertEqual(b'foo\xc2\xae', osutils.safe_utf8(b'foo\xc2\xae'))
754
822
def test_bad_utf8_string(self):
755
823
self.assertRaises(errors.BzrBadParameterNotUnicode,
756
osutils.safe_utf8, '\xbb\xbb')
824
osutils.safe_utf8, b'\xbb\xbb')
759
827
class TestSafeRevisionId(tests.TestCase):
761
829
def test_from_ascii_string(self):
762
830
# this shouldn't give a warning because it's getting an ascii string
763
self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
831
self.assertEqual(b'foobar', osutils.safe_revision_id(b'foobar'))
765
833
def test_from_unicode_string_ascii_contents(self):
766
self.assertEqual('bargam',
767
osutils.safe_revision_id(u'bargam', warn=False))
769
def test_from_unicode_deprecated(self):
770
self.assertEqual('bargam',
771
self.callDeprecated([osutils._revision_id_warning],
772
osutils.safe_revision_id, u'bargam'))
834
self.assertRaises(TypeError,
835
osutils.safe_revision_id, u'bargam')
774
837
def test_from_unicode_string_unicode_contents(self):
775
self.assertEqual('bargam\xc2\xae',
776
osutils.safe_revision_id(u'bargam\xae', warn=False))
838
self.assertRaises(TypeError,
839
osutils.safe_revision_id, u'bargam\xae')
778
841
def test_from_utf8_string(self):
779
self.assertEqual('foo\xc2\xae',
780
osutils.safe_revision_id('foo\xc2\xae'))
842
self.assertEqual(b'foo\xc2\xae',
843
osutils.safe_revision_id(b'foo\xc2\xae'))
782
845
def test_none(self):
783
846
"""Currently, None is a valid revision_id"""
787
850
class TestSafeFileId(tests.TestCase):
789
852
def test_from_ascii_string(self):
790
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
853
self.assertEqual(b'foobar', osutils.safe_file_id(b'foobar'))
792
855
def test_from_unicode_string_ascii_contents(self):
793
self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
795
def test_from_unicode_deprecated(self):
796
self.assertEqual('bargam',
797
self.callDeprecated([osutils._file_id_warning],
798
osutils.safe_file_id, u'bargam'))
856
self.assertRaises(TypeError, osutils.safe_file_id, u'bargam')
800
858
def test_from_unicode_string_unicode_contents(self):
801
self.assertEqual('bargam\xc2\xae',
802
osutils.safe_file_id(u'bargam\xae', warn=False))
859
self.assertRaises(TypeError,
860
osutils.safe_file_id, u'bargam\xae')
804
862
def test_from_utf8_string(self):
805
self.assertEqual('foo\xc2\xae',
806
osutils.safe_file_id('foo\xc2\xae'))
863
self.assertEqual(b'foo\xc2\xae',
864
osutils.safe_file_id(b'foo\xc2\xae'))
808
866
def test_none(self):
809
867
"""Currently, None is a valid revision_id"""
810
868
self.assertEqual(None, osutils.safe_file_id(None))
871
class TestSendAll(tests.TestCase):
873
def test_send_with_disconnected_socket(self):
874
class DisconnectedSocket(object):
875
def __init__(self, err):
878
def send(self, content):
883
# All of these should be treated as ConnectionReset
885
for err_cls in (IOError, socket.error):
886
for errnum in osutils._end_of_stream_errors:
887
errs.append(err_cls(errnum))
889
sock = DisconnectedSocket(err)
890
self.assertRaises(errors.ConnectionReset,
891
osutils.send_all, sock, b'some more content')
893
def test_send_with_no_progress(self):
894
# See https://bugs.launchpad.net/bzr/+bug/1047309
895
# It seems that paramiko can get into a state where it doesn't error,
896
# but it returns 0 bytes sent for requests over and over again.
897
class NoSendingSocket(object):
901
def send(self, bytes):
903
if self.call_count > 100:
904
# Prevent the test suite from hanging
905
raise RuntimeError('too many calls')
907
sock = NoSendingSocket()
908
self.assertRaises(errors.ConnectionReset,
909
osutils.send_all, sock, b'content')
910
self.assertEqual(1, sock.call_count)
913
class TestPosixFuncs(tests.TestCase):
914
"""Test that the posix version of normpath returns an appropriate path
915
when used with 2 leading slashes."""
917
def test_normpath(self):
918
self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
920
'/etc/shadow', osutils._posix_normpath('//etc/shadow'))
922
'/etc/shadow', osutils._posix_normpath('///etc/shadow'))
813
925
class TestWin32Funcs(tests.TestCase):
814
926
"""Test that _win32 versions of os utilities return appropriate paths."""
816
928
def test_abspath(self):
929
self.requireFeature(features.win32_feature)
817
930
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
818
931
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
819
932
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
892
995
def test_minimum_path_selection(self):
893
996
self.assertEqual(set(),
894
osutils.minimum_path_selection([]))
895
self.assertEqual(set(['a']),
896
osutils.minimum_path_selection(['a']))
897
self.assertEqual(set(['a', 'b']),
898
osutils.minimum_path_selection(['a', 'b']))
899
self.assertEqual(set(['a/', 'b']),
900
osutils.minimum_path_selection(['a/', 'b']))
901
self.assertEqual(set(['a/', 'b']),
902
osutils.minimum_path_selection(['a/c', 'a/', 'b']))
903
self.assertEqual(set(['a-b', 'a', 'a0b']),
904
osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
997
osutils.minimum_path_selection([]))
998
self.assertEqual({'a'},
999
osutils.minimum_path_selection(['a']))
1000
self.assertEqual({'a', 'b'},
1001
osutils.minimum_path_selection(['a', 'b']))
1002
self.assertEqual({'a/', 'b'},
1003
osutils.minimum_path_selection(['a/', 'b']))
1004
self.assertEqual({'a/', 'b'},
1005
osutils.minimum_path_selection(['a/c', 'a/', 'b']))
1006
self.assertEqual({'a-b', 'a', 'a0b'},
1007
osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
906
1009
def test_mkdtemp(self):
907
1010
tmpdir = osutils._win32_mkdtemp(dir='.')
908
1011
self.assertFalse('\\' in tmpdir)
910
1013
def test_rename(self):
1014
with open('a', 'wb') as a:
1016
with open('b', 'wb') as b:
918
1019
osutils._win32_rename('b', 'a')
919
self.failUnlessExists('a')
920
self.failIfExists('b')
921
self.assertFileEqual('baz\n', 'a')
1020
self.assertPathExists('a')
1021
self.assertPathDoesNotExist('b')
1022
self.assertFileEqual(b'baz\n', 'a')
923
1024
def test_rename_missing_file(self):
1025
with open('a', 'wb') as a:
929
1029
osutils._win32_rename('b', 'a')
930
except (IOError, OSError), e:
1030
except (IOError, OSError) as e:
931
1031
self.assertEqual(errno.ENOENT, e.errno)
932
self.assertFileEqual('foo\n', 'a')
1032
self.assertFileEqual(b'foo\n', 'a')
934
1034
def test_rename_missing_dir(self):
937
1037
osutils._win32_rename('b', 'a')
938
except (IOError, OSError), e:
1038
except (IOError, OSError) as e:
939
1039
self.assertEqual(errno.ENOENT, e.errno)
941
1041
def test_rename_current_dir(self):
1071
1171
self.assertExpectedBlocks(expected_dirblocks[1:], result)
1073
1173
def test_walkdirs_os_error(self):
1074
# <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1174
# <https://bugs.launchpad.net/bzr/+bug/338653>
1075
1175
# Pyrex readdir didn't raise useful messages if it had an error
1076
1176
# reading the directory
1077
1177
if sys.platform == 'win32':
1078
1178
raise tests.TestNotApplicable(
1079
1179
"readdir IOError not tested on win32")
1180
self.requireFeature(features.not_running_as_root)
1080
1181
os.mkdir("test-unreadable")
1081
1182
os.chmod("test-unreadable", 0000)
1082
1183
# must chmod it back so that it can be removed
1083
self.addCleanup(os.chmod, "test-unreadable", 0700)
1184
self.addCleanup(os.chmod, "test-unreadable", 0o700)
1084
1185
# The error is not raised until the generator is actually evaluated.
1085
1186
# (It would be ok if it happened earlier but at the moment it
1087
1188
e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
1088
self.assertEquals('./test-unreadable', e.filename)
1089
self.assertEquals(errno.EACCES, e.errno)
1189
self.assertEqual('./test-unreadable', osutils.safe_unicode(e.filename))
1190
self.assertEqual(errno.EACCES, e.errno)
1090
1191
# Ensure the message contains the file name
1091
self.assertContainsRe(str(e), "\./test-unreadable")
1192
self.assertContainsRe(str(e), "\\./test-unreadable")
1194
def test_walkdirs_encoding_error(self):
1195
# <https://bugs.launchpad.net/bzr/+bug/488519>
1196
# walkdirs didn't raise a useful message when the filenames
1197
# are not using the filesystem's encoding
1199
# require a bytestring based filesystem
1200
self.requireFeature(features.ByteStringNamedFilesystem)
1211
self.build_tree(tree)
1213
# rename the 1file to a latin-1 filename
1214
os.rename(b"./1file", b"\xe8file")
1215
if b"\xe8file" not in os.listdir("."):
1216
self.skipTest("Lack filesystem that preserves arbitrary bytes")
1218
self._save_platform_info()
1219
osutils._fs_enc = 'UTF-8'
1221
# this should raise on error
1223
for dirdetail, dirblock in osutils.walkdirs(b'.'):
1226
self.assertRaises(errors.BadFilenameEncoding, attempt)
1093
1228
def test__walkdirs_utf8(self):
1102
1237
self.build_tree(tree)
1103
1238
expected_dirblocks = [
1105
[('0file', '0file', 'file'),
1106
('1dir', '1dir', 'directory'),
1107
('2file', '2file', 'file'),
1110
(('1dir', './1dir'),
1111
[('1dir/0file', '0file', 'file'),
1112
('1dir/1dir', '1dir', 'directory'),
1115
(('1dir/1dir', './1dir/1dir'),
1240
[('0file', '0file', 'file'),
1241
('1dir', '1dir', 'directory'),
1242
('2file', '2file', 'file'),
1245
(('1dir', './1dir'),
1246
[('1dir/0file', '0file', 'file'),
1247
('1dir/1dir', '1dir', 'directory'),
1250
(('1dir/1dir', './1dir/1dir'),
1121
1256
found_bzrdir = False
1122
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1123
if len(dirblock) and dirblock[0][1] == '.bzr':
1257
for dirdetail, dirblock in osutils._walkdirs_utf8(b'.'):
1258
if len(dirblock) and dirblock[0][1] == b'.bzr':
1124
1259
# this tests the filtering of selected paths
1125
1260
found_bzrdir = True
1126
1261
del dirblock[0]
1262
dirdetail = (dirdetail[0].decode('utf-8'),
1263
osutils.safe_unicode(dirdetail[1]))
1265
(entry[0].decode('utf-8'), entry[1].decode('utf-8'), entry[2])
1266
for entry in dirblock]
1127
1267
result.append((dirdetail, dirblock))
1129
1269
self.assertTrue(found_bzrdir)
1145
1285
dirblock[:] = new_dirblock
1147
1287
def _save_platform_info(self):
1148
self.overrideAttr(win32utils, 'winver')
1149
1288
self.overrideAttr(osutils, '_fs_enc')
1150
1289
self.overrideAttr(osutils, '_selected_dir_reader')
1152
def assertDirReaderIs(self, expected):
1291
def assertDirReaderIs(self, expected, top):
1153
1292
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
1154
1293
# Force it to redetect
1155
1294
osutils._selected_dir_reader = None
1156
1295
# Nothing to list, but should still trigger the selection logic
1157
self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
1296
self.assertEqual([((b'', top), [])], list(osutils._walkdirs_utf8('.')))
1158
1297
self.assertIsInstance(osutils._selected_dir_reader, expected)
1160
1299
def test_force_walkdirs_utf8_fs_utf8(self):
1161
1300
self.requireFeature(UTF8DirReaderFeature)
1162
1301
self._save_platform_info()
1163
win32utils.winver = None # Avoid the win32 detection code
1164
osutils._fs_enc = 'UTF-8'
1165
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1302
osutils._fs_enc = 'utf-8'
1303
self.assertDirReaderIs(UTF8DirReaderFeature.module.UTF8DirReader, b".")
1167
1305
def test_force_walkdirs_utf8_fs_ascii(self):
1168
1306
self.requireFeature(UTF8DirReaderFeature)
1169
1307
self._save_platform_info()
1170
win32utils.winver = None # Avoid the win32 detection code
1171
osutils._fs_enc = 'US-ASCII'
1172
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1174
def test_force_walkdirs_utf8_fs_ANSI(self):
1175
self.requireFeature(UTF8DirReaderFeature)
1176
self._save_platform_info()
1177
win32utils.winver = None # Avoid the win32 detection code
1178
osutils._fs_enc = 'ANSI_X3.4-1968'
1179
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1308
osutils._fs_enc = 'ascii'
1309
self.assertDirReaderIs(
1310
UTF8DirReaderFeature.module.UTF8DirReader, b".")
1181
1312
def test_force_walkdirs_utf8_fs_latin1(self):
1182
1313
self._save_platform_info()
1183
win32utils.winver = None # Avoid the win32 detection code
1184
osutils._fs_enc = 'latin1'
1185
self.assertDirReaderIs(osutils.UnicodeDirReader)
1314
osutils._fs_enc = 'iso-8859-1'
1315
self.assertDirReaderIs(osutils.UnicodeDirReader, ".")
1187
1317
def test_force_walkdirs_utf8_nt(self):
1188
1318
# Disabled because the thunk of the whole walkdirs api is disabled.
1189
1319
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1190
1320
self._save_platform_info()
1191
win32utils.winver = 'Windows NT'
1192
from bzrlib._walkdirs_win32 import Win32ReadDir
1193
self.assertDirReaderIs(Win32ReadDir)
1195
def test_force_walkdirs_utf8_98(self):
1196
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1197
self._save_platform_info()
1198
win32utils.winver = 'Windows 98'
1199
self.assertDirReaderIs(osutils.UnicodeDirReader)
1321
from .._walkdirs_win32 import Win32ReadDir
1322
self.assertDirReaderIs(Win32ReadDir, ".")
1201
1324
def test_unicode_walkdirs(self):
1202
1325
"""Walkdirs should always return unicode paths."""
1203
self.requireFeature(tests.UnicodeFilenameFeature)
1326
self.requireFeature(features.UnicodeFilenameFeature)
1204
1327
name0 = u'0file-\xb6'
1205
1328
name1 = u'1dir-\u062c\u0648'
1206
1329
name2 = u'2file-\u0633'
1214
1337
self.build_tree(tree)
1215
1338
expected_dirblocks = [
1217
[(name0, name0, 'file', './' + name0),
1218
(name1, name1, 'directory', './' + name1),
1219
(name2, name2, 'file', './' + name2),
1222
((name1, './' + name1),
1223
[(name1 + '/' + name0, name0, 'file', './' + name1
1225
(name1 + '/' + name1, name1, 'directory', './' + name1
1229
((name1 + '/' + name1, './' + name1 + '/' + name1),
1340
[(name0, name0, 'file', './' + name0),
1341
(name1, name1, 'directory', './' + name1),
1342
(name2, name2, 'file', './' + name2),
1345
((name1, './' + name1),
1346
[(name1 + '/' + name0, name0, 'file', './' + name1
1348
(name1 + '/' + name1, name1, 'directory', './' + name1
1352
((name1 + '/' + name1, './' + name1 + '/' + name1),
1234
1357
result = list(osutils.walkdirs('.'))
1235
1358
self._filter_out_stat(result)
1236
1359
self.assertEqual(expected_dirblocks, result)
1237
result = list(osutils.walkdirs(u'./'+name1, name1))
1360
result = list(osutils.walkdirs(u'./' + name1, name1))
1238
1361
self._filter_out_stat(result)
1239
1362
self.assertEqual(expected_dirblocks[1:], result)
1260
1383
name2 = name2.encode('utf8')
1262
1385
expected_dirblocks = [
1264
[(name0, name0, 'file', './' + name0),
1265
(name1, name1, 'directory', './' + name1),
1266
(name2, name2, 'file', './' + name2),
1269
((name1, './' + name1),
1270
[(name1 + '/' + name0, name0, 'file', './' + name1
1272
(name1 + '/' + name1, name1, 'directory', './' + name1
1276
((name1 + '/' + name1, './' + name1 + '/' + name1),
1387
[(name0, name0, 'file', b'./' + name0),
1388
(name1, name1, 'directory', b'./' + name1),
1389
(name2, name2, 'file', b'./' + name2),
1392
((name1, b'./' + name1),
1393
[(name1 + b'/' + name0, name0, 'file', b'./' + name1
1395
(name1 + b'/' + name1, name1, 'directory', b'./' + name1
1399
((name1 + b'/' + name1, b'./' + name1 + b'/' + name1),
1282
1405
# For ease in testing, if walkdirs_utf8 returns Unicode, assert that
1283
1406
# all abspaths are Unicode, and encode them back into utf8.
1284
1407
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1285
self.assertIsInstance(dirdetail[0], str)
1286
if isinstance(dirdetail[1], unicode):
1408
self.assertIsInstance(dirdetail[0], bytes)
1409
if isinstance(dirdetail[1], text_type):
1287
1410
dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
1288
1411
dirblock = [list(info) for info in dirblock]
1289
1412
for info in dirblock:
1290
self.assertIsInstance(info[4], unicode)
1413
self.assertIsInstance(info[4], text_type)
1291
1414
info[4] = info[4].encode('utf8')
1292
1415
new_dirblock = []
1293
1416
for info in dirblock:
1294
self.assertIsInstance(info[0], str)
1295
self.assertIsInstance(info[1], str)
1296
self.assertIsInstance(info[4], str)
1417
self.assertIsInstance(info[0], bytes)
1418
self.assertIsInstance(info[1], bytes)
1419
self.assertIsInstance(info[4], bytes)
1297
1420
# Remove the stat information
1298
1421
new_dirblock.append((info[0], info[1], info[2], info[4]))
1299
1422
result.append((dirdetail, new_dirblock))
1580
1703
def setUp(self):
1581
1704
super(TestSetUnsetEnv, self).setUp()
1583
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
1706
self.assertEqual(None, os.environ.get('BRZ_TEST_ENV_VAR'),
1584
1707
'Environment was not cleaned up properly.'
1585
' Variable BZR_TEST_ENV_VAR should not exist.')
1708
' Variable BRZ_TEST_ENV_VAR should not exist.')
1587
if 'BZR_TEST_ENV_VAR' in os.environ:
1588
del os.environ['BZR_TEST_ENV_VAR']
1711
if 'BRZ_TEST_ENV_VAR' in os.environ:
1712
del os.environ['BRZ_TEST_ENV_VAR']
1589
1713
self.addCleanup(cleanup)
1591
1715
def test_set(self):
1592
1716
"""Test that we can set an env variable"""
1593
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1717
old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
1594
1718
self.assertEqual(None, old)
1595
self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
1719
self.assertEqual('foo', os.environ.get('BRZ_TEST_ENV_VAR'))
1597
1721
def test_double_set(self):
1598
1722
"""Test that we get the old value out"""
1599
osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1600
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
1723
osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
1724
old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'bar')
1601
1725
self.assertEqual('foo', old)
1602
self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
1726
self.assertEqual('bar', os.environ.get('BRZ_TEST_ENV_VAR'))
1604
1728
def test_unicode(self):
1605
1729
"""Environment can only contain plain strings
1663
1790
class TestResourceLoading(tests.TestCaseInTempDir):
1665
1792
def test_resource_string(self):
1666
# test resource in bzrlib
1667
text = osutils.resource_string('bzrlib', 'debug.py')
1793
# test resource in breezy
1794
text = osutils.resource_string('breezy', 'debug.py')
1668
1795
self.assertContainsRe(text, "debug_flags = set()")
1669
# test resource under bzrlib
1670
text = osutils.resource_string('bzrlib.ui', 'text.py')
1796
# test resource under breezy
1797
text = osutils.resource_string('breezy.ui', 'text.py')
1671
1798
self.assertContainsRe(text, "class TextUIFactory")
1672
1799
# test unsupported package
1673
1800
self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1675
1802
# test unknown resource
1676
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1679
class TestReCompile(tests.TestCase):
1681
def test_re_compile_checked(self):
1682
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1683
self.assertTrue(r.match('aaaa'))
1684
self.assertTrue(r.match('aAaA'))
1686
def test_re_compile_checked_error(self):
1687
# like https://bugs.launchpad.net/bzr/+bug/251352
1688
err = self.assertRaises(
1689
errors.BzrCommandError,
1690
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1692
"Invalid regular expression in test case: '*': "
1693
"nothing to repeat",
1803
self.assertRaises(IOError, osutils.resource_string, 'breezy', 'yyy.xx')
1697
1806
class TestDirReader(tests.TestCaseInTempDir):
1808
scenarios = dir_reader_scenarios()
1699
1810
# Set by load_tests
1700
1811
_dir_reader_class = None
1701
1812
_native_to_unicode = None
1703
1814
def setUp(self):
1704
tests.TestCaseInTempDir.setUp(self)
1815
super(TestDirReader, self).setUp()
1705
1816
self.overrideAttr(osutils,
1706
1817
'_selected_dir_reader', self._dir_reader_class())
1796
1905
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1798
1907
for line in block:
1799
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1909
line[0:3] + (self._native_to_unicode(line[4]), ))
1800
1910
filtered_dirblocks.append((dirinfo, details))
1801
1911
return filtered_dirblocks
1803
1913
def test_walk_unicode_tree(self):
1804
self.requireFeature(tests.UnicodeFilenameFeature)
1914
self.requireFeature(features.UnicodeFilenameFeature)
1805
1915
tree, expected_dirblocks = self._get_unicode_tree()
1806
1916
self.build_tree(tree)
1807
1917
result = list(osutils._walkdirs_utf8('.'))
1808
1918
self.assertEqual(expected_dirblocks, self._filter_out(result))
1810
1920
def test_symlink(self):
1811
self.requireFeature(tests.SymlinkFeature)
1812
self.requireFeature(tests.UnicodeFilenameFeature)
1921
self.requireFeature(features.SymlinkFeature)
1922
self.requireFeature(features.UnicodeFilenameFeature)
1813
1923
target = u'target\N{Euro Sign}'
1814
1924
link_name = u'l\N{Euro Sign}nk'
1815
1925
os.symlink(target, link_name)
1816
target_utf8 = target.encode('UTF-8')
1817
1926
link_name_utf8 = link_name.encode('UTF-8')
1818
1927
expected_dirblocks = [
1820
[(link_name_utf8, link_name_utf8,
1821
'symlink', './' + link_name),],
1929
[(link_name_utf8, link_name_utf8,
1930
'symlink', './' + link_name), ],
1823
1932
result = list(osutils._walkdirs_utf8('.'))
1824
1933
self.assertEqual(expected_dirblocks, self._filter_out(result))
1894
2001
def test_failure_to_load(self):
1895
2002
self._try_loading()
1896
2003
self.assertLength(1, osutils._extension_load_failures)
1897
self.assertEquals(osutils._extension_load_failures[0],
1898
"No module named _fictional_extension_py")
2006
osutils._extension_load_failures[0],
2007
"No module named 'breezy._fictional_extension_py'")
2009
self.assertEqual(osutils._extension_load_failures[0],
2010
"No module named _fictional_extension_py")
1900
2012
def test_report_extension_load_failures_no_warning(self):
1901
2013
self.assertTrue(self._try_loading())
1902
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
2014
warnings, result = self.callCatchWarnings(
2015
osutils.report_extension_load_failures)
1903
2016
# it used to give a Python warning; it no longer does
1904
2017
self.assertLength(0, warnings)
1906
2019
def test_report_extension_load_failures_message(self):
1908
2021
trace.push_log_file(log)
1909
2022
self.assertTrue(self._try_loading())
1910
2023
osutils.report_extension_load_failures()
1911
2024
self.assertContainsRe(
1912
2025
log.getvalue(),
1913
r"bzr: warning: some compiled extensions could not be loaded; "
1914
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
2026
br"brz: warning: some compiled extensions could not be loaded; "
2027
b"see ``brz help missing-extensions``\n"
1918
2031
class TestTerminalWidth(tests.TestCase):
2034
super(TestTerminalWidth, self).setUp()
2035
self._orig_terminal_size_state = osutils._terminal_size_state
2036
self._orig_first_terminal_size = osutils._first_terminal_size
2037
self.addCleanup(self.restore_osutils_globals)
2038
osutils._terminal_size_state = 'no_data'
2039
osutils._first_terminal_size = None
2041
def restore_osutils_globals(self):
2042
osutils._terminal_size_state = self._orig_terminal_size_state
2043
osutils._first_terminal_size = self._orig_first_terminal_size
1920
2045
def replace_stdout(self, new):
1921
2046
self.overrideAttr(sys, 'stdout', new)
2000
2130
def test_copy_ownership_from_path(self):
2001
2131
"""copy_ownership_from_path test with specified src."""
2003
f = open('test_file', 'wt')
2133
open('test_file', 'wt').close()
2004
2134
osutils.copy_ownership_from_path('test_file', ownsrc)
2006
2136
s = os.stat(ownsrc)
2007
self.assertEquals(self.path, 'test_file')
2008
self.assertEquals(self.uid, s.st_uid)
2009
self.assertEquals(self.gid, s.st_gid)
2137
self.assertEqual(self.path, 'test_file')
2138
self.assertEqual(self.uid, s.st_uid)
2139
self.assertEqual(self.gid, s.st_gid)
2011
2141
def test_copy_ownership_nonesrc(self):
2012
2142
"""copy_ownership_from_path test with src=None."""
2013
f = open('test_file', 'wt')
2143
open('test_file', 'wt').close()
2014
2144
# should use parent dir for permissions
2015
2145
osutils.copy_ownership_from_path('test_file')
2017
2147
s = os.stat('..')
2018
self.assertEquals(self.path, 'test_file')
2019
self.assertEquals(self.uid, s.st_uid)
2020
self.assertEquals(self.gid, s.st_gid)
2148
self.assertEqual(self.path, 'test_file')
2149
self.assertEqual(self.uid, s.st_uid)
2150
self.assertEqual(self.gid, s.st_gid)
2153
class TestPathFromEnviron(tests.TestCase):
2155
def test_is_unicode(self):
2156
self.overrideEnv('BRZ_TEST_PATH', './anywhere at all/')
2157
path = osutils.path_from_environ('BRZ_TEST_PATH')
2158
self.assertIsInstance(path, text_type)
2159
self.assertEqual(u'./anywhere at all/', path)
2161
def test_posix_path_env_ascii(self):
2162
self.overrideEnv('BRZ_TEST_PATH', '/tmp')
2163
home = osutils._posix_path_from_environ('BRZ_TEST_PATH')
2164
self.assertIsInstance(home, text_type)
2165
self.assertEqual(u'/tmp', home)
2167
def test_posix_path_env_unicode(self):
2168
self.requireFeature(features.ByteStringNamedFilesystem)
2169
self.overrideEnv('BRZ_TEST_PATH', '/home/\xa7test')
2170
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2171
self.assertEqual(u'/home/\xa7test',
2172
osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2173
osutils._fs_enc = "iso8859-5"
2175
# In Python 3, os.environ returns unicode.
2176
self.assertEqual(u'/home/\xa7test',
2177
osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2179
self.assertEqual(u'/home/\u0407test',
2180
osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2181
osutils._fs_enc = "utf-8"
2183
errors.BadFilenameEncoding,
2184
osutils._posix_path_from_environ, 'BRZ_TEST_PATH')
2187
class TestGetHomeDir(tests.TestCase):
2189
def test_is_unicode(self):
2190
home = osutils._get_home_dir()
2191
self.assertIsInstance(home, text_type)
2193
def test_posix_homeless(self):
2194
self.overrideEnv('HOME', None)
2195
home = osutils._get_home_dir()
2196
self.assertIsInstance(home, text_type)
2198
def test_posix_home_ascii(self):
2199
self.overrideEnv('HOME', '/home/test')
2200
home = osutils._posix_get_home_dir()
2201
self.assertIsInstance(home, text_type)
2202
self.assertEqual(u'/home/test', home)
2204
def test_posix_home_unicode(self):
2205
self.requireFeature(features.ByteStringNamedFilesystem)
2206
self.overrideEnv('HOME', '/home/\xa7test')
2207
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2208
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2209
osutils._fs_enc = "iso8859-5"
2211
# In python 3, os.environ returns unicode
2212
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2214
self.assertEqual(u'/home/\u0407test',
2215
osutils._posix_get_home_dir())
2216
osutils._fs_enc = "utf-8"
2217
self.assertRaises(errors.BadFilenameEncoding,
2218
osutils._posix_get_home_dir)
2221
class TestGetuserUnicode(tests.TestCase):
2223
def test_is_unicode(self):
2224
user = osutils.getuser_unicode()
2225
self.assertIsInstance(user, text_type)
2227
def envvar_to_override(self):
2228
if sys.platform == "win32":
2229
# Disable use of platform calls on windows so envvar is used
2230
self.overrideAttr(win32utils, 'has_ctypes', False)
2231
return 'USERNAME' # only variable used on windows
2232
return 'LOGNAME' # first variable checked by getpass.getuser()
2234
def test_ascii_user(self):
2235
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2236
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2238
def test_unicode_user(self):
2239
ue = osutils.get_user_encoding()
2240
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2242
raise tests.TestSkipped(
2243
'Cannot find a unicode character that works in encoding %s'
2244
% (osutils.get_user_encoding(),))
2245
uni_username = u'jrandom' + uni_val
2246
encoded_username = uni_username.encode(ue)
2248
self.overrideEnv(self.envvar_to_override(), uni_username)
2250
self.overrideEnv(self.envvar_to_override(), encoded_username)
2251
self.assertEqual(uni_username, osutils.getuser_unicode())
2254
class TestBackupNames(tests.TestCase):
2257
super(TestBackupNames, self).setUp()
2260
def backup_exists(self, name):
2261
return name in self.backups
2263
def available_backup_name(self, name):
2264
backup_name = osutils.available_backup_name(name, self.backup_exists)
2265
self.backups.append(backup_name)
2268
def assertBackupName(self, expected, name):
2269
self.assertEqual(expected, self.available_backup_name(name))
2271
def test_empty(self):
2272
self.assertBackupName('file.~1~', 'file')
2274
def test_existing(self):
2275
self.available_backup_name('file')
2276
self.available_backup_name('file')
2277
self.assertBackupName('file.~3~', 'file')
2278
# Empty slots are found, this is not a strict requirement and may be
2279
# revisited if we test against all implementations.
2280
self.backups.remove('file.~2~')
2281
self.assertBackupName('file.~2~', 'file')
2284
class TestFindExecutableInPath(tests.TestCase):
2286
def test_windows(self):
2287
if sys.platform != 'win32':
2288
raise tests.TestSkipped('test requires win32')
2289
self.assertTrue(osutils.find_executable_on_path(
2290
'explorer') is not None)
2292
osutils.find_executable_on_path('explorer.exe') is not None)
2294
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2296
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2297
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2299
def test_windows_app_path(self):
2300
if sys.platform != 'win32':
2301
raise tests.TestSkipped('test requires win32')
2302
# Override PATH env var so that exe can only be found on App Path
2303
self.overrideEnv('PATH', '')
2304
# Internt Explorer is always registered in the App Path
2305
self.assertTrue(osutils.find_executable_on_path(
2306
'iexplore') is not None)
2308
def test_other(self):
2309
if sys.platform == 'win32':
2310
raise tests.TestSkipped('test requires non-win32')
2311
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2313
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2316
class TestEnvironmentErrors(tests.TestCase):
2317
"""Test handling of environmental errors"""
2319
def test_is_oserror(self):
2320
self.assertTrue(osutils.is_environment_error(
2321
OSError(errno.EINVAL, "Invalid parameter")))
2323
def test_is_ioerror(self):
2324
self.assertTrue(osutils.is_environment_error(
2325
IOError(errno.EINVAL, "Invalid parameter")))
2327
def test_is_socket_error(self):
2328
self.assertTrue(osutils.is_environment_error(
2329
socket.error(errno.EINVAL, "Invalid parameter")))
2331
def test_is_select_error(self):
2332
self.assertTrue(osutils.is_environment_error(
2333
select.error(errno.EINVAL, "Invalid parameter")))
2335
def test_is_pywintypes_error(self):
2336
self.requireFeature(features.pywintypes)
2338
self.assertTrue(osutils.is_environment_error(
2339
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))