~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

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
        )
34
38
from bzrlib.tests import (
35
 
    file_utils,
36
 
    test__walkdirs_win32,
 
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,
37
50
    )
38
 
 
39
 
 
40
 
class _UTF8DirReaderFeature(tests.Feature):
 
51
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
 
52
 
 
53
 
 
54
class _UTF8DirReaderFeature(Feature):
41
55
 
42
56
    def _probe(self):
43
57
        try:
53
67
UTF8DirReaderFeature = _UTF8DirReaderFeature()
54
68
 
55
69
 
56
 
def _already_unicode(s):
57
 
    return s
58
 
 
59
 
 
60
 
def _fs_enc_to_unicode(s):
61
 
    return s.decode(osutils._fs_enc)
62
 
 
63
 
 
64
 
def _utf8_to_unicode(s):
65
 
    return s.decode('UTF-8')
66
 
 
67
 
 
68
 
def dir_reader_scenarios():
69
 
    # For each dir reader we define:
70
 
 
71
 
    # - native_to_unicode: a function converting the native_abspath as returned
72
 
    #   by DirReader.read_dir to its unicode representation
73
 
 
74
 
    # UnicodeDirReader is the fallback, it should be tested on all platforms.
75
 
    scenarios = [('unicode',
76
 
                  dict(_dir_reader_class=osutils.UnicodeDirReader,
77
 
                       _native_to_unicode=_already_unicode))]
78
 
    # Some DirReaders are platform specific and even there they may not be
79
 
    # available.
80
 
    if UTF8DirReaderFeature.available():
81
 
        from bzrlib import _readdir_pyx
82
 
        scenarios.append(('utf8',
83
 
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
84
 
                               _native_to_unicode=_utf8_to_unicode)))
85
 
 
86
 
    if test__walkdirs_win32.Win32ReadDirFeature.available():
87
 
        try:
88
 
            from bzrlib import _walkdirs_win32
89
 
            # TODO: check on windows, it may be that we need to use/add
90
 
            # safe_unicode instead of _fs_enc_to_unicode
91
 
            scenarios.append(
92
 
                ('win32',
93
 
                 dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
94
 
                      _native_to_unicode=_fs_enc_to_unicode)))
95
 
        except ImportError:
96
 
            pass
97
 
    return scenarios
98
 
 
99
 
 
100
 
def load_tests(basic_tests, module, loader):
101
 
    suite = loader.suiteClass()
102
 
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
103
 
        basic_tests, tests.condition_isinstance(TestDirReader))
104
 
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
105
 
    suite.addTest(remaining_tests)
106
 
    return suite
107
 
 
108
 
 
109
 
class TestContainsWhitespace(tests.TestCase):
 
70
class TestOSUtils(TestCaseInTempDir):
110
71
 
111
72
    def test_contains_whitespace(self):
112
73
        self.failUnless(osutils.contains_whitespace(u' '))
122
83
        self.failIf(osutils.contains_whitespace(u'hellothere'))
123
84
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
124
85
 
125
 
 
126
 
class TestRename(tests.TestCaseInTempDir):
127
 
 
128
86
    def test_fancy_rename(self):
129
87
        # This should work everywhere
130
88
        def rename(a, b):
168
126
        shape = sorted(os.listdir('.'))
169
127
        self.assertEquals(['A', 'B'], shape)
170
128
 
171
 
 
172
 
class TestRandChars(tests.TestCase):
173
 
 
174
129
    def test_01_rand_chars_empty(self):
175
130
        result = osutils.rand_chars(0)
176
131
        self.assertEqual(result, '')
181
136
        self.assertEqual(type(result), str)
182
137
        self.assertContainsRe(result, r'^[a-z0-9]{100}$')
183
138
 
184
 
 
185
 
class TestIsInside(tests.TestCase):
186
 
 
187
139
    def test_is_inside(self):
188
140
        is_inside = osutils.is_inside
189
141
        self.assertTrue(is_inside('src', 'src/foo.c'))
218
170
                         (['src'], 'srccontrol/foo')]:
219
171
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
220
172
 
221
 
 
222
 
class TestRmTree(tests.TestCaseInTempDir):
223
 
 
224
173
    def test_rmtree(self):
225
174
        # Check to remove tree with read-only files/dirs
226
175
        os.mkdir('dir')
239
188
        self.failIfExists('dir/file')
240
189
        self.failIfExists('dir')
241
190
 
242
 
 
243
 
class TestDeleteAny(tests.TestCaseInTempDir):
244
 
 
245
 
    def test_delete_any_readonly(self):
246
 
        # from <https://bugs.launchpad.net/bzr/+bug/218206>
247
 
        self.build_tree(['d/', 'f'])
248
 
        osutils.make_readonly('d')
249
 
        osutils.make_readonly('f')
250
 
 
251
 
        osutils.delete_any('f')
252
 
        osutils.delete_any('d')
253
 
 
254
 
 
255
 
