~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 19:18:39 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216191839-eg681lxqibi1qxu1
Fix remaining tests.

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
20
20
import errno
21
21
import os
22
22
import re
 
23
import select
23
24
import socket
24
25
import sys
25
26
import time
38
39
    file_utils,
39
40
    test__walkdirs_win32,
40
41
    )
41
 
 
42
 
 
43
 
class _UTF8DirReaderFeature(tests.Feature):
 
42
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
43
 
 
44
 
 
45
class _UTF8DirReaderFeature(features.Feature):
44
46
 
45
47
    def _probe(self):
46
48
        try:
53
55
    def feature_name(self):
54
56
        return 'bzrlib._readdir_pyx'
55
57
 
56
 
UTF8DirReaderFeature = _UTF8DirReaderFeature()
 
58
UTF8DirReaderFeature = features.ModuleAvailableFeature('bzrlib._readdir_pyx')
57
59
 
58
 
term_ios_feature = tests.ModuleAvailableFeature('termios')
 
60
term_ios_feature = features.ModuleAvailableFeature('termios')
59
61
 
60
62
 
61
63
def _already_unicode(s):
96
98
    return scenarios
97
99
 
98
100
 
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
 
101
load_tests = load_tests_apply_scenarios
106
102
 
107
103
 
108
104
class TestContainsWhitespace(tests.TestCase):
109
105
 
110
106
    def test_contains_whitespace(self):
111
 
        self.failUnless(osutils.contains_whitespace(u' '))
112
 
        self.failUnless(osutils.contains_whitespace(u'hello there'))
113
 
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
114
 
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
115
 
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
116
 
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
 
107
        self.assertTrue(osutils.contains_whitespace(u' '))
 
108
        self.assertTrue(osutils.contains_whitespace(u'hello there'))
 
109
        self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
 
110
        self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
 
111
        self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
 
112
        self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
117
113
 
118
114
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
119
115
        # is whitespace, but we do not.
120
 
        self.failIf(osutils.contains_whitespace(u''))
121
 
        self.failIf(osutils.contains_whitespace(u'hellothere'))
122
 
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
 
116
        self.assertFalse(osutils.contains_whitespace(u''))
 
117
        self.assertFalse(osutils.contains_whitespace(u'hellothere'))
 
118
        self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
123
119
 
124
120
 
125
121
class TestRename(tests.TestCaseInTempDir):
139
135
        # This should work everywhere
140
136
        self.create_file('a', 'something in a\n')
141
137
        self._fancy_rename('a', 'b')
142
 
        self.failIfExists('a')
143
 
        self.failUnlessExists('b')
 
138
        self.assertPathDoesNotExist('a')
 
139
        self.assertPathExists('b')
144
140
        self.check_file_contents('b', 'something in a\n')
145
141
 
146
142
        self.create_file('a', 'new something in a\n')
153
149
        self.create_file('target', 'data in target\n')
154
150
        self.assertRaises((IOError, OSError), self._fancy_rename,
155
151
                          'missingsource', 'target')
156
 
        self.failUnlessExists('target')
 
152
        self.assertPathExists('target')
157
153
        self.check_file_contents('target', 'data in target\n')
158
154
 
159
155
    def test_fancy_rename_fails_if_source_and_target_missing(self):
164
160
        # Rename should be semi-atomic on all platforms
165
161
        self.create_file('a', 'something in a\n')
166
162
        osutils.rename('a', 'b')
167
 
        self.failIfExists('a')
168
 
        self.failUnlessExists('b')
 
163
        self.assertPathDoesNotExist('a')
 
164
        self.assertPathExists('b')
169
165
        self.check_file_contents('b', 'something in a\n')
170
166
 
171
167
        self.create_file('a', 'new something in a\n')
236
232
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
237
233
 
238
234
 
 
235
class TestLstat(tests.TestCaseInTempDir):
 
236
 
 
237
    def test_lstat_matches_fstat(self):
 
238
        # On Windows, lstat and fstat don't always agree, primarily in the
 
239
        # 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
 
240
        # custom implementation.
 
