/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-22 00:56:52 UTC
  • mfrom: (6621.2.26 py3_pokes)
  • Revision ID: jelmer@jelmer.uk-20170522005652-yjahcr9hwmjkno7n
Merge Python3 porting work ('py3 pokes')

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the osutils wrapper."""
18
18
 
19
 
from cStringIO import StringIO
20
19
import errno
21
20
import os
22
21
import re
26
25
import tempfile
27
26
import time
28
27
 
29
 
from breezy import (
 
28
from .. import (
30
29
    errors,
31
30
    lazy_regex,
32
31
    osutils,
35
34
    trace,
36
35
    win32utils,
37
36
    )
38
 
from breezy.tests import (
 
37
from ..sixish import (
 
38
    BytesIO,
 
39
    )
 
40
from . import (
39
41
    features,
40
42
    file_utils,
41
43
    test__walkdirs_win32,
42
44
    )
43
 
from breezy.tests.scenarios import load_tests_apply_scenarios
 
45
from .scenarios import load_tests_apply_scenarios
44
46
 
45
47
 
46
48
class _UTF8DirReaderFeature(features.Feature):
47
49
 
48
50
    def _probe(self):
49
51
        try:
50
 
            from breezy import _readdir_pyx
 
52
            from .. import _readdir_pyx
51
53
            self.reader = _readdir_pyx.UTF8DirReader
52
54
            return True
53
55
        except ImportError:
82
84
    # Some DirReaders are platform specific and even there they may not be
83
85
    # available.
84
86
    if UTF8DirReaderFeature.available():
85
 
        from breezy import _readdir_pyx
 
87
        from .. import _readdir_pyx
86
88
        scenarios.append(('utf8',
87
89
                          dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
88
90
                               _native_to_unicode=_utf8_to_unicode)))
89
91
 
90
92
    if test__walkdirs_win32.win32_readdir_feature.available():
91
93
        try:
92
 
            from breezy import _walkdirs_win32
 
94
            from .. import _walkdirs_win32
93
95
            scenarios.append(
94
96
                ('win32',
95
97
                 dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
185
187
    def test_rename_exception(self):
186
188
        try:
187
189
            osutils.rename('nonexistent_path', 'different_nonexistent_path')
188
 
        except OSError, e:
 
190
        except OSError as e:
189
191
            self.assertEqual(e.old_filename, 'nonexistent_path')
190
192
            self.assertEqual(e.new_filename, 'different_nonexistent_path')
191
193
            self.assertTrue('nonexistent_path' in e.strerror)
307
309
        # TODO: jam 20060529 Test a block device
308
310
        try:
309
311
            os.lstat('/dev/null')
310
 
        except OSError, e:
 
312
        except OSError as e:
311
313
            if e.errno not in (errno.ENOENT,):
312
314
                raise
313
315
        else:
351
353
 
352
354
        orig_umask = osutils.get_umask()
353
355
        self.addCleanup(os.umask, orig_umask)
354
 
        os.umask(0222)
355
 
        self.assertEqual(0222, osutils.get_umask())
356
 
        os.umask(0022)
357
 
        self.assertEqual(0022, osutils.get_umask())
358
 
        os.umask(0002)
359
 
        self.assertEqual(0002, osutils.get_umask())
360
 
        os.umask(0027)
361
 
        self.assertEqual(0027, osutils.get_umask())
 
356
        os.umask(0o222)
 
357
        self.assertEqual(0o222, osutils.get_umask())
 
358
        os.umask(0o022)
 
359
        self.assertEqual(0o022, osutils.get_umask())
 
360
        os.umask(0o002)
 
361
        self.assertEqual(0o002, osutils.get_umask())
 
362
        os.umask(0o027)
 
363
        self.assertEqual(0o027, osutils.get_umask())
362
364
 
363
365
 
364
366
class TestDateTime(tests.TestCase):
517
519
        # Make a file readonly
518
520
        osutils.make_readonly('file')
519
521
        mode = os.lstat('file').st_mode
520
 
        self.assertEqual(mode, mode & 0777555)
 
522
        self.assertEqual(mode, mode & 0o777555)
521
523
 
522
524
        # Make a file writable
523
525
        osutils.make_writable('file')
524
526
        mode = os.lstat('file').st_mode
525
 
        self.assertEqual(mode, mode | 0200)
 
527
        self.assertEqual(mode, mode | 0o200)
526
528
 
527
529
        if osutils.has_symlinks():
528
530
            # should not error when handed a symlink
612
614
        self.assertTrue(self.test_data_len > self.block_size)
613
615
 
614
616
        from_file = file_utils.FakeReadFile(self.test_data)
615
 
        to_file = StringIO()
 
617
        to_file = BytesIO()
616
618
 
617
619
        # read (max / 2) bytes and verify read size wasn't affected
618
620
        num_bytes_to_read = self.block_size / 2
653
655
 
654
656
        # retrieve data in blocks
655
657
        from_file = file_utils.FakeReadFile(self.test_data)
656
 
        to_file = StringIO()
 
658
        to_file = BytesIO()
657
659
        osutils.pumpfile(from_file, to_file, self.test_data_len,
658
660
                         self.block_size)
659
661
 
677
679
 
678
680
        # retrieve data to EOF
679
681
        from_file = file_utils.FakeReadFile(self.test_data)
680
 
        to_file = StringIO()
 
682
        to_file = BytesIO()
681
683
        osutils.pumpfile(from_file, to_file, -1, self.block_size)
682
684
 
683
685
        # verify read size was equal to the maximum read size
697
699
        with this new version."""
