~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: 2011-04-05 14:47:26 UTC
  • mfrom: (5752.2.11 2.4-windows-lfstat)
  • Revision ID: pqm@pqm.ubuntu.com-20110405144726-zi3lj2kwvjml4kx5
(jameinel) Add osutils.lstat/fstat so that even on Windows lstat(fname) ==
 fstat(open(fname).fileno()) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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,
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
 
242
231
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
243
232
 
244
233
 
 
234
class TestLstat(tests.TestCaseInTempDir):
 
235
 
 
236
    def test_lstat_matches_fstat(self):
 
237
        # On Windows, lstat and fstat don't always agree, primarily in the
 
238
        # 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
 
239
        # custom implementation.
 
240
        if sys.platform == 'win32':
 
241
            # We only have special lstat/fstat if we have the extension.
 
242
            # Without it, we may end up re-reading content when we don't have
 
243
            # to, but otherwise it doesn't effect correctness.
 
244
            self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
245
        f = open('test-file.txt', 'wb')
 
246
        self.addCleanup(f.close)
 
247
        f.write('some content\n')
 
248
        f.flush()
 
249
        self.assertEqualStat(osutils.fstat(f.fileno()),
 
250
                             osutils.lstat('test-file.txt'))
 
251
 
 
252
 
245
253
class TestRmTree(tests.TestCaseInTempDir):
246
254
 
247
255
    def test_rmtree(self):
868
876
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
869
877
        # relative path
870
878
        cwd = osutils.getcwd().rstrip('/')
871
 
        drive = osutils._nt_splitdrive(cwd)[0]
 
879
        drive = osutils.ntpath.splitdrive(cwd)[0]
872
880
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
873
881
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
874
882
        # unicode path
1071
1079
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
1072
1080
 
1073
1081
    def test_walkdirs_os_error(self):
1074
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
 
1082
        # <https://bugs.launchpad.net/bzr/+bug/338653>
1075
1083
        # Pyrex readdir didn't raise useful messages if it had an error
1076
1084
        # reading the directory
1077
1085
        if sys.platform == 'win32':
1078
1086
            raise tests.TestNotApplicable(
1079
1087
                "readdir IOError not tested on win32")
 
1088
        self.requireFeature(features.not_running_as_root)
1080
1089
        os.mkdir("test-unreadable")
1081
1090
        os.chmod("test-unreadable", 0000)
1082
1091
        # must chmod it back so that it can be removed
1090
1099
        # Ensure the message contains the file name
1091
1100
        self.assertContainsRe(str(e), "\./test-unreadable")
1092
1101
 
 
1102
 
 
1103
    def test_walkdirs_encoding_error(self):
 
1104
        # <https://bugs.launchpad.net/bzr/+bug/488519>
 
1105
        # walkdirs didn't raise a useful message when the filenames
 
1106
        # are not using the filesystem's encoding
 
1107
 
 
1108
        # require a bytestring based filesystem
 
1109
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1110
 
 
1111
        tree = [
 
1112
            '.bzr',
 
1113
            '0file',
 
1114
            '1dir/',
 
1115
            '1dir/0file',
 
1116
            '1dir/1dir/',
 
1117
            '1file'
 
1118
            ]
 
1119
 
 
1120
        self.build_tree(tree)
 
1121
 
 
1122
        # rename the 1file to a latin-1 filename
 
1123
        os.rename("./1file", "\xe8file")
 
1124
        if "\xe8file" not in os.listdir("."):
 
1125
            self.skip("Lack filesystem that preserves arbitrary bytes")
 
1126
 
 
1127
        self._save_platform_info()
 
1128
        win32utils.winver = None # Avoid the win32 detection code
 
1129
        osutils._fs_enc = 'UTF-8'
 
1130
 
 
1131
        # this should raise on error
 
1132
        def attempt():
 
1133
            for dirdetail, dirblock in osutils.walkdirs('.'):
 
1134
                pass
 
1135
 
 
1136
        self.assertRaises(errors.BadFilenameEncoding, attempt)
 
1137
 
1093
1138
    def test__walkdirs_utf8(self):
1094
1139
        tree = [
1095
1140
            '.bzr',
1678
1723
 
1679
1724
class TestReCompile(tests.TestCase):
1680
1725
 
 
1726
    def _deprecated_re_compile_checked(self, *args, **kwargs):
 
1727
        return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
 
1728
            osutils.re_compile_checked, *args, **kwargs)
 
1729
 
1681
1730
    def test_re_compile_checked(self):
1682
 
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
 
