~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Martin
  • Date: 2011-04-15 21:22:57 UTC
  • mto: This revision was merged to the branch mainline in revision 5797.
  • Revision ID: gzlist@googlemail.com-20110415212257-jgtovwwp4be7egd9
Add release notes

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
38
38
    file_utils,
39
39
    test__walkdirs_win32,
40
40
    )
 
41
from bzrlib.tests.scenarios import load_tests_apply_scenarios
41
42
 
42
43
 
43
44
class _UTF8DirReaderFeature(tests.Feature):
96
97
    return scenarios
97
98
 
98
99
 
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
 
100
load_tests = load_tests_apply_scenarios
106
101
 
107
102
 
108
103
class TestContainsWhitespace(tests.TestCase):
236
231
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
237
232
 
238
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
 
239
253
class TestRmTree(tests.TestCaseInTempDir):
240
254
 
241
255
    def test_rmtree(self):
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
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
1900
1918
        self.assertIsInstance(concurrency, int)
1901
1919
 
1902
1920
    def test_local_concurrency_environment_variable(self):
1903
 
        os.environ['BZR_CONCURRENCY'] = '2'
 
1921
        self.overrideEnv('BZR_CONCURRENCY', '2')
1904
1922
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
 
        os.environ['BZR_CONCURRENCY'] = '3'
 
1923
        self.overrideEnv('BZR_CONCURRENCY', '3')
1906
1924
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
 
        os.environ['BZR_CONCURRENCY'] = 'foo'
 
1925
        self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
1926
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1909
1927
 
1910
1928
    def test_option_concurrency(self):
1911
 
        os.environ['BZR_CONCURRENCY'] = '1'
 
1929
        self.overrideEnv('BZR_CONCURRENCY', '1')
1912
1930
        self.run_bzr('rocks --concurrency 42')
1913
 
        # Command line overrides envrionment variable
 
1931
        # Command line overrides environment variable
1914
1932
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
1933
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1916
1934
 
1986
2004
    def test_defaults_to_BZR_COLUMNS(self):
1987
2005
        # BZR_COLUMNS is set by the test framework
1988
2006
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
 
        os.environ['BZR_COLUMNS'] = '12'
 
2007
        self.overrideEnv('BZR_COLUMNS', '12')
1990
2008
        self.assertEqual(12, osutils.terminal_width())
1991
2009
 
 
2010
    def test_BZR_COLUMNS_0_no_limit(self):
 
2011
        self.overrideEnv('BZR_COLUMNS', '0')
 
2012
        self.assertEqual(None, osutils.terminal_width())
 
2013
 
1992
2014
    def test_falls_back_to_COLUMNS(self):
1993
 
        del os.environ['BZR_COLUMNS']
 
2015
        self.overrideEnv('BZR_COLUMNS', None)
1994
2016
        self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2017
        self.set_fake_tty()
1996
 
        os.environ['COLUMNS'] = '42'
 
2018
        self.overrideEnv('COLUMNS', '42')
1997
2019
        self.assertEqual(42, osutils.terminal_width())
1998
2020
 
1999
2021
    def test_tty_default_without_columns(self):
2000
 
        del os.environ['BZR_COLUMNS']
2001
 
        del os.environ['COLUMNS']
 
2022
        self.overrideEnv('BZR_COLUMNS', None)
 
2023
        self.overrideEnv('COLUMNS', None)
2002
2024
 
2003
2025
        def terminal_size(w, h):
2004
2026
            return 42, 42
2011
2033
        self.assertEqual(42, osutils.terminal_width())
2012
2034
 
2013
2035
    def test_non_tty_default_without_columns(self):
2014
 
        del os.environ['BZR_COLUMNS']
2015
 
        del os.environ['COLUMNS']
 
2036
        self.overrideEnv('BZR_COLUMNS', None)
 
2037
        self.overrideEnv('COLUMNS', None)
2016
2038
        self.replace_stdout(None)
2017
2039
        self.assertEqual(None, osutils.terminal_width())
2018
2040
 
2028
2050
        else:
2029
2051
            self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2052
            del termios.TIOCGWINSZ
2031
 
        del os.environ['BZR_COLUMNS']
2032
 
        del os.environ['COLUMNS']
 
2053
        self.overrideEnv('BZR_COLUMNS', None)
 
2054
        self.overrideEnv('COLUMNS', None)
2033
2055
        # Whatever the result is, if we don't raise an exception, it's ok.
2034
2056
        osutils.terminal_width()
2035
2057
 
2071
2093
class TestGetuserUnicode(tests.TestCase):
2072
2094
 
2073
2095
    def test_ascii_user(self):
2074
 
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
 
2096
        self.overrideEnv('LOGNAME', 'jrandom')
2075
2097
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
2076
2098
 
2077
2099
    def test_unicode_user(self):
2083
2105
                % (osutils.get_user_encoding(),))
2084
2106
        uni_username = u'jrandom' + uni_val
2085
2107
        encoded_username = uni_username.encode(ue)
2086
 
        osutils.set_or_unset_env('LOGNAME', encoded_username)
 
2108
        self.overrideEnv('LOGNAME', encoded_username)
2087
2109
        self.assertEqual(uni_username, osutils.getuser_unicode())
2088
 
        osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2110
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2089
2111
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2090
2112
 
2091
2113
class TestBackupNames(tests.TestCase):
2116
2138
        # revisited if we test against all implementations.
2117
2139
        self.backups.remove('file.~2~')
2118
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)