34
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
35
from bzrlib.osutils import (
38
34
from bzrlib.tests import (
40
probe_unicode_in_user_encoding,
43
CaseInsCasePresFilenameFeature,
48
from bzrlib.tests.file_utils import (
51
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
54
class _UTF8DirReaderFeature(Feature):
40
class _UTF8DirReaderFeature(tests.Feature):
67
53
UTF8DirReaderFeature = _UTF8DirReaderFeature()
70
class TestOSUtils(TestCaseInTempDir):
56
class TestOSUtils(tests.TestCaseInTempDir):
72
58
def test_contains_whitespace(self):
73
59
self.failUnless(osutils.contains_whitespace(u' '))
237
223
orig_umask = osutils.get_umask()
240
self.assertEqual(0222, osutils.get_umask())
242
self.assertEqual(0022, osutils.get_umask())
244
self.assertEqual(0002, osutils.get_umask())
246
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())
250
234
def assertFormatedDelta(self, expected, seconds):
251
235
"""Assert osutils.format_delta formats as expected"""
294
278
# dates once they do occur in output strings.
296
280
def test_dereference_path(self):
297
self.requireFeature(SymlinkFeature)
281
self.requireFeature(tests.SymlinkFeature)
298
282
cwd = osutils.realpath('.')
300
284
bar_path = osutils.pathjoin(cwd, 'bar')
351
335
osutils.host_os_dereferences_symlinks()
354
class TestCanonicalRelPath(TestCaseInTempDir):
338
class TestCanonicalRelPath(tests.TestCaseInTempDir):
356
_test_needs_features = [CaseInsCasePresFilenameFeature]
340
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
358
342
def test_canonical_relpath_simple(self):
359
343
f = file('MixedCaseName', 'w')
372
356
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
375
class TestPumpFile(TestCase):
359
class TestPumpFile(tests.TestCase):
376
360
"""Test pumpfile method."""
363
tests.TestCase.setUp(self)
379
364
# create a test datablock
380
365
self.block_size = 512
381
366
pattern = '0123456789ABCDEF'
388
373
# make sure test data is larger than max read size
389
374
self.assertTrue(self.test_data_len > self.block_size)
391
from_file = FakeReadFile(self.test_data)
376
from_file = file_utils.FakeReadFile(self.test_data)
392
377
to_file = StringIO()
394
379
# read (max / 2) bytes and verify read size wasn't affected
429
414
self.assertTrue(self.test_data_len > self.block_size)
431
416
# retrieve data in blocks
432
from_file = FakeReadFile(self.test_data)
417
from_file = file_utils.FakeReadFile(self.test_data)
433
418
to_file = StringIO()
434
419
osutils.pumpfile(from_file, to_file, self.test_data_len,
453
438
self.assertTrue(self.test_data_len > self.block_size)
455
440
# retrieve data to EOF
456
from_file = FakeReadFile(self.test_data)
441
from_file = file_utils.FakeReadFile(self.test_data)
457
442
to_file = StringIO()
458
443
osutils.pumpfile(from_file, to_file, -1, self.block_size)
473
458
test verifies that any existing usages of pumpfile will not be broken
474
459
with this new version."""
475
460
# retrieve data using default (old) pumpfile method
476
from_file = FakeReadFile(self.test_data)
461
from_file = file_utils.FakeReadFile(self.test_data)
477
462
to_file = StringIO()
478
463
osutils.pumpfile(from_file, to_file)
536
521
self.assertEqual("1234", output.getvalue())
539
class TestSafeUnicode(TestCase):
524
class TestSafeUnicode(tests.TestCase):
541
526
def test_from_ascii_string(self):
542
527
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
551
536
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
553
538
def test_bad_utf8_string(self):
554
self.assertRaises(BzrBadParameterNotUnicode,
539
self.assertRaises(errors.BzrBadParameterNotUnicode,
555
540
osutils.safe_unicode,
559
class TestSafeUtf8(TestCase):
544
class TestSafeUtf8(tests.TestCase):
561
546
def test_from_ascii_string(self):
572
557
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
574
559
def test_bad_utf8_string(self):
575
self.assertRaises(BzrBadParameterNotUnicode,
560
self.assertRaises(errors.BzrBadParameterNotUnicode,
576
561
osutils.safe_utf8, '\xbb\xbb')
579
class TestSafeRevisionId(TestCase):
564
class TestSafeRevisionId(tests.TestCase):
581
566
def test_from_ascii_string(self):
582
567
# this shouldn't give a warning because it's getting an ascii string
604
589
self.assertEqual(None, osutils.safe_revision_id(None))
607
class TestSafeFileId(TestCase):
592
class TestSafeFileId(tests.TestCase):
609
594
def test_from_ascii_string(self):
610
595
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
630
615
self.assertEqual(None, osutils.safe_file_id(None))
633
class TestWin32Funcs(TestCase):
634
"""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."""
636
621
def test_abspath(self):
637
622
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
644
629
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
646
631
def test_pathjoin(self):
647
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
648
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
649
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
650
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
651
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
652
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'))
654
645
def test_normpath(self):
655
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
656
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'))
658
651
def test_getcwd(self):
659
652
cwd = osutils._win32_getcwd()
688
681
self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
691
class TestWin32FuncsDirs(TestCaseInTempDir):
684
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
692
685
"""Test win32 functions that create files."""
694
687
def test_getcwd(self):
695
if win32utils.winver == 'Windows 98':
696
raise TestSkipped('Windows 98 cannot handle unicode filenames')
697
# Make sure getcwd can handle unicode filenames
701
raise TestSkipped("Unable to create Unicode filename")
688
self.requireFeature(tests.UnicodeFilenameFeature)
703
690
os.chdir(u'mu-\xb5')
704
691
# TODO: jam 20060427 This will probably fail on Mac OSX because
705
692
# it will change the normalization of B\xe5gfors
777
764
self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
780
class TestMacFuncsDirs(TestCaseInTempDir):
767
class TestMacFuncsDirs(tests.TestCaseInTempDir):
781
768
"""Test mac special functions that require directories."""
783
770
def test_getcwd(self):
784
# On Mac, this will actually create Ba\u030agfors
785
# but chdir will still work, because it accepts both paths
787
os.mkdir(u'B\xe5gfors')
789
raise TestSkipped("Unable to create Unicode filename")
771
self.requireFeature(tests.UnicodeFilenameFeature)
772
os.mkdir(u'B\xe5gfors')
791
773
os.chdir(u'B\xe5gfors')
792
774
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
794
776
def test_getcwd_nonnorm(self):
777
self.requireFeature(tests.UnicodeFilenameFeature)
795
778
# Test that _mac_getcwd() will normalize this path
797
os.mkdir(u'Ba\u030agfors')
799
raise TestSkipped("Unable to create Unicode filename")
779
os.mkdir(u'Ba\u030agfors')
801
780
os.chdir(u'Ba\u030agfors')
802
781
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
805
class TestChunksToLines(TestCase):
784
class TestChunksToLines(tests.TestCase):
807
786
def test_smoketest(self):
808
787
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
819
798
self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
822
class TestSplitLines(TestCase):
801
class TestSplitLines(tests.TestCase):
824
803
def test_split_unicode(self):
825
804
self.assertEqual([u'foo\n', u'bar\xae'],
1003
982
def test_force_walkdirs_utf8_nt(self):
1004
983
# Disabled because the thunk of the whole walkdirs api is disabled.
1005
self.requireFeature(Win32ReadDirFeature)
984
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1006
985
self._save_platform_info()
1007
986
win32utils.winver = 'Windows NT'
1008
987
from bzrlib._walkdirs_win32 import Win32ReadDir
1009
988
self.assertReadFSDirIs(Win32ReadDir)
1011
990
def test_force_walkdirs_utf8_98(self):
1012
self.requireFeature(Win32ReadDirFeature)
991
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1013
992
self._save_platform_info()
1014
993
win32utils.winver = 'Windows 98'
1015
994
self.assertReadFSDirIs(osutils.UnicodeDirReader)
1017
996
def test_unicode_walkdirs(self):
1018
997
"""Walkdirs should always return unicode paths."""
998
self.requireFeature(tests.UnicodeFilenameFeature)
1019
999
name0 = u'0file-\xb6'
1020
1000
name1 = u'1dir-\u062c\u0648'
1021
1001
name2 = u'2file-\u0633'
1026
1006
name1 + '/' + name1 + '/',
1030
self.build_tree(tree)
1031
except UnicodeError:
1032
raise TestSkipped('Could not represent Unicode chars'
1033
' in current encoding.')
1009
self.build_tree(tree)
1034
1010
expected_dirblocks = [
1036
1012
[(name0, name0, 'file', './' + name0),
1072
1049
name1 + '/' + name1 + '/',
1076
self.build_tree(tree)
1077
except UnicodeError:
1078
raise TestSkipped('Could not represent Unicode chars'
1079
' in current encoding.')
1052
self.build_tree(tree)
1080
1053
name0 = name0.encode('utf8')
1081
1054
name1 = name1.encode('utf8')
1082
1055
name2 = name2.encode('utf8')
1140
1114
name1u + '/' + name1u + '/',
1144
self.build_tree(tree)
1145
except UnicodeError:
1146
raise TestSkipped('Could not represent Unicode chars'
1147
' in current encoding.')
1117
self.build_tree(tree)
1148
1118
name0 = name0u.encode('utf8')
1149
1119
name1 = name1u.encode('utf8')
1150
1120
name2 = name2u.encode('utf8')
1175
1145
self.assertEqual(expected_dirblocks, result)
1177
1147
def test__walkdirs_utf8_win32readdir(self):
1178
self.requireFeature(Win32ReadDirFeature)
1148
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1179
1149
self.requireFeature(tests.UnicodeFilenameFeature)
1180
1150
from bzrlib._walkdirs_win32 import Win32ReadDir
1181
1151
self._save_platform_info()
1233
1203
def test__walkdirs_utf_win32_find_file_stat_file(self):
1234
1204
"""make sure our Stat values are valid"""
1235
self.requireFeature(Win32ReadDirFeature)
1205
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1236
1206
self.requireFeature(tests.UnicodeFilenameFeature)
1237
1207
from bzrlib._walkdirs_win32 import Win32ReadDir
1238
1208
name0u = u'0file-\xb6'
1257
1227
def test__walkdirs_utf_win32_find_file_stat_directory(self):
1258
1228
"""make sure our Stat values are valid"""
1259
self.requireFeature(Win32ReadDirFeature)
1229
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1260
1230
self.requireFeature(tests.UnicodeFilenameFeature)
1261
1231
from bzrlib._walkdirs_win32 import Win32ReadDir
1262
1232
name0u = u'0dir-\u062c\u0648'
1347
1317
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
1350
class TestCopyTree(TestCaseInTempDir):
1320
class TestCopyTree(tests.TestCaseInTempDir):
1352
1322
def test_copy_basic_tree(self):
1353
1323
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1363
1333
self.assertEqual(['c'], os.listdir('target/b'))
1365
1335
def test_copy_tree_symlinks(self):
1366
self.requireFeature(SymlinkFeature)
1336
self.requireFeature(tests.SymlinkFeature)
1367
1337
self.build_tree(['source/'])
1368
1338
os.symlink('a/generic/path', 'source/lnk')
1369
1339
osutils.copy_tree('source', 'target')
1399
1369
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1402
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
1403
# [bialix] 2006/12/26
1406
class TestSetUnsetEnv(TestCase):
1372
class TestSetUnsetEnv(tests.TestCase):
1407
1373
"""Test updating the environment"""
1409
1375
def setUp(self):
1437
1403
So Unicode strings must be encoded.
1439
uni_val, env_val = probe_unicode_in_user_encoding()
1405
uni_val, env_val = tests.probe_unicode_in_user_encoding()
1440
1406
if uni_val is None:
1441
raise TestSkipped('Cannot find a unicode character that works in'
1442
' 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(),))
1444
1411
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1445
1412
self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1453
1420
self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1456
class TestLocalTimeOffset(TestCase):
1423
class TestLocalTimeOffset(tests.TestCase):
1458
1425
def test_local_time_offset(self):
1459
1426
"""Test that local_time_offset() returns a sane value."""
1475
1442
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1478
class TestSizeShaFile(TestCaseInTempDir):
1445
class TestSizeShaFile(tests.TestCaseInTempDir):
1480
1447
def test_sha_empty(self):
1481
1448
self.build_tree_contents([('foo', '')])
1497
1464
self.assertEqual(expected_sha, sha)
1500
class TestShaFileByName(TestCaseInTempDir):
1467
class TestShaFileByName(tests.TestCaseInTempDir):
1502
1469
def test_sha_empty(self):
1503
1470
self.build_tree_contents([('foo', '')])
1511
1478
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1514
class TestResourceLoading(TestCaseInTempDir):
1481
class TestResourceLoading(tests.TestCaseInTempDir):
1516
1483
def test_resource_string(self):
1517
1484
# test resource in bzrlib
1527
1494
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1530
class TestReCompile(TestCase):
1497
class TestReCompile(tests.TestCase):
1532
1499
def test_re_compile_checked(self):
1533
1500
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)