241
        if sys.platform == 'win32':
 
242
            # We only have special lstat/fstat if we have the extension.
 
243
            # Without it, we may end up re-reading content when we don't have
 
244
            # to, but otherwise it doesn't effect correctness.
 
245
            self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
246
        f = open('test-file.txt', 'wb')
 
247
        self.addCleanup(f.close)
 
248
        f.write('some content\n')
 
249
        f.flush()
 
250
        self.assertEqualStat(osutils.fstat(f.fileno()),
 
251
                             osutils.lstat('test-file.txt'))
 
252
 
 
253
 
239
254
class TestRmTree(tests.TestCaseInTempDir):
240
255
 
241
256
    def test_rmtree(self):
253
268
 
254
269
        osutils.rmtree('dir')
255
270
 
256
 
        self.failIfExists('dir/file')
257
 
        self.failIfExists('dir')
 
271
        self.assertPathDoesNotExist('dir/file')
 
272
        self.assertPathDoesNotExist('dir')
258
273
 
259
274
 
260
275
class TestDeleteAny(tests.TestCaseInTempDir):
415
430
class TestLinks(tests.TestCaseInTempDir):
416
431
 
417
432
    def test_dereference_path(self):
418
 
        self.requireFeature(tests.SymlinkFeature)
 
433
        self.requireFeature(features.SymlinkFeature)
419
434
        cwd = osutils.realpath('.')
420
435
        os.mkdir('bar')
421
436
        bar_path = osutils.pathjoin(cwd, 'bar')
468
483
 
469
484
class TestCanonicalRelPath(tests.TestCaseInTempDir):
470
485
 
471
 
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
 
486
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
472
487
 
473
488
    def test_canonical_relpath_simple(self):
474
489
        f = file('MixedCaseName', 'w')
475
490
        f.close()
476
491
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
477
 
        self.failUnlessEqual('work/MixedCaseName', actual)
 
492
        self.assertEqual('work/MixedCaseName', actual)
478
493
 
479
494
    def test_canonical_relpath_missing_tail(self):
480
495
        os.mkdir('MixedCaseParent')
481
496
        actual = osutils.canonical_relpath(self.test_base_dir,
482
497
                                           'mixedcaseparent/nochild')
483
 
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
 
498
        self.assertEqual('work/MixedCaseParent/nochild', actual)
484
499
 
485
500
 
486
501
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
804
819
        self.assertEqual(None, osutils.safe_file_id(None))
805
820
 
806
821
 
 
822
class TestPosixFuncs(tests.TestCase):
 
823
    """Test that the posix version of normpath returns an appropriate path
 
824
       when used with 2 leading slashes."""
 
825
 
 
826
    def test_normpath(self):
 
827
        self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
 
828
        self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
 
829
        self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
 
830
 
 
831
 
807
832
class TestWin32Funcs(tests.TestCase):
808
833
    """Test that _win32 versions of os utilities return appropriate paths."""
809
834
 
874
899
    """Test win32 functions that create files."""
875
900
 
876
901
    def test_getcwd(self):
877
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
902
        self.requireFeature(features.UnicodeFilenameFeature)
878
903
        os.mkdir(u'mu-\xb5')
879
904
        os.chdir(u'mu-\xb5')
880
905
        # TODO: jam 20060427 This will probably fail on Mac OSX because
910
935
        b.close()
911
936
 
912
937
        osutils._win32_rename('b', 'a')
913
 
        self.failUnlessExists('a')
914
 
        self.failIfExists('b')
 
938
        self.assertPathExists('a')
 
939
        self.assertPathDoesNotExist('b')
915
940
        self.assertFileEqual('baz\n', 'a')
916
941
 
917
942
    def test_rename_missing_file(self):
970
995
    """Test mac special functions that require directories."""
971
996
 
972
997
    def test_getcwd(self):
973
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
998
        self.requireFeature(features.UnicodeFilenameFeature)
974
999
        os.mkdir(u'B\xe5gfors')
975
1000
        os.chdir(u'B\xe5gfors')
976
1001
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
977
1002
 
