~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

(gz) Backslash escape selftest output when printing to non-unicode consoles
 (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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,
34
35
    )
35
36
from bzrlib.tests import (
 
37
    features,
36
38
    file_utils,
37
39
    test__walkdirs_win32,
38
40
    )
309
311
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
310
312
        self.assertEqual("@", osutils.kind_marker("symlink"))
311
313
        self.assertEqual("+", osutils.kind_marker("tree-reference"))
312
 
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
 
314
        self.assertEqual("", osutils.kind_marker("fifo"))
 
315
        self.assertEqual("", osutils.kind_marker("socket"))
 
316
        self.assertEqual("", osutils.kind_marker("unknown"))
313
317
 
314
318
 
315
319
class TestUmask(tests.TestCaseInTempDir):
858
862
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
859
863
        # relative path
860
864
        cwd = osutils.getcwd().rstrip('/')
861
 
        drive = osutils._nt_splitdrive(cwd)[0]
 
865
        drive = osutils.ntpath.splitdrive(cwd)[0]
862
866
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
863
867
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
864
868
        # unicode path
1061
1065
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
1062
1066
 
1063
1067
    def test_walkdirs_os_error(self):
1064
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
 
1068
        # <https://bugs.launchpad.net/bzr/+bug/338653>
1065
1069
        # Pyrex readdir didn't raise useful messages if it had an error
1066
1070
        # reading the directory
1067
1071
        if sys.platform == 'win32':
1068
1072
            raise tests.TestNotApplicable(
1069
1073
                "readdir IOError not tested on win32")
 
1074
        self.requireFeature(features.not_running_as_root)
1070
1075
        os.mkdir("test-unreadable")
1071
1076
        os.chmod("test-unreadable", 0000)
1072
1077
        # must chmod it back so that it can be removed
1080
1085
        # Ensure the message contains the file name
1081
1086
        self.assertContainsRe(str(e), "\./test-unreadable")
1082
1087
 
 
1088
 
 
1089
    def test_walkdirs_encoding_error(self):
 
1090
        # <https://bugs.launchpad.net/bzr/+bug/488519>
 
1091
        # walkdirs didn't raise a useful message when the filenames
 
1092
        # are not using the filesystem's encoding
 
1093
 
 
1094
        # require a bytestring based filesystem
 
1095
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1096
 
 
1097
        tree = [
 
1098
            '.bzr',
 
1099
            '0file',
 
1100
            '1dir/',
 
1101
            '1dir/0file',
 
1102
            '1dir/1dir/',
 
1103
            '1file'
 
1104
            ]
 
1105
 
 
1106
        self.build_tree(tree)
 
1107
 
 
1108
        # rename the 1file to a latin-1 filename
 
1109
        os.rename("./1file", "\xe8file")
 
1110
 
 
1111
        self._save_platform_info()
 
1112
        win32utils.winver = None # Avoid the win32 detection code
 
1113
        osutils._fs_enc = 'UTF-8'
 
1114
 
 
1115
        # this should raise on error
 
1116
        def attempt():
 
1117
            for dirdetail, dirblock in osutils.walkdirs('.'):
 
1118
                pass
 
1119
 
 
1120
        self.assertRaises(errors.BadFilenameEncoding, attempt)
 
1121
 
1083
1122
    def test__walkdirs_utf8(self):
1084
1123
        tree = [
1085
1124
            '.bzr',
1668
1707
 
1669
1708
class TestReCompile(tests.TestCase):
1670
1709
 
 
1710
    def _deprecated_re_compile_checked(self, *args, **kwargs):
 
1711
        return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
 
1712
            osutils.re_compile_checked, *args, **kwargs)
 
1713
 
1671
1714
    def test_re_compile_checked(self):
1672
 
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
 
1715
        r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1673
1716
        self.assertTrue(r.match('aaaa'))
1674
1717
        self.assertTrue(r.match('aAaA'))
1675
1718
 
1676
1719
    def test_re_compile_checked_error(self):
1677
1720
        # like https://bugs.launchpad.net/bzr/+bug/251352
 
1721
 
 
1722
        # Due to possible test isolation error, re.compile is not lazy at
 
1723
        # this point. We re-install lazy compile.
 
1724
        lazy_regex.install_lazy_compile()
