~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Vincent Ladeuil
  • Date: 2009-04-09 10:17:09 UTC
  • mto: (4331.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4332.
  • Revision ID: v.ladeuil+lp@free.fr-20090409101709-qnux2zts33tazuo9
Cleanup imports.

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
 
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,
50
 
    )
51
34
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
52
35
 
53
36
 
54
 
class _UTF8DirReaderFeature(Feature):
 
37
class _UTF8DirReaderFeature(tests.Feature):
55
38
 
56
39
    def _probe(self):
57
40
        try:
67
50
UTF8DirReaderFeature = _UTF8DirReaderFeature()
68
51
 
69
52
 
70
 
class TestOSUtils(TestCaseInTempDir):
 
53
class TestOSUtils(tests.TestCaseInTempDir):
71
54
 
72
55
    def test_contains_whitespace(self):
73
56
        self.failUnless(osutils.contains_whitespace(u' '))
294
277
        # dates once they do occur in output strings.
295
278
 
296
279
    def test_dereference_path(self):
297
 
        self.requireFeature(SymlinkFeature)
 
280
        self.requireFeature(tests.SymlinkFeature)
298
281
        cwd = osutils.realpath('.')
299
282
        os.mkdir('bar')
300
283
        bar_path = osutils.pathjoin(cwd, 'bar')
351
334
        osutils.host_os_dereferences_symlinks()
352
335
 
353
336
 
354
 
class TestCanonicalRelPath(TestCaseInTempDir):
 
337
class TestCanonicalRelPath(tests.TestCaseInTempDir):
355
338
 
356
 
    _test_needs_features = [CaseInsCasePresFilenameFeature]
 
339
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
357
340
 
358
341
    def test_canonical_relpath_simple(self):
359
342
        f = file('MixedCaseName', 'w')
372
355
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
373
356
 
374
357
 
375
 
class TestPumpFile(TestCase):
 
358
class TestPumpFile(tests.TestCase):
376
359
    """Test pumpfile method."""
 
360
 
377
361
    def setUp(self):
378
362
        TestCase.setUp(self)
379
363
        # create a test datablock
388
372
        # make sure test data is larger than max read size
389
373
        self.assertTrue(self.test_data_len > self.block_size)
390
374
 
391
 
        from_file = FakeReadFile(self.test_data)
 
375
        from_file = file_utils.FakeReadFile(self.test_data)
392
376
        to_file = StringIO()
393
377
 
394
378
        # read (max / 2) bytes and verify read size wasn't affected
429
413
        self.assertTrue(self.test_data_len > self.block_size)
430
414
 
431
415
        # retrieve data in blocks
432
 
        from_file = FakeReadFile(self.test_data)
 
416
        from_file = file_utils.FakeReadFile(self.test_data)
433
417
        to_file = StringIO()
434
418
        osutils.pumpfile(from_file, to_file, self.test_data_len,
435
419
                         self.block_size)
453
437
        self.assertTrue(self.test_data_len > self.block_size)
454
438
 
455
439
        # retrieve data to EOF
456
 
        from_file = FakeReadFile(self.test_data)
 
440
        from_file = file_utils.FakeReadFile(self.test_data)
457
441
        to_file = StringIO()
458
442
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
459
443
 
473
457
        test verifies that any existing usages of pumpfile will not be broken
