/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 bzrlib/tests/test_osutils.py

  • Committer: Andrew Bennetts
  • Date: 2011-01-06 06:26:23 UTC
  • mto: This revision was merged to the branch mainline in revision 5612.
  • Revision ID: andrew.bennetts@canonical.com-20110106062623-43tda58mqroybjui
Start of a developer doc describing how fetch works.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import os
22
22
import re
23
23
import socket
24
 
import stat
25
24
import sys
26
25
import time
27
26
 
28
27
from bzrlib import (
29
28
    errors,
 
29
    lazy_regex,
30
30
    osutils,
 
31
    symbol_versioning,
31
32
    tests,
32
33
    trace,
33
34
    win32utils,
37
38
    file_utils,
38
39
    test__walkdirs_win32,
39
40
    )
 
41
from bzrlib.tests.scenarios import load_tests_apply_scenarios
40
42
 
41
43
 
42
44
class _UTF8DirReaderFeature(tests.Feature):
95
97
    return scenarios
96
98
 
97
99
 
98
 
def load_tests(basic_tests, module, loader):
99
 
    suite = loader.suiteClass()
100
 
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
101
 
        basic_tests, tests.condition_isinstance(TestDirReader))
102
 
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
103
 
    suite.addTest(remaining_tests)
104
 
    return suite
 
100
load_tests = load_tests_apply_scenarios
105
101
 
106
102
 
107
103
class TestContainsWhitespace(tests.TestCase):
184
180
        shape = sorted(os.listdir('.'))
185
181
        self.assertEquals(['A', 'B'], shape)
186
182
 
187
 
    def test_rename_error(self):
188
 
        # We wrap os.rename to make it give an error including the filenames
189
 
        # https://bugs.launchpad.net/bzr/+bug/491763
190
 
        err = self.assertRaises(OSError, osutils.rename,
191
 
            'nonexistent', 'target')
192
 
        self.assertContainsString(str(err), 'nonexistent')
193
 
 
194
183
 
195
184
class TestRandChars(tests.TestCase):
196
185
 
868
857
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
869
858
        # relative path
870
859
        cwd = osutils.getcwd().rstrip('/')
871
 
        drive = osutils._nt_splitdrive(cwd)[0]
 
860
        drive = osutils.ntpath.splitdrive(cwd)[0]
872
861
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
873
862
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
874
863
        # unicode path
1071
1060
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
1072
1061
 
1073
1062
    def test_walkdirs_os_error(self):
1074
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
 
1063
        # <https://bugs.launchpad.net/bzr/+bug/338653>
1075
1064
        # Pyrex readdir didn't raise useful messages if it had an error
1076
1065
        # reading the directory
1077
1066
        if sys.platform == 'win32':
1078
1067
            raise tests.TestNotApplicable(
1079
1068
                "readdir IOError not tested on win32")
 
1069
        self.requireFeature(features.not_running_as_root)
1080
1070
        os.mkdir("test-unreadable")
1081
1071
        os.chmod("test-unreadable", 0000)
1082
1072
        # must chmod it back so that it can be removed
1090
1080
        # Ensure the message contains the file name
1091
1081
        self.assertContainsRe(str(e), "\./test-unreadable")
1092
1082
 
 
1083
 
 
1084
    def test_walkdirs_encoding_error(self):
 
1085
        # <https://bugs.launchpad.net/bzr/+bug/488519>
 
1086
        # walkdirs didn't raise a useful message when the filenames
 
1087
        # are not using the filesystem's encoding
 
1088
 
 
1089
        # require a bytestring based filesystem
 
1090
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1091
 
 
1092
        tree = [
 
1093
            '.bzr',
 
1094
            '0file',
 
1095
            '1dir/',
 
1096
            '1dir/0file',
 
1097
            '1dir/1dir/',
 
1098
            '1file'
 
1099
            ]
 
1100
 
 
1101
        self.build_tree(tree)
 
1102
 
 
1103
        # rename the 1file to a latin-1 filename
 
1104
        os.rename("./1file", "\xe8file")
 
1105
        if "\xe8file" not in os.listdir("."):
 
1106
            self.skip("Lack filesystem that preserves arbitrary bytes")
 
1107
 
 
1108
        self._save_platform_info()
 
1109
        win32utils.winver = None # Avoid the win32 detection code
 
1110
        osutils._fs_enc = 'UTF-8'
 
1111
 
 
1112
        # this should raise on error
 
1113
        def attempt():
 
1114
            for dirdetail, dirblock in osutils.walkdirs('.'):
 
1115
                pass
 
1116
 
 
1117
        self.assertRaises(errors.BadFilenameEncoding, attempt)
 
1118
 
1093
1119
    def test__walkdirs_utf8(self):
