/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-06-12 01:45:56 UTC
  • mfrom: (7513.1.2 pypy3)
  • Revision ID: breezy.the.bot@gmail.com-20200612014556-tsc8assk3d0luziu
Avoid deprecated behaviour in ElementTree.

Merged from https://code.launchpad.net/~jelmer/brz/pypy3/+merge/385611

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
947
941
                         osutils._win32_pathjoin('path/to/', 'foo'))
948
942
 
949
943
    def test_pathjoin_late_bugfix(self):
950
 
        if sys.version_info < (2, 7, 6):
951
 
            expected = '/foo'
952
 
        else:
953
 
            expected = 'C:/foo'
 
944
        expected = 'C:/foo'
954
945
        self.assertEqual(expected,
955
946
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
956
947
        self.assertEqual(expected,
1409
1400
        # all abspaths are Unicode, and encode them back into utf8.
1410
1401
        for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
1411
1402
            self.assertIsInstance(dirdetail[0], bytes)
1412
 
            if isinstance(dirdetail[1], text_type):
 
1403
            if isinstance(dirdetail[1], str):
1413
1404
                dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
1414
1405
                dirblock = [list(info) for info in dirblock]
1415
1406
                for info in dirblock:
1416
 
                    self.assertIsInstance(info[4], text_type)
 
1407
                    self.assertIsInstance(info[4], str)
1417
1408
                    info[4] = info[4].encode('utf8')
1418
1409
            new_dirblock = []
1419
1410
            for info in dirblock:
1740
1731
                % (osutils.get_user_encoding(),))
1741
1732
 
1742
1733
        osutils.set_or_unset_env('BRZ_TEST_ENV_VAR', uni_val)
1743
 
        if PY3:
1744
 
            self.assertEqual(uni_val, os.environ.get('BRZ_TEST_ENV_VAR'))
1745
 
        else:
1746
 
            self.assertEqual(env_val, os.environ.get('BRZ_TEST_ENV_VAR'))
 
1734
        self.assertEqual(uni_val, os.environ.get('BRZ_TEST_ENV_VAR'))
1747
1735
 
1748
1736
    def test_unset(self):
1749
1737
        """Test that passing None will remove the env var"""
2004
1992
    def test_failure_to_load(self):
2005
1993
        self._try_loading()
2006
1994
        self.assertLength(1, osutils._extension_load_failures)
2007
 
        if PY3:
2008
 
            self.assertEqual(
2009
 
                osutils._extension_load_failures[0],
2010
 
                "No module named 'breezy._fictional_extension_py'")
2011
 
        else:
2012
 
            self.assertEqual(osutils._extension_load_failures[0],
2013
 
                             "No module named _fictional_extension_py")
 
1995
        self.assertEqual(
 
1996
            osutils._extension_load_failures[0],
 
1997
            "No module named 'breezy._fictional_extension_py'")
2014
1998
 
2015
1999
    def test_report_extension_load_failures_no_warning(self):
2016
2000
        self.assertTrue(self._try_loading())
2153
2137
        self.assertEqual(self.gid, s.st_gid)
2154
2138
 
2155
2139
 
2156
 
class TestPathFromEnviron(tests.TestCase):
2157
 
 
2158
 
    def test_is_unicode(self):
2159
 
        self.overrideEnv('BRZ_TEST_PATH', './anywhere at all/')
2160
 
        path = osutils.path_from_environ('BRZ_TEST_PATH')
2161
 
        self.assertIsInstance(path, text_type)
2162
 
        self.assertEqual(u'./anywhere at all/', path)
2163
 
 
2164
 
    def test_posix_path_env_ascii(self):
2165
 
        self.overrideEnv('BRZ_TEST_PATH', '/tmp')
2166
 
        home = osutils._posix_path_from_environ('BRZ_TEST_PATH')
2167
 
        self.assertIsInstance(home, text_type)
2168
 
        self.assertEqual(u'/tmp', home)
2169
 
 
2170
 
    def test_posix_path_env_unicode(self):
2171
 
        self.requireFeature(features.ByteStringNamedFilesystem)
2172
 
        self.overrideEnv('BRZ_TEST_PATH', '/home/\xa7test')
2173
 
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2174
 
        self.assertEqual(u'/home/\xa7test',
2175
 
                         osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2176
 
        osutils._fs_enc = "iso8859-5"
2177
 
        if PY3:
2178
 
            # In Python 3, os.environ returns unicode.
2179
 
            self.assertEqual(u'/home/\xa7test',
2180
 
                             osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2181
 
        else:
2182
 
            self.assertEqual(u'/home/\u0407test',
2183
 
                             osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2184
 
            osutils._fs_enc = "utf-8"
2185
 
            self.assertRaises(
2186
 
                errors.BadFilenameEncoding,
2187
 
                osutils._posix_path_from_environ, 'BRZ_TEST_PATH')
2188
 
 
2189
 
 
2190
2140
class TestGetHomeDir(tests.TestCase):
2191
2141
 
2192
2142
    def test_is_unicode(self):
2193
2143
        home = osutils._get_home_dir()
2194
 
        self.assertIsInstance(home, text_type)
 
2144
        self.assertIsInstance(home, str)
2195
2145
 
2196
2146
    def test_posix_homeless(self):
2197
2147
        self.overrideEnv('HOME', None)
2198
2148
        home = osutils._get_home_dir()
2199
 
        self.assertIsInstance(home, text_type)
 
2149
        self.assertIsInstance(home, str)
2200
2150
 
2201
2151
    def test_posix_home_ascii(self):
2202
2152
        self.overrideEnv('HOME', '/home/test')
2203
2153
        home = osutils._posix_get_home_dir()
2204
 
        self.assertIsInstance(home, text_type)
 
2154
        self.assertIsInstance(home, str)
2205
2155
        self.assertEqual(u'/home/test', home)
2206
2156
 
2207
2157
    def test_posix_home_unicode(self):
2210
2160
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2211
2161
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2212
2162
        osutils._fs_enc = "iso8859-5"
2213
 
        if PY3:
2214
 
            # In python 3, os.environ returns unicode
2215
 
            self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2216
 
        else:
2217
 
            self.assertEqual(u'/home/\u0407test',
2218
 
                             osutils._posix_get_home_dir())
2219
 
            osutils._fs_enc = "utf-8"
2220
 
            self.assertRaises(errors.BadFilenameEncoding,
2221
 
                              osutils._posix_get_home_dir)
 
2163
        # In python 3, os.environ returns unicode
 
2164
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2222
2165
 
2223
2166
 
2224
2167
class TestGetuserUnicode(tests.TestCase):
2225
2168
 
2226
2169
    def test_is_unicode(self):
2227
2170
        user = osutils.getuser_unicode()
2228
 
        self.assertIsInstance(user, text_type)
 
2171
        self.assertIsInstance(user, str)
2229
2172
 
2230
2173
    def envvar_to_override(self):
2231
2174
        if sys.platform == "win32":
2247
2190
                % (osutils.get_user_encoding(),))
2248
2191
        uni_username = u'jrandom' + uni_val
2249
2192
        encoded_username = uni_username.encode(ue)
2250
 
        if PY3:
2251
 
            self.overrideEnv(self.envvar_to_override(), uni_username)
2252
 
        else:
2253
 
            self.overrideEnv(self.envvar_to_override(), encoded_username)
 
2193
        self.overrideEnv(self.envvar_to_override(), uni_username)
2254
2194
        self.assertEqual(uni_username, osutils.getuser_unicode())
2255
2195
 
2256
2196
 
2316
2256
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2317
2257
 
2318
2258
 
2319
 
class TestEnvironmentErrors(tests.TestCase):
2320
 
    """Test handling of environmental errors"""
2321
 
 
2322
 
    def test_is_oserror(self):
2323
 
        self.assertTrue(osutils.is_environment_error(
2324
 
            OSError(errno.EINVAL, "Invalid parameter")))
2325
 
 
2326
 
    def test_is_ioerror(self):
2327
 
        self.assertTrue(osutils.is_environment_error(
2328
 
            IOError(errno.EINVAL, "Invalid parameter")))
2329
 
 
2330
 
    def test_is_socket_error(self):
2331
 
        self.assertTrue(osutils.is_environment_error(
2332
 
            socket.error(errno.EINVAL, "Invalid parameter")))
2333
 
 
2334
 
    def test_is_select_error(self):
2335
 
        self.assertTrue(osutils.is_environment_error(
2336
 
            select.error(errno.EINVAL, "Invalid parameter")))
2337
 
 
2338
 
    def test_is_pywintypes_error(self):
2339
 
        self.requireFeature(features.pywintypes)
2340
 
        import pywintypes
2341
 
        self.assertTrue(osutils.is_environment_error(
2342
 
            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))
2343
 
 
2344
 
 
2345
2259
class SupportsExecutableTests(tests.TestCaseInTempDir):
2346
2260
 
2347
2261
    def test_returns_bool(self):
2374
2288
 
2375
2289
    def test_returns_string_or_none(self):
2376
2290
        ret = osutils.get_fs_type(self.test_dir)
2377
 
        self.assertTrue(isinstance(ret, text_type) or ret is None)
 
2291
        self.assertTrue(isinstance(ret, str) or ret is None)
2378
2292
 
2379
2293
    def test_returns_most_specific(self):
2380
2294
        self.overrideAttr(