474
458
        with this new version."""
475
459
        # retrieve data using default (old) pumpfile method
476
 
        from_file = FakeReadFile(self.test_data)
 
460
        from_file = file_utils.FakeReadFile(self.test_data)
477
461
        to_file = StringIO()
478
462
        osutils.pumpfile(from_file, to_file)
479
463
 
536
520
        self.assertEqual("1234", output.getvalue())
537
521
 
538
522
 
539
 
class TestSafeUnicode(TestCase):
 
523
class TestSafeUnicode(tests.TestCase):
540
524
 
541
525
    def test_from_ascii_string(self):
542
526
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
551
535
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
552
536
 
553
537
    def test_bad_utf8_string(self):
554
 
        self.assertRaises(BzrBadParameterNotUnicode,
 
538
        self.assertRaises(errors.BzrBadParameterNotUnicode,
555
539
                          osutils.safe_unicode,
556
540
                          '\xbb\xbb')
557
541
 
558
542
 
559
 
class TestSafeUtf8(TestCase):
 
543
class TestSafeUtf8(tests.TestCase):
560
544
 
561
545
    def test_from_ascii_string(self):
562
546
        f = 'foobar'
572
556
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
573
557
 
574
558
    def test_bad_utf8_string(self):
575
 
        self.assertRaises(BzrBadParameterNotUnicode,
 
559
        self.assertRaises(errors.BzrBadParameterNotUnicode,
576
560
                          osutils.safe_utf8, '\xbb\xbb')
577
561
 
578
562
 
579
 
class TestSafeRevisionId(TestCase):
 
563
class TestSafeRevisionId(tests.TestCase):
580
564
 
581
565
    def test_from_ascii_string(self):
582
566
        # this shouldn't give a warning because it's getting an ascii string
604
588
        self.assertEqual(None, osutils.safe_revision_id(None))
605
589
 
606
590
 
607
 
class TestSafeFileId(TestCase):
 
591
class TestSafeFileId(tests.TestCase):
608
592
 
609
593
    def test_from_ascii_string(self):
610
594
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
630
614
        self.assertEqual(None, osutils.safe_file_id(None))
631
615
 
632
616
 
633
 
class TestWin32Funcs(TestCase):
634
 
    """Test that the _win32 versions of os utilities return appropriate paths."""
 
617
class TestWin32Funcs(tests.TestCase):
 
618
    """Test that _win32 versions of os utilities return appropriate paths."""
635
619
 
636
620
    def test_abspath(self):
637
621
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
688
672
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
689
673
 
690
674
 
691
 
class TestWin32FuncsDirs(TestCaseInTempDir):
 
675
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
692
676
    """Test win32 functions that create files."""
693
677
 
694
678
    def test_getcwd(self):
695
679
        if win32utils.winver == 'Windows 98':
696
 
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
 
680
            raise tests.TestSkipped(
 
681
                'Windows 98 cannot handle unicode filenames')
697
682
        # Make sure getcwd can handle unicode filenames
698
683
        try:
699
684
            os.mkdir(u'mu-\xb5')
700
685
        except UnicodeError:
701
 
            raise TestSkipped("Unable to create Unicode filename")
 
686
            raise tests.TestSkipped("Unable to create Unicode filename")
702
687
 
703
688
        os.chdir(u'mu-\xb5')
704
689
        # TODO: jam 20060427 This will probably fail on Mac OSX because
777
762
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
778
763
 
779
764
 
780
 
class TestMacFuncsDirs(TestCaseInTempDir):
 
765
class TestMacFuncsDirs(tests.TestCaseInTempDir):
781
766
    """Test mac special functions that require directories."""
782
767
 
783
768
    def test_getcwd(self):
786
771
        try:
787
772
            os.mkdir(u'B\xe5gfors')
788
773
        except UnicodeError:
789
 
            raise TestSkipped("Unable to create Unicode filename")
 
774
            raise tests.TestSkipped("Unable to create Unicode filename")
790
775
 
791
776
        os.chdir(u'B\xe5gfors')
792
777
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
796
781
        try:
797
782
            os.mkdir(u'Ba\u030agfors')
798
783
        except UnicodeError:
799
 
            raise TestSkipped("Unable to create Unicode filename")
 
784
            raise tests.TestSkipped("Unable to create Unicode filename")
800
785
 
801
786
        os.chdir(u'Ba\u030agfors')
802
787
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
803
788
 
804
789
 
805
 
class TestChunksToLines(TestCase):
 
790
class TestChunksToLines(tests.TestCase):
806
791
 
807
792
    def test_smoketest(self):
808
793
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
819
804
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
820
805
 
821
806
 
822
 
class TestSplitLines(TestCase):
 
807
class TestSplitLines(tests.TestCase):
823
808
 
824
809
    def test_split_unicode(self):
825
810
        self.assertEqual([u'foo\n', u'bar\xae'],
832
817
                         osutils.split_lines('foo\rbar\n'))
833
818
 
834
819
 
835
 
class TestWalkDirs(TestCaseInTempDir):
 
820
class TestWalkDirs(tests.TestCaseInTempDir):
836
821
 
837
822
    def test_walkdirs(self):
838
823
        tree = [
971
956
        osutils._selected_dir_reader = None
972
957
        # Nothing to list, but should still trigger the selection logic
973
958
        self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
974
 
        self.assertIsInstance(osutils._selected_dir_reader, expected)
 
959
        self.assertIsInstance(expected, osutils._selected_dir_reader)
975
960
 
976
961
    def test_force_walkdirs_utf8_fs_utf8(self):
977
962
        self.requireFeature(UTF8DirReaderFeature)
1029
1014
        try:
1030
1015
            self.build_tree(tree)
1031
1016
        except UnicodeError:
1032
 
            raise TestSkipped('Could not represent Unicode chars'
1033
 
                              ' in current encoding.')
 
1017
            raise tests.TestSkipped('Could not represent Unicode chars'
 
1018
                                    ' in current encoding.')
1034
1019
        expected_dirblocks = [
1035
1020
                ((u'', u'.'),
1036
1021
                 [(name0, name0, 'file', './' + name0),
1075
1060
        try:
1076
1061
            self.build_tree(tree)
1077
1062
        except UnicodeError:
1078
 
            raise TestSkipped('Could not represent Unicode chars'
 
1063
            raise tests.TestSkipped('Could not represent Unicode chars'
1079
1064
                              ' in current encoding.')
1080
1065
        name0 = name0.encode('utf8')
1081
1066
        name1 = name1.encode('utf8')
1143
1128
        try:
1144
1129
            self.build_tree(tree)
1145
1130
        except UnicodeError:
1146
 
            raise TestSkipped('Could not represent Unicode chars'
1147
 
                              ' in current encoding.')
 
1131
            raise tests.TestSkipped('Could not represent Unicode chars'
 
1132
                                    ' in current encoding.')
1148
1133
        name0 = name0u.encode('utf8')
1149
1134
        name1 = name1u.encode('utf8')
1150
1135
        name2 = name2u.encode('utf8')
1347
1332
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
1348
1333
 
1349
1334
 
1350
 
class TestCopyTree(TestCaseInTempDir):
 
1335
class TestCopyTree(tests.TestCaseInTempDir):
1351
1336
 
1352
1337
    def test_copy_basic_tree(self):
1353
1338
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1363
1348
        self.assertEqual(['c'], os.listdir('target/b'))
1364
1349
 
1365
1350
    def test_copy_tree_symlinks(self):
1366
 
        self.requireFeature(SymlinkFeature)
 
1351
        self.requireFeature(tests.SymlinkFeature)
1367
1352
        self.build_tree(['source/'])
1368
1353
        os.symlink('a/generic/path', 'source/lnk')
1369
1354
        osutils.copy_tree('source', 'target')
1403
1388
# [bialix] 2006/12/26
1404
1389
 
1405
1390
 
1406
 
class TestSetUnsetEnv(TestCase):
 
1391
class TestSetUnsetEnv(tests.TestCase):
1407
1392
    """Test updating the environment"""
1408
1393
 
1409
1394
    def setUp(self):
1436
1421
 
1437
1422
        So Unicode strings must be encoded.
1438
1423
        """
