~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Robert J. Tanner
  • Date: 2009-04-20 08:37:32 UTC
  • mfrom: (4299 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4300.
  • Revision ID: tanner@real-time.com-20090420083732-bzx919oo7wpmqc2u
[merge] 1.14rc2 back into bzr.dev (Bob Tanner)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    tests,
32
32
    win32utils,
33
33
    )
34
 
from bzrlib.errors import BzrBadParameterNotUnicode, InvalidURL
35
 
from bzrlib.osutils import (
36
 
        canonical_relpath,
37
 
        )
38
34
from bzrlib.tests import (
39
 
        Feature,
40
 
        probe_unicode_in_user_encoding,
41
 
        StringIOWrapper,
42
 
        SymlinkFeature,
43
 
        CaseInsCasePresFilenameFeature,
44
 
        TestCase,
45
 
        TestCaseInTempDir,
46
 
        TestSkipped,
47
 
        )
48
 
from bzrlib.tests.file_utils import (
49
 
    FakeReadFile,
 
35
    file_utils,
 
36
    test__walkdirs_win32,
50
37
    )
51
 
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
52
 
 
53
 
 
54
 
class _UTF8DirReaderFeature(Feature):
 
38
 
 
39
 
 
40
class _UTF8DirReaderFeature(tests.Feature):
55
41
 
56
42
    def _probe(self):
57
43
        try:
67
53
UTF8DirReaderFeature = _UTF8DirReaderFeature()
68
54
 
69
55
 
70
 
class TestOSUtils(TestCaseInTempDir):
 
56
class TestOSUtils(tests.TestCaseInTempDir):
71
57
 
72
58
    def test_contains_whitespace(self):
73
59
        self.failUnless(osutils.contains_whitespace(u' '))
235
221
            return
236
222
 
237
223
        orig_umask = osutils.get_umask()
238
 
        try:
239
 
            os.umask(0222)
240
 
            self.assertEqual(0222, osutils.get_umask())
241
 
            os.umask(0022)
242
 
            self.assertEqual(0022, osutils.get_umask())
243
 
            os.umask(0002)
244
 
            self.assertEqual(0002, osutils.get_umask())
245
 
            os.umask(0027)
246
 
            self.assertEqual(0027, osutils.get_umask())
247
 
        finally:
248
 
            os.umask(orig_umask)
 
224
        self.addCleanup(os.umask, orig_umask)
 
225
        os.umask(0222)
 
226
        self.assertEqual(0222, osutils.get_umask())
 
227
        os.umask(0022)
 
228
        self.assertEqual(0022, osutils.get_umask())
 
229
        os.umask(0002)
 
230
        self.assertEqual(0002, osutils.get_umask())
 
231
        os.umask(0027)
 
232
        self.assertEqual(0027, osutils.get_umask())
249
233
 
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.
295
279
 
296
280
    def test_dereference_path(self):
297
 
        self.requireFeature(SymlinkFeature)
 
281
        self.requireFeature(tests.SymlinkFeature)
298
282
        cwd = osutils.realpath('.')
299
283
        os.mkdir('bar')
300
284
        bar_path = osutils.pathjoin(cwd, 'bar')
351
335
        osutils.host_os_dereferences_symlinks()
352
336
 
353
337
 
354
 
class TestCanonicalRelPath(TestCaseInTempDir):
 
338
class TestCanonicalRelPath(tests.TestCaseInTempDir):
355
339
 
356
 
    _test_needs_features = [CaseInsCasePresFilenameFeature]
 
340
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
357
341
 
358
342
    def test_canonical_relpath_simple(self):
359
343
        f = file('MixedCaseName', 'w')
372
356
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
373
357
 
374
358
 
375
 
class TestPumpFile(TestCase):
 
359
class TestPumpFile(tests.TestCase):
376
360
    """Test pumpfile method."""
 
361
 
377
362
    def setUp(self):
378
 
        TestCase.setUp(self)
 
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)
390
375
 
391
 
        from_file = FakeReadFile(self.test_data)
 