978
1003
    def test_getcwd_nonnorm(self):
979
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1004
        self.requireFeature(features.UnicodeFilenameFeature)
980
1005
        # Test that _mac_getcwd() will normalize this path
981
1006
        os.mkdir(u'Ba\u030agfors')
982
1007
        os.chdir(u'Ba\u030agfors')
1092
1117
        # are not using the filesystem's encoding
1093
1118
 
1094
1119
        # require a bytestring based filesystem
1095
 
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1120
        self.requireFeature(features.ByteStringNamedFilesystem)
1096
1121
 
1097
1122
        tree = [
1098
1123
            '.bzr',
1107
1132
 
1108
1133
        # rename the 1file to a latin-1 filename
1109
1134
        os.rename("./1file", "\xe8file")
 
1135
        if "\xe8file" not in os.listdir("."):
 
1136
            self.skip("Lack filesystem that preserves arbitrary bytes")
1110
1137
 
1111
1138
        self._save_platform_info()
1112
1139
        win32utils.winver = None # Avoid the win32 detection code
1190
1217
        self.requireFeature(UTF8DirReaderFeature)
1191
1218
        self._save_platform_info()
1192
1219
        win32utils.winver = None # Avoid the win32 detection code
1193
 
        osutils._fs_enc = 'UTF-8'
1194
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1220
        osutils._fs_enc = 'utf-8'
 
1221
        self.assertDirReaderIs(
 
1222
            UTF8DirReaderFeature.module.UTF8DirReader)
1195
1223
 
1196
1224
    def test_force_walkdirs_utf8_fs_ascii(self):
1197
1225
        self.requireFeature(UTF8DirReaderFeature)
1198
1226
        self._save_platform_info()
1199
1227
        win32utils.winver = None # Avoid the win32 detection code
1200
 
        osutils._fs_enc = 'US-ASCII'
1201
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1202
 
 
1203
 
    def test_force_walkdirs_utf8_fs_ANSI(self):
1204
 
        self.requireFeature(UTF8DirReaderFeature)
1205
 
        self._save_platform_info()
1206
 
        win32utils.winver = None # Avoid the win32 detection code
1207
 
        osutils._fs_enc = 'ANSI_X3.4-1968'
1208
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1228
        osutils._fs_enc = 'ascii'
 
1229
        self.assertDirReaderIs(
 
1230
            UTF8DirReaderFeature.module.UTF8DirReader)
1209
1231
 
1210
1232
    def test_force_walkdirs_utf8_fs_latin1(self):
1211
1233
        self._save_platform_info()
1212
1234
        win32utils.winver = None # Avoid the win32 detection code
1213
 
        osutils._fs_enc = 'latin1'
 
1235
        osutils._fs_enc = 'iso-8859-1'
1214
1236
        self.assertDirReaderIs(osutils.UnicodeDirReader)
1215
1237
 
1216
1238
    def test_force_walkdirs_utf8_nt(self):
1229
1251
 
1230
1252
    def test_unicode_walkdirs(self):
1231
1253
        """Walkdirs should always return unicode paths."""
1232
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1254
        self.requireFeature(features.UnicodeFilenameFeature)
1233
1255
        name0 = u'0file-\xb6'
1234
1256
        name1 = u'1dir-\u062c\u0648'
1235
1257
        name2 = u'2file-\u0633'
1272
1294
 
1273
1295
        The abspath portion might be in unicode or utf-8
1274
1296
        """
1275
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1297
        self.requireFeature(features.UnicodeFilenameFeature)
1276
1298
        name0 = u'0file-\xb6'
1277
1299
        name1 = u'1dir-\u062c\u0648'
1278
1300
        name2 = u'2file-\u0633'
1333
1355
 
1334
1356
        The abspath portion should be in unicode
1335
1357
        """
1336
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1358
        self.requireFeature(features.UnicodeFilenameFeature)
1337
1359
        # Use the unicode reader. TODO: split into driver-and-driven unit
1338
1360
        # tests.
1339
1361
        self._save_platform_info()
1380
1402
 
1381
1403
    def test__walkdirs_utf8_win32readdir(self):
1382
1404
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1383
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1405
        self.requireFeature(features.UnicodeFilenameFeature)
1384
1406
        from bzrlib._walkdirs_win32 import Win32ReadDir
1385
1407
        self._save_platform_info()
1386
1408
        osutils._selected_dir_reader = Win32ReadDir()
1437
1459
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1438
1460
        """make sure our Stat values are valid"""
1439
1461
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1440
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1462
        self.requireFeature(features.UnicodeFilenameFeature)
1441
1463
        from bzrlib._walkdirs_win32 import Win32ReadDir
1442
1464
        name0u = u'0file-\xb6'
1443
1465
        name0 = name0u.encode('utf8')
1461
1483
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1462
1484
        """make sure our Stat values are valid"""
1463
1485
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1464
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1486
        self.requireFeature(features.UnicodeFilenameFeature)
1465
1487
        from bzrlib._walkdirs_win32 import Win32ReadDir
1466
1488
        name0u = u'0dir-\u062c\u0648'
1467
1489
        name0 = name0u.encode('utf8')
1567
1589
        self.assertEqual(['c'], os.listdir('target/b'))
1568
1590
 
1569
1591
    def test_copy_tree_symlinks(self):
1570
 
        self.requireFeature(tests.SymlinkFeature)
 
1592
        self.requireFeature(features.SymlinkFeature)
1571
1593
        self.build_tree(['source/'])
1572
1594
        os.symlink('a/generic/path', 'source/lnk')
1573
1595
        osutils.copy_tree('source', 'target')
1598
1620
                          ('d', 'source/b', 'target/b'),
1599
1621
                          ('f', 'source/b/c', 'target/b/c'),
1600
1622
                         ], processed_files)
