~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 04:25:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5472.
  • Revision ID: andrew.bennetts@canonical.com-20101008042510-sg9vdhmnggilzxsk
Fix stray TAB in source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 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
38
38
    file_utils,
39
39
    test__walkdirs_win32,
40
40
    )
41
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
42
41
 
43
42
 
44
43
class _UTF8DirReaderFeature(tests.Feature):
97
96
    return scenarios
98
97
 
99
98
 
100
 
load_tests = load_tests_apply_scenarios
 
99
def load_tests(basic_tests, module, loader):
 
100
    suite = loader.suiteClass()
 
101
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
 
102
        basic_tests, tests.condition_isinstance(TestDirReader))
 
103
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
 
104
    suite.addTest(remaining_tests)
 
105
    return suite
101
106
 
102
107
 
103
108
class TestContainsWhitespace(tests.TestCase):
104
109
 
105
110
    def test_contains_whitespace(self):
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'))
 
111
        self.failUnless(osutils.contains_whitespace(u' '))
 
112
        self.failUnless(osutils.contains_whitespace(u'hello there'))
 
113
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
 
114
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
 
115
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
 
116
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
112
117
 
113
118
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
114
119
        # is whitespace, but we do not.
115
 
        self.assertFalse(osutils.contains_whitespace(u''))
116
 
        self.assertFalse(osutils.contains_whitespace(u'hellothere'))
117
 
        self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
 
120
        self.failIf(osutils.contains_whitespace(u''))
 
121
        self.failIf(osutils.contains_whitespace(u'hellothere'))
 
122
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
118
123
 
119
124
 
120
125
class TestRename(tests.TestCaseInTempDir):
134
139
        # This should work everywhere
135
140
        self.create_file('a', 'something in a\n')
136
141
        self._fancy_rename('a', 'b')
137
 
        self.assertPathDoesNotExist('a')
138
 
        self.assertPathExists('b')
 
142
        self.failIfExists('a')
 
143
        self.failUnlessExists('b')
139
144
        self.check_file_contents('b', 'something in a\n')
140
145
 
141
146
        self.create_file('a', 'new something in a\n')
148
153
        self.create_file('target', 'data in target\n')
149
154
        self.assertRaises((IOError, OSError), self._fancy_rename,
150
155
                          'missingsource', 'target')
151
 
        self.assertPathExists('target')
 
156
        self.failUnlessExists('target')
152
157
        self.check_file_contents('target', 'data in target\n')
153
158
 
154
159
    def test_fancy_rename_fails_if_source_and_target_missing(self):
159
164
        # Rename should be semi-atomic on all platforms
160
165
        self.create_file('a', 'something in a\n')
161
166
        osutils.rename('a', 'b')
162
 
        self.assertPathDoesNotExist('a')
163
 
        self.assertPathExists('b')
 
167
        self.failIfExists('a')
 
168
        self.failUnlessExists('b')
164
169
        self.check_file_contents('b', 'something in a\n')
165
170
 
166
171
        self.create_file('a', 'new something in a\n')
231
236
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
232
237
 
233
238
 
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
 
 
253
239
class TestRmTree(tests.TestCaseInTempDir):
254
240
 
255
241
    def test_rmtree(self):
267
253
 
268
254
        osutils.rmtree('dir')
269
255
 
270
 
        self.assertPathDoesNotExist('dir/file')
271
 
        self.assertPathDoesNotExist('dir')
 
256
        self.failIfExists('dir/file')
 
257
        self.failIfExists('dir')
272
258
 
273
259
 
274
260
class TestDeleteAny(tests.TestCaseInTempDir):
488
474
        f = file('MixedCaseName', 'w')
489
475
        f.close()
490
476
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
491
 
        self.assertEqual('work/MixedCaseName', actual)
 
477
        self.failUnlessEqual('work/MixedCaseName', actual)
492
478
 
493
479
    def test_canonical_relpath_missing_tail(self):
494
480
        os.mkdir('MixedCaseParent')
495
481
        actual = osutils.canonical_relpath(self.test_base_dir,
496
482
                                           'mixedcaseparent/nochild')
497
 
        self.assertEqual('work/MixedCaseParent/nochild', actual)
 
483
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
498
484
 
499
485
 
500
486
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
924
910
        b.close()
925
911
 
926
912
        osutils._win32_rename('b', 'a')
927
 
        self.assertPathExists('a')
928
 
        self.assertPathDoesNotExist('b')
 
913
        self.failUnlessExists('a')
 
914
        self.failIfExists('b')
929
915
        self.assertFileEqual('baz\n', 'a')
930
916
 
931
917
    def test_rename_missing_file(self):
1121
1107
 
1122
1108
        # rename the 1file to a latin-1 filename
1123
1109
        os.rename("./1file", "\xe8file")
1124
 
        if "\xe8file" not in os.listdir("."):
1125
 
            self.skip("Lack filesystem that preserves arbitrary bytes")
1126
1110
 
1127
1111
        self._save_platform_info()
1128
1112
        win32utils.winver = None # Avoid the win32 detection code
1614
1598
                          ('d', 'source/b', 'target/b'),
1615
1599
                          ('f', 'source/b/c', 'target/b/c'),
1616
1600
                         ], processed_files)
