/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-07-05 12:50:01 UTC
  • mfrom: (7490.40.46 work)
  • mto: (7490.40.48 work)
  • mto: This revision was merged to the branch mainline in revision 7519.
  • Revision ID: jelmer@jelmer.uk-20200705125001-7s3vo0p55szbbws7
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
 
19
21
import errno
20
 
from io import BytesIO
21
22
import os
22
23
import select
23
24
import socket
32
33
    trace,
33
34
    win32utils,
34
35
    )
 
36
from ..sixish import (
 
37
    BytesIO,
 
38
    PY3,
 
39
    text_type,
 
40
    )
35
41
from . import (
36
42
    features,
37
43
    file_utils,
399
405
        self.assertRaises(osutils.UnsupportedTimezoneFormat,
400
406
                          osutils.format_date, 0, timezone='foo')
401
407
        self.assertIsInstance(osutils.format_date(0), str)
402
 
        self.assertIsInstance(osutils.format_local_date(0), str)
 
408
        self.assertIsInstance(osutils.format_local_date(0), text_type)
403
409
        # Testing for the actual value of the local weekday without
404
410
        # duplicating the code from format_date is difficult.
405
411
        # Instead blackbox.test_locale should check for localized
897
903
                         osutils._win32_pathjoin('path/to/', 'foo'))
898
904
 
899
905
    def test_pathjoin_late_bugfix(self):
900
 
        expected = 'C:/foo'
 
906
        if sys.version_info < (2, 7, 6):
 
907
            expected = '/foo'
 
908
        else:
 
909
            expected = 'C:/foo'
901
910
        self.assertEqual(expected,
902
911
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
903
912
        self.assertEqual(expected,
1356
1365
        # all abspaths are Unicode, and encode them back into utf8.
1357
1366
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1358
1367
            self.assertIsInstance(dirdetail[0], bytes)
1359
 
            if isinstance(dirdetail[1], str):
 
1368
            if isinstance(dirdetail[1], text_type):
1360
1369
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
1361
1370
                dirblock = [list(info) for info in dirblock]
1362
1371
                for info in dirblock:
1363
 
                    self.assertIsInstance(info[4], str)
 
1372
                    self.assertIsInstance(info[4], text_type)
1364
1373
                    info[4] = info[4].encode('utf8')
1365
1374
            new_dirblock = []
1366
1375
            for info in dirblock:
1687
1696
                % (osutils.get_user_encoding(),))
1688
1697
 
1689
1698
        osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', uni_val)
1690
 
        self.assertEqual(uni_val, os.environ.get('BRZ_TEST_ENV_VAR'))
 
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'))
1691
1703
 
1692
1704
    def test_unset(self):
1693
1705
        """Test that passing None will remove the env var"""
1948
1960
    def test_failure_to_load(self):
1949
1961
        self._try_loading()
1950
1962
        self.assertLength(1, osutils._extension_load_failures)
1951
 
        self.assertEqual(
1952
 
            osutils._extension_load_failures[0],
1953
 
            "No module named 'breezy._fictional_extension_py'")
 
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")
1954
1970
 
1955
1971
    def test_report_extension_load_failures_no_warning(self):
1956
1972
        self.assertTrue(self._try_loading())
2093
2109
        self.assertEqual(self.gid, s.st_gid)
2094
2110
 
2095
2111
 
 
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
 
2096
2146
class TestGetHomeDir(tests.TestCase):
2097
2147
 
2098
2148
    def test_is_unicode(self):
2099
2149
        home = osutils._get_home_dir()
2100
 
        self.assertIsInstance(home, str)
 
2150
        self.assertIsInstance(home, text_type)
2101
2151
 
2102
2152
    def test_posix_homeless(self):
2103
2153
        self.overrideEnv('HOME', None)
2104
2154
        home = osutils._get_home_dir()
2105
 
        self.assertIsInstance(home, str)
 
2155
        self.assertIsInstance(home, text_type)
2106
2156
 
2107
2157
    def test_posix_home_ascii(self):
2108
2158
        self.overrideEnv('HOME', '/home/test')
2109
2159
        home = osutils._posix_get_home_dir()
2110
 
        self.assertIsInstance(home, str)
 
2160
        self.assertIsInstance(home, text_type)
2111
2161
        self.assertEqual(u'/home/test', home)
2112
2162
 
2113
2163
    def test_posix_home_unicode(self):
2116
2166
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2117
2167
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2118
2168
        osutils._fs_enc = "iso8859-5"
2119
 
        # In python 3, os.environ returns unicode
2120
 
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
 
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)
2121
2178
 
2122
2179
 
2123
2180
class TestGetuserUnicode(tests.TestCase):
2124
2181
 
2125
2182
    def test_is_unicode(self):
2126
2183
        user = osutils.getuser_unicode()
2127
 
        self.assertIsInstance(user, str)
 
2184
        self.assertIsInstance(user, text_type)
2128
2185
 
2129
2186
    def envvar_to_override(self):
2130
2187
        if sys.platform == "win32":
2146
2203
                % (osutils.get_user_encoding(),))
2147
2204
        uni_username = u'jrandom' + uni_val
2148
2205
        encoded_username = uni_username.encode(ue)
2149
 
        self.overrideEnv(self.envvar_to_override(), uni_username)
 
2206
        if PY3:
 
2207
            self.overrideEnv(self.envvar_to_override(), uni_username)
 
2208
        else:
 
2209
            self.overrideEnv(self.envvar_to_override(), encoded_username)
2150
2210
        self.assertEqual(uni_username, osutils.getuser_unicode())
2151
2211
 
2152
2212
 
2212
2272
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2213
2273
 
2214
2274
 
 
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
 
2215
2301
class SupportsExecutableTests(tests.TestCaseInTempDir):
2216
2302
 
2217
2303
    def test_returns_bool(self):
2244
2330
 
2245
2331
    def test_returns_string_or_none(self):
2246
2332
        ret = osutils.get_fs_type(self.test_dir)
2247
 
        self.assertTrue(isinstance(ret, str) or ret is None)
 
2333
        self.assertTrue(isinstance(ret, text_type) or ret is None)
2248
2334
 
2249
2335
    def test_returns_most_specific(self):
2250
2336
        self.overrideAttr(