1601
 
        self.failIfExists('target')
 
1623
        self.assertPathDoesNotExist('target')
1602
1624
        if osutils.has_symlinks():
1603
1625
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1604
1626
 
1650
1672
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1651
1673
        self.assertEqual('foo', old)
1652
1674
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1653
 
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
 
1675
        self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
1654
1676
 
1655
1677
 
1656
1678
class TestSizeShaFile(tests.TestCaseInTempDir):
1733
1755
 
1734
1756
class TestDirReader(tests.TestCaseInTempDir):
1735
1757
 
 
1758
    scenarios = dir_reader_scenarios()
 
1759
 
1736
1760
    # Set by load_tests
1737
1761
    _dir_reader_class = None
1738
1762
    _native_to_unicode = None
1838
1862
        return filtered_dirblocks
1839
1863
 
1840
1864
    def test_walk_unicode_tree(self):
1841
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1865
        self.requireFeature(features.UnicodeFilenameFeature)
1842
1866
        tree, expected_dirblocks = self._get_unicode_tree()
1843
1867
        self.build_tree(tree)
1844
1868
        result = list(osutils._walkdirs_utf8('.'))
1845
1869
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1846
1870
 
1847
1871
    def test_symlink(self):
1848
 
        self.requireFeature(tests.SymlinkFeature)
1849
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1872
        self.requireFeature(features.SymlinkFeature)
 
1873
        self.requireFeature(features.UnicodeFilenameFeature)
1850
1874
        target = u'target\N{Euro Sign}'
1851
1875
        link_name = u'l\N{Euro Sign}nk'
1852
1876
        os.symlink(target, link_name)
1870
1894
    But prior python versions failed to properly encode the passed unicode
1871
1895
    string.