class TestKind(tests.TestCaseInTempDir):
256
 
 
257
191
    def test_file_kind(self):
258
192
        self.build_tree(['file', 'dir/'])
259
193
        self.assertEquals('file', osutils.file_kind('file'))
289
223
                os.remove('socket')
290
224
 
291
225
    def test_kind_marker(self):
292
 
        self.assertEqual("", osutils.kind_marker("file"))
293
 
        self.assertEqual("/", osutils.kind_marker('directory'))
294
 
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
295
 
        self.assertEqual("@", osutils.kind_marker("symlink"))
296
 
        self.assertEqual("+", osutils.kind_marker("tree-reference"))
297
 
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
298
 
 
299
 
 
300
 
class TestUmask(tests.TestCaseInTempDir):
 
226
        self.assertEqual(osutils.kind_marker('file'), '')
 
227
        self.assertEqual(osutils.kind_marker('directory'), '/')
 
228
        self.assertEqual(osutils.kind_marker('symlink'), '@')
 
229
        self.assertEqual(osutils.kind_marker('tree-reference'), '+')
301
230
 
302
231
    def test_get_umask(self):
303
232
        if sys.platform == 'win32':
306
235
            return
307
236
 
308
237
        orig_umask = osutils.get_umask()
309
 
        self.addCleanup(os.umask, orig_umask)
310
 
        os.umask(0222)
311
 
        self.assertEqual(0222, osutils.get_umask())
312
 
        os.umask(0022)
313
 
        self.assertEqual(0022, osutils.get_umask())
314
 
        os.umask(0002)
315
 
        self.assertEqual(0002, osutils.get_umask())
316
 
        os.umask(0027)
317
 
        self.assertEqual(0027, osutils.get_umask())
318
 
 
319
 
 
320
 
class TestDateTime(tests.TestCase):
 
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)
321
249
 
322
250
    def assertFormatedDelta(self, expected, seconds):
323
251
        """Assert osutils.format_delta formats as expected"""
365
293
        # Instead blackbox.test_locale should check for localized
366
294
        # dates once they do occur in output strings.
367
295
 
368
 
    def test_local_time_offset(self):
369
 
        """Test that local_time_offset() returns a sane value."""
370
 
        offset = osutils.local_time_offset()
371
 
        self.assertTrue(isinstance(offset, int))
372
 
        # Test that the offset is no more than a eighteen hours in
373
 
        # either direction.
374
 
        # Time zone handling is system specific, so it is difficult to
375
 
        # do more specific tests, but a value outside of this range is
376
 
        # probably wrong.
377
 
        eighteen_hours = 18 * 3600
378
 
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
379
 
 
380
 
    def test_local_time_offset_with_timestamp(self):
381
 
        """Test that local_time_offset() works with a timestamp."""
382
 
        offset = osutils.local_time_offset(1000000000.1234567)
383
 
        self.assertTrue(isinstance(offset, int))
384
 
        eighteen_hours = 18 * 3600
385
 
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
386
 
 
387
 
 
388
 
class TestLinks(tests.TestCaseInTempDir):
389
 
 
390
296
    def test_dereference_path(self):
391
 
        self.requireFeature(tests.SymlinkFeature)
 
297
        self.requireFeature(SymlinkFeature)
392
298
        cwd = osutils.realpath('.')
393
299
        os.mkdir('bar')
394
300
        bar_path = osutils.pathjoin(cwd, 'bar')
435
341
            osutils.make_readonly('dangling')
436
342
            osutils.make_writable('dangling')
437
343
 
 
344
    def test_kind_marker(self):
 
345
        self.assertEqual("", osutils.kind_marker("file"))
 
346
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
 
347
        self.assertEqual("@", osutils.kind_marker("symlink"))
 
348
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
 
349
 
438
350
    def test_host_os_dereferences_symlinks(self):
439
351
        osutils.host_os_dereferences_symlinks()
440
352
 
441
353
 
442
 
class TestCanonicalRelPath(tests.TestCaseInTempDir):
 
354
class TestCanonicalRelPath(TestCaseInTempDir):
443
355
 
444
 
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
 
356
    _test_needs_features = [CaseInsCasePresFilenameFeature]
445
357
 
446
358
    def test_canonical_relpath_simple(self):
447
359
        f = file('MixedCaseName', 'w')
460
372
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
461
373
 
462
374
 
463
 
class TestPumpFile(tests.TestCase):
 
375
class TestPumpFile(TestCase):
464
376
    """Test pumpfile method."""
465
 
 
466
377
    def setUp(self):
467
 
        tests.TestCase.setUp(self)
 
378
        TestCase.setUp(self)
468
379
        # create a test datablock
469
380
        self.block_size = 512
470
381
        pattern = '0123456789ABCDEF'
477
388
        # make sure test data is larger than max read size
478
389
        self.assertTrue(self.test_data_len > self.block_size)
479
390
 
480
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
391
        from_file = FakeReadFile(self.test_data)
