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'))
899
905
def test_pathjoin_late_bugfix(self):
906
if sys.version_info < (2, 7, 6):
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(),))
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'))
1700
self.assertEqual(uni_val, os.environ.get('BRZ_TEST_ENV_VAR'))
1702
self.assertEqual(env_val, os.environ.get('BRZ_TEST_ENV_VAR'))
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)
1952
osutils._extension_load_failures[0],
1953
"No module named 'breezy._fictional_extension_py'")
1965
osutils._extension_load_failures[0],
1966
"No module named 'breezy._fictional_extension_py'")
1968
self.assertEqual(osutils._extension_load_failures[0],
1969
"No module named _fictional_extension_py")
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)
2112
class TestPathFromEnviron(tests.TestCase):
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)
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)
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"
2134
# In Python 3, os.environ returns unicode.
2135
self.assertEqual(u'/home/\xa7test',
2136
osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2138
self.assertEqual(u'/home/\u0407test',
2139
osutils._posix_path_from_environ('BRZ_TEST_PATH'))
2140
osutils._fs_enc = "utf-8"
2142
errors.BadFilenameEncoding,
2143
osutils._posix_path_from_environ, 'BRZ_TEST_PATH')
2096
2146
class TestGetHomeDir(tests.TestCase):
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)
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)
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)
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())
2170
# In python 3, os.environ returns unicode
2171
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
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)
2123
2180
class TestGetuserUnicode(tests.TestCase):
2125
2182
def test_is_unicode(self):
2126
2183
user = osutils.getuser_unicode()
2127
self.assertIsInstance(user, str)
2184
self.assertIsInstance(user, text_type)
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)
2207
self.overrideEnv(self.envvar_to_override(), uni_username)
2209
self.overrideEnv(self.envvar_to_override(), encoded_username)
2150
2210
self.assertEqual(uni_username, osutils.getuser_unicode())
2212
2272
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2275
class TestEnvironmentErrors(tests.TestCase):
2276
"""Test handling of environmental errors"""
2278
def test_is_oserror(self):
2279
self.assertTrue(osutils.is_environment_error(
2280
OSError(errno.EINVAL, "Invalid parameter")))
2282
def test_is_ioerror(self):
2283
self.assertTrue(osutils.is_environment_error(
2284
IOError(errno.EINVAL, "Invalid parameter")))
2286
def test_is_socket_error(self):
2287
self.assertTrue(osutils.is_environment_error(
2288
socket.error(errno.EINVAL, "Invalid parameter")))
2290
def test_is_select_error(self):
2291
self.assertTrue(osutils.is_environment_error(
2292
select.error(errno.EINVAL, "Invalid parameter")))
2294
def test_is_pywintypes_error(self):
2295
self.requireFeature(features.pywintypes)
2297
self.assertTrue(osutils.is_environment_error(
2298
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))
2215
2301
class SupportsExecutableTests(tests.TestCaseInTempDir):
2217
2303
def test_returns_bool(self):