698
700
        # retrieve data using default (old) pumpfile method
699
701
        from_file = file_utils.FakeReadFile(self.test_data)
700
 
        to_file = StringIO()
 
702
        to_file = BytesIO()
701
703
        osutils.pumpfile(from_file, to_file)
702
704
 
703
705
        # report error if the data wasn't equal (we only report the size due
711
713
        activity = []
712
714
        def log_activity(length, direction):
713
715
            activity.append((length, direction))
714
 
        from_file = StringIO(self.test_data)
715
 
        to_file = StringIO()
 
716
        from_file = BytesIO(self.test_data)
 
717
        to_file = BytesIO()
716
718
        osutils.pumpfile(from_file, to_file, buff_size=500,
717
719
                         report_activity=log_activity, direction='read')
718
720
        self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
719
721
                          (36, 'read')], activity)
720
722
 
721
 
        from_file = StringIO(self.test_data)
722
 
        to_file = StringIO()
 
723
        from_file = BytesIO(self.test_data)
 
724
        to_file = BytesIO()
723
725
        del activity[:]
724
726
        osutils.pumpfile(from_file, to_file, buff_size=500,
725
727
                         report_activity=log_activity, direction='write')
727
729
                          (36, 'write')], activity)
728
730
 
729
731
        # And with a limited amount of data
730
 
        from_file = StringIO(self.test_data)
731
 
        to_file = StringIO()
 
732
        from_file = BytesIO(self.test_data)
 
733
        to_file = BytesIO()
732
734
        del activity[:]
733
735
        osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
734
736
                         report_activity=log_activity, direction='read')
739
741
class TestPumpStringFile(tests.TestCase):
740
742
 
741
743
    def test_empty(self):
742
 
        output = StringIO()
 
744
        output = BytesIO()
743
745
        osutils.pump_string_file("", output)
744
746
        self.assertEqual("", output.getvalue())
745
747
 
746
748
    def test_more_than_segment_size(self):
747
 
        output = StringIO()
 
749
        output = BytesIO()
748
750
        osutils.pump_string_file("123456789", output, 2)
749
751
        self.assertEqual("123456789", output.getvalue())
750
752
 
751
753
    def test_segment_size(self):
752
 
        output = StringIO()
 
754
        output = BytesIO()
753
755
        osutils.pump_string_file("12", output, 2)
754
756
        self.assertEqual("12", output.getvalue())
755
757
 
756
758
    def test_segment_size_multiple(self):
757
 
        output = StringIO()
 
759
        output = BytesIO()
758
760
        osutils.pump_string_file("1234", output, 2)
759
761
        self.assertEqual("1234", output.getvalue())
760
762
 
963
965
 
964
966
    def test_getcwd(self):
965
967
        cwd = osutils._win32_getcwd()
966
 
        os_cwd = os.getcwdu()
 
968
        os_cwd = osutils._getcwd()
967
969
        self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
968
970
        # win32 is inconsistent whether it returns lower or upper case
969
971
        # and even if it was consistent the user might type the other
1011
1013
    def test_minimum_path_selection(self):
1012
1014
        self.assertEqual(set(),
1013
1015
            osutils.minimum_path_selection([]))