1872
1896
    """
1873
 
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
 
1897
    _test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
1874
1898
 
1875
1899
    def setUp(self):
1876
1900
        super(tests.TestCaseInTempDir, self).setUp()
1879
1903
        os.symlink(self.target, self.link)
1880
1904
 
1881
1905
    def test_os_readlink_link_encoding(self):
1882
 
        if sys.version_info < (2, 6):
1883
 
            self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1884
 
        else:
1885
 
            self.assertEquals(self.target,  os.readlink(self.link))
 
1906
        self.assertEquals(self.target,  os.readlink(self.link))
1886
1907
 
1887
1908
    def test_os_readlink_link_decoding(self):
1888
1909
        self.assertEquals(self.target.encode(osutils._fs_enc),
1900
1921
        self.assertIsInstance(concurrency, int)
1901
1922
 
1902
1923
    def test_local_concurrency_environment_variable(self):
1903
 
        os.environ['BZR_CONCURRENCY'] = '2'
 
1924
        self.overrideEnv('BZR_CONCURRENCY', '2')
1904
1925
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
 
        os.environ['BZR_CONCURRENCY'] = '3'
 
1926
        self.overrideEnv('BZR_CONCURRENCY', '3')
1906
1927
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
 
        os.environ['BZR_CONCURRENCY'] = 'foo'
 
1928
        self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
1929
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1909
1930
 
1910
1931
    def test_option_concurrency(self):
1911
 
        os.environ['BZR_CONCURRENCY'] = '1'
 
1932
        self.overrideEnv('BZR_CONCURRENCY', '1')
1912
1933
        self.run_bzr('rocks --concurrency 42')
1913
 
        # Command line overrides envrionment variable
 
1934
        # Command line overrides environment variable
1914
1935
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
1936
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1916
1937
 
1986
2007
    def test_defaults_to_BZR_COLUMNS(self):
1987
2008
        # BZR_COLUMNS is set by the test framework
1988
2009
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
 
        os.environ['BZR_COLUMNS'] = '12'
 
2010
        self.overrideEnv('BZR_COLUMNS', '12')
1990
2011
        self.assertEqual(12, osutils.terminal_width())
1991
2012
 
 
2013
    def test_BZR_COLUMNS_0_no_limit(self):
 
2014
        self.overrideEnv('BZR_COLUMNS', '0')
 
2015
        self.assertEqual(None, osutils.terminal_width())
 
2016
 
1992
2017
    def test_falls_back_to_COLUMNS(self):
1993
 
        del os.environ['BZR_COLUMNS']
 
2018
        self.overrideEnv('BZR_COLUMNS', None)
1994
2019
        self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2020
        self.set_fake_tty()
1996
 
        os.environ['COLUMNS'] = '42'
 
2021
        self.overrideEnv('COLUMNS', '42')
1997
2022
        self.assertEqual(42, osutils.terminal_width())
1998
2023
 
1999
2024
    def test_tty_default_without_columns(self):
2000
 
        del os.environ['BZR_COLUMNS']
2001
 
        del os.environ['COLUMNS']
 
2025
        self.overrideEnv('BZR_COLUMNS', None)
 
2026
        self.overrideEnv('COLUMNS', None)
2002
2027
 
2003
2028
        def terminal_size(w, h):
2004
2029
            return 42, 42
2011
2036
        self.assertEqual(42, osutils.terminal_width())
2012
2037
 
2013
2038
    def test_non_tty_default_without_columns(self):
2014
 
        del os.environ['BZR_COLUMNS']
2015
 
        del os.environ['COLUMNS']
 
2039
        self.overrideEnv('BZR_COLUMNS', None)
 
2040
        self.overrideEnv('COLUMNS', None)
2016
2041
        self.replace_stdout(None)
2017
2042
        self.assertEqual(None, osutils.terminal_width())
2018
2043
 
2028
2053
        else:
2029
2054
            self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2055
            del termios.TIOCGWINSZ
2031
 
        del os.environ['BZR_COLUMNS']
2032
 
        del os.environ['COLUMNS']
 
2056
        self.overrideEnv('BZR_COLUMNS', None)
 
2057
        self.overrideEnv('COLUMNS', None)
2033
2058
        # Whatever the result is, if we don't raise an exception, it's ok.
2034
2059
        osutils.terminal_width()
2035
2060
 
 
2061
 
2036
2062
class TestCreationOps(tests.TestCaseInTempDir):
2037
2063
    _test_needs_features = [features.chown_feature]
2038
2064
 
2068
2094
        self.assertEquals(self.uid, s.st_uid)
2069
2095
        self.assertEquals(self.gid, s.st_gid)
2070
2096
 
 
2097
 
2071
2098
class TestGetuserUnicode(tests.TestCase):
2072
2099
 
2073
2100
    def test_ascii_user(self):
2074
 
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
 
2101
        self.overrideEnv('LOGNAME', 'jrandom')
2075
2102
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
2076
2103
 
2077
2104
    def test_unicode_user(self):
2083
2110
                % (osutils.get_user_encoding(),))
2084
2111
        uni_username = u'jrandom' + uni_val
2085
2112
        encoded_username = uni_username.encode(ue)
2086
 
        osutils.set_or_unset_env('LOGNAME', encoded_username)
 
2113
        self.overrideEnv('LOGNAME', encoded_username)
2087
2114
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2115
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2116
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
 
2117
 
 
2118
 
 
2119
class TestBackupNames(tests.TestCase):
 
2120
 
 
2121
    def setUp(self):
 
2122
        super(TestBackupNames, self).setUp()
 
2123
        self.backups = []
 
2124
 
 
2125
    def backup_exists(self, name):
 
2126
        return name in self.backups
 
2127
 
 
2128
    def available_backup_name(self, name):
 
2129
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2130
        self.backups.append(backup_name)
 
2131
        return backup_name
 
2132
 
 
2133
    def assertBackupName(self, expected, name):
 
2134
        self.assertEqual(expected, self.available_backup_name(name))
 
2135
 
 
2136
    def test_empty(self):
 
2137
        self.assertBackupName('file.~1~', 'file')
 
2138
 
 
2139
    def test_existing(self):
 
2140
        self.available_backup_name('file')
 
2141
        self.available_backup_name('file')
 
2142
        self.assertBackupName('file.~3~', 'file')
 
2143
        # Empty slots are found, this is not a strict requirement and may be
 
2144
        # revisited if we test against all implementations.
 
2145
        self.backups.remove('file.~2~')
 
2146
        self.assertBackupName('file.~2~', 'file')
 
2147
 
 
2148
 
 
2149
class TestFindExecutableInPath(tests.TestCase):
 
2150
 
 
2151
    def test_windows(self):
 
2152
        if sys.platform != 'win32':
 
2153
            raise tests.TestSkipped('test requires win32')
 
2154
        self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
 
2155
        self.assertTrue(
 
2156
            osutils.find_executable_on_path('explorer.exe') is not None)
 
2157
        self.assertTrue(
 
2158
            osutils.find_executable_on_path('EXPLORER.EXE') is not None)
 
2159
        self.assertTrue(
 
2160
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2161
        self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
 
2162
 
 
2163
    def test_other(self):
 
2164
        if sys.platform == 'win32':
 
2165
            raise tests.TestSkipped('test requires non-win32')
 
2166
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
 
2167
        self.assertTrue(
 
2168
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2169
 
 
2170
 
 
2171
class TestEnvironmentErrors(tests.TestCase):
 
2172
    """Test handling of environmental errors"""
 
2173
 
 
2174
    def test_is_oserror(self):
 
2175
        self.assertTrue(osutils.is_environment_error(
 
2176
            OSError(errno.EINVAL, "Invalid parameter")))
 
2177
 
 
2178
    def test_is_ioerror(self):
 
2179
        self.assertTrue(osutils.is_environment_error(
 
2180
            IOError(errno.EINVAL, "Invalid parameter")))
 
2181
 
 
2182
    def test_is_socket_error(self):
 
2183
        self.assertTrue(osutils.is_environment_error(
 
2184
            socket.error(errno.EINVAL, "Invalid parameter")))
 
2185
 
 
2186
    def test_is_select_error(self):
 
2187
        self.assertTrue(osutils.is_environment_error(
 
2188
            select.error(errno.EINVAL, "Invalid parameter")))
 
2189
 
 
2190
    def test_is_pywintypes_error(self):
 
2191
        self.requireFeature(features.pywintypes)
 
2192
        import pywintypes
 
2193
        self.assertTrue(osutils.is_environment_error(
 
2194
            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))