376
        from_file = file_utils.FakeReadFile(self.test_data)
392
377
        to_file = StringIO()
393
378
 
394
379
        # read (max / 2) bytes and verify read size wasn't affected
429
414
        self.assertTrue(self.test_data_len > self.block_size)
430
415
 
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,
435
420
                         self.block_size)
453
438
        self.assertTrue(self.test_data_len > self.block_size)
454
439
 
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)
459
444
 
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)
479
464
 
513
498
 
514
499
 
515
500
 
516
 
class TestPumpStringFile(TestCase):
 
501
class TestPumpStringFile(tests.TestCase):
517
502
 
518
503
    def test_empty(self):
519
504
        output = StringIO()
536
521
        self.assertEqual("1234", output.getvalue())
537
522
 
538
523
 
539
 
class TestSafeUnicode(TestCase):
 
524
class TestSafeUnicode(tests.TestCase):
540
525
 
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'))
552
537
 
553
538
    def test_bad_utf8_string(self):
554
 
        self.assertRaises(BzrBadParameterNotUnicode,
 
539
        self.assertRaises(errors.BzrBadParameterNotUnicode,
555
540
                          osutils.safe_unicode,
556
541
                          '\xbb\xbb')
557
542
 
558
543
 
559
 
class TestSafeUtf8(TestCase):
 
544
class TestSafeUtf8(tests.TestCase):
560
545
 
561
546
    def test_from_ascii_string(self):
562
547
        f = 'foobar'
572
557
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
573
558
 
574
559
    def test_bad_utf8_string(self):
575
 
        self.assertRaises(BzrBadParameterNotUnicode,
 
560
        self.assertRaises(errors.BzrBadParameterNotUnicode,
576
561
                          osutils.safe_utf8, '\xbb\xbb')
577
562
 
578
563
 
579
 
class TestSafeRevisionId(TestCase):
 
564
class TestSafeRevisionId(tests.TestCase):
580
565
 
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))
605
590
 
606
591
 
607
 
class TestSafeFileId(TestCase):
 
592
class TestSafeFileId(tests.TestCase):
608
593
 
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))
631
616
 
632
617
 
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."""
635
620
 
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'))
645
630
 
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'))
653
644
 
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'))
657
650
 
658
651
    def test_getcwd(self):
659
652
        cwd = osutils._win32_getcwd()
688
681
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
689
682
 
690
683
 
691
 
class TestWin32FuncsDirs(TestCaseInTempDir):
 
684
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
692
685
    """Test win32 functions that create files."""
693
686
 
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
698
 
        try:
699
 
            os.mkdir(u'mu-\xb5')
700
 
        except UnicodeError:
701
 
            raise TestSkipped("Unable to create Unicode filename")
702
 
 
 
688
        self.requireFeature(tests.UnicodeFilenameFeature)
 
689
        os.mkdir(u'mu-\xb5')
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')
778
765
 
779
766
 
780
 
class TestMacFuncsDirs(TestCaseInTempDir):
 
767
class TestMacFuncsDirs(tests.TestCaseInTempDir):
781
768
    """Test mac special functions that require directories."""
782
769
 
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
786
 
        try:
787
 
            os.mkdir(u'B\xe5gfors')
788
 
        except UnicodeError:
789
 
            raise TestSkipped("Unable to create Unicode filename")
790
 
 
 
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')
793
775
 
794
776
    def test_getcwd_nonnorm(self):
 
777
        self.requireFeature(tests.UnicodeFilenameFeature)
795
778
        # Test that _mac_getcwd() will normalize this path
796
 
        try:
797
 
            os.mkdir(u'Ba\u030agfors')
798
 
        except UnicodeError:
799
 
            raise TestSkipped("Unable to create Unicode filename")
800
 
 
 
779
        os.mkdir(u'Ba\u030agfors')
801
780
        os.chdir(u'Ba\u030agfors')
802
781
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
803
782
 
804
783
 
805
 
class TestChunksToLines(TestCase):
 
784
class TestChunksToLines(tests.TestCase):
806
785
 
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)
820
799
 
821
800
 
822
 
class TestSplitLines(TestCase):
 
801
class TestSplitLines(tests.TestCase):
823
802
 
824
803
    def test_split_unicode(self):
825
804
        self.assertEqual([u'foo\n', u'bar\xae'],
832
811
                         osutils.split_lines('foo\rbar\n'))
833
812
 
834
813
 
835
 
class TestWalkDirs(TestCaseInTempDir):
 
814
class TestWalkDirs(tests.TestCaseInTempDir):
836
815
 
837
816
    def test_walkdirs(self):
838
817
        tree = [
1002
981
 
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)
1010
989
 
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)
1016
995
 
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 + '/',
1027
1007
            name2,
1028
1008
            ]
1029
 
        try:
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 = [
1035
1011
                ((u'', u'.'),
1036
1012
                 [(name0, name0, 'file', './' + name0),
1062
1038
 
1063
1039
        The abspath portion might be in unicode or utf-8
1064
1040
        """
 