1678
1725
        err = self.assertRaises(
1679
1726
            errors.BzrCommandError,
1680
 
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
 
1727
            self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1681
1728
        self.assertEqual(
1682
 
            "Invalid regular expression in test case: '*': "
1683
 
            "nothing to repeat",
 
1729
            'Invalid regular expression in test case: '
 
1730
            '"*" nothing to repeat',
1684
1731
            str(err))
1685
1732
 
1686
1733
 
1907
1954
 
1908
1955
class TestTerminalWidth(tests.TestCase):
1909
1956
 
 
1957
    def setUp(self):
 
1958
        tests.TestCase.setUp(self)
 
1959
        self._orig_terminal_size_state = osutils._terminal_size_state
 
1960
        self._orig_first_terminal_size = osutils._first_terminal_size
 
1961
        self.addCleanup(self.restore_osutils_globals)
 
1962
        osutils._terminal_size_state = 'no_data'
 
1963
        osutils._first_terminal_size = None
 
1964
 
 
1965
    def restore_osutils_globals(self):
 
1966
        osutils._terminal_size_state = self._orig_terminal_size_state
 
1967
        osutils._first_terminal_size = self._orig_first_terminal_size
 
1968
 
1910
1969
    def replace_stdout(self, new):
1911
1970
        self.overrideAttr(sys, 'stdout', new)
1912
1971
 
1973
2032
        del os.environ['COLUMNS']
1974
2033
        # Whatever the result is, if we don't raise an exception, it's ok.
1975
2034
        osutils.terminal_width()
 
2035
 
 
2036
class TestCreationOps(tests.TestCaseInTempDir):
 
2037
    _test_needs_features = [features.chown_feature]
 
2038
 
 
2039
    def setUp(self):
 
2040
        tests.TestCaseInTempDir.setUp(self)
 
2041
        self.overrideAttr(os, 'chown', self._dummy_chown)
 
2042
 
 
2043
        # params set by call to _dummy_chown
 
2044
        self.path = self.uid = self.gid = None
 
2045
 
 
2046
    def _dummy_chown(self, path, uid, gid):
 
2047
        self.path, self.uid, self.gid = path, uid, gid
 
2048
 
 
2049
    def test_copy_ownership_from_path(self):
 
2050
        """copy_ownership_from_path test with specified src."""
 
2051
        ownsrc = '/'
 
2052
        f = open('test_file', 'wt')
 
2053
        osutils.copy_ownership_from_path('test_file', ownsrc)
 
2054
 
 
2055
        s = os.stat(ownsrc)
 
2056
        self.assertEquals(self.path, 'test_file')
 
2057
        self.assertEquals(self.uid, s.st_uid)
 
2058
        self.assertEquals(self.gid, s.st_gid)
 
2059
 
 
2060
    def test_copy_ownership_nonesrc(self):
 
2061
        """copy_ownership_from_path test with src=None."""
 
2062
        f = open('test_file', 'wt')
 
2063
        # should use parent dir for permissions
 
2064
        osutils.copy_ownership_from_path('test_file')
 
2065
 
 
2066
        s = os.stat('..')
 
2067
        self.assertEquals(self.path, 'test_file')
 
2068
        self.assertEquals(self.uid, s.st_uid)
 
2069
        self.assertEquals(self.gid, s.st_gid)
 
2070
 
 
2071
class TestGetuserUnicode(tests.TestCase):
 
2072
 
 
2073
    def test_ascii_user(self):
 
2074
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
 
2075
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
 
2076
 
 
2077
    def test_unicode_user(self):
 
2078
        ue = osutils.get_user_encoding()
 
2079
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
2080
        if uni_val is None:
 
2081
            raise tests.TestSkipped(
 
2082
                'Cannot find a unicode character that works in encoding %s'
 
2083
                % (osutils.get_user_encoding(),))
 
2084
        uni_username = u'jrandom' + uni_val
 
2085
        encoded_username = uni_username.encode(ue)
 
2086
        osutils.set_or_unset_env('LOGNAME', encoded_username)
 
2087
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2088
        osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2089
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
 
2090
 
 
2091
class TestBackupNames(tests.TestCase):
 
2092
 
 
2093
    def setUp(self):
 
2094
        super(TestBackupNames, self).setUp()
 
2095
        self.backups = []
 
2096
 
 
2097
    def backup_exists(self, name):
 
2098
        return name in self.backups
 
2099
 
 
2100
    def available_backup_name(self, name):
 
2101
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2102
        self.backups.append(backup_name)
 
2103
        return backup_name
 
2104
 
 
2105
    def assertBackupName(self, expected, name):
 
2106
        self.assertEqual(expected, self.available_backup_name(name))
 
2107
 
 
2108
    def test_empty(self):
 
2109
        self.assertBackupName('file.~1~', 'file')
 
2110
 
 
2111
    def test_existing(self):
 
2112
        self.available_backup_name('file')
 
2113
        self.available_backup_name('file')
 
2114
        self.assertBackupName('file.~3~', 'file')
 
2115
        # Empty slots are found, this is not a strict requirement and may be
 
2116
        # revisited if we test against all implementations.
 
2117
        self.backups.remove('file.~2~')
 
2118
        self.assertBackupName('file.~2~', 'file')