98
load_tests = load_tests_apply_scenarios
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)
101
109
class TestContainsWhitespace(tests.TestCase):
103
111
def test_contains_whitespace(self):
104
self.assertTrue(osutils.contains_whitespace(u' '))
105
self.assertTrue(osutils.contains_whitespace(u'hello there'))
106
self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
107
self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
108
self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
109
self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
112
self.failUnless(osutils.contains_whitespace(u' '))
113
self.failUnless(osutils.contains_whitespace(u'hello there'))
114
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
115
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
116
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
117
self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
111
119
# \xa0 is "Non-breaking-space" which on some python locales thinks it
112
120
# is whitespace, but we do not.
113
self.assertFalse(osutils.contains_whitespace(u''))
114
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
115
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
121
self.failIf(osutils.contains_whitespace(u''))
122
self.failIf(osutils.contains_whitespace(u'hellothere'))
123
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
118
126
class TestRename(tests.TestCaseInTempDir):
131
139
def test_fancy_rename(self):
132
140
# This should work everywhere
133
self.create_file('a', b'something in a\n')
141
self.create_file('a', 'something in a\n')
134
142
self._fancy_rename('a', 'b')
135
self.assertPathDoesNotExist('a')
136
self.assertPathExists('b')
137
self.check_file_contents('b', b'something in a\n')
143
self.failIfExists('a')
144
self.failUnlessExists('b')
145
self.check_file_contents('b', 'something in a\n')
139
self.create_file('a', b'new something in a\n')
147
self.create_file('a', 'new something in a\n')
140
148
self._fancy_rename('b', 'a')
142
self.check_file_contents('a', b'something in a\n')
150
self.check_file_contents('a', 'something in a\n')
144
152
def test_fancy_rename_fails_source_missing(self):
145
153
# An exception should be raised, and the target should be left in place
146
self.create_file('target', b'data in target\n')
154
self.create_file('target', 'data in target\n')
147
155
self.assertRaises((IOError, OSError), self._fancy_rename,
148
156
'missingsource', 'target')
149
self.assertPathExists('target')
150
self.check_file_contents('target', b'data in target\n')
157
self.failUnlessExists('target')
158
self.check_file_contents('target', 'data in target\n')
152
160
def test_fancy_rename_fails_if_source_and_target_missing(self):
153
161
self.assertRaises((IOError, OSError), self._fancy_rename,
433
413
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
436
class TestFdatasync(tests.TestCaseInTempDir):
438
def do_fdatasync(self):
439
f = tempfile.NamedTemporaryFile()
440
osutils.fdatasync(f.fileno())
444
def raise_eopnotsupp(*args, **kwargs):
445
raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
448
def raise_enotsup(*args, **kwargs):
449
raise IOError(errno.ENOTSUP, os.strerror(errno.ENOTSUP))
451
def test_fdatasync_handles_system_function(self):
452
self.overrideAttr(os, "fdatasync")
455
def test_fdatasync_handles_no_fdatasync_no_fsync(self):
456
self.overrideAttr(os, "fdatasync")
457
self.overrideAttr(os, "fsync")
460
def test_fdatasync_handles_no_EOPNOTSUPP(self):
461
self.overrideAttr(errno, "EOPNOTSUPP")
464
def test_fdatasync_catches_ENOTSUP(self):
465
enotsup = getattr(errno, "ENOTSUP", None)
467
raise tests.TestNotApplicable("No ENOTSUP on this platform")
468
self.overrideAttr(os, "fdatasync", self.raise_enotsup)
471
def test_fdatasync_catches_EOPNOTSUPP(self):
472
enotsup = getattr(errno, "EOPNOTSUPP", None)
474
raise tests.TestNotApplicable("No EOPNOTSUPP on this platform")
475
self.overrideAttr(os, "fdatasync", self.raise_eopnotsupp)
479
416
class TestLinks(tests.TestCaseInTempDir):
481
418
def test_dereference_path(self):
482
self.requireFeature(features.SymlinkFeature)
419
self.requireFeature(tests.SymlinkFeature)
483
420
cwd = osutils.realpath('.')
485
422
bar_path = osutils.pathjoin(cwd, 'bar')
607
545
self.assertTrue(self.test_data_len > self.block_size)
609
547
from_file = file_utils.FakeReadFile(self.test_data)
612
# read (max // 2) bytes and verify read size wasn't affected
613
num_bytes_to_read = self.block_size // 2
614
osutils.pumpfile(from_file, to_file,
615
num_bytes_to_read, self.block_size)
550
# read (max / 2) bytes and verify read size wasn't affected
551
num_bytes_to_read = self.block_size / 2
552
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
616
553
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
617
554
self.assertEqual(from_file.get_read_count(), 1)
619
556
# read (max) bytes and verify read size wasn't affected
620
557
num_bytes_to_read = self.block_size
621
558
from_file.reset_read_count()
622
osutils.pumpfile(from_file, to_file,
623
num_bytes_to_read, self.block_size)
559
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
624
560
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
625
561
self.assertEqual(from_file.get_read_count(), 1)
627
563
# read (max + 1) bytes and verify read size was limited
628
564
num_bytes_to_read = self.block_size + 1
629
565
from_file.reset_read_count()
630
osutils.pumpfile(from_file, to_file,
631
num_bytes_to_read, self.block_size)
566
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
632
567
self.assertEqual(from_file.get_max_read_size(), self.block_size)
633
568
self.assertEqual(from_file.get_read_count(), 2)
635
570
# finish reading the rest of the data
636
571
num_bytes_to_read = self.test_data_len - to_file.tell()
637
osutils.pumpfile(from_file, to_file,
638
num_bytes_to_read, self.block_size)
572
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
640
574
# report error if the data wasn't equal (we only report the size due
641
575
# to the length of the data)
727
660
(36, 'write')], activity)
729
662
# And with a limited amount of data
730
from_file = BytesIO(self.test_data)
663
from_file = StringIO(self.test_data)
733
666
osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
734
667
report_activity=log_activity, direction='read')
736
[(500, 'read'), (500, 'read'), (28, 'read')], activity)
668
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
739
672
class TestPumpStringFile(tests.TestCase):
741
674
def test_empty(self):
743
osutils.pump_string_file(b"", output)
744
self.assertEqual(b"", output.getvalue())
676
osutils.pump_string_file("", output)
677
self.assertEqual("", output.getvalue())
746
679
def test_more_than_segment_size(self):
748
osutils.pump_string_file(b"123456789", output, 2)
749
self.assertEqual(b"123456789", output.getvalue())
681
osutils.pump_string_file("123456789", output, 2)
682
self.assertEqual("123456789", output.getvalue())
751
684
def test_segment_size(self):
753
osutils.pump_string_file(b"12", output, 2)
754
self.assertEqual(b"12", output.getvalue())
686
osutils.pump_string_file("12", output, 2)
687
self.assertEqual("12", output.getvalue())
756
689
def test_segment_size_multiple(self):
758
osutils.pump_string_file(b"1234", output, 2)
759
self.assertEqual(b"1234", output.getvalue())
691
osutils.pump_string_file("1234", output, 2)
692
self.assertEqual("1234", output.getvalue())
762
695
class TestRelpath(tests.TestCase):
790
723
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
792
725
def test_from_utf8_string(self):
793
self.assertEqual(u'foo\xae', osutils.safe_unicode(b'foo\xc2\xae'))
726
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
795
728
def test_bad_utf8_string(self):
796
729
self.assertRaises(errors.BzrBadParameterNotUnicode,
797
730
osutils.safe_unicode,
801
734
class TestSafeUtf8(tests.TestCase):
803
736
def test_from_ascii_string(self):
805
self.assertEqual(b'foobar', osutils.safe_utf8(f))
738
self.assertEqual('foobar', osutils.safe_utf8(f))
807
740
def test_from_unicode_string_ascii_contents(self):
808
self.assertEqual(b'bargam', osutils.safe_utf8(u'bargam'))
741
self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
810
743
def test_from_unicode_string_unicode_contents(self):
811
self.assertEqual(b'bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
744
self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
813
746
def test_from_utf8_string(self):
814
self.assertEqual(b'foo\xc2\xae', osutils.safe_utf8(b'foo\xc2\xae'))
747
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
816
749
def test_bad_utf8_string(self):
817
750
self.assertRaises(errors.BzrBadParameterNotUnicode,
818
osutils.safe_utf8, b'\xbb\xbb')
821
class TestSendAll(tests.TestCase):
823
def test_send_with_disconnected_socket(self):
824
class DisconnectedSocket(object):
825
def __init__(self, err):
828
def send(self, content):
833
# All of these should be treated as ConnectionReset
835
for err_cls in (IOError, socket.error):
836
for errnum in osutils._end_of_stream_errors:
837
errs.append(err_cls(errnum))
839
sock = DisconnectedSocket(err)
840
self.assertRaises(errors.ConnectionReset,
841
osutils.send_all, sock, b'some more content')
843
def test_send_with_no_progress(self):
844
# See https://bugs.launchpad.net/bzr/+bug/1047309
845
# It seems that paramiko can get into a state where it doesn't error,
846
# but it returns 0 bytes sent for requests over and over again.
847
class NoSendingSocket(object):
851
def send(self, bytes):
853
if self.call_count > 100:
854
# Prevent the test suite from hanging
855
raise RuntimeError('too many calls')
857
sock = NoSendingSocket()
858
self.assertRaises(errors.ConnectionReset,
859
osutils.send_all, sock, b'content')
860
self.assertEqual(1, sock.call_count)
863
class TestPosixFuncs(tests.TestCase):
864
"""Test that the posix version of normpath returns an appropriate path
865
when used with 2 leading slashes."""
867
def test_normpath(self):
868
self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
870
'/etc/shadow', osutils._posix_normpath('//etc/shadow'))
872
'/etc/shadow', osutils._posix_normpath('///etc/shadow'))
751
osutils.safe_utf8, '\xbb\xbb')
754
class TestSafeRevisionId(tests.TestCase):
756
def test_from_ascii_string(self):
757
# this shouldn't give a warning because it's getting an ascii string
758
self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
760
def test_from_unicode_string_ascii_contents(self):
761
self.assertEqual('bargam',
762
osutils.safe_revision_id(u'bargam', warn=False))
764
def test_from_unicode_deprecated(self):
765
self.assertEqual('bargam',
766
self.callDeprecated([osutils._revision_id_warning],
767
osutils.safe_revision_id, u'bargam'))
769
def test_from_unicode_string_unicode_contents(self):
770
self.assertEqual('bargam\xc2\xae',
771
osutils.safe_revision_id(u'bargam\xae', warn=False))
773
def test_from_utf8_string(self):
774
self.assertEqual('foo\xc2\xae',
775
osutils.safe_revision_id('foo\xc2\xae'))
778
"""Currently, None is a valid revision_id"""
779
self.assertEqual(None, osutils.safe_revision_id(None))
782
class TestSafeFileId(tests.TestCase):
784
def test_from_ascii_string(self):
785
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
787
def test_from_unicode_string_ascii_contents(self):
788
self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
790
def test_from_unicode_deprecated(self):
791
self.assertEqual('bargam',
792
self.callDeprecated([osutils._file_id_warning],
793
osutils.safe_file_id, u'bargam'))
795
def test_from_unicode_string_unicode_contents(self):
796
self.assertEqual('bargam\xc2\xae',
797
osutils.safe_file_id(u'bargam\xae', warn=False))
799
def test_from_utf8_string(self):
800
self.assertEqual('foo\xc2\xae',
801
osutils.safe_file_id('foo\xc2\xae'))
804
"""Currently, None is a valid revision_id"""
805
self.assertEqual(None, osutils.safe_file_id(None))
875
808
class TestWin32Funcs(tests.TestCase):
876
809
"""Test that _win32 versions of os utilities return appropriate paths."""
878
811
def test_abspath(self):
879
self.requireFeature(features.win32_feature)
880
812
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
881
813
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
882
814
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
942
887
def test_minimum_path_selection(self):
943
888
self.assertEqual(set(),
944
osutils.minimum_path_selection([]))
945
self.assertEqual({'a'},
946
osutils.minimum_path_selection(['a']))
947
self.assertEqual({'a', 'b'},
948
osutils.minimum_path_selection(['a', 'b']))
949
self.assertEqual({'a/', 'b'},
950
osutils.minimum_path_selection(['a/', 'b']))
951
self.assertEqual({'a/', 'b'},
952
osutils.minimum_path_selection(['a/c', 'a/', 'b']))
953
self.assertEqual({'a-b', 'a', 'a0b'},
954
osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
889
osutils.minimum_path_selection([]))
890
self.assertEqual(set(['a']),
891
osutils.minimum_path_selection(['a']))
892
self.assertEqual(set(['a', 'b']),
893
osutils.minimum_path_selection(['a', 'b']))
894
self.assertEqual(set(['a/', 'b']),
895
osutils.minimum_path_selection(['a/', 'b']))
896
self.assertEqual(set(['a/', 'b']),
897
osutils.minimum_path_selection(['a/c', 'a/', 'b']))
898
self.assertEqual(set(['a-b', 'a', 'a0b']),
899
osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
956
901
def test_mkdtemp(self):
957
902
tmpdir = osutils._win32_mkdtemp(dir='.')
958
903
self.assertFalse('\\' in tmpdir)
960
905
def test_rename(self):
961
with open('a', 'wb') as a:
963
with open('b', 'wb') as b:
966
913
osutils._win32_rename('b', 'a')
967
self.assertPathExists('a')
968
self.assertPathDoesNotExist('b')
969
self.assertFileEqual(b'baz\n', 'a')
914
self.failUnlessExists('a')
915
self.failIfExists('b')
916
self.assertFileEqual('baz\n', 'a')
971
918
def test_rename_missing_file(self):
972
with open('a', 'wb') as a:
976
924
osutils._win32_rename('b', 'a')
977
except (IOError, OSError) as e:
925
except (IOError, OSError), e:
978
926
self.assertEqual(errno.ENOENT, e.errno)
979
self.assertFileEqual(b'foo\n', 'a')
927
self.assertFileEqual('foo\n', 'a')
981
929
def test_rename_missing_dir(self):
984
932
osutils._win32_rename('b', 'a')
985
except (IOError, OSError) as e:
933
except (IOError, OSError), e:
986
934
self.assertEqual(errno.ENOENT, e.errno)
988
936
def test_rename_current_dir(self):
1187
1131
self.build_tree(tree)
1188
1132
expected_dirblocks = [
1190
[('0file', '0file', 'file'),
1191
('1dir', '1dir', 'directory'),
1192
('2file', '2file', 'file'),
1195
(('1dir', './1dir'),
1196
[('1dir/0file', '0file', 'file'),
1197
('1dir/1dir', '1dir', 'directory'),
1200
(('1dir/1dir', './1dir/1dir'),
1134
[('0file', '0file', 'file'),
1135
('1dir', '1dir', 'directory'),
1136
('2file', '2file', 'file'),
1139
(('1dir', './1dir'),
1140
[('1dir/0file', '0file', 'file'),
1141
('1dir/1dir', '1dir', 'directory'),
1144
(('1dir/1dir', './1dir/1dir'),
1206
1150
found_bzrdir = False
1207
for dirdetail, dirblock in osutils._walkdirs_utf8(b'.'):
1208
if len(dirblock) and dirblock[0][1] == b'.bzr':
1151
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1152
if len(dirblock) and dirblock[0][1] == '.bzr':
1209
1153
# this tests the filtering of selected paths
1210
1154
found_bzrdir = True
1211
1155
del dirblock[0]
1212
dirdetail = (dirdetail[0].decode('utf-8'),
1213
osutils.safe_unicode(dirdetail[1]))
1215
(entry[0].decode('utf-8'), entry[1].decode('utf-8'), entry[2])
1216
for entry in dirblock]
1217
1156
result.append((dirdetail, dirblock))
1219
1158
self.assertTrue(found_bzrdir)
1235
1174
dirblock[:] = new_dirblock
1237
1176
def _save_platform_info(self):
1177
self.overrideAttr(win32utils, 'winver')
1238
1178
self.overrideAttr(osutils, '_fs_enc')
1239
1179
self.overrideAttr(osutils, '_selected_dir_reader')
1241
def assertDirReaderIs(self, expected, top):
1181
def assertDirReaderIs(self, expected):
1242
1182
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
1243
1183
# Force it to redetect
1244
1184
osutils._selected_dir_reader = None
1245
1185
# Nothing to list, but should still trigger the selection logic
1246
self.assertEqual([((b'', top), [])], list(osutils._walkdirs_utf8('.')))
1186
self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
1247
1187
self.assertIsInstance(osutils._selected_dir_reader, expected)
1249
1189
def test_force_walkdirs_utf8_fs_utf8(self):
1250
1190
self.requireFeature(UTF8DirReaderFeature)
1251
1191
self._save_platform_info()
1252
osutils._fs_enc = 'utf-8'
1253
self.assertDirReaderIs(UTF8DirReaderFeature.module.UTF8DirReader, b".")
1192
win32utils.winver = None # Avoid the win32 detection code
1193
osutils._fs_enc = 'UTF-8'
1194
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1255
1196
def test_force_walkdirs_utf8_fs_ascii(self):
1256
1197
self.requireFeature(UTF8DirReaderFeature)
1257
1198
self._save_platform_info()
1258
osutils._fs_enc = 'ascii'
1259
self.assertDirReaderIs(
1260
UTF8DirReaderFeature.module.UTF8DirReader, b".")
1199
win32utils.winver = None # Avoid the win32 detection code
1200
osutils._fs_enc = 'US-ASCII'
1201
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1203
def test_force_walkdirs_utf8_fs_ANSI(self):
1204
self.requireFeature(UTF8DirReaderFeature)
1205
self._save_platform_info()
1206
win32utils.winver = None # Avoid the win32 detection code
1207
osutils._fs_enc = 'ANSI_X3.4-1968'
1208
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1262
1210
def test_force_walkdirs_utf8_fs_latin1(self):
1263
1211
self._save_platform_info()
1264
osutils._fs_enc = 'iso-8859-1'
1265
self.assertDirReaderIs(osutils.UnicodeDirReader, ".")
1212
win32utils.winver = None # Avoid the win32 detection code
1213
osutils._fs_enc = 'latin1'
1214
self.assertDirReaderIs(osutils.UnicodeDirReader)
1267
1216
def test_force_walkdirs_utf8_nt(self):
1268
1217
# Disabled because the thunk of the whole walkdirs api is disabled.
1269
1218
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1270
1219
self._save_platform_info()
1271
from .._walkdirs_win32 import Win32ReadDir
1272
self.assertDirReaderIs(Win32ReadDir, ".")
1220
win32utils.winver = 'Windows NT'
1221
from bzrlib._walkdirs_win32 import Win32ReadDir
1222
self.assertDirReaderIs(Win32ReadDir)
1224
def test_force_walkdirs_utf8_98(self):
1225
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1226
self._save_platform_info()
1227
win32utils.winver = 'Windows 98'
1228
self.assertDirReaderIs(osutils.UnicodeDirReader)
1274
1230
def test_unicode_walkdirs(self):
1275
1231
"""Walkdirs should always return unicode paths."""
1276
self.requireFeature(features.UnicodeFilenameFeature)
1232
self.requireFeature(tests.UnicodeFilenameFeature)
1277
1233
name0 = u'0file-\xb6'
1278
1234
name1 = u'1dir-\u062c\u0648'
1279
1235
name2 = u'2file-\u0633'
1287
1243
self.build_tree(tree)
1288
1244
expected_dirblocks = [
1290
[(name0, name0, 'file', './' + name0),
1291
(name1, name1, 'directory', './' + name1),
1292
(name2, name2, 'file', './' + name2),
1295
((name1, './' + name1),
1296
[(name1 + '/' + name0, name0, 'file', './' + name1
1298
(name1 + '/' + name1, name1, 'directory', './' + name1
1302
((name1 + '/' + name1, './' + name1 + '/' + name1),
1246
[(name0, name0, 'file', './' + name0),
1247
(name1, name1, 'directory', './' + name1),
1248
(name2, name2, 'file', './' + name2),
1251
((name1, './' + name1),
1252
[(name1 + '/' + name0, name0, 'file', './' + name1
1254
(name1 + '/' + name1, name1, 'directory', './' + name1
1258
((name1 + '/' + name1, './' + name1 + '/' + name1),
1307
1263
result = list(osutils.walkdirs('.'))
1308
1264
self._filter_out_stat(result)
1309
1265
self.assertEqual(expected_dirblocks, result)
1310
result = list(osutils.walkdirs(u'./' + name1, name1))
1266
result = list(osutils.walkdirs(u'./'+name1, name1))
1311
1267
self._filter_out_stat(result)
1312
1268
self.assertEqual(expected_dirblocks[1:], result)
1333
1289
name2 = name2.encode('utf8')
1335
1291
expected_dirblocks = [
1337
[(name0, name0, 'file', b'./' + name0),
1338
(name1, name1, 'directory', b'./' + name1),
1339
(name2, name2, 'file', b'./' + name2),
1342
((name1, b'./' + name1),
1343
[(name1 + b'/' + name0, name0, 'file', b'./' + name1
1345
(name1 + b'/' + name1, name1, 'directory', b'./' + name1
1349
((name1 + b'/' + name1, b'./' + name1 + b'/' + name1),
1293
[(name0, name0, 'file', './' + name0),
1294
(name1, name1, 'directory', './' + name1),
1295
(name2, name2, 'file', './' + name2),
1298
((name1, './' + name1),
1299
[(name1 + '/' + name0, name0, 'file', './' + name1
1301
(name1 + '/' + name1, name1, 'directory', './' + name1
1305
((name1 + '/' + name1, './' + name1 + '/' + name1),
1355
1311
# For ease in testing, if walkdirs_utf8 returns Unicode, assert that
1356
1312
# all abspaths are Unicode, and encode them back into utf8.
1357
1313
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1358
self.assertIsInstance(dirdetail[0], bytes)
1359
if isinstance(dirdetail[1], str):
1314
self.assertIsInstance(dirdetail[0], str)
1315
if isinstance(dirdetail[1], unicode):
1360
1316
dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
1361
1317
dirblock = [list(info) for info in dirblock]
1362
1318
for info in dirblock:
1363
self.assertIsInstance(info[4], str)
1319
self.assertIsInstance(info[4], unicode)
1364
1320
info[4] = info[4].encode('utf8')
1365
1321
new_dirblock = []
1366
1322
for info in dirblock:
1367
self.assertIsInstance(info[0], bytes)
1368
self.assertIsInstance(info[1], bytes)
1369
self.assertIsInstance(info[4], bytes)
1323
self.assertIsInstance(info[0], str)
1324
self.assertIsInstance(info[1], str)
1325
self.assertIsInstance(info[4], str)
1370
1326
# Remove the stat information
1371
1327
new_dirblock.append((info[0], info[1], info[2], info[4]))
1372
1328
result.append((dirdetail, new_dirblock))
1400
1356
# All of the abspaths should be in unicode, all of the relative paths
1401
1357
# should be in utf8
1402
1358
expected_dirblocks = [
1404
[(name0, name0, 'file', './' + name0u),
1405
(name1, name1, 'directory', './' + name1u),
1406
(name2, name2, 'file', './' + name2u),
1409
((name1, './' + name1u),
1410
[(name1 + b'/' + name0, name0, 'file', './' + name1u
1412
(name1 + b'/' + name1, name1, 'directory', './' + name1u
1416
((name1 + b'/' + name1, './' + name1u + '/' + name1u),
1360
[(name0, name0, 'file', './' + name0u),
1361
(name1, name1, 'directory', './' + name1u),
1362
(name2, name2, 'file', './' + name2u),
1365
((name1, './' + name1u),
1366
[(name1 + '/' + name0, name0, 'file', './' + name1u
1368
(name1 + '/' + name1, name1, 'directory', './' + name1u
1372
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1421
1377
result = list(osutils._walkdirs_utf8('.'))
1422
1378
self._filter_out_stat(result)
1446
1402
# All of the abspaths should be in unicode, all of the relative paths
1447
1403
# should be in utf8
1448
1404
expected_dirblocks = [
1450
[(name0, name0, 'file', './' + name0u),
1451
(name1, name1, 'directory', './' + name1u),
1452
(name2, name2, 'file', './' + name2u),
1455
((name1, './' + name1u),
1456
[(name1 + '/' + name0, name0, 'file', './' + name1u
1458
(name1 + '/' + name1, name1, 'directory', './' + name1u
1462
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1406
[(name0, name0, 'file', './' + name0u),
1407
(name1, name1, 'directory', './' + name1u),
1408
(name2, name2, 'file', './' + name2u),
1411
((name1, './' + name1u),
1412
[(name1 + '/' + name0, name0, 'file', './' + name1u
1414
(name1 + '/' + name1, name1, 'directory', './' + name1u
1418
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1467
1423
result = list(osutils._walkdirs_utf8(u'.'))
1468
1424
self._filter_out_stat(result)
1653
1609
def setUp(self):
1654
1610
super(TestSetUnsetEnv, self).setUp()
1656
self.assertEqual(None, os.environ.get('BRZ_TEST_ENV_VAR'),
1612
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
1657
1613
'Environment was not cleaned up properly.'
1658
' Variable BRZ_TEST_ENV_VAR should not exist.')
1614
' Variable BZR_TEST_ENV_VAR should not exist.')
1661
if 'BRZ_TEST_ENV_VAR' in os.environ:
1662
del os.environ['BRZ_TEST_ENV_VAR']
1616
if 'BZR_TEST_ENV_VAR' in os.environ:
1617
del os.environ['BZR_TEST_ENV_VAR']
1663
1618
self.addCleanup(cleanup)
1665
1620
def test_set(self):
1666
1621
"""Test that we can set an env variable"""
1667
old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
1622
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1668
1623
self.assertEqual(None, old)
1669
self.assertEqual('foo', os.environ.get('BRZ_TEST_ENV_VAR'))
1624
self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
1671
1626
def test_double_set(self):
1672
1627
"""Test that we get the old value out"""
1673
osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'foo')
1674
old = osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', 'bar')
1628
osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
1629
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
1675
1630
self.assertEqual('foo', old)
1676
self.assertEqual('bar', os.environ.get('BRZ_TEST_ENV_VAR'))
1631
self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
1678
1633
def test_unicode(self):
1679
1634
"""Environment can only contain plain strings
1737
1692
class TestResourceLoading(tests.TestCaseInTempDir):
1739
1694
def test_resource_string(self):
1740
# test resource in breezy
1741
text = osutils.resource_string('breezy', 'debug.py')
1695
# test resource in bzrlib
1696
text = osutils.resource_string('bzrlib', 'debug.py')
1742
1697
self.assertContainsRe(text, "debug_flags = set()")
1743
# test resource under breezy
1744
text = osutils.resource_string('breezy.ui', 'text.py')
1698
# test resource under bzrlib
1699
text = osutils.resource_string('bzrlib.ui', 'text.py')
1745
1700
self.assertContainsRe(text, "class TextUIFactory")
1746
1701
# test unsupported package
1747
1702
self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
1749
1704
# test unknown resource
1750
self.assertRaises(IOError, osutils.resource_string, 'breezy', 'yyy.xx')
1705
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1708
class TestReCompile(tests.TestCase):
1710
def _deprecated_re_compile_checked(self, *args, **kwargs):
1711
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1712
osutils.re_compile_checked, *args, **kwargs)
1714
def test_re_compile_checked(self):
1715
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1716
self.assertTrue(r.match('aaaa'))
1717
self.assertTrue(r.match('aAaA'))
1719
def test_re_compile_checked_error(self):
1720
# like https://bugs.launchpad.net/bzr/+bug/251352
1722
# Due to possible test isolation error, re.compile is not lazy at
1723
# this point. We re-install lazy compile.
1724
lazy_regex.install_lazy_compile()
1725
err = self.assertRaises(
1726
errors.BzrCommandError,
1727
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1729
'Invalid regular expression in test case: '
1730
'"*" nothing to repeat',
1753
1734
class TestDirReader(tests.TestCaseInTempDir):
1755
scenarios = dir_reader_scenarios()
1757
1736
# Set by load_tests
1758
1737
_dir_reader_class = None
1759
1738
_native_to_unicode = None
1761
1740
def setUp(self):
1762
super(TestDirReader, self).setUp()
1741
tests.TestCaseInTempDir.setUp(self)
1763
1742
self.overrideAttr(osutils,
1764
1743
'_selected_dir_reader', self._dir_reader_class())
1822
1803
name1 = name1u.encode('UTF-8')
1823
1804
name2 = name2u.encode('UTF-8')
1824
1805
expected_dirblocks = [
1826
[(name0, name0, 'file', './' + name0u),
1827
(name1, name1, 'directory', './' + name1u),
1828
(name2, name2, 'file', './' + name2u),
1831
((name1, './' + name1u),
1832
[(name1 + b'/' + name0, name0, 'file', './' + name1u
1834
(name1 + b'/' + name1, name1, 'directory', './' + name1u
1838
((name1 + b'/' + name1, './' + name1u + '/' + name1u),
1807
[(name0, name0, 'file', './' + name0u),
1808
(name1, name1, 'directory', './' + name1u),
1809
(name2, name2, 'file', './' + name2u),
1812
((name1, './' + name1u),
1813
[(name1 + '/' + name0, name0, 'file', './' + name1u
1815
(name1 + '/' + name1, name1, 'directory', './' + name1u
1819
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1843
1824
return tree, expected_dirblocks
1852
1833
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1854
1835
for line in block:
1856
line[0:3] + (self._native_to_unicode(line[4]), ))
1836
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1857
1837
filtered_dirblocks.append((dirinfo, details))
1858
1838
return filtered_dirblocks
1860
1840
def test_walk_unicode_tree(self):
1861
self.requireFeature(features.UnicodeFilenameFeature)
1841
self.requireFeature(tests.UnicodeFilenameFeature)
1862
1842
tree, expected_dirblocks = self._get_unicode_tree()
1863
1843
self.build_tree(tree)
1864
1844
result = list(osutils._walkdirs_utf8('.'))
1865
1845
self.assertEqual(expected_dirblocks, self._filter_out(result))
1867
1847
def test_symlink(self):
1868
self.requireFeature(features.SymlinkFeature)
1869
self.requireFeature(features.UnicodeFilenameFeature)
1848
self.requireFeature(tests.SymlinkFeature)
1849
self.requireFeature(tests.UnicodeFilenameFeature)
1870
1850
target = u'target\N{Euro Sign}'
1871
1851
link_name = u'l\N{Euro Sign}nk'
1872
1852
os.symlink(target, link_name)
1853
target_utf8 = target.encode('UTF-8')
1873
1854
link_name_utf8 = link_name.encode('UTF-8')
1874
1855
expected_dirblocks = [
1876
[(link_name_utf8, link_name_utf8,
1877
'symlink', './' + link_name), ],
1857
[(link_name_utf8, link_name_utf8,
1858
'symlink', './' + link_name),],
1879
1860
result = list(osutils._walkdirs_utf8('.'))
1880
1861
self.assertEqual(expected_dirblocks, self._filter_out(result))
2002
1983
def test_default_values(self):
2003
1984
self.assertEqual(80, osutils.default_terminal_width)
2005
def test_defaults_to_BRZ_COLUMNS(self):
2006
# BRZ_COLUMNS is set by the test framework
2007
self.assertNotEqual('12', os.environ['BRZ_COLUMNS'])
2008
self.overrideEnv('BRZ_COLUMNS', '12')
1986
def test_defaults_to_BZR_COLUMNS(self):
1987
# BZR_COLUMNS is set by the test framework
1988
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
os.environ['BZR_COLUMNS'] = '12'
2009
1990
self.assertEqual(12, osutils.terminal_width())
2011
def test_BRZ_COLUMNS_0_no_limit(self):
2012
self.overrideEnv('BRZ_COLUMNS', '0')
2013
self.assertEqual(None, osutils.terminal_width())
2015
1992
def test_falls_back_to_COLUMNS(self):
2016
self.overrideEnv('BRZ_COLUMNS', None)
1993
del os.environ['BZR_COLUMNS']
2017
1994
self.assertNotEqual('42', os.environ['COLUMNS'])
2018
1995
self.set_fake_tty()
2019
self.overrideEnv('COLUMNS', '42')
1996
os.environ['COLUMNS'] = '42'
2020
1997
self.assertEqual(42, osutils.terminal_width())
2022
1999
def test_tty_default_without_columns(self):
2023
self.overrideEnv('BRZ_COLUMNS', None)
2024
self.overrideEnv('COLUMNS', None)
2000
del os.environ['BZR_COLUMNS']
2001
del os.environ['COLUMNS']
2026
2003
def terminal_size(w, h):
2073
2049
def test_copy_ownership_from_path(self):
2074
2050
"""copy_ownership_from_path test with specified src."""
2076
open('test_file', 'wt').close()
2052
f = open('test_file', 'wt')
2077
2053
osutils.copy_ownership_from_path('test_file', ownsrc)
2079
2055
s = os.stat(ownsrc)
2080
self.assertEqual(self.path, 'test_file')
2081
self.assertEqual(self.uid, s.st_uid)
2082
self.assertEqual(self.gid, s.st_gid)
2056
self.assertEquals(self.path, 'test_file')
2057
self.assertEquals(self.uid, s.st_uid)
2058
self.assertEquals(self.gid, s.st_gid)
2084
2060
def test_copy_ownership_nonesrc(self):
2085
2061
"""copy_ownership_from_path test with src=None."""
2086
open('test_file', 'wt').close()
2062
f = open('test_file', 'wt')
2087
2063
# should use parent dir for permissions
2088
2064
osutils.copy_ownership_from_path('test_file')
2090
2066
s = os.stat('..')
2091
self.assertEqual(self.path, 'test_file')
2092
self.assertEqual(self.uid, s.st_uid)
2093
self.assertEqual(self.gid, s.st_gid)
2096
class TestGetHomeDir(tests.TestCase):
2098
def test_is_unicode(self):
2099
home = osutils._get_home_dir()
2100
self.assertIsInstance(home, str)
2102
def test_posix_homeless(self):
2103
self.overrideEnv('HOME', None)
2104
home = osutils._get_home_dir()
2105
self.assertIsInstance(home, str)
2107
def test_posix_home_ascii(self):
2108
self.overrideEnv('HOME', '/home/test')
2109
home = osutils._posix_get_home_dir()
2110
self.assertIsInstance(home, str)
2111
self.assertEqual(u'/home/test', home)
2113
def test_posix_home_unicode(self):
2114
self.requireFeature(features.ByteStringNamedFilesystem)
2115
self.overrideEnv('HOME', '/home/\xa7test')
2116
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2117
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2118
osutils._fs_enc = "iso8859-5"
2119
# In python 3, os.environ returns unicode
2120
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2067
self.assertEquals(self.path, 'test_file')
2068
self.assertEquals(self.uid, s.st_uid)
2069
self.assertEquals(self.gid, s.st_gid)
2123
2071
class TestGetuserUnicode(tests.TestCase):
2125
def test_is_unicode(self):
2126
user = osutils.getuser_unicode()
2127
self.assertIsInstance(user, str)
2129
def envvar_to_override(self):
2130
if sys.platform == "win32":
2131
# Disable use of platform calls on windows so envvar is used
2132
self.overrideAttr(win32utils, 'has_ctypes', False)
2133
return 'USERNAME' # only variable used on windows
2134
return 'LOGNAME' # first variable checked by getpass.getuser()
2136
2073
def test_ascii_user(self):
2137
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2138
2075
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2140
2077
def test_unicode_user(self):
2141
2078
ue = osutils.get_user_encoding()
2142
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2144
raise tests.TestSkipped(
2145
'Cannot find a unicode character that works in encoding %s'
2146
% (osutils.get_user_encoding(),))
2147
uni_username = u'jrandom' + uni_val
2148
encoded_username = uni_username.encode(ue)
2149
self.overrideEnv(self.envvar_to_override(), uni_username)
2150
self.assertEqual(uni_username, osutils.getuser_unicode())
2153
class TestBackupNames(tests.TestCase):
2156
super(TestBackupNames, self).setUp()
2159
def backup_exists(self, name):
2160
return name in self.backups
2162
def available_backup_name(self, name):
2163
backup_name = osutils.available_backup_name(name, self.backup_exists)
2164
self.backups.append(backup_name)
2167
def assertBackupName(self, expected, name):
2168
self.assertEqual(expected, self.available_backup_name(name))
2170
def test_empty(self):
2171
self.assertBackupName('file.~1~', 'file')
2173
def test_existing(self):
2174
self.available_backup_name('file')
2175
self.available_backup_name('file')
2176
self.assertBackupName('file.~3~', 'file')
2177
# Empty slots are found, this is not a strict requirement and may be
2178
# revisited if we test against all implementations.
2179
self.backups.remove('file.~2~')
2180
self.assertBackupName('file.~2~', 'file')
2183
class TestFindExecutableInPath(tests.TestCase):
2185
def test_windows(self):
2186
if sys.platform != 'win32':
2187
raise tests.TestSkipped('test requires win32')
2188
self.assertTrue(osutils.find_executable_on_path(
2189
'explorer') is not None)
2191
osutils.find_executable_on_path('explorer.exe') is not None)
2193
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2195
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2196
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2198
def test_windows_app_path(self):
2199
if sys.platform != 'win32':
2200
raise tests.TestSkipped('test requires win32')
2201
# Override PATH env var so that exe can only be found on App Path
2202
self.overrideEnv('PATH', '')
2203
# Internt Explorer is always registered in the App Path
2204
self.assertTrue(osutils.find_executable_on_path(
2205
'iexplore') is not None)
2207
def test_other(self):
2208
if sys.platform == 'win32':
2209
raise tests.TestSkipped('test requires non-win32')
2210
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2212
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2215
class SupportsExecutableTests(tests.TestCaseInTempDir):
2217
def test_returns_bool(self):
2218
self.assertIsInstance(osutils.supports_executable(self.test_dir), bool)
2221
class SupportsSymlinksTests(tests.TestCaseInTempDir):
2223
def test_returns_bool(self):
2224
self.assertIsInstance(osutils.supports_symlinks(self.test_dir), bool)
2227
class MtabReader(tests.TestCaseInTempDir):
2229
def test_read_mtab(self):
2230
self.build_tree_contents([('mtab', """\
2231
/dev/mapper/blah--vg-root / ext4 rw,relatime,errors=remount-ro 0 0
2232
/dev/mapper/blah--vg-home /home vfat rw,relatime 0 0
2238
list(osutils.read_mtab('mtab')),
2240
(b'/home', 'vfat')])
2243
class GetFsTypeTests(tests.TestCaseInTempDir):
2245
def test_returns_string_or_none(self):
2246
ret = osutils.get_fs_type(self.test_dir)
2247
self.assertTrue(isinstance(ret, str) or ret is None)
2249
def test_returns_most_specific(self):
2251
osutils, '_FILESYSTEM_FINDER',
2252
osutils.FilesystemFinder(
2253
[(b'/', 'ext4'), (b'/home', 'vfat'),
2254
(b'/home/jelmer', 'ext2')]))
2255
self.assertEqual(osutils.get_fs_type(b'/home/jelmer/blah'), 'ext2')
2256
self.assertEqual(osutils.get_fs_type('/home/jelmer/blah'), 'ext2')
2257
self.assertEqual(osutils.get_fs_type(b'/home/jelmer'), 'ext2')
2258
self.assertEqual(osutils.get_fs_type(b'/home/martin'), 'vfat')
2259
self.assertEqual(osutils.get_fs_type(b'/home'), 'vfat')
2260
self.assertEqual(osutils.get_fs_type(b'/other'), 'ext4')
2262
def test_returns_none(self):
2264
osutils, '_FILESYSTEM_FINDER',
2265
osutils.FilesystemFinder([]))
2266
self.assertIs(osutils.get_fs_type('/home/jelmer/blah'), None)
2267
self.assertIs(osutils.get_fs_type(b'/home/jelmer/blah'), None)
2268
self.assertIs(osutils.get_fs_type('/home/jelmer'), None)
2079
osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
2080
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())