1041
        self.requireFeature(tests.UnicodeFilenameFeature)
1065
1042
        name0 = u'0file-\xb6'
1066
1043
        name1 = u'1dir-\u062c\u0648'
1067
1044
        name2 = u'2file-\u0633'
1072
1049
            name1 + '/' + name1 + '/',
1073
1050
            name2,
1074
1051
            ]
1075
 
        try:
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')
1126
1099
 
1127
1100
        The abspath portion should be in unicode
1128
1101
        """
 
1102
        self.requireFeature(tests.UnicodeFilenameFeature)
1129
1103
        # Use the unicode reader. TODO: split into driver-and-driven unit
1130
1104
        # tests.
1131
1105
        self._save_platform_info()
1140
1114
            name1u + '/' + name1u + '/',
1141
1115
            name2u,
1142
1116
            ]
1143
 
        try:
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)
1176
1146
 
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()
1232
1202
 
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'
1256
1226
 
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))
1348
1318
 
1349
1319
 
1350
 
class TestCopyTree(TestCaseInTempDir):
 
1320
class TestCopyTree(tests.TestCaseInTempDir):
1351
1321
 
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'))
1364
1334
 
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)
1400
1370
 
1401
1371
 
1402
 
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
1403
 
# [bialix] 2006/12/26
1404
 
 
1405
 
 
1406
 
class TestSetUnsetEnv(TestCase):
 
1372
class TestSetUnsetEnv(tests.TestCase):
1407
1373
    """Test updating the environment"""
1408
1374
 
1409
1375
    def setUp(self):
1436
1402
 
1437
1403
        So Unicode strings must be encoded.
1438
1404
        """
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(),))
1443
1410
 
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)
1454
1421
 
1455
1422
 
1456
 
class TestLocalTimeOffset(TestCase):
 
1423
class TestLocalTimeOffset(tests.TestCase):
1457
1424
 
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)
1476
1443
 
1477
1444
 
1478
 
class TestSizeShaFile(TestCaseInTempDir):
 
1445
class TestSizeShaFile(tests.TestCaseInTempDir):
1479
1446
 
1480
1447
    def test_sha_empty(self):
1481
1448
        self.build_tree_contents([('foo', '')])
1497
1464
        self.assertEqual(expected_sha, sha)
1498
1465
 
1499
1466
 
1500
 
class TestShaFileByName(TestCaseInTempDir):
 
1467
class TestShaFileByName(tests.TestCaseInTempDir):
1501
1468
 
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'))
1512
1479
 
1513
1480
 
1514
 
class TestResourceLoading(TestCaseInTempDir):
 
1481
class TestResourceLoading(tests.TestCaseInTempDir):
1515
1482
 
1516
1483
    def test_resource_string(self):
1517
1484
        # test resource in bzrlib
1527
1494
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1528
1495
 
1529
1496
 
1530
 
class TestReCompile(TestCase):
 
1497
class TestReCompile(tests.TestCase):
1531
1498
 
1532
1499
    def test_re_compile_checked(self):
1533
1500
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)