~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

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
 
39
38
    file_utils,
40
39
    test__walkdirs_win32,
41
40
    )
 
41
from bzrlib.tests.scenarios import load_tests_apply_scenarios
42
42
 
43
43
 
44
44
class _UTF8DirReaderFeature(tests.Feature):
97
97
    return scenarios
98
98
 
99
99
 
100
 
def load_tests(basic_tests, module, loader):
101
 
    suite = loader.suiteClass()
102
 
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
103
 
        basic_tests, tests.condition_isinstance(TestDirReader))
104
 
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
105
 
    suite.addTest(remaining_tests)
106
 
    return suite
 
100
load_tests = load_tests_apply_scenarios
107
101
 
108
102
 
109
103
class TestContainsWhitespace(tests.TestCase):
110
104
 
111
105
    def test_contains_whitespace(self):
112
 
        self.failUnless(osutils.contains_whitespace(u' '))
113
 
        self.failUnless(osutils.contains_whitespace(u'hello there'))
114
 
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
115
 
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
116
 
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
117
 
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
 
106
        self.assertTrue(osutils.contains_whitespace(u' '))
 
107
        self.assertTrue(osutils.contains_whitespace(u'hello there'))
 
108
        self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
 
109
        self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
 
110
        self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
 
111
        self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
118
112
 
119
113
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
120
114
        # is whitespace, but we do not.
121
 
        self.failIf(osutils.contains_whitespace(u''))
122
 
        self.failIf(osutils.contains_whitespace(u'hellothere'))
123
 
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
 
115
        self.assertFalse(osutils.contains_whitespace(u''))
 
116
        self.assertFalse(osutils.contains_whitespace(u'hellothere'))
 
117
        self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
124
118
 
125
119
 
126
120
class TestRename(tests.TestCaseInTempDir):
140
134
        # This should work everywhere
141
135
        self.create_file('a', 'something in a\n')
142
136
        self._fancy_rename('a', 'b')
143
 
        self.failIfExists('a')
144
 
        self.failUnlessExists('b')
 
137
        self.assertPathDoesNotExist('a')
 
138
        self.assertPathExists('b')
145
139
        self.check_file_contents('b', 'something in a\n')
146
140
 
147
141
        self.create_file('a', 'new something in a\n')
154
148
        self.create_file('target', 'data in target\n')
155
149
        self.assertRaises((IOError, OSError), self._fancy_rename,
156
150
                          'missingsource', 'target')
157
 
        self.failUnlessExists('target')
 
151
        self.assertPathExists('target')
158
152
        self.check_file_contents('target', 'data in target\n')
159
153
 
160
154
    def test_fancy_rename_fails_if_source_and_target_missing(self):
165
159
        # Rename should be semi-atomic on all platforms
166
160
        self.create_file('a', 'something in a\n')
167
161
        osutils.rename('a', 'b')
168
 
        self.failIfExists('a')
169
 
        self.failUnlessExists('b')
 
162
        self.assertPathDoesNotExist('a')
 
163
        self.assertPathExists('b')
170
164
        self.check_file_contents('b', 'something in a\n')
171
165
 
172
166
        self.create_file('a', 'new something in a\n')
237
231
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
238
232
 
239
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
 
240
253
class TestRmTree(tests.TestCaseInTempDir):
241
254
 
242
255
    def test_rmtree(self):
254
267
 
255
268
        osutils.rmtree('dir')
256
269
 
257
 
        self.failIfExists('dir/file')
258
 
        self.failIfExists('dir')
 
270
        self.assertPathDoesNotExist('dir/file')
 
271
        self.assertPathDoesNotExist('dir')
259
272
 
260
273
 
261
274
class TestDeleteAny(tests.TestCaseInTempDir):
475
488
        f = file('MixedCaseName', 'w')
476
489
        f.close()
477
490
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
478
 
        self.failUnlessEqual('work/MixedCaseName', actual)
 
491
        self.assertEqual('work/MixedCaseName', actual)
479
492
 
480
493
    def test_canonical_relpath_missing_tail(self):
481
494
        os.mkdir('MixedCaseParent')
482
495
        actual = osutils.canonical_relpath(self.test_base_dir,
483
496
                                           'mixedcaseparent/nochild')
484
 
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
 
497
        self.assertEqual('work/MixedCaseParent/nochild', actual)
485
498
 
486
499
 
487
500
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
911
924
        b.close()
912
925
 
913
926
        osutils._win32_rename('b', 'a')
914
 
        self.failUnlessExists('a')