481
392
        to_file = StringIO()
482
393
 
483
394
        # read (max / 2) bytes and verify read size wasn't affected
518
429
        self.assertTrue(self.test_data_len > self.block_size)
519
430
 
520
431
        # retrieve data in blocks
521
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
432
        from_file = FakeReadFile(self.test_data)
522
433
        to_file = StringIO()
523
434
        osutils.pumpfile(from_file, to_file, self.test_data_len,
524
435
                         self.block_size)
542
453
        self.assertTrue(self.test_data_len > self.block_size)
543
454
 
544
455
        # retrieve data to EOF
545
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
456
        from_file = FakeReadFile(self.test_data)
546
457
        to_file = StringIO()
547
458
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
548
459
 
562
473
        test verifies that any existing usages of pumpfile will not be broken
563
474
        with this new version."""
564
475
        # retrieve data using default (old) pumpfile method
565
 
        from_file = file_utils.FakeReadFile(self.test_data)
 
476
        from_file = FakeReadFile(self.test_data)
566
477
        to_file = StringIO()
567
478
        osutils.pumpfile(from_file, to_file)
568
479
 
602
513
 
603
514
 
604
515
 
605
 
class TestPumpStringFile(tests.TestCase):
 
516
class TestPumpStringFile(TestCase):
606
517
 
607
518
    def test_empty(self):
608
519
        output = StringIO()
625
536
        self.assertEqual("1234", output.getvalue())
626
537
 
627
538
 
628
 
class TestRelpath(tests.TestCase):
629
 
 
630
 
    def test_simple_relpath(self):
631
 
        cwd = osutils.getcwd()
632
 
        subdir = cwd + '/subdir'
633
 
        self.assertEqual('subdir', osutils.relpath(cwd, subdir))
634
 
 
635
 
    def test_deep_relpath(self):
636
 
        cwd = osutils.getcwd()
637
 
        subdir = cwd + '/sub/subsubdir'
638
 
        self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
639
 
 
640
 
    def test_not_relative(self):
641
 
        self.assertRaises(errors.PathNotChild,
642
 
                          osutils.relpath, 'C:/path', 'H:/path')
643
 
        self.assertRaises(errors.PathNotChild,
644
 
                          osutils.relpath, 'C:/', 'H:/path')
645
 
 
646
 
 
647
 
class TestSafeUnicode(tests.TestCase):
 
539
class TestSafeUnicode(TestCase):
648
540
 
649
541
    def test_from_ascii_string(self):
650
542
        self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
659
551
        self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
660
552
 
661
553
    def test_bad_utf8_string(self):
662
 
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
554
        self.assertRaises(BzrBadParameterNotUnicode,
663
555
                          osutils.safe_unicode,
664
556
                          '\xbb\xbb')
665
557
 
666
558
 
667
 
class TestSafeUtf8(tests.TestCase):
 
559
class TestSafeUtf8(TestCase):
668
560
 
669
561
    def test_from_ascii_string(self):
670
562
        f = 'foobar'
680
572
        self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
681
573
 
682
574
    def test_bad_utf8_string(self):
683
 
        self.assertRaises(errors.BzrBadParameterNotUnicode,
 
575
        self.assertRaises(BzrBadParameterNotUnicode,
684
576
                          osutils.safe_utf8, '\xbb\xbb')
685
577
 
686
578
 
687
 
class TestSafeRevisionId(tests.TestCase):
 
579
class TestSafeRevisionId(TestCase):
688
580
 
689
581
    def test_from_ascii_string(self):
690
582
        # this shouldn't give a warning because it's getting an ascii string
712
604
        self.assertEqual(None, osutils.safe_revision_id(None))
713
605
 
714
606
 
715
 
class TestSafeFileId(tests.TestCase):
 
607
class TestSafeFileId(TestCase):
716
608
 
717
609
    def test_from_ascii_string(self):
718
610
        self.assertEqual('foobar', osutils.safe_file_id('foobar'))
738
630
        self.assertEqual(None, osutils.safe_file_id(None))
739
631
 
740
632
 
741
 
class TestWin32Funcs(tests.TestCase):
742
 
    """Test that _win32 versions of os utilities return appropriate paths."""
 
633
class TestWin32Funcs(TestCase):
 
634
    """Test that the _win32 versions of os utilities return appropriate paths."""
743
635
 
744
636
    def test_abspath(self):
745
637
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
752
644
        self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
753
645
 
754
646
    def test_pathjoin(self):
755
 
        self.assertEqual('path/to/foo',
756
 
                         osutils._win32_pathjoin('path', 'to', 'foo'))
757
 
        self.assertEqual('C:/foo',
758
 
                         osutils._win32_pathjoin('path\\to', 'C:\\foo'))
759
 
        self.assertEqual('C:/foo',
760
 
                         osutils._win32_pathjoin('path/to', 'C:/foo'))
761
 
        self.assertEqual('path/to/foo',
762
 
                         osutils._win32_pathjoin('path/to/', 'foo'))
763
 
        self.assertEqual('/foo',
764
 
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
765
 
        self.assertEqual('/foo',
766
 
                         osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
 
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'))
767
653
 
768
654
    def test_normpath(self):
769
 
        self.assertEqual('path/to/foo',
770
 
                         osutils._win32_normpath(r'path\\from\..\to\.\foo'))
771
 
        self.assertEqual('path/to/foo',
772
 
                         osutils._win32_normpath('path//from/../to/./foo'))
 
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'))
773
657
 
774
658
    def test_getcwd(self):
775
659
        cwd = osutils._win32_getcwd()
804
688
        self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
805
689
 
806
690
 
807
 
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
 
691
class TestWin32FuncsDirs(TestCaseInTempDir):
808
692
    """Test win32 functions that create files."""
809
693
 
810
694
    def test_getcwd(self):
811
 
        self.requireFeature(tests.UnicodeFilenameFeature)
812
 
        os.mkdir(u'mu-\xb5')
 
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
 
813
703
        os.chdir(u'mu-\xb5')
814
704
        # TODO: jam 20060427 This will probably fail on Mac OSX because
815
705
        #       it will change the normalization of B\xe5gfors
820
710
    def test_minimum_path_selection(self):
821
711
        self.assertEqual(set(),
822
712
            osutils.minimum_path_selection([]))
823
 
        self.assertEqual(set(['a']),
824
 
            osutils.minimum_path_selection(['a']))
825
713
        self.assertEqual(set(['a', 'b']),
826
714
            osutils.minimum_path_selection(['a', 'b']))
827
715
        self.assertEqual(set(['a/', 'b']),
828
716
            osutils.minimum_path_selection(['a/', 'b']))
829
717
        self.assertEqual(set(['a/', 'b']),
830
718
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
831
 
        self.assertEqual(set(['a-b', 'a', 'a0b']),
832
 
            osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
833
719
 
834
720
    def test_mkdtemp(self):
835
721
        tmpdir = osutils._win32_mkdtemp(dir='.')
891
777
        self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
892
778
 
893
779
 
894
 
class TestParentDirectories(tests.TestCaseInTempDir):
895
 
    """Test osutils.parent_directories()"""
896
 
 
897
 
    def test_parent_directories(self):
898
 
        self.assertEqual([], osutils.parent_directories('a'))
899
 
        self.assertEqual(['a'], osutils.parent_directories('a/b'))
900
 
        self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
901
 
 
902
 
 
903
 
class TestMacFuncsDirs(tests.TestCaseInTempDir):
 
780
class TestMacFuncsDirs(TestCaseInTempDir):
904
781
    """Test mac special functions that require directories."""
905
782
 
906
783
    def test_getcwd(self):
907
 
        self.requireFeature(tests.UnicodeFilenameFeature)
908
 
        os.mkdir(u'B\xe5gfors')
 
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
 
909
791
        os.chdir(u'B\xe5gfors')
910
792
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
911
793
 
912
794
    def test_getcwd_nonnorm(self):
913
 
        self.requireFeature(tests.UnicodeFilenameFeature)
914
795
        # Test that _mac_getcwd() will normalize this path
915
 
        os.mkdir(u'Ba\u030agfors')
 
796
        try:
 
797
            os.mkdir(u'Ba\u030agfors')
 
798
        except UnicodeError:
 
799
            raise TestSkipped("Unable to create Unicode filename")
 
800
 
916
801
        os.chdir(u'Ba\u030agfors')
917
802
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
918
803
 
919
804
 
920
 
class TestChunksToLines(tests.TestCase):
 
805
class TestChunksToLines(TestCase):
921
806
 
922
807
    def test_smoketest(self):
923
808
        self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
934
819
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
935
820
 
936
821
 
937
 
class TestSplitLines(tests.TestCase):
 
822
class TestSplitLines(TestCase):
938
823
 
939
824
    def test_split_unicode(self):
940
825
        self.assertEqual([u'foo\n', u'bar\xae'],
947
832
                         osutils.split_lines('foo\rbar\n'))
948
833
 
949
834
 
950
 
class TestWalkDirs(tests.TestCaseInTempDir):
951
 
 
952
 
    def assertExpectedBlocks(self, expected, result):
953
 
        self.assertEqual(expected,
954
 
                         [(dirinfo, [line[0:3] for line in block])
955
 
                          for dirinfo, block in result])
 
835
class TestWalkDirs(TestCaseInTempDir):
956
836
 
957
837
    def test_walkdirs(self):
958
838
        tree = [
991
871
            result.append((dirdetail, dirblock))
992
872
 
993
873
        self.assertTrue(found_bzrdir)
994
 
        self.assertExpectedBlocks(expected_dirblocks, result)
 
874
        self.assertEqual(expected_dirblocks,
 
875
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
995
876
        # you can search a subdir only, with a supplied prefix.
996
877
        result = []
997
878
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
998
879
            result.append(dirblock)
999
 
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
880
        self.assertEqual(expected_dirblocks[1:],
 
881
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1000
882
 
1001
883
    def test_walkdirs_os_error(self):
1002
884
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1008
890
        os.mkdir("test-unreadable")
1009
891
        os.chmod("test-unreadable", 0000)
1010
892
        # must chmod it back so that it can be removed
1011
 
        self.addCleanup(os.chmod, "test-unreadable", 0700)
 
893
        self.addCleanup(lambda: os.chmod("test-unreadable", 0700))
1012
894
        # The error is not raised until the generator is actually evaluated.
1013
895
        # (It would be ok if it happened earlier but at the moment it
1014
896
        # doesn't.)
1055
937
            result.append((dirdetail, dirblock))
1056
938
 
1057
939
        self.assertTrue(found_bzrdir)
1058
 
        self.assertExpectedBlocks(expected_dirblocks, result)
1059
 
 
 
940
        self.assertEqual(expected_dirblocks,
 
941
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1060
942
        # you can search a subdir only, with a supplied prefix.
1061
943
        result = []
1062
944
        for dirblock in osutils.walkdirs('./1dir', '1dir'):
1063
945
            result.append(dirblock)
1064
 
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
 
946
        self.assertEqual(expected_dirblocks[1:],
 
947
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
1065
948
 
1066
949
    def _filter_out_stat(self, result):
1067
950
        """Filter out the stat value from the walkdirs result"""
1082
965
            osutils._selected_dir_reader = cur_dir_reader
1083
966
        self.addCleanup(restore)
1084
967
 
1085
 
    def assertDirReaderIs(self, expected):
 
968
    def assertReadFSDirIs(self, expected):
1086
969
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
1087
970
        # Force it to redetect
1088
971
        osutils._selected_dir_reader = None
1095
978
        self._save_platform_info()
1096
979
        win32utils.winver = None # Avoid the win32 detection code
1097
980
        osutils._fs_enc = 'UTF-8'
1098
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
981
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1099
982
 
1100
983
    def test_force_walkdirs_utf8_fs_ascii(self):
1101
984
        self.requireFeature(UTF8DirReaderFeature)
1102
985
        self._save_platform_info()
1103
986
        win32utils.winver = None # Avoid the win32 detection code
1104
987
        osutils._fs_enc = 'US-ASCII'
1105
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
988
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1106
989
 
1107
990
    def test_force_walkdirs_utf8_fs_ANSI(self):
1108
991
        self.requireFeature(UTF8DirReaderFeature)
1109
992
        self._save_platform_info()
1110
993
        win32utils.winver = None # Avoid the win32 detection code
1111
994
        osutils._fs_enc = 'ANSI_X3.4-1968'
1112
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
995
        self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1113
996
 
1114
997
    def test_force_walkdirs_utf8_fs_latin1(self):
1115
998
        self._save_platform_info()
1116
999
        win32utils.winver = None # Avoid the win32 detection code
1117
1000
        osutils._fs_enc = 'latin1'
1118
 
        self.assertDirReaderIs(osutils.UnicodeDirReader)
 
1001
        self.assertReadFSDirIs(osutils.UnicodeDirReader)
1119
1002
 
1120
1003
    def test_force_walkdirs_utf8_nt(self):
1121
1004
        # Disabled because the thunk of the whole walkdirs api is disabled.
1122
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1005
        self.requireFeature(Win32ReadDirFeature)
1123
1006
        self._save_platform_info()
1124
1007
        win32utils.winver = 'Windows NT'
1125
1008
        from bzrlib._walkdirs_win32 import Win32ReadDir
1126
 
        self.assertDirReaderIs(Win32ReadDir)
 
1009
        self.assertReadFSDirIs(Win32ReadDir)
1127
1010
 
1128
1011
    def test_force_walkdirs_utf8_98(self):
1129
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1012
        self.requireFeature(Win32ReadDirFeature)
1130
1013
        self._save_platform_info()
1131
1014
        win32utils.winver = 'Windows 98'
1132
 
        self.assertDirReaderIs(osutils.UnicodeDirReader)
 
1015
        self.assertReadFSDirIs(osutils.UnicodeDirReader)
1133
1016
 
1134
1017
    def test_unicode_walkdirs(self):
1135
1018
        """Walkdirs should always return unicode paths."""
1136
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1137
1019
        name0 = u'0file-\xb6'
1138
1020
        name1 = u'1dir-\u062c\u0648'
1139
1021
        name2 = u'2file-\u0633'
1144
1026
            name1 + '/' + name1 + '/',
1145
1027
            name2,
1146
1028
            ]
1147
 
        self.build_tree(tree)
 
1029
        try:
 
1030
            self.build_tree(tree)
 
1031
        except UnicodeError:
 
1032
            raise TestSkipped('Could not represent Unicode chars'
 
1033
                              ' in current encoding.')
1148
1034
        expected_dirblocks = [
1149
1035
                ((u'', u'.'),
1150
1036
                 [(name0, name0, 'file', './' + name0),
1176
1062
 
1177
1063
        The abspath portion might be in unicode or utf-8
1178
1064
        """
