/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: Martin Pool
  • Date: 2010-06-24 06:53:06 UTC
  • mfrom: (5317 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5343.
  • Revision ID: mbp@sourcefrog.net-20100624065306-qcx1wg84ufuckime
merge trunk

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
33
33
    win32utils,
34
34
    )
35
35
from bzrlib.tests import (
 
36
    features,
36
37
    file_utils,
37
38
    test__walkdirs_win32,
38
39
    )
309
310
        self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
310
311
        self.assertEqual("@", osutils.kind_marker("symlink"))
311
312
        self.assertEqual("+", osutils.kind_marker("tree-reference"))
312
 
        self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
 
313
        self.assertEqual("", osutils.kind_marker("fifo"))
 
314
        self.assertEqual("", osutils.kind_marker("socket"))
 
315
        self.assertEqual("", osutils.kind_marker("unknown"))
313
316
 
314
317
 
315
318
class TestUmask(tests.TestCaseInTempDir):
1061
1064
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
1062
1065
 
1063
1066
    def test_walkdirs_os_error(self):
1064
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
 
1067
        # <https://bugs.launchpad.net/bzr/+bug/338653>
1065
1068
        # Pyrex readdir didn't raise useful messages if it had an error
1066
1069
        # reading the directory
1067
1070
        if sys.platform == 'win32':
1080
1083
        # Ensure the message contains the file name
1081
1084
        self.assertContainsRe(str(e), "\./test-unreadable")
1082
1085
 
 
1086
 
 
1087
    def test_walkdirs_encoding_error(self):
 
1088
        # <https://bugs.launchpad.net/bzr/+bug/488519>
 
1089
        # walkdirs didn't raise a useful message when the filenames
 
1090
        # are not using the filesystem's encoding
 
1091
 
 
1092
        # require a bytestring based filesystem
 
1093
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1094
 
 
1095
        tree = [
 
1096
            '.bzr',
 
1097
            '0file',
 
1098
            '1dir/',
 
1099
            '1dir/0file',
 
1100
            '1dir/1dir/',
 
1101
            '1file'
 
1102
            ]
 
1103
 
 
1104
        self.build_tree(tree)
 
1105
 
 
1106
        # rename the 1file to a latin-1 filename
 
1107
        os.rename("./1file", "\xe8file")
 
1108
 
 
1109
        self._save_platform_info()
 
1110
        win32utils.winver = None # Avoid the win32 detection code
 
1111
        osutils._fs_enc = 'UTF-8'
 
1112
 
 
1113
        # this should raise on error
 
1114
        def attempt():
 
1115
            for dirdetail, dirblock in osutils.walkdirs('.'):
 
1116
                pass
 
1117
 
 
1118
        self.assertRaises(errors.BadFilenameEncoding, attempt)
 
1119
 
1083
1120
    def test__walkdirs_utf8(self):