915
 
        self.failIfExists('b')
 
927
        self.assertPathExists('a')
 
928
        self.assertPathDoesNotExist('b')
916
929
        self.assertFileEqual('baz\n', 'a')
917
930
 
918
931
    def test_rename_missing_file(self):
1072
1085
        if sys.platform == 'win32':
1073
1086
            raise tests.TestNotApplicable(
1074
1087
                "readdir IOError not tested on win32")
 
1088
        self.requireFeature(features.not_running_as_root)
1075
1089
        os.mkdir("test-unreadable")
1076
1090
        os.chmod("test-unreadable", 0000)
1077
1091
        # must chmod it back so that it can be removed
1107
1121
 
1108
1122
        # rename the 1file to a latin-1 filename
1109
1123
        os.rename("./1file", "\xe8file")
 
1124
        if "\xe8file" not in os.listdir("."):
 
1125
            self.skip("Lack filesystem that preserves arbitrary bytes")
1110
1126
 
1111
1127
        self._save_platform_info()
1112
1128
        win32utils.winver = None # Avoid the win32 detection code
1598
1614
                          ('d', 'source/b', 'target/b'),
1599
1615
                          ('f', 'source/b/c', 'target/b/c'),
1600
1616
                         ], processed_files)
1601
 
        self.failIfExists('target')
 
1617
        self.assertPathDoesNotExist('target')
1602
1618
        if osutils.has_symlinks():
1603
1619
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1604
1620
 
1650
1666
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1651
1667
        self.assertEqual('foo', old)
1652
1668
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1653
 
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
 
1669
        self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
1654
1670
 
1655
1671
 
1656
1672
class TestSizeShaFile(tests.TestCaseInTempDir):
1733
1749
 
1734
1750
class TestDirReader(tests.TestCaseInTempDir):
1735
1751
 
 
1752
    scenarios = dir_reader_scenarios()
 
1753
 
1736
1754
    # Set by load_tests
1737
1755
    _dir_reader_class = None
1738
1756
    _native_to_unicode = None
1879
1897
        os.symlink(self.target, self.link)
1880
1898
 
1881
1899
    def test_os_readlink_link_encoding(self):
1882
 
        if sys.version_info < (2, 6):
1883
 
            self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1884
 
        else:
1885
 
            self.assertEquals(self.target,  os.readlink(self.link))
 
1900
        self.assertEquals(self.target,  os.readlink(self.link))
1886
1901
 
1887
1902
    def test_os_readlink_link_decoding(self):