1179
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1180
1065
        name0 = u'0file-\xb6'
1181
1066
        name1 = u'1dir-\u062c\u0648'
1182
1067
        name2 = u'2file-\u0633'
1187
1072
            name1 + '/' + name1 + '/',
1188
1073
            name2,
1189
1074
            ]
1190
 
        self.build_tree(tree)
 
1075
        try:
 
1076
            self.build_tree(tree)
 
1077
        except UnicodeError:
 
1078
            raise TestSkipped('Could not represent Unicode chars'
 
1079
                              ' in current encoding.')
1191
1080
        name0 = name0.encode('utf8')
1192
1081
        name1 = name1.encode('utf8')
1193
1082
        name2 = name2.encode('utf8')
1237
1126
 
1238
1127
        The abspath portion should be in unicode
1239
1128
        """
1240
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1241
1129
        # Use the unicode reader. TODO: split into driver-and-driven unit
1242
1130
        # tests.
1243
1131
        self._save_platform_info()
1252
1140
            name1u + '/' + name1u + '/',
1253
1141
            name2u,
1254
1142
            ]
1255
 
        self.build_tree(tree)
 
1143
        try:
 
1144
            self.build_tree(tree)
 
1145
        except UnicodeError:
 
1146
            raise TestSkipped('Could not represent Unicode chars'
 
1147
                              ' in current encoding.')
1256
1148
        name0 = name0u.encode('utf8')
1257
1149
        name1 = name1u.encode('utf8')
1258
1150
        name2 = name2u.encode('utf8')
1283
1175
        self.assertEqual(expected_dirblocks, result)
1284
1176
 
1285
1177
    def test__walkdirs_utf8_win32readdir(self):
1286
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1178
        self.requireFeature(Win32ReadDirFeature)
1287
1179
        self.requireFeature(tests.UnicodeFilenameFeature)
1288
1180
        from bzrlib._walkdirs_win32 import Win32ReadDir
1289
1181
        self._save_platform_info()
1340
1232
 
1341
1233
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1342
1234
        """make sure our Stat values are valid"""
1343
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1235
        self.requireFeature(Win32ReadDirFeature)
1344
1236
        self.requireFeature(tests.UnicodeFilenameFeature)
1345
1237
        from bzrlib._walkdirs_win32 import Win32ReadDir
1346
1238
        name0u = u'0file-\xb6'
1364
1256
 
1365
1257
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1366
1258
        """make sure our Stat values are valid"""
1367
 
        self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
 
1259
        self.requireFeature(Win32ReadDirFeature)
1368
1260
        self.requireFeature(tests.UnicodeFilenameFeature)
1369
1261
        from bzrlib._walkdirs_win32 import Win32ReadDir
1370
1262
        name0u = u'0dir-\u062c\u0648'
1455
1347
            sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
1456
1348
 
1457
1349
 
1458
 
class TestCopyTree(tests.TestCaseInTempDir):
 
1350
class TestCopyTree(TestCaseInTempDir):
1459
1351
 
1460
1352
    def test_copy_basic_tree(self):
1461
1353
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1471
1363
        self.assertEqual(['c'], os.listdir('target/b'))
1472
1364
 
1473
1365
    def test_copy_tree_symlinks(self):
1474
 
        self.requireFeature(tests.SymlinkFeature)
 
1366
        self.requireFeature(SymlinkFeature)
1475
1367
        self.build_tree(['source/'])
1476
1368
        os.symlink('a/generic/path', 'source/lnk')
1477
1369
        osutils.copy_tree('source', 'target')
1507
1399
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1508
1400
 
1509
1401
 
1510
 
class TestSetUnsetEnv(tests.TestCase):
 
1402
#class TestTerminalEncoding has been moved to test_osutils_encodings.py
 
1403
# [bialix] 2006/12/26
 
1404
 
 
1405
 
 
1406
class TestSetUnsetEnv(TestCase):
1511
1407
    """Test updating the environment"""
1512
1408
 
1513
1409
    def setUp(self):
1540
1436
 
1541
1437
        So Unicode strings must be encoded.
1542
1438
        """