1617
 
        self.assertPathDoesNotExist('target')
 
1601
        self.failIfExists('target')
1618
1602
        if osutils.has_symlinks():
1619
1603
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1620
1604
 
1666
1650
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1667
1651
        self.assertEqual('foo', old)
1668
1652
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1669
 
        self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
 
1653
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1670
1654
 
1671
1655
 
1672
1656
class TestSizeShaFile(tests.TestCaseInTempDir):
1749
1733
 
1750
1734
class TestDirReader(tests.TestCaseInTempDir):
1751
1735
 
1752
 
    scenarios = dir_reader_scenarios()
1753
 
 
1754
1736
    # Set by load_tests
1755
1737
    _dir_reader_class = None
1756
1738
    _native_to_unicode = None
1918
1900
        self.assertIsInstance(concurrency, int)
1919
1901
 
1920
1902
    def test_local_concurrency_environment_variable(self):
1921
 
        self.overrideEnv('BZR_CONCURRENCY', '2')
 
1903
        os.environ['BZR_CONCURRENCY'] = '2'
1922
1904
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1923
 
        self.overrideEnv('BZR_CONCURRENCY', '3')
 
1905
        os.environ['BZR_CONCURRENCY'] = '3'
1924
1906
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1925
 
        self.overrideEnv('BZR_CONCURRENCY', 'foo')
 
1907
        os.environ['BZR_CONCURRENCY'] = 'foo'
1926
1908
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1927
1909
 
1928
1910
    def test_option_concurrency(self):
1929
 
        self.overrideEnv('BZR_CONCURRENCY', '1')
 
1911
        os.environ['BZR_CONCURRENCY'] = '1'
1930
1912
        self.run_bzr('rocks --concurrency 42')
1931
 
        # Command line overrides environment variable
 
1913
        # Command line overrides envrionment variable
1932
1914
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1933
1915
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1934
1916
 
2004
1986
    def test_defaults_to_BZR_COLUMNS(self):
2005
1987
        # BZR_COLUMNS is set by the test framework
2006
1988
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2007
 
        self.overrideEnv('BZR_COLUMNS', '12')
 
1989
        os.environ['BZR_COLUMNS'] = '12'
2008
1990
        self.assertEqual(12, osutils.terminal_width())
2009
1991
 
2010
 
    def test_BZR_COLUMNS_0_no_limit(self):
2011
 
        self.overrideEnv('BZR_COLUMNS', '0')
2012
 
        self.assertEqual(None, osutils.terminal_width())
2013
 
 
2014
1992
    def test_falls_back_to_COLUMNS(self):
2015
 
        self.overrideEnv('BZR_COLUMNS', None)
 
1993
        del os.environ['BZR_COLUMNS']
2016
1994
        self.assertNotEqual('42', os.environ['COLUMNS'])
2017
1995
        self.set_fake_tty()
2018
 
        self.overrideEnv('COLUMNS', '42')
 
1996
        os.environ['COLUMNS'] = '42'
2019
1997
        self.assertEqual(42, osutils.terminal_width())
2020
1998
 
2021
1999
    def test_tty_default_without_columns(self):
2022
 
        self.overrideEnv('BZR_COLUMNS', None)
2023
 
        self.overrideEnv('COLUMNS', None)
 
2000
        del os.environ['BZR_COLUMNS']
 
2001
        del os.environ['COLUMNS']
2024
2002
 
2025
2003
        def terminal_size(w, h):
2026
2004
            return 42, 42
2033
2011
        self.assertEqual(42, osutils.terminal_width())
2034
2012
 
2035
2013
    def test_non_tty_default_without_columns(self):
2036
 
        self.overrideEnv('BZR_COLUMNS', None)
2037
 
        self.overrideEnv('COLUMNS', None)
 
2014
        del os.environ['BZR_COLUMNS']
 
2015
        del os.environ['COLUMNS']
2038
2016
        self.replace_stdout(None)
2039
2017
        self.assertEqual(None, osutils.terminal_width())
2040
2018
 
2050
2028
        else:
2051
2029
            self.overrideAttr(termios, 'TIOCGWINSZ')
2052
2030
            del termios.TIOCGWINSZ
2053
 
        self.overrideEnv('BZR_COLUMNS', None)
2054
 
        self.overrideEnv('COLUMNS', None)
 
2031
        del os.environ['BZR_COLUMNS']
 
2032
        del os.environ['COLUMNS']
2055
2033
        # Whatever the result is, if we don't raise an exception, it's ok.
2056
2034
        osutils.terminal_width()
2057
2035
 
2093
2071
class TestGetuserUnicode(tests.TestCase):
2094
2072
 
2095
2073
    def test_ascii_user(self):
2096
 
        self.overrideEnv('LOGNAME', 'jrandom')
 
2074
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
2097
2075
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
2098
2076
 
2099
2077
    def test_unicode_user(self):
2105
2083
                % (osutils.get_user_encoding(),))
2106
2084
        uni_username = u'jrandom' + uni_val
2107
2085
        encoded_username = uni_username.encode(ue)
2108
 
        self.overrideEnv('LOGNAME', encoded_username)
 
2086
        osutils.set_or_unset_env('LOGNAME', encoded_username)
2109
2087
        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)