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