1543
 
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
1439
        uni_val, env_val = probe_unicode_in_user_encoding()
1544
1440
        if uni_val is None:
1545
 
            raise tests.TestSkipped(
1546
 
                'Cannot find a unicode character that works in encoding %s'
1547
 
                % (osutils.get_user_encoding(),))
 
1441
            raise TestSkipped('Cannot find a unicode character that works in'
 
1442
                              ' encoding %s' % (osutils.get_user_encoding(),))
1548
1443
 
1549
1444
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
1550
1445
        self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
1558
1453
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1559
1454
 
1560
1455
 
1561
 
class TestSizeShaFile(tests.TestCaseInTempDir):
 
1456
class TestLocalTimeOffset(TestCase):
 
1457
 
 
1458
    def test_local_time_offset(self):
 
1459
        """Test that local_time_offset() returns a sane value."""
 
1460
        offset = osutils.local_time_offset()
 
1461
        self.assertTrue(isinstance(offset, int))
 
1462
        # Test that the offset is no more than a eighteen hours in
 
1463
        # either direction.
 
1464
        # Time zone handling is system specific, so it is difficult to
 
1465
        # do more specific tests, but a value outside of this range is
 
1466
        # probably wrong.
 
1467
        eighteen_hours = 18 * 3600
 
1468
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
1469
 
 
1470
    def test_local_time_offset_with_timestamp(self):
 