1888
1903
        self.assertEquals(self.target.encode(osutils._fs_enc),
1900
1915
        self.assertIsInstance(concurrency, int)
1901
1916
 
1902
1917
    def test_local_concurrency_environment_variable(self):
1903
 
        os.environ['BZR_CONCURRENCY'] = '2'
 
1918
        self.overrideEnv('BZR_CONCURRENCY', '2')
1904
1919
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
 
        os.environ['BZR_CONCURRENCY'] = '3'
 
1920
        self.overrideEnv('BZR_CONCURRENCY', '3')
1906
1921
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
 
        os.environ['BZR_CONCURRENCY'] = 'foo'
 
1922
        self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
1923
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1909
1924
 
1910
1925
    def test_option_concurrency(self):
1911
 
        os.environ['BZR_CONCURRENCY'] = '1'
 
1926
        self.overrideEnv('BZR_CONCURRENCY', '1')
1912
1927
        self.run_bzr('rocks --concurrency 42')
1913
 
        # Command line overrides envrionment variable
 
1928
        # Command line overrides environment variable
1914
1929
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
1930
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1916
1931
 
1986
2001
    def test_defaults_to_BZR_COLUMNS(self):
1987
2002
        # BZR_COLUMNS is set by the test framework
1988
2003
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
 
        os.environ['BZR_COLUMNS'] = '12'
 
2004
        self.overrideEnv('BZR_COLUMNS', '12')
1990
2005
        self.assertEqual(12, osutils.terminal_width())
1991
2006
 
 
2007
    def test_BZR_COLUMNS_0_no_limit(self):
 
2008
        self.overrideEnv('BZR_COLUMNS', '0')
 
2009
        self.assertEqual(None, osutils.terminal_width())
 
2010
 
1992
2011
    def test_falls_back_to_COLUMNS(self):
1993
 
        del os.environ['BZR_COLUMNS']
 
2012
        self.overrideEnv('BZR_COLUMNS', None)
1994
2013
        self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2014
        self.set_fake_tty()
1996
 
        os.environ['COLUMNS'] = '42'
 
2015
        self.overrideEnv('COLUMNS', '42')
1997
2016
        self.assertEqual(42, osutils.terminal_width())
1998
2017
 
1999
2018
    def test_tty_default_without_columns(self):
2000
 
        del os.environ['BZR_COLUMNS']
2001
 
        del os.environ['COLUMNS']
 
2019
        self.overrideEnv('BZR_COLUMNS', None)
 
2020
        self.overrideEnv('COLUMNS', None)
2002
2021
 
2003
2022
        def terminal_size(w, h):
2004
2023
            return 42, 42
2011
2030
        self.assertEqual(42, osutils.terminal_width())
2012
2031
 
2013
2032
    def test_non_tty_default_without_columns(self):
2014
 
        del os.environ['BZR_COLUMNS']
2015
 
        del os.environ['COLUMNS']
 
2033
        self.overrideEnv('BZR_COLUMNS', None)
 
2034
        self.overrideEnv('COLUMNS', None)
2016
2035
        self.replace_stdout(None)
2017
2036
        self.assertEqual(None, osutils.terminal_width())
2018
2037
 
2028
2047
        else:
2029
2048
            self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2049
            del termios.TIOCGWINSZ
2031
 
        del os.environ['BZR_COLUMNS']
2032
 
        del os.environ['COLUMNS']
 
2050
        self.overrideEnv('BZR_COLUMNS', None)
 
2051
        self.overrideEnv('COLUMNS', None)
2033
2052
        # Whatever the result is, if we don't raise an exception, it's ok.
2034
2053
        osutils.terminal_width()
2035
2054
 
2071
2090
class TestGetuserUnicode(tests.TestCase):
2072
2091
 
2073
2092
    def test_ascii_user(self):
2074
 
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
 
2093
        self.overrideEnv('LOGNAME', 'jrandom')
2075
2094
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
2076
2095
 
2077
2096
    def test_unicode_user(self):
2078
2097
        ue = osutils.get_user_encoding()
2079
 
        osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2098
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
2099
        if uni_val is None:
 
2100
            raise tests.TestSkipped(
 
2101
                'Cannot find a unicode character that works in encoding %s'
 
2102
                % (osutils.get_user_encoding(),))
 
2103
        uni_username = u'jrandom' + uni_val
 
2104
        encoded_username = uni_username.encode(ue)
 
2105
        self.overrideEnv('LOGNAME', encoded_username)
 
2106
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2107
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2080
2108
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
 
2109
 
 
2110
class TestBackupNames(tests.TestCase):
 
2111
 
 
2112
    def setUp(self):
 
2113
        super(TestBackupNames, self).setUp()
 
2114
        self.backups = []
 
2115
 
 
2116
    def backup_exists(self, name):
 
2117
        return name in self.backups
 
2118
 
 
2119
    def available_backup_name(self, name):
 
2120
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2121
        self.backups.append(backup_name)
 
2122
        return backup_name
 
2123
 
 
2124
    def assertBackupName(self, expected, name):
 
2125
        self.assertEqual(expected, self.available_backup_name(name))
 
2126
 
 
2127
    def test_empty(self):
 
2128
        self.assertBackupName('file.~1~', 'file')
 
2129
 
 
2130
    def test_existing(self):
 
2131
        self.available_backup_name('file')
 
2132
        self.available_backup_name('file')
 
2133
        self.assertBackupName('file.~3~', 'file')
 
2134
        # Empty slots are found, this is not a strict requirement and may be
 
2135
        # revisited if we test against all implementations.
 
2136
        self.backups.remove('file.~2~')
 
2137
        self.assertBackupName('file.~2~', 'file')
 
2138
 
 
2139
 
 
2140
class TestFindExecutableInPath(tests.TestCase):
 
2141
 
 
2142
    def test_windows(self):
 
2143
        if sys.platform != 'win32':
 
2144
            raise tests.TestSkipped('test requires win32')
 
2145
        self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
 
2146
        self.assertTrue(
 
2147
            osutils.find_executable_on_path('explorer.exe') is not None)
 
2148
        self.assertTrue(
 
2149
            osutils.find_executable_on_path('EXPLORER.EXE') is not None)
 
2150
        self.assertTrue(
 
2151
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2152
        self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
 
2153
 
 
2154
    def test_other(self):
 
2155
        if sys.platform == 'win32':
 
2156
            raise tests.TestSkipped('test requires non-win32')
 
2157
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
 
2158
        self.assertTrue(
 
2159
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)