1731
        r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1683
1732
        self.assertTrue(r.match('aaaa'))
1684
1733
        self.assertTrue(r.match('aAaA'))
1685
1734
 
1686
1735
    def test_re_compile_checked_error(self):
1687
1736
        # like https://bugs.launchpad.net/bzr/+bug/251352
 
1737
 
 
1738
        # Due to possible test isolation error, re.compile is not lazy at
 
1739
        # this point. We re-install lazy compile.
 
1740
        lazy_regex.install_lazy_compile()
1688
1741
        err = self.assertRaises(
1689
1742
            errors.BzrCommandError,
1690
 
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
 
1743
            self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1691
1744
        self.assertEqual(
1692
 
            "Invalid regular expression in test case: '*': "
1693
 
            "nothing to repeat",
 
1745
            'Invalid regular expression in test case: '
 
1746
            '"*" nothing to repeat',
1694
1747
            str(err))
1695
1748
 
1696
1749
 
1697
1750
class TestDirReader(tests.TestCaseInTempDir):
1698
1751
 
 
1752
    scenarios = dir_reader_scenarios()
 
1753
 
1699
1754
    # Set by load_tests
1700
1755
    _dir_reader_class = None
1701
1756
    _native_to_unicode = None
1863
1918
        self.assertIsInstance(concurrency, int)
1864
1919
 
1865
1920
    def test_local_concurrency_environment_variable(self):
1866
 
        os.environ['BZR_CONCURRENCY'] = '2'
 
1921
        self.overrideEnv('BZR_CONCURRENCY', '2')
1867
1922
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1868
 
        os.environ['BZR_CONCURRENCY'] = '3'
 
1923
        self.overrideEnv('BZR_CONCURRENCY', '3')
1869
1924
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1870
 
        os.environ['BZR_CONCURRENCY'] = 'foo'
 
1925
        self.overrideEnv('BZR_CONCURRENCY', 'foo')
1871
1926
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1872
1927
 
1873
1928
    def test_option_concurrency(self):
1874
 
        os.environ['BZR_CONCURRENCY'] = '1'
 
1929
        self.overrideEnv('BZR_CONCURRENCY', '1')
1875
1930
        self.run_bzr('rocks --concurrency 42')
1876
 
        # Command line overrides envrionment variable
 
1931
        # Command line overrides environment variable
1877
1932
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1878
1933
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1879
1934
 
1917
1972
 
1918
1973
class TestTerminalWidth(tests.TestCase):
1919
1974
 
 
1975
    def setUp(self):
 
1976
        tests.TestCase.setUp(self)
 
1977
        self._orig_terminal_size_state = osutils._terminal_size_state
 
1978
        self._orig_first_terminal_size = osutils._first_terminal_size
 
1979
        self.addCleanup(self.restore_osutils_globals)
 
1980
        osutils._terminal_size_state = 'no_data'
 
1981
        osutils._first_terminal_size = None
 
1982
 
 
1983
    def restore_osutils_globals(self):
 
1984
        osutils._terminal_size_state = self._orig_terminal_size_state
 
1985
        osutils._first_terminal_size = self._orig_first_terminal_size
 
1986
 
1920
1987
    def replace_stdout(self, new):
1921
1988
        self.overrideAttr(sys, 'stdout', new)
1922
1989
 
1937
2004
    def test_defaults_to_BZR_COLUMNS(self):
1938
2005
        # BZR_COLUMNS is set by the test framework
1939
2006
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1940
 
        os.environ['BZR_COLUMNS'] = '12'
 
2007
        self.overrideEnv('BZR_COLUMNS', '12')
1941
2008
        self.assertEqual(12, osutils.terminal_width())
1942
2009
 
 
2010
    def test_BZR_COLUMNS_0_no_limit(self):
 
2011
        self.overrideEnv('BZR_COLUMNS', '0')
 
2012
        self.assertEqual(None, osutils.terminal_width())
 
2013
 
1943
2014
    def test_falls_back_to_COLUMNS(self):
1944
 
        del os.environ['BZR_COLUMNS']
 
2015
        self.overrideEnv('BZR_COLUMNS', None)
1945
2016
        self.assertNotEqual('42', os.environ['COLUMNS'])
1946
2017
        self.set_fake_tty()
1947
 
        os.environ['COLUMNS'] = '42'
 
2018
        self.overrideEnv('COLUMNS', '42')
1948
2019
        self.assertEqual(42, osutils.terminal_width())
1949
2020
 
1950
2021
    def test_tty_default_without_columns(self):
1951
 
        del os.environ['BZR_COLUMNS']
1952
 
        del os.environ['COLUMNS']
 
2022
        self.overrideEnv('BZR_COLUMNS', None)
 
2023
        self.overrideEnv('COLUMNS', None)
1953
2024
 
1954
2025
        def terminal_size(w, h):
1955
2026
            return 42, 42
1962
2033
        self.assertEqual(42, osutils.terminal_width())
1963
2034
 
1964
2035
    def test_non_tty_default_without_columns(self):
1965
 
        del os.environ['BZR_COLUMNS']
1966
 
        del os.environ['COLUMNS']
 
2036
        self.overrideEnv('BZR_COLUMNS', None)
 
2037
        self.overrideEnv('COLUMNS', None)
1967
2038
        self.replace_stdout(None)
1968
2039
        self.assertEqual(None, osutils.terminal_width())
1969
2040
 
1979
2050
        else:
1980
2051
            self.overrideAttr(termios, 'TIOCGWINSZ')
1981
2052
            del termios.TIOCGWINSZ
1982
 
        del os.environ['BZR_COLUMNS']
1983
 
        del os.environ['COLUMNS']
 
2053
        self.overrideEnv('BZR_COLUMNS', None)
 
2054
        self.overrideEnv('COLUMNS', None)
1984
2055
        # Whatever the result is, if we don't raise an exception, it's ok.
1985
2056
        osutils.terminal_width()
1986
2057
 
2018
2089
        self.assertEquals(self.path, 'test_file')
2019
2090
        self.assertEquals(self.uid, s.st_uid)
2020
2091
        self.assertEquals(self.gid, s.st_gid)
 
2092
 
 
2093
class TestGetuserUnicode(tests.TestCase):
 
2094
 
 
2095
    def test_ascii_user(self):
 
2096
        self.overrideEnv('LOGNAME', 'jrandom')
 
2097
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
 
2098
 
 
2099
    def test_unicode_user(self):
 
2100
        ue = osutils.get_user_encoding()
 
2101
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
2102
        if uni_val is None:
 
2103
            raise tests.TestSkipped(
 
2104
                'Cannot find a unicode character that works in encoding %s'
 
2105
                % (osutils.get_user_encoding(),))
 
2106
        uni_username = u'jrandom' + uni_val
 
2107
        encoded_username = uni_username.encode(ue)
 
2108
        self.overrideEnv('LOGNAME', encoded_username)
 
2109
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2110
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2111
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
 
2112
 
 
2113
class TestBackupNames(tests.TestCase):
 
2114
 
 
2115
    def setUp(self):
 
2116
        super(TestBackupNames, self).setUp()
 
2117
        self.backups = []
 
2118
 
 
2119
    def backup_exists(self, name):
 
2120
        return name in self.backups
 
2121
 
 
2122
    def available_backup_name(self, name):
 
2123
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2124
        self.backups.append(backup_name)
 
2125
        return backup_name
 
2126
 
 
2127
    def assertBackupName(self, expected, name):
 
2128
        self.assertEqual(expected, self.available_backup_name(name))
 
2129
 
 
2130
    def test_empty(self):
 
2131
        self.assertBackupName('file.~1~', 'file')
 
2132
 
 
2133
    def test_existing(self):
 
2134
        self.available_backup_name('file')
 
2135
        self.available_backup_name('file')
 
2136
        self.assertBackupName('file.~3~', 'file')
 
2137
        # Empty slots are found, this is not a strict requirement and may be
 
2138
        # revisited if we test against all implementations.
 
2139
        self.backups.remove('file.~2~')
 
2140
        self.assertBackupName('file.~2~', 'file')
 
2141
 
 
2142
 
 
2143
class TestFindExecutableInPath(tests.TestCase):
 
2144
 
 
2145
    def test_windows(self):
 
2146
        if sys.platform != 'win32':
 
2147
            raise tests.TestSkipped('test requires win32')
 
2148
        self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
 
2149
        self.assertTrue(
 
2150
            osutils.find_executable_on_path('explorer.exe') is not None)
 
2151
        self.assertTrue(
 
2152
            osutils.find_executable_on_path('EXPLORER.EXE') is not None)
 
2153
        self.assertTrue(
 
2154
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2155
        self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
 
2156
 
 
2157
    def test_other(self):
 
2158
        if sys.platform == 'win32':
 
2159
            raise tests.TestSkipped('test requires non-win32')
 
2160
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
 
2161
        self.assertTrue(
 
2162
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)