1471
        """Test that local_time_offset() works with a timestamp."""
 
1472
        offset = osutils.local_time_offset(1000000000.1234567)
 
1473
        self.assertTrue(isinstance(offset, int))
 
1474
        eighteen_hours = 18 * 3600
 
1475
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
 
1476
 
 
1477
 
 
1478
class TestSizeShaFile(TestCaseInTempDir):
1562
1479
 
1563
1480
    def test_sha_empty(self):
1564
1481
        self.build_tree_contents([('foo', '')])
1580
1497
        self.assertEqual(expected_sha, sha)
1581
1498
 
1582
1499
 
1583
 
class TestShaFileByName(tests.TestCaseInTempDir):
 
1500
class TestShaFileByName(TestCaseInTempDir):
1584
1501
 
1585
1502
    def test_sha_empty(self):
1586
1503
        self.build_tree_contents([('foo', '')])
1594
1511
        self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1595
1512
 
1596
1513
 
1597
 
class TestResourceLoading(tests.TestCaseInTempDir):
 
1514
class TestResourceLoading(TestCaseInTempDir):
1598
1515
 
1599
1516
    def test_resource_string(self):
1600
1517
        # test resource in bzrlib
1610
1527
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1611
1528
 
1612
1529
 
1613
 
class TestReCompile(tests.TestCase):
 