1084
1121
        tree = [
1085
1122
            '.bzr',
1135
1172
            dirblock[:] = new_dirblock
1136
1173
 
1137
1174
    def _save_platform_info(self):
1138
 
        cur_winver = win32utils.winver
1139
 
        cur_fs_enc = osutils._fs_enc
1140
 
        cur_dir_reader = osutils._selected_dir_reader
1141
 
        def restore():
1142
 
            win32utils.winver = cur_winver
1143
 
            osutils._fs_enc = cur_fs_enc
1144
 
            osutils._selected_dir_reader = cur_dir_reader
1145
 
        self.addCleanup(restore)
 
1175
        self.overrideAttr(win32utils, 'winver')
 
1176
        self.overrideAttr(osutils, '_fs_enc')
 
1177
        self.overrideAttr(osutils, '_selected_dir_reader')
1146
1178
 
1147
1179
    def assertDirReaderIs(self, expected):
1148
1180
        """Assert the right implementation for _walkdirs_utf8 is chosen."""
1581
1613
        def cleanup():
1582
1614
            if 'BZR_TEST_ENV_VAR' in os.environ:
1583
1615
                del os.environ['BZR_TEST_ENV_VAR']
1584
 
 
1585
1616
        self.addCleanup(cleanup)
1586
1617
 
1587
1618
    def test_set(self):
1698
1729
 
1699
1730
    def setUp(self):
1700
1731
        tests.TestCaseInTempDir.setUp(self)
1701
 
 
1702
 
        # Save platform specific info and reset it
1703
 
        cur_dir_reader = osutils._selected_dir_reader
1704
 
 
1705
 
        def restore():
1706
 
            osutils._selected_dir_reader = cur_dir_reader
1707
 
        self.addCleanup(restore)
1708
 
 
1709
 
        osutils._selected_dir_reader = self._dir_reader_class()
 
1732
        self.overrideAttr(osutils,
 
1733
                          '_selected_dir_reader', self._dir_reader_class())
1710
1734
 
1711
1735
    def _get_ascii_tree(self):
1712
1736
        tree = [
1859
1883
 
1860
1884
    def setUp(self):
1861
1885
        super(TestConcurrency, self).setUp()
1862
 
        orig = osutils._cached_local_concurrency
1863
 
        def restore():
1864
 
            osutils._cached_local_concurrency = orig
1865
 
        self.addCleanup(restore)
 
1886
        self.overrideAttr(osutils, '_cached_local_concurrency')
1866
1887
 
1867
1888
    def test_local_concurrency(self):
1868
1889
        concurrency = osutils.local_concurrency()
1895
1916
 
1896
1917
    def setUp(self):
1897
1918
        super(TestFailedToLoadExtension, self).setUp()
1898
 
        self.saved_failures = osutils._extension_load_failures[:]
1899
 
        del osutils._extension_load_failures[:]
1900
 
        self.addCleanup(self.restore_failures)
1901
 
 
1902
 
    def restore_failures(self):
1903
 
        osutils._extension_load_failures = self.saved_failures
 
1919
        self.overrideAttr(osutils, '_extension_load_failures', [])
1904
1920
 
1905
1921
    def test_failure_to_load(self):
1906
1922
        self._try_loading()
1928
1944
 
1929
1945
class TestTerminalWidth(tests.TestCase):
1930
1946
 
 
1947
    def setUp(self):
 
1948
        tests.TestCase.setUp(self)
 
1949
        self._orig_terminal_size_state = osutils._terminal_size_state
 
1950
        self._orig_first_terminal_size = osutils._first_terminal_size
 
1951
        self.addCleanup(self.restore_osutils_globals)
 
1952
        osutils._terminal_size_state = 'no_data'
 
1953
        osutils._first_terminal_size = None
 
1954
 
 
1955
    def restore_osutils_globals(self):
 
1956
        osutils._terminal_size_state = self._orig_terminal_size_state
 
1957
        osutils._first_terminal_size = self._orig_first_terminal_size
 
1958
 
1931
1959
    def replace_stdout(self, new):
1932
 
        orig_stdout = sys.stdout
1933
 
        def restore():
1934
 
            sys.stdout = orig_stdout
1935
 
        self.addCleanup(restore)
1936
 
        sys.stdout = new
 
1960
        self.overrideAttr(sys, 'stdout', new)
1937
1961
 
1938
1962
    def replace__terminal_size(self, new):
1939
 
        orig__terminal_size = osutils._terminal_size
1940
 
        def restore():
1941
 
            osutils._terminal_size = orig__terminal_size
1942
 
        self.addCleanup(restore)
1943
 
        osutils._terminal_size = new
 
1963
        self.overrideAttr(osutils, '_terminal_size', new)
1944
1964
 
1945
1965
    def set_fake_tty(self):
1946
1966
 
1996
2016
            # We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
1997
2017
            pass
1998
2018
        else:
1999
 
            def restore():
2000
 
                termios.TIOCGWINSZ = orig
2001
 
            self.addCleanup(restore)
 
2019
            self.overrideAttr(termios, 'TIOCGWINSZ')
2002
2020
            del termios.TIOCGWINSZ
2003
2021
        del os.environ['BZR_COLUMNS']
2004
2022
        del os.environ['COLUMNS']
2005
2023
        # Whatever the result is, if we don't raise an exception, it's ok.
2006
2024
        osutils.terminal_width()
 
2025
 
 
2026
class TestCreationOps(tests.TestCaseInTempDir):
 
2027
    _test_needs_features = [features.chown_feature]
 
2028
 
 
2029
    def setUp(self):
 
2030
        tests.TestCaseInTempDir.setUp(self)
 
2031
        self.overrideAttr(os, 'chown', self._dummy_chown)
 
2032
 
 
2033
        # params set by call to _dummy_chown
 
2034
        self.path = self.uid = self.gid = None
 
2035
 
 
2036
    def _dummy_chown(self, path, uid, gid):
 
2037
        self.path, self.uid, self.gid = path, uid, gid
 
2038
 
 
2039
    def test_copy_ownership_from_path(self):
 
2040
        """copy_ownership_from_path test with specified src."""
 
2041
        ownsrc = '/'
 
2042
        f = open('test_file', 'wt')
 
2043
        osutils.copy_ownership_from_path('test_file', ownsrc)
 
2044
 
 
2045
        s = os.stat(ownsrc)
 
2046
        self.assertEquals(self.path, 'test_file')
 
2047
        self.assertEquals(self.uid, s.st_uid)
 
2048
        self.assertEquals(self.gid, s.st_gid)
 
2049
 
 
2050
    def test_copy_ownership_nonesrc(self):
 
2051
        """copy_ownership_from_path test with src=None."""
 
2052
        f = open('test_file', 'wt')
 
2053
        # should use parent dir for permissions
 
2054
        osutils.copy_ownership_from_path('test_file')
 
2055
 
 
2056
        s = os.stat('..')
 
2057
        self.assertEquals(self.path, 'test_file')
 
2058
        self.assertEquals(self.uid, s.st_uid)
 
2059
        self.assertEquals(self.gid, s.st_gid)
 
2060
 
 
2061
class TestGetuserUnicode(tests.TestCase):
 
2062
 
 
2063
    def test_ascii_user(self):
 
2064
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
 
2065
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
 
2066
 
 
2067
    def test_unicode_user(self):
 
2068
        ue = osutils.get_user_encoding()
 
2069
        osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2070
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())