1439
 
        uni_val, env_val = probe_unicode_in_user_encoding()
 
1424
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
1440
1425
        if uni_val is None:
1441
 
            raise TestSkipped('Cannot find a unicode character that works in'
1442
 
                              ' encoding %s' % (osutils.get_user_encoding(),))
 
1426
            raise tests.TestSkipped(
 
1427
                'Cannot find a unicode character that works in encoding %s'
 
1428
                % (osutils.get_user_encoding(),))
1443
1429
 
1444
1430
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1445
1431
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1453
1439
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1454
1440
 
1455
1441
 
1456
 
class TestLocalTimeOffset(TestCase):
 
1442
class TestLocalTimeOffset(tests.TestCase):
1457
1443
 
1458
1444
    def test_local_time_offset(self):
1459
1445
        """Test that local_time_offset() returns a sane value."""
1475
1461
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1476
1462
 
1477
1463
 
1478
 
class TestSizeShaFile(TestCaseInTempDir):
 
1464
class TestSizeShaFile(tests.TestCaseInTempDir):
1479
1465
 
1480
1466
    def test_sha_empty(self):
1481
1467
        self.build_tree_contents([('foo', '')])
1497
1483
        self.assertEqual(expected_sha, sha)
1498
1484
 
1499
1485
 
1500
 
class TestShaFileByName(TestCaseInTempDir):
 
1486
class TestShaFileByName(tests.TestCaseInTempDir):
1501
1487
 
1502
1488
    def test_sha_empty(self):
1503
1489
        self.build_tree_contents([('foo', '')])
1511
1497
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1512
1498
 
1513
1499
 
1514
 
class TestResourceLoading(TestCaseInTempDir):
 
1500
class TestResourceLoading(tests.TestCaseInTempDir):
1515
1501
 
1516
1502
    def test_resource_string(self):
1517
1503
        # test resource in bzrlib
1527
1513
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1528
1514
 
1529
1515
 
1530
 
class TestReCompile(TestCase):
 
1516
class TestReCompile(tests.TestCase):
1531
1517
 
1532
1518
    def test_re_compile_checked(self):
1533
1519
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)