1530
class TestReCompile(TestCase):
1614
1531
 
1615
1532
    def test_re_compile_checked(self):
1616
1533
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1626
1543
            "Invalid regular expression in test case: '*': "
1627
1544
            "nothing to repeat",
1628
1545
            str(err))
1629
 
 
1630
 
 
1631
 
class TestDirReader(tests.TestCaseInTempDir):
1632
 
 
1633
 
    # Set by load_tests
1634
 
    _dir_reader_class = None
1635
 
    _native_to_unicode = None
1636
 
 
1637
 
    def setUp(self):
1638
 
        tests.TestCaseInTempDir.setUp(self)
1639
 
 
1640
 
        # Save platform specific info and reset it
1641
 
        cur_dir_reader = osutils._selected_dir_reader
1642
 
 
1643
 
        def restore():
1644
 
            osutils._selected_dir_reader = cur_dir_reader
1645
 
        self.addCleanup(restore)
1646
 
 
1647
 
        osutils._selected_dir_reader = self._dir_reader_class()
1648
 
 
1649
 
    def _get_ascii_tree(self):
1650
 
        tree = [
1651
 
            '0file',
1652
 
            '1dir/',
1653
 
            '1dir/0file',
1654
 
            '1dir/1dir/',
1655
 
            '2file'
1656
 
            ]
1657
 
        expected_dirblocks = [
1658
 
                (('', '.'),
1659
 
                 [('0file', '0file', 'file'),
1660
 
                  ('1dir', '1dir', 'directory'),
1661
 
                  ('2file', '2file', 'file'),
1662
 
                 ]
1663
 
                ),
1664
 
                (('1dir', './1dir'),
1665
 
                 [('1dir/0file', '0file', 'file'),
1666
 
                  ('1dir/1dir', '1dir', 'directory'),
1667
 
                 ]
1668
 
                ),
1669
 
                (('1dir/1dir', './1dir/1dir'),
1670
 
                 [
1671
 
                 ]
1672
 
                ),
1673
 
            ]
1674
 
        return tree, expected_dirblocks
1675
 
 
1676
 
    def test_walk_cur_dir(self):
1677
 
        tree, expected_dirblocks = self._get_ascii_tree()
1678
 
        self.build_tree(tree)
1679
 
        result = list(osutils._walkdirs_utf8('.'))
1680
 
        # Filter out stat and abspath
1681
 
        self.assertEqual(expected_dirblocks,
1682
 
                         [(dirinfo, [line[0:3] for line in block])
1683
 
                          for dirinfo, block in result])
1684
 
 
1685
 
    def test_walk_sub_dir(self):
1686
 
        tree, expected_dirblocks = self._get_ascii_tree()
1687
 
        self.build_tree(tree)
1688
 
        # you can search a subdir only, with a supplied prefix.
1689
 
        result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1690
 
        # Filter out stat and abspath
