/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 breezy/tests/test_osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2020-08-10 15:00:17 UTC
  • mfrom: (7490.40.99 work)
  • mto: This revision was merged to the branch mainline in revision 7521.
  • Revision ID: jelmer@jelmer.uk-20200810150017-vs7xnrd1vat4iktg
Merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the osutils wrapper."""
18
18
 
19
 
from __future__ import absolute_import, division
20
 
 
21
19
import errno
 
20
from io import BytesIO
22
21
import os
23
22
import select
24
23
import socket
33
32
    trace,
34
33
    win32utils,
35
34
    )
36
 
from ..sixish import (
37
 
    BytesIO,
38
 
    PY3,
39
 
    text_type,
40
 
    )
41
35
from . import (
42
36
    features,
43
37
    file_utils,
405
399
        self.assertRaises(osutils.UnsupportedTimezoneFormat,
406
400
                          osutils.format_date, 0, timezone='foo')
407
401
        self.assertIsInstance(osutils.format_date(0), str)
408
 
        self.assertIsInstance(osutils.format_local_date(0), text_type)
 
402
        self.assertIsInstance(osutils.format_local_date(0), str)
409
403
        # Testing for the actual value of the local weekday without
410
404
        # duplicating the code from format_date is difficult.
411
405
        # Instead blackbox.test_locale should check for localized
903
897
                         osutils._win32_pathjoin('path/to/', 'foo'))
904
898
 
905
899
    def test_pathjoin_late_bugfix(self):
906
 
        if sys.version_info < (2, 7, 6):
907
 
            expected = '/foo'
908
 
        else:
909
 
            expected = 'C:/foo'
 
900
        expected = 'C:/foo'
910
901
        self.assertEqual(expected,
911
902
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
912
903
        self.assertEqual(expected,
1365
1356
        # all abspaths are Unicode, and encode them back into utf8.
1366
1357
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1367
1358
            self.assertIsInstance(dirdetail[0], bytes)
1368
 
            if isinstance(dirdetail[1], text_type):
 
1359
            if isinstance(dirdetail[1], str):
1369
1360
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
1370
1361
                dirblock = [list(info) for info in dirblock]
1371
1362
                for info in dirblock:
1372
 
                    self.assertIsInstance(info[4], text_type)
 
1363
                    self.assertIsInstance(info[4], str)
1373
1364
                    info[4] = info[4].encode('utf8')
1374
1365
            new_dirblock = []
1375
1366
            for info in dirblock:
1696
1687
                % (osutils.get_user_encoding(),))
1697
1688
 
1698
1689
        osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', uni_val)
1699
 
        if PY3:
1700
 
            self.assertEqual(uni_val, os.environ.get('BRZ_TEST_ENV_VAR'))
1701
 
        else:
1702
 
            self.assertEqual(env_val, os.environ.get('BRZ_TEST_ENV_VAR'))
 
1690
        self.assertEqual(uni_val, os.environ.get('BRZ_TEST_ENV_VAR'))
1703
1691
 
1704
1692
    def test_unset(self):
1705
1693
        """Test that passing None will remove the env var"""
1960
1948
    def test_failure_to_load(self):
1961
1949
        self._try_loading()
1962
1950
        self.assertLength(1, osutils._extension_load_failures)
1963
 
        if PY3:
1964
 
            self.assertEqual(
1965
 
                osutils._extension_load_failures[0],
1966
 
                "No module named 'breezy._fictional_extension_py'")
1967
 
        else:
1968
 
            self.assertEqual(osutils._extension_load_failures[0],
1969
 
                             "No module named _fictional_extension_py")
 
1951
        self.assertEqual(
 
1952
            osutils._extension_load_failures[0],
 
1953
            "No module named 'breezy._fictional_extension_py'")
1970
1954
 
1971
1955
    def test_report_extension_load_failures_no_warning(self):
1972
1956
        self.assertTrue(self._try_loading())
2109
2093
        self.assertEqual(self.gid, s.st_gid)
2110
2094
 
2111
2095
 
2112
 
class TestPathFromEnviron(tests.TestCase):
2113
 
 
2114
 
    def test_is_unicode(self):
2115
 
        self.overrideEnv('BRZ_TEST_PATH', './anywhere at all/')
2116
 
        path = osutils.path_from_environ('BRZ_TEST_PATH')
2117
 
        self.assertIsInstance(path, text_type)
2118
 
        self.assertEqual(u'./anywhere at all/', path)
2119
 
 
2120
 
    def test_posix_path_env_ascii(self):
2121
 
        self.overrideEnv('BRZ_TEST_PATH', '/tmp')
2122
 
        home = osutils._posix_path_from_environ('BRZ_TEST_PATH')
2123
 
        self.assertIsInstance(home, text_type)
2124
 
        self.assertEqual(u'/tmp', home)
2125
 
 
2126
 
    def test_posix_path_env_unicode(self):
2127
 
        self.requireFeature(features.ByteStringNamedFilesystem)
2128
 
        self.overrideEnv('BRZ_TEST_PATH', '/home/\xa7test')
2129
 
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2130
 
        self.assertEqual(u'/home/\xa7test',
2131
 
                         osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2132
 
        osutils._fs_enc = "iso8859-5"
2133
 
        if PY3:
2134
 
            # In Python 3, os.environ returns unicode.
2135
 
            self.assertEqual(u'/home/\xa7test',
2136
 
                             osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2137
 
        else:
2138
 
            self.assertEqual(u'/home/\u0407test',
2139
 
                             osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2140
 
            osutils._fs_enc = "utf-8"
2141
 
            self.assertRaises(
2142
 
                errors.BadFilenameEncoding,
2143
 
                osutils._posix_path_from_environ, 'BRZ_TEST_PATH')
2144
 
 
2145
 
 
2146
2096
class TestGetHomeDir(tests.TestCase):
2147
2097
 
2148
2098
    def test_is_unicode(self):
2149
2099
        home = osutils._get_home_dir()
2150
 
        self.assertIsInstance(home, text_type)
 
2100
        self.assertIsInstance(home, str)
2151
2101
 
2152
2102
    def test_posix_homeless(self):
2153
2103
        self.overrideEnv('HOME', None)
2154
2104
        home = osutils._get_home_dir()
2155
 
        self.assertIsInstance(home, text_type)
 
2105
        self.assertIsInstance(home, str)
2156
2106
 
2157
2107
    def test_posix_home_ascii(self):
2158
2108
        self.overrideEnv('HOME', '/home/test')
2159
2109
        home = osutils._posix_get_home_dir()
2160
 
        self.assertIsInstance(home, text_type)
 
2110
        self.assertIsInstance(home, str)
2161
2111
        self.assertEqual(u'/home/test', home)
2162
2112
 
2163
2113
    def test_posix_home_unicode(self):
2166
2116
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2167
2117
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2168
2118
        osutils._fs_enc = "iso8859-5"
2169
 
        if PY3:
2170
 
            # In python 3, os.environ returns unicode
2171
 
            self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2172
 
        else:
2173
 
            self.assertEqual(u'/home/\u0407test',
2174
 
                             osutils._posix_get_home_dir())
2175
 
            osutils._fs_enc = "utf-8"
2176
 
            self.assertRaises(errors.BadFilenameEncoding,
2177
 
                              osutils._posix_get_home_dir)
 
2119
        # In python 3, os.environ returns unicode
 
2120
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2178
2121
 
2179
2122
 
2180
2123
class TestGetuserUnicode(tests.TestCase):
2181
2124
 
2182
2125
    def test_is_unicode(self):
2183
2126
        user = osutils.getuser_unicode()
2184
 
        self.assertIsInstance(user, text_type)
 
2127
        self.assertIsInstance(user, str)
2185
2128
 
2186
2129
    def envvar_to_override(self):
2187
2130
        if sys.platform == "win32":
2203
2146
                % (osutils.get_user_encoding(),))
2204
2147
        uni_username = u'jrandom' + uni_val
2205
2148
        encoded_username = uni_username.encode(ue)
2206
 
        if PY3:
2207
 
            self.overrideEnv(self.envvar_to_override(), uni_username)
2208
 
        else:
2209
 
            self.overrideEnv(self.envvar_to_override(), encoded_username)
 
2149
        self.overrideEnv(self.envvar_to_override(), uni_username)
2210
2150
        self.assertEqual(uni_username, osutils.getuser_unicode())
2211
2151
 
2212
2152
 
2272
2212
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2273
2213
 
2274
2214
 
2275
 
class TestEnvironmentErrors(tests.TestCase):
2276
 
    """Test handling of environmental errors"""
2277
 
 
2278
 
    def test_is_oserror(self):
2279
 
        self.assertTrue(osutils.is_environment_error(
2280
 
            OSError(errno.EINVAL, "Invalid parameter")))
2281
 
 
2282
 
    def test_is_ioerror(self):
2283
 
        self.assertTrue(osutils.is_environment_error(
2284
 
            IOError(errno.EINVAL, "Invalid parameter")))
2285
 
 
2286
 
    def test_is_socket_error(self):
2287
 
        self.assertTrue(osutils.is_environment_error(
2288
 
            socket.error(errno.EINVAL, "Invalid parameter")))
2289
 
 
2290
 
    def test_is_select_error(self):
2291
 
        self.assertTrue(osutils.is_environment_error(
2292
 
            select.error(errno.EINVAL, "Invalid parameter")))
2293
 
 
2294
 
    def test_is_pywintypes_error(self):
2295
 
        self.requireFeature(features.pywintypes)
2296
 
        import pywintypes
2297
 
        self.assertTrue(osutils.is_environment_error(
2298
 
            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))
2299
 
 
2300
 
 
2301
2215
class SupportsExecutableTests(tests.TestCaseInTempDir):
2302
2216
 
2303
2217
    def test_returns_bool(self):
2330
2244
 
2331
2245
    def test_returns_string_or_none(self):
2332
2246
        ret = osutils.get_fs_type(self.test_dir)
2333
 
        self.assertTrue(isinstance(ret, text_type) or ret is None)
 
2247
        self.assertTrue(isinstance(ret, str) or ret is None)
2334
2248
 
2335
2249
    def test_returns_most_specific(self):
2336
2250
        self.overrideAttr(