34
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
35
from bzrlib.osutils import (
37
is_inside_or_parent_of_any,
34
43
from bzrlib.tests import (
45
probe_unicode_in_user_encoding,
48
CaseInsCasePresFilenameFeature,
53
from bzrlib.tests.file_utils import (
40
class _UTF8DirReaderFeature(tests.Feature):
56
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
59
class _UTF8DirReaderFeature(Feature):
132
151
self.assertTrue(is_inside('', 'foo.c'))
134
153
def test_is_inside_any(self):
135
SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
154
SRC_FOO_C = pathjoin('src', 'foo.c')
136
155
for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
137
156
(['src'], SRC_FOO_C),
138
157
(['src'], 'src'),
140
self.assert_(osutils.is_inside_any(dirs, fn))
159
self.assert_(is_inside_any(dirs, fn))
141
160
for dirs, fn in [(['src'], 'srccontrol'),
142
161
(['src'], 'srccontrol/foo')]:
143
self.assertFalse(osutils.is_inside_any(dirs, fn))
162
self.assertFalse(is_inside_any(dirs, fn))
145
164
def test_is_inside_or_parent_of_any(self):
146
165
for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
149
168
(['src/bar.c', 'bla/foo.c'], 'src'),
150
169
(['src'], 'src'),
152
self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
171
self.assert_(is_inside_or_parent_of_any(dirs, fn))
154
173
for dirs, fn in [(['src'], 'srccontrol'),
155
174
(['srccontrol/foo.c'], 'src'),
156
175
(['src'], 'srccontrol/foo')]:
157
self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
176
self.assertFalse(is_inside_or_parent_of_any(dirs, fn))
159
178
def test_rmtree(self):
160
179
# Check to remove tree with read-only files/dirs
223
242
orig_umask = osutils.get_umask()
224
self.addCleanup(os.umask, orig_umask)
226
self.assertEqual(0222, osutils.get_umask())
228
self.assertEqual(0022, osutils.get_umask())
230
self.assertEqual(0002, osutils.get_umask())
232
self.assertEqual(0027, osutils.get_umask())
245
self.assertEqual(0222, osutils.get_umask())
247
self.assertEqual(0022, osutils.get_umask())
249
self.assertEqual(0002, osutils.get_umask())
251
self.assertEqual(0027, osutils.get_umask())
234
255
def assertFormatedDelta(self, expected, seconds):
235
256
"""Assert osutils.format_delta formats as expected"""
278
299
# dates once they do occur in output strings.
280
301
def test_dereference_path(self):
281
self.requireFeature(tests.SymlinkFeature)
302
self.requireFeature(SymlinkFeature)
282
303
cwd = osutils.realpath('.')
284
305
bar_path = osutils.pathjoin(cwd, 'bar')
335
356
osutils.host_os_dereferences_symlinks()
338
class TestCanonicalRelPath(tests.TestCaseInTempDir):
359
class TestCanonicalRelPath(TestCaseInTempDir):
340
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
361
_test_needs_features = [CaseInsCasePresFilenameFeature]
342
363
def test_canonical_relpath_simple(self):
343
364
f = file('MixedCaseName', 'w')
345
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
346
real_base_dir = osutils.realpath(self.test_base_dir)
347
actual = osutils.canonical_relpath(real_base_dir, 'mixedcasename')
348
self.failUnlessEqual('work/MixedCaseName', actual)
366
self.failUnlessEqual(
367
canonical_relpath(self.test_base_dir, 'mixedcasename'),
368
'work/MixedCaseName')
350
370
def test_canonical_relpath_missing_tail(self):
351
371
os.mkdir('MixedCaseParent')
352
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
353
real_base_dir = osutils.realpath(self.test_base_dir)
354
actual = osutils.canonical_relpath(real_base_dir,
355
'mixedcaseparent/nochild')
356
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
359
class TestPumpFile(tests.TestCase):
372
self.failUnlessEqual(
373
canonical_relpath(self.test_base_dir, 'mixedcaseparent/nochild'),
374
'work/MixedCaseParent/nochild')
377
class TestPumpFile(TestCase):
360
378
"""Test pumpfile method."""
363
tests.TestCase.setUp(self)
364
381
# create a test datablock
365
382
self.block_size = 512
366
383
pattern = '0123456789ABCDEF'
373
390
# make sure test data is larger than max read size
374
391
self.assertTrue(self.test_data_len > self.block_size)
376
from_file = file_utils.FakeReadFile(self.test_data)
393
from_file = FakeReadFile(self.test_data)
377
394
to_file = StringIO()
379
396
# read (max / 2) bytes and verify read size wasn't affected
380
397
num_bytes_to_read = self.block_size / 2
381
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
398
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
382
399
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
383
400
self.assertEqual(from_file.get_read_count(), 1)
385
402
# read (max) bytes and verify read size wasn't affected
386
403
num_bytes_to_read = self.block_size
387
404
from_file.reset_read_count()
388
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
405
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
389
406
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
390
407
self.assertEqual(from_file.get_read_count(), 1)
392
409
# read (max + 1) bytes and verify read size was limited
393
410
num_bytes_to_read = self.block_size + 1
394
411
from_file.reset_read_count()
395
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
412
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
396
413
self.assertEqual(from_file.get_max_read_size(), self.block_size)
397
414
self.assertEqual(from_file.get_read_count(), 2)
399
416
# finish reading the rest of the data
400
417
num_bytes_to_read = self.test_data_len - to_file.tell()
401
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
418
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
403
420
# report error if the data wasn't equal (we only report the size due
404
421
# to the length of the data)
414
431
self.assertTrue(self.test_data_len > self.block_size)
416
433
# retrieve data in blocks
417
from_file = file_utils.FakeReadFile(self.test_data)
434
from_file = FakeReadFile(self.test_data)
418
435
to_file = StringIO()
419
osutils.pumpfile(from_file, to_file, self.test_data_len,
436
pumpfile(from_file, to_file, self.test_data_len, self.block_size)
422
438
# verify read size was equal to the maximum read size
423
439
self.assertTrue(from_file.get_max_read_size() > 0)
438
454
self.assertTrue(self.test_data_len > self.block_size)
440
456
# retrieve data to EOF
441
from_file = file_utils.FakeReadFile(self.test_data)
457
from_file = FakeReadFile(self.test_data)
442
458
to_file = StringIO()
443
osutils.pumpfile(from_file, to_file, -1, self.block_size)
459
pumpfile(from_file, to_file, -1, self.block_size)
445
461
# verify read size was equal to the maximum read size
446
462
self.assertEqual(from_file.get_max_read_size(), self.block_size)
458
474
test verifies that any existing usages of pumpfile will not be broken
459
475
with this new version."""
460
476
# retrieve data using default (old) pumpfile method
461
from_file = file_utils.FakeReadFile(self.test_data)
477
from_file = FakeReadFile(self.test_data)
462
478
to_file = StringIO()
463
osutils.pumpfile(from_file, to_file)
479
pumpfile(from_file, to_file)
465
481
# report error if the data wasn't equal (we only report the size due
466
482
# to the length of the data)
475
491
activity.append((length, direction))
476
492
from_file = StringIO(self.test_data)
477
493
to_file = StringIO()
478
osutils.pumpfile(from_file, to_file, buff_size=500,
479
report_activity=log_activity, direction='read')
494
pumpfile(from_file, to_file, buff_size=500,
495
report_activity=log_activity, direction='read')
480
496
self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
481
497
(36, 'read')], activity)
483
499
from_file = StringIO(self.test_data)
484
500
to_file = StringIO()
486
osutils.pumpfile(from_file, to_file, buff_size=500,
487
report_activity=log_activity, direction='write')
502
pumpfile(from_file, to_file, buff_size=500,
503
report_activity=log_activity, direction='write')
488
504
self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
489
505
(36, 'write')], activity)
492
508
from_file = StringIO(self.test_data)
493
509
to_file = StringIO()
495
osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
496
report_activity=log_activity, direction='read')
511
pumpfile(from_file, to_file, buff_size=500, read_length=1028,
512
report_activity=log_activity, direction='read')
497
513
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
501
class TestPumpStringFile(tests.TestCase):
517
class TestPumpStringFile(TestCase):
503
519
def test_empty(self):
504
520
output = StringIO()
505
osutils.pump_string_file("", output)
521
pump_string_file("", output)
506
522
self.assertEqual("", output.getvalue())
508
524
def test_more_than_segment_size(self):
509
525
output = StringIO()
510
osutils.pump_string_file("123456789", output, 2)
526
pump_string_file("123456789", output, 2)
511
527
self.assertEqual("123456789", output.getvalue())
513
529
def test_segment_size(self):
514
530
output = StringIO()
515
osutils.pump_string_file("12", output, 2)
531
pump_string_file("12", output, 2)
516
532
self.assertEqual("12", output.getvalue())
518
534
def test_segment_size_multiple(self):
519
535
output = StringIO()
520
osutils.pump_string_file("1234", output, 2)
536
pump_string_file("1234", output, 2)
521
537
self.assertEqual("1234", output.getvalue())
524
class TestSafeUnicode(tests.TestCase):
540
class TestSafeUnicode(TestCase):
526
542
def test_from_ascii_string(self):
527
543
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
536
552
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
538
554
def test_bad_utf8_string(self):
539
self.assertRaises(errors.BzrBadParameterNotUnicode,
555
self.assertRaises(BzrBadParameterNotUnicode,
540
556
osutils.safe_unicode,
544
class TestSafeUtf8(tests.TestCase):
560
class TestSafeUtf8(TestCase):
546
562
def test_from_ascii_string(self):
557
573
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
559
575
def test_bad_utf8_string(self):
560
self.assertRaises(errors.BzrBadParameterNotUnicode,
576
self.assertRaises(BzrBadParameterNotUnicode,
561
577
osutils.safe_utf8, '\xbb\xbb')
564
class TestSafeRevisionId(tests.TestCase):
580
class TestSafeRevisionId(TestCase):
566
582
def test_from_ascii_string(self):
567
583
# this shouldn't give a warning because it's getting an ascii string
589
605
self.assertEqual(None, osutils.safe_revision_id(None))
592
class TestSafeFileId(tests.TestCase):
608
class TestSafeFileId(TestCase):
594
610
def test_from_ascii_string(self):
595
611
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
615
631
self.assertEqual(None, osutils.safe_file_id(None))
618
class TestWin32Funcs(tests.TestCase):
619
"""Test that _win32 versions of os utilities return appropriate paths."""
634
class TestWin32Funcs(TestCase):
635
"""Test that the _win32 versions of os utilities return appropriate paths."""
621
637
def test_abspath(self):
622
638
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
629
645
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
631
647
def test_pathjoin(self):
632
self.assertEqual('path/to/foo',
633
osutils._win32_pathjoin('path', 'to', 'foo'))
634
self.assertEqual('C:/foo',
635
osutils._win32_pathjoin('path\\to', 'C:\\foo'))
636
self.assertEqual('C:/foo',
637
osutils._win32_pathjoin('path/to', 'C:/foo'))
638
self.assertEqual('path/to/foo',
639
osutils._win32_pathjoin('path/to/', 'foo'))
640
self.assertEqual('/foo',
641
osutils._win32_pathjoin('C:/path/to/', '/foo'))
642
self.assertEqual('/foo',
643
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
648
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
649
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
650
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
651
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
652
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
653
self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
645
655
def test_normpath(self):
646
self.assertEqual('path/to/foo',
647
osutils._win32_normpath(r'path\\from\..\to\.\foo'))
648
self.assertEqual('path/to/foo',
649
osutils._win32_normpath('path//from/../to/./foo'))
656
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
657
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
651
659
def test_getcwd(self):
652
660
cwd = osutils._win32_getcwd()
681
689
self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
684
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
692
class TestWin32FuncsDirs(TestCaseInTempDir):
685
693
"""Test win32 functions that create files."""
687
695
def test_getcwd(self):
688
self.requireFeature(tests.UnicodeFilenameFeature)
696
if win32utils.winver == 'Windows 98':
697
raise TestSkipped('Windows 98 cannot handle unicode filenames')
698
# Make sure getcwd can handle unicode filenames
702
raise TestSkipped("Unable to create Unicode filename")
690
704
os.chdir(u'mu-\xb5')
691
705
# TODO: jam 20060427 This will probably fail on Mac OSX because
692
706
# it will change the normalization of B\xe5gfors
764
778
self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
767
class TestMacFuncsDirs(tests.TestCaseInTempDir):
781
class TestMacFuncsDirs(TestCaseInTempDir):
768
782
"""Test mac special functions that require directories."""
770
784
def test_getcwd(self):
771
self.requireFeature(tests.UnicodeFilenameFeature)
772
os.mkdir(u'B\xe5gfors')
785
# On Mac, this will actually create Ba\u030agfors
786
# but chdir will still work, because it accepts both paths
788
os.mkdir(u'B\xe5gfors')
790
raise TestSkipped("Unable to create Unicode filename")
773
792
os.chdir(u'B\xe5gfors')
774
793
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
776
795
def test_getcwd_nonnorm(self):
777
self.requireFeature(tests.UnicodeFilenameFeature)
778
796
# Test that _mac_getcwd() will normalize this path
779
os.mkdir(u'Ba\u030agfors')
798
os.mkdir(u'Ba\u030agfors')
800
raise TestSkipped("Unable to create Unicode filename")
780
802
os.chdir(u'Ba\u030agfors')
781
803
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
784
class TestChunksToLines(tests.TestCase):
806
class TestChunksToLines(TestCase):
786
808
def test_smoketest(self):
787
809
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
798
820
self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
801
class TestSplitLines(tests.TestCase):
823
class TestSplitLines(TestCase):
803
825
def test_split_unicode(self):
804
826
self.assertEqual([u'foo\n', u'bar\xae'],
982
1004
def test_force_walkdirs_utf8_nt(self):
983
1005
# Disabled because the thunk of the whole walkdirs api is disabled.
984
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1006
self.requireFeature(Win32ReadDirFeature)
985
1007
self._save_platform_info()
986
1008
win32utils.winver = 'Windows NT'
987
1009
from bzrlib._walkdirs_win32 import Win32ReadDir
988
1010
self.assertReadFSDirIs(Win32ReadDir)
990
1012
def test_force_walkdirs_utf8_98(self):
991
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1013
self.requireFeature(Win32ReadDirFeature)
992
1014
self._save_platform_info()
993
1015
win32utils.winver = 'Windows 98'
994
1016
self.assertReadFSDirIs(osutils.UnicodeDirReader)
996
1018
def test_unicode_walkdirs(self):
997
1019
"""Walkdirs should always return unicode paths."""
998
self.requireFeature(tests.UnicodeFilenameFeature)
999
1020
name0 = u'0file-\xb6'
1000
1021
name1 = u'1dir-\u062c\u0648'
1001
1022
name2 = u'2file-\u0633'
1145
1176
self.assertEqual(expected_dirblocks, result)
1147
1178
def test__walkdirs_utf8_win32readdir(self):
1148
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1179
self.requireFeature(Win32ReadDirFeature)
1149
1180
self.requireFeature(tests.UnicodeFilenameFeature)
1150
1181
from bzrlib._walkdirs_win32 import Win32ReadDir
1151
1182
self._save_platform_info()
1203
1234
def test__walkdirs_utf_win32_find_file_stat_file(self):
1204
1235
"""make sure our Stat values are valid"""
1205
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1236
self.requireFeature(Win32ReadDirFeature)
1206
1237
self.requireFeature(tests.UnicodeFilenameFeature)
1207
1238
from bzrlib._walkdirs_win32 import Win32ReadDir
1208
1239
name0u = u'0file-\xb6'
1227
1258
def test__walkdirs_utf_win32_find_file_stat_directory(self):
1228
1259
"""make sure our Stat values are valid"""
1229
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1260
self.requireFeature(Win32ReadDirFeature)
1230
1261
self.requireFeature(tests.UnicodeFilenameFeature)
1231
1262
from bzrlib._walkdirs_win32 import Win32ReadDir
1232
1263
name0u = u'0dir-\u062c\u0648'
1317
1348
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
1320
class TestCopyTree(tests.TestCaseInTempDir):
1351
class TestCopyTree(TestCaseInTempDir):
1322
1353
def test_copy_basic_tree(self):
1323
1354
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1333
1364
self.assertEqual(['c'], os.listdir('target/b'))
1335
1366
def test_copy_tree_symlinks(self):
1336
self.requireFeature(tests.SymlinkFeature)
1367
self.requireFeature(SymlinkFeature)
1337
1368
self.build_tree(['source/'])
1338
1369
os.symlink('a/generic/path', 'source/lnk')
1339
1370
osutils.copy_tree('source', 'target')
1369
1400
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1372
class TestSetUnsetEnv(tests.TestCase):
1403
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
1404
# [bialix] 2006/12/26
1407
class TestSetUnsetEnv(TestCase):
1373
1408
"""Test updating the environment"""
1375
1410
def setUp(self):
1403
1438
So Unicode strings must be encoded.
1405
uni_val, env_val = tests.probe_unicode_in_user_encoding()
1440
uni_val, env_val = probe_unicode_in_user_encoding()
1406
1441
if uni_val is None:
1407
raise tests.TestSkipped(
1408
'Cannot find a unicode character that works in encoding %s'
1409
% (osutils.get_user_encoding(),))
1442
raise TestSkipped('Cannot find a unicode character that works in'
1443
' encoding %s' % (osutils.get_user_encoding(),))
1411
1445
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1412
1446
self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1464
1498
self.assertEqual(expected_sha, sha)
1467
class TestShaFileByName(tests.TestCaseInTempDir):
1501
class TestShaFileByName(TestCaseInTempDir):
1469
1503
def test_sha_empty(self):
1470
1504
self.build_tree_contents([('foo', '')])
1494
1528
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1497
class TestReCompile(tests.TestCase):
1531
class TestReCompile(TestCase):
1499
1533
def test_re_compile_checked(self):
1500
1534
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)