1691
 
        self.assertEqual(expected_dirblocks[1:],
1692
 
                         [(dirinfo, [line[0:3] for line in block])
1693
 
                          for dirinfo, block in result])
1694
 
 
1695
 
    def _get_unicode_tree(self):
1696
 
        name0u = u'0file-\xb6'
1697
 
        name1u = u'1dir-\u062c\u0648'
1698
 
        name2u = u'2file-\u0633'
1699
 
        tree = [
1700
 
            name0u,
1701
 
            name1u + '/',
1702
 
            name1u + '/' + name0u,
1703
 
            name1u + '/' + name1u + '/',
1704
 
            name2u,
1705
 
            ]
1706
 
        name0 = name0u.encode('UTF-8')
1707
 
        name1 = name1u.encode('UTF-8')
1708
 
        name2 = name2u.encode('UTF-8')
1709
 
        expected_dirblocks = [
1710
 
                (('', '.'),
1711
 
                 [(name0, name0, 'file', './' + name0u),
1712
 
                  (name1, name1, 'directory', './' + name1u),
1713
 
                  (name2, name2, 'file', './' + name2u),
1714
 
                 ]
1715
 
                ),
1716
 
                ((name1, './' + name1u),
1717
 
                 [(name1 + '/' + name0, name0, 'file', './' + name1u
1718
 
                                                        + '/' + name0u),
1719
 
                  (name1 + '/' + name1, name1, 'directory', './' + name1u
1720
 
                                                            + '/' + name1u),
1721
 
                 ]
1722
 
                ),
1723
 
                ((name1 + '/' + name1, './' + name1u + '/' + name1u),
1724
 
                 [
1725
 
                 ]
1726
 
                ),
1727
 
            ]
1728
 
        return tree, expected_dirblocks
1729
 
 
1730
 
    def _filter_out(self, raw_dirblocks):
1731
 
        """Filter out a walkdirs_utf8 result.
1732
 
 
1733
 
        stat field is removed, all native paths are converted to unicode
1734
 
        """
1735
 
        filtered_dirblocks = []
1736
 
        for dirinfo, block in raw_dirblocks:
1737
 
            dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1738
 
            details = []
1739
 
            for line in block:
1740
 
                details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1741
 
            filtered_dirblocks.append((dirinfo, details))
1742
 
        return filtered_dirblocks
1743
 
 
1744
 
    def test_walk_unicode_tree(self):
1745
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1746
 
        tree, expected_dirblocks = self._get_unicode_tree()
1747
 
        self.build_tree(tree)
1748
 
        result = list(osutils._walkdirs_utf8('.'))
1749
 
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1750
 
 
1751
 
    def test_symlink(self):
1752
 
        self.requireFeature(tests.SymlinkFeature)
1753
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1754
 
        target = u'target\N{Euro Sign}'
1755
 
        link_name = u'l\N{Euro Sign}nk'
1756
 
        os.symlink(target, link_name)
1757
 
        target_utf8 = target.encode('UTF-8')
1758
 
        link_name_utf8 = link_name.encode('UTF-8')
1759
 
        expected_dirblocks = [
1760
 
                (('', '.'),
1761
 
                 [(link_name_utf8, link_name_utf8,
1762
 
                   'symlink', './' + link_name),],
1763
 
                 )]
1764
 
        result = list(osutils._walkdirs_utf8('.'))
1765
 
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1766
 
 
1767
 
 
1768
 
class TestReadLink(tests.TestCaseInTempDir):
1769
 
    """Exposes os.readlink() problems and the osutils solution.
1770
 
 
1771
 
    The only guarantee offered by os.readlink(), starting with 2.6, is that a
1772
 
    unicode string will be returned if a unicode string is passed.
1773
 
 
1774
 
    But prior python versions failed to properly encode the passed unicode
1775
 
    string.
1776
 
    """
1777
 
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
1778
 
 
1779
 
    def setUp(self):
1780
 
        super(tests.TestCaseInTempDir, self).setUp()
1781
 
        self.link = u'l\N{Euro Sign}ink'
1782
 
        self.target = u'targe\N{Euro Sign}t'
1783
 
        os.symlink(self.target, self.link)
1784
 
 
1785
 
    def test_os_readlink_link_encoding(self):
1786
 
        if sys.version_info < (2, 6):
1787
 
            self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1788
 
        else:
1789
 
            self.assertEquals(self.target,  os.readlink(self.link))
1790
 
 
1791
 
    def test_os_readlink_link_decoding(self):
1792
 
        self.assertEquals(self.target.encode(osutils._fs_enc),
1793
 
                          os.readlink(self.link.encode(osutils._fs_enc)))
1794
 
 
1795
 
 
1796
 
class TestConcurrency(tests.TestCase):
1797
 
 
1798
 
    def test_local_concurrency(self):
1799
 
        concurrency = osutils.local_concurrency()
1800
 
        self.assertIsInstance(concurrency, int)