1094
1120
        tree = [
1095
1121
            '.bzr',
1678
1704
 
1679
1705
class TestReCompile(tests.TestCase):
1680
1706
 
 
1707
    def _deprecated_re_compile_checked(self, *args, **kwargs):
 
1708
        return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
 
1709
            osutils.re_compile_checked, *args, **kwargs)
 
1710
 
1681
1711
    def test_re_compile_checked(self):
1682
 
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
 
1712
        r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1683
1713
        self.assertTrue(r.match('aaaa'))
1684
1714
        self.assertTrue(r.match('aAaA'))
1685
1715
 
1686
1716
    def test_re_compile_checked_error(self):
1687
1717
        # like https://bugs.launchpad.net/bzr/+bug/251352
 
1718
 
 
1719
        # Due to possible test isolation error, re.compile is not lazy at
 
1720
        # this point. We re-install lazy compile.
 
1721
        lazy_regex.install_lazy_compile()
1688
1722
        err = self.assertRaises(
1689
1723
            errors.BzrCommandError,
1690
 
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
 
1724
            self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1691
1725
        self.assertEqual(
1692
 
            "Invalid regular expression in test case: '*': "
1693
 
            "nothing to repeat",
 
1726
            'Invalid regular expression in test case: '
 
1727
            '"*" nothing to repeat',
1694
1728
            str(err))
1695
1729
 
1696
1730
 
1697
1731
class TestDirReader(tests.TestCaseInTempDir):
1698
1732
 
 
1733
    scenarios = dir_reader_scenarios()
 
1734
 
1699
1735
    # Set by load_tests
1700
1736
    _dir_reader_class = None
1701
1737
    _native_to_unicode = None
1917
1953
 
1918
1954
class TestTerminalWidth(tests.TestCase):
1919
1955
 
 
1956
    def setUp(self):
 
1957
        tests.TestCase.setUp(self)
 
1958
        self._orig_terminal_size_state = osutils._terminal_size_state
 
1959
        self._orig_first_terminal_size = osutils._first_terminal_size
 
1960
        self.addCleanup(self.restore_osutils_globals)
 
1961
        osutils._terminal_size_state = 'no_data'
 
1962
        osutils._first_terminal_size = None
 
1963
 
 
1964
    def restore_osutils_globals(self):
 
1965
        osutils._terminal_size_state = self._orig_terminal_size_state
 
1966
        osutils._first_terminal_size = self._orig_first_terminal_size
 
1967
 
1920
1968
    def replace_stdout(self, new):
1921
1969
        self.overrideAttr(sys, 'stdout', new)
1922
1970
 
2018
2066
        self.assertEquals(self.path, 'test_file')
2019
2067
        self.assertEquals(self.uid, s.st_uid)
2020
2068
        self.assertEquals(self.gid, s.st_gid)
 
2069
 
 
2070
class TestGetuserUnicode(tests.TestCase):
 
2071
 
 
2072
    def test_ascii_user(self):
 
2073
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
 
2074
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
 
2075
 
 
2076
    def test_unicode_user(self):
 
2077
        ue = osutils.get_user_encoding()
 
2078
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
2079
        if uni_val is None:
 
2080
            raise tests.TestSkipped(
 
2081
                'Cannot find a unicode character that works in encoding %s'
 
2082
                % (osutils.get_user_encoding(),))
 
2083
        uni_username = u'jrandom' + uni_val
 
2084
        encoded_username = uni_username.encode(ue)
 
2085
        osutils.set_or_unset_env('LOGNAME', encoded_username)
 
2086
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2087
        osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2088
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
 
2089
 
 
2090
class TestBackupNames(tests.TestCase):
 
2091
 
 
2092
    def setUp(self):
 
2093
        super(TestBackupNames, self).setUp()
 
2094
        self.backups = []
 
2095
 
 
2096
    def backup_exists(self, name):
 
2097
        return name in self.backups
 
2098
 
 
2099
    def available_backup_name(self, name):
 
2100
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2101
        self.backups.append(backup_name)
 
2102
        return backup_name
 
2103
 
 
2104
    def assertBackupName(self, expected, name):
 
2105
        self.assertEqual(expected, self.available_backup_name(name))
 
2106
 
 
2107
    def test_empty(self):
 
2108
        self.assertBackupName('file.~1~', 'file')
 
2109
 
 
2110
    def test_existing(self):
 
2111
        self.available_backup_name('file')
 
2112
        self.available_backup_name('file')
 
2113
        self.assertBackupName('file.~3~', 'file')
 
2114
        # Empty slots are found, this is not a strict requirement and may be
 
2115
        # revisited if we test against all implementations.
 
2116
        self.backups.remove('file.~2~')
 
2117
        self.assertBackupName('file.~2~', 'file')