1014
 
        self.assertEqual(set(['a']),
 
1016
        self.assertEqual({'a'},
1015
1017
            osutils.minimum_path_selection(['a']))
1016
 
        self.assertEqual(set(['a', 'b']),
 
1018
        self.assertEqual({'a', 'b'},
1017
1019
            osutils.minimum_path_selection(['a', 'b']))
1018
 
        self.assertEqual(set(['a/', 'b']),
 
1020
        self.assertEqual({'a/', 'b'},
1019
1021
            osutils.minimum_path_selection(['a/', 'b']))
1020
 
        self.assertEqual(set(['a/', 'b']),
 
1022
        self.assertEqual({'a/', 'b'},
1021
1023
            osutils.minimum_path_selection(['a/c', 'a/', 'b']))
1022
 
        self.assertEqual(set(['a-b', 'a', 'a0b']),
 
1024
        self.assertEqual({'a-b', 'a', 'a0b'},
1023
1025
            osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
1024
1026
 
1025
1027
    def test_mkdtemp(self):
1046
1048
 
1047
1049
        try:
1048
1050
            osutils._win32_rename('b', 'a')
1049
 
        except (IOError, OSError), e:
 
1051
        except (IOError, OSError) as e:
1050
1052
            self.assertEqual(errno.ENOENT, e.errno)
1051
1053
        self.assertFileEqual('foo\n', 'a')
1052
1054
 
1054
1056
        os.mkdir('a')
1055
1057
        try:
1056
1058
            osutils._win32_rename('b', 'a')
1057
 
        except (IOError, OSError), e:
 
1059
        except (IOError, OSError) as e:
1058
1060
            self.assertEqual(errno.ENOENT, e.errno)
1059
1061
 
1060
1062
    def test_rename_current_dir(self):
1066
1068
        # doesn't exist.
1067
1069
        try:
1068
1070
            osutils._win32_rename('b', '.')
1069
 
        except (IOError, OSError), e:
 
1071
        except (IOError, OSError) as e:
1070
1072
            self.assertEqual(errno.ENOENT, e.errno)
1071
1073
 
1072
1074
    def test_splitpath(self):
1117
1119
                         osutils.chunks_to_lines(['foo\n', 'bar\n', 'baz\n']))
1118
1120
 
1119
1121
    def test_osutils_binding(self):
1120
 
        from breezy.tests import test__chunks_to_lines
 
1122
        from . import test__chunks_to_lines
1121
1123
        if test__chunks_to_lines.compiled_chunkstolines_feature.available():
1122
 
            from breezy._chunks_to_lines_pyx import chunks_to_lines
 
1124
            from .._chunks_to_lines_pyx import chunks_to_lines
1123
1125
        else:
1124
 
            from breezy._chunks_to_lines_py import chunks_to_lines
 
1126
            from .._chunks_to_lines_py import chunks_to_lines
1125
1127
        self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
1126
1128
 
1127
1129
 
1200
1202
        os.mkdir("test-unreadable")
1201
1203
        os.chmod("test-unreadable", 0000)
1202
1204
        # must chmod it back so that it can be removed
1203
 
        self.addCleanup(os.chmod, "test-unreadable", 0700)
 
1205
        self.addCleanup(os.chmod, "test-unreadable", 0o700)
1204
1206
        # The error is not raised until the generator is actually evaluated.
1205
1207
        # (It would be ok if it happened earlier but at the moment it
1206
1208
        # doesn't.)
1233
1235
        # rename the 1file to a latin-1 filename
1234
1236
        os.rename("./1file", "\xe8file")
1235
1237
        if "\xe8file" not in os.listdir("."):
1236
 
            self.skip("Lack filesystem that preserves arbitrary bytes")
 
1238
            self.skipTest("Lack filesystem that preserves arbitrary bytes")
1237
1239
 
1238
1240
        self._save_platform_info()
1239
1241
        win32utils.winver = None # Avoid the win32 detection code
1340
1342
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1341
1343
        self._save_platform_info()
1342
1344
        win32utils.winver = 'Windows NT'
1343
 
        from breezy._walkdirs_win32 import Win32ReadDir
 
1345
        from .._walkdirs_win32 import Win32ReadDir
1344
1346
        self.assertDirReaderIs(Win32ReadDir)
1345
1347
 
1346
1348
    def test_force_walkdirs_utf8_98(self):
1503
1505
    def test__walkdirs_utf8_win32readdir(self):
1504
1506
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1505
1507
        self.requireFeature(features.UnicodeFilenameFeature)
1506
 
        from breezy._walkdirs_win32 import Win32ReadDir
 
1508
        from .._walkdirs_win32 import Win32ReadDir
1507
1509
        self._save_platform_info()
1508
1510
        osutils._selected_dir_reader = Win32ReadDir()
1509
1511
        name0u = u'0file-\xb6'
1560
1562
        """make sure our Stat values are valid"""
1561
1563
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1562
1564
        self.requireFeature(features.UnicodeFilenameFeature)
1563
 
        from breezy._walkdirs_win32 import Win32ReadDir
 
1565
        from .._walkdirs_win32 import Win32ReadDir
1564
1566
        name0u = u'0file-\xb6'
1565
1567
        name0 = name0u.encode('utf8')
1566
1568
        self.build_tree([name0u])
1584
1586
        """make sure our Stat values are valid"""
1585
1587
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1586
1588
        self.requireFeature(features.UnicodeFilenameFeature)
1587
 
        from breezy._walkdirs_win32 import Win32ReadDir
 
1589
        from .._walkdirs_win32 import Win32ReadDir
1588
1590
        name0u = u'0dir-\u062c\u0648'
1589
1591
        name0 = name0u.encode('utf8')
1590
1592
        self.build_tree([name0u + '/'])
2041
2043
    def _try_loading(self):
2042
2044
        try:
2043
2045
            import breezy._fictional_extension_py
2044
 
        except ImportError, e:
 
2046
        except ImportError as e:
2045
2047
            osutils.failed_to_load_extension(e)
2046
2048
            return True
2047
2049
 
2062
2064
        self.assertLength(0, warnings)
2063
2065
 
2064
2066
    def test_report_extension_load_failures_message(self):
2065
 
        log = StringIO()
 
2067
        log = BytesIO()
2066
2068
        trace.push_log_file(log)
2067
2069
        self.assertTrue(self._try_loading())
2068
2070
        osutils.report_extension_load_failures()