~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Patch Queue Manager
  • Date: 2011-09-22 14:12:18 UTC
  • mfrom: (6155.3.1 jam)
  • Revision ID: pqm@pqm.ubuntu.com-20110922141218-86s4uu6nqvourw4f
(jameinel) Cleanup comments bzrlib/smart/__init__.py (John A Meinel)

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
38
38
    file_utils,
39
39
    test__walkdirs_win32,
40
40
    )
41
 
 
42
 
 
43
 
class _UTF8DirReaderFeature(tests.Feature):
 
41
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
42
 
 
43
 
 
44
class _UTF8DirReaderFeature(features.Feature):
44
45
 
45
46
    def _probe(self):
46
47
        try:
53
54
    def feature_name(self):
54
55
        return 'bzrlib._readdir_pyx'
55
56
 
56
 
UTF8DirReaderFeature = _UTF8DirReaderFeature()
 
57
UTF8DirReaderFeature = features.ModuleAvailableFeature('bzrlib._readdir_pyx')
57
58
 
58
 
term_ios_feature = tests.ModuleAvailableFeature('termios')
 
59
term_ios_feature = features.ModuleAvailableFeature('termios')
59
60
 
60
61
 
61
62
def _already_unicode(s):
96
97
    return scenarios
97
98
 
98
99
 
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
 
100
load_tests = load_tests_apply_scenarios
106
101
 
107
102
 
108
103
class TestContainsWhitespace(tests.TestCase):
109
104
 
110
105
    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'))
 
106
        self.assertTrue(osutils.contains_whitespace(u' '))
 
107
        self.assertTrue(osutils.contains_whitespace(u'hello there'))
 
108
        self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
 
109
        self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
 
110
        self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
 
111
        self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
117
112
 
118
113
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
119
114
        # 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'))
 
115
        self.assertFalse(osutils.contains_whitespace(u''))
 
116
        self.assertFalse(osutils.contains_whitespace(u'hellothere'))
 
117
        self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
123
118
 
124
119
 
125
120
class TestRename(tests.TestCaseInTempDir):
139
134
        # This should work everywhere
140
135
        self.create_file('a', 'something in a\n')
141
136
        self._fancy_rename('a', 'b')
142
 
        self.failIfExists('a')
143
 
        self.failUnlessExists('b')
 
137
        self.assertPathDoesNotExist('a')
 
138
        self.assertPathExists('b')
144
139
        self.check_file_contents('b', 'something in a\n')
145
140
 
146
141
        self.create_file('a', 'new something in a\n')
153
148
        self.create_file('target', 'data in target\n')
154
149
        self.assertRaises((IOError, OSError), self._fancy_rename,
155
150
                          'missingsource', 'target')
156
 
        self.failUnlessExists('target')
 
151
        self.assertPathExists('target')
157
152
        self.check_file_contents('target', 'data in target\n')
158
153
 
159
154
    def test_fancy_rename_fails_if_source_and_target_missing(self):
164
159
        # Rename should be semi-atomic on all platforms
165
160
        self.create_file('a', 'something in a\n')
166
161
        osutils.rename('a', 'b')
167
 
        self.failIfExists('a')
168
 
        self.failUnlessExists('b')
 
162
        self.assertPathDoesNotExist('a')
 
163
        self.assertPathExists('b')
169
164
        self.check_file_contents('b', 'something in a\n')
170
165
 
171
166
        self.create_file('a', 'new something in a\n')
236
231
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
237
232
 
238
233
 
 
234
class TestLstat(tests.TestCaseInTempDir):
 
235
 
 
236
    def test_lstat_matches_fstat(self):
 
237
        # On Windows, lstat and fstat don't always agree, primarily in the
 
238
        # 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
 
239
        # custom implementation.
 
240
        if sys.platform == 'win32':
 
241
            # We only have special lstat/fstat if we have the extension.
 
242
            # Without it, we may end up re-reading content when we don't have
 
243
            # to, but otherwise it doesn't effect correctness.
 
244
            self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
245
        f = open('test-file.txt', 'wb')
 
246
        self.addCleanup(f.close)
 
247
        f.write('some content\n')
 
248
        f.flush()
 
249
        self.assertEqualStat(osutils.fstat(f.fileno()),
 
250
                             osutils.lstat('test-file.txt'))
 
251
 
 
252
 
239
253
class TestRmTree(tests.TestCaseInTempDir):
240
254
 
241
255
    def test_rmtree(self):
253
267
 
254
268
        osutils.rmtree('dir')
255
269
 
256
 
        self.failIfExists('dir/file')
257
 
        self.failIfExists('dir')
 
270
        self.assertPathDoesNotExist('dir/file')
 
271
        self.assertPathDoesNotExist('dir')
258
272
 
259
273
 
260
274
class TestDeleteAny(tests.TestCaseInTempDir):
415
429
class TestLinks(tests.TestCaseInTempDir):
416
430
 
417
431
    def test_dereference_path(self):
418
 
        self.requireFeature(tests.SymlinkFeature)
 
432
        self.requireFeature(features.SymlinkFeature)
419
433
        cwd = osutils.realpath('.')
420
434
        os.mkdir('bar')
421
435
        bar_path = osutils.pathjoin(cwd, 'bar')
468
482
 
469
483
class TestCanonicalRelPath(tests.TestCaseInTempDir):
470
484
 
471
 
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
 
485
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
472
486
 
473
487
    def test_canonical_relpath_simple(self):
474
488
        f = file('MixedCaseName', 'w')
475
489
        f.close()
476
490
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
477
 
        self.failUnlessEqual('work/MixedCaseName', actual)
 
491
        self.assertEqual('work/MixedCaseName', actual)
478
492
 
479
493
    def test_canonical_relpath_missing_tail(self):
480
494
        os.mkdir('MixedCaseParent')
481
495
        actual = osutils.canonical_relpath(self.test_base_dir,
482
496
                                           'mixedcaseparent/nochild')
483
 
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
 
497
        self.assertEqual('work/MixedCaseParent/nochild', actual)
484
498
 
485
499
 
486
500
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
874
888
    """Test win32 functions that create files."""
875
889
 
876
890
    def test_getcwd(self):
877
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
891
        self.requireFeature(features.UnicodeFilenameFeature)
878
892
        os.mkdir(u'mu-\xb5')
879
893
        os.chdir(u'mu-\xb5')
880
894
        # TODO: jam 20060427 This will probably fail on Mac OSX because
910
924
        b.close()
911
925
 
912
926
        osutils._win32_rename('b', 'a')
913
 
        self.failUnlessExists('a')
914
 
        self.failIfExists('b')
 
927
        self.assertPathExists('a')
 
928
        self.assertPathDoesNotExist('b')
915
929
        self.assertFileEqual('baz\n', 'a')
916
930
 
917
931
    def test_rename_missing_file(self):
970
984
    """Test mac special functions that require directories."""
971
985
 
972
986
    def test_getcwd(self):
973
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
987
        self.requireFeature(features.UnicodeFilenameFeature)
974
988
        os.mkdir(u'B\xe5gfors')
975
989
        os.chdir(u'B\xe5gfors')
976
990
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
977
991
 
978
992
    def test_getcwd_nonnorm(self):
979
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
993
        self.requireFeature(features.UnicodeFilenameFeature)
980
994
        # Test that _mac_getcwd() will normalize this path
981
995
        os.mkdir(u'Ba\u030agfors')
982
996
        os.chdir(u'Ba\u030agfors')
1092
1106
        # are not using the filesystem's encoding
1093
1107
 
1094
1108
        # require a bytestring based filesystem
1095
 
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1109
        self.requireFeature(features.ByteStringNamedFilesystem)
1096
1110
 
1097
1111
        tree = [
1098
1112
            '.bzr',
1107
1121
 
1108
1122
        # rename the 1file to a latin-1 filename
1109
1123
        os.rename("./1file", "\xe8file")
 
1124
        if "\xe8file" not in os.listdir("."):
 
1125
            self.skip("Lack filesystem that preserves arbitrary bytes")
1110
1126
 
1111
1127
        self._save_platform_info()
1112
1128
        win32utils.winver = None # Avoid the win32 detection code
1191
1207
        self._save_platform_info()
1192
1208
        win32utils.winver = None # Avoid the win32 detection code
1193
1209
        osutils._fs_enc = 'UTF-8'
1194
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1210
        self.assertDirReaderIs(
 
1211
            UTF8DirReaderFeature.module.UTF8DirReader)
1195
1212
 
1196
1213
    def test_force_walkdirs_utf8_fs_ascii(self):
1197
1214
        self.requireFeature(UTF8DirReaderFeature)
1198
1215
        self._save_platform_info()
1199
1216
        win32utils.winver = None # Avoid the win32 detection code
1200
1217
        osutils._fs_enc = 'US-ASCII'
1201
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1218
        self.assertDirReaderIs(
 
1219
            UTF8DirReaderFeature.module.UTF8DirReader)
1202
1220
 
1203
1221
    def test_force_walkdirs_utf8_fs_ANSI(self):
1204
1222
        self.requireFeature(UTF8DirReaderFeature)
1205
1223
        self._save_platform_info()
1206
1224
        win32utils.winver = None # Avoid the win32 detection code
1207
1225
        osutils._fs_enc = 'ANSI_X3.4-1968'
1208
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1226
        self.assertDirReaderIs(
 
1227
            UTF8DirReaderFeature.module.UTF8DirReader)
1209
1228
 
1210
1229
    def test_force_walkdirs_utf8_fs_latin1(self):
1211
1230
        self._save_platform_info()
1229
1248
 
1230
1249
    def test_unicode_walkdirs(self):
1231
1250
        """Walkdirs should always return unicode paths."""
1232
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1251
        self.requireFeature(features.UnicodeFilenameFeature)
1233
1252
        name0 = u'0file-\xb6'
1234
1253
        name1 = u'1dir-\u062c\u0648'
1235
1254
        name2 = u'2file-\u0633'
1272
1291
 
1273
1292
        The abspath portion might be in unicode or utf-8
1274
1293
        """
1275
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1294
        self.requireFeature(features.UnicodeFilenameFeature)
1276
1295
        name0 = u'0file-\xb6'
1277
1296
        name1 = u'1dir-\u062c\u0648'
1278
1297
        name2 = u'2file-\u0633'
1333
1352
 
1334
1353
        The abspath portion should be in unicode
1335
1354
        """
1336
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1355
        self.requireFeature(features.UnicodeFilenameFeature)
1337
1356
        # Use the unicode reader. TODO: split into driver-and-driven unit
1338
1357
        # tests.
1339
1358
        self._save_platform_info()
1380
1399
 
1381
1400
    def test__walkdirs_utf8_win32readdir(self):
1382
1401
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1383
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1402
        self.requireFeature(features.UnicodeFilenameFeature)
1384
1403
        from bzrlib._walkdirs_win32 import Win32ReadDir
1385
1404
        self._save_platform_info()
1386
1405
        osutils._selected_dir_reader = Win32ReadDir()
1437
1456
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1438
1457
        """make sure our Stat values are valid"""
1439
1458
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1440
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1459
        self.requireFeature(features.UnicodeFilenameFeature)
1441
1460
        from bzrlib._walkdirs_win32 import Win32ReadDir
1442
1461
        name0u = u'0file-\xb6'
1443
1462
        name0 = name0u.encode('utf8')
1461
1480
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1462
1481
        """make sure our Stat values are valid"""
1463
1482
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1464
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1483
        self.requireFeature(features.UnicodeFilenameFeature)
1465
1484
        from bzrlib._walkdirs_win32 import Win32ReadDir
1466
1485
        name0u = u'0dir-\u062c\u0648'
1467
1486
        name0 = name0u.encode('utf8')
1567
1586
        self.assertEqual(['c'], os.listdir('target/b'))
1568
1587
 
1569
1588
    def test_copy_tree_symlinks(self):
1570
 
        self.requireFeature(tests.SymlinkFeature)
 
1589
        self.requireFeature(features.SymlinkFeature)
1571
1590
        self.build_tree(['source/'])
1572
1591
        os.symlink('a/generic/path', 'source/lnk')
1573
1592
        osutils.copy_tree('source', 'target')
1598
1617
                          ('d', 'source/b', 'target/b'),
1599
1618
                          ('f', 'source/b/c', 'target/b/c'),
1600
1619
                         ], processed_files)
1601
 
        self.failIfExists('target')
 
1620
        self.assertPathDoesNotExist('target')
1602
1621
        if osutils.has_symlinks():
1603
1622
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1604
1623
 
1650
1669
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1651
1670
        self.assertEqual('foo', old)
1652
1671
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1653
 
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
 
1672
        self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
1654
1673
 
1655
1674
 
1656
1675
class TestSizeShaFile(tests.TestCaseInTempDir):
1733
1752
 
1734
1753
class TestDirReader(tests.TestCaseInTempDir):
1735
1754
 
 
1755
    scenarios = dir_reader_scenarios()
 
1756
 
1736
1757
    # Set by load_tests
1737
1758
    _dir_reader_class = None
1738
1759
    _native_to_unicode = None
1838
1859
        return filtered_dirblocks
1839
1860
 
1840
1861
    def test_walk_unicode_tree(self):
1841
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1862
        self.requireFeature(features.UnicodeFilenameFeature)
1842
1863
        tree, expected_dirblocks = self._get_unicode_tree()
1843
1864
        self.build_tree(tree)
1844
1865
        result = list(osutils._walkdirs_utf8('.'))
1845
1866
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1846
1867
 
1847
1868
    def test_symlink(self):
1848
 
        self.requireFeature(tests.SymlinkFeature)
1849
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1869
        self.requireFeature(features.SymlinkFeature)
 
1870
        self.requireFeature(features.UnicodeFilenameFeature)
1850
1871
        target = u'target\N{Euro Sign}'
1851
1872
        link_name = u'l\N{Euro Sign}nk'
1852
1873
        os.symlink(target, link_name)
1870
1891
    But prior python versions failed to properly encode the passed unicode
1871
1892
    string.
1872
1893
    """
1873
 
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
 
1894
    _test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
1874
1895
 
1875
1896
    def setUp(self):
1876
1897
        super(tests.TestCaseInTempDir, self).setUp()
1879
1900
        os.symlink(self.target, self.link)
1880
1901
 
1881
1902
    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))
 
1903
        self.assertEquals(self.target,  os.readlink(self.link))
1886
1904
 
1887
1905
    def test_os_readlink_link_decoding(self):
1888
1906
        self.assertEquals(self.target.encode(osutils._fs_enc),
1900
1918
        self.assertIsInstance(concurrency, int)
1901
1919
 
1902
1920
    def test_local_concurrency_environment_variable(self):
1903
 
        os.environ['BZR_CONCURRENCY'] = '2'
 
1921
        self.overrideEnv('BZR_CONCURRENCY', '2')
1904
1922
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
 
        os.environ['BZR_CONCURRENCY'] = '3'
 
1923
        self.overrideEnv('BZR_CONCURRENCY', '3')
1906
1924
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
 
        os.environ['BZR_CONCURRENCY'] = 'foo'
 
1925
        self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
1926
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1909
1927
 
1910
1928
    def test_option_concurrency(self):
1911
 
        os.environ['BZR_CONCURRENCY'] = '1'
 
1929
        self.overrideEnv('BZR_CONCURRENCY', '1')
1912
1930
        self.run_bzr('rocks --concurrency 42')
1913
 
        # Command line overrides envrionment variable
 
1931
        # Command line overrides environment variable
1914
1932
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
1933
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1916
1934
 
1986
2004
    def test_defaults_to_BZR_COLUMNS(self):
1987
2005
        # BZR_COLUMNS is set by the test framework
1988
2006
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
 
        os.environ['BZR_COLUMNS'] = '12'
 
2007
        self.overrideEnv('BZR_COLUMNS', '12')
1990
2008
        self.assertEqual(12, osutils.terminal_width())
1991
2009
 
 
2010
    def test_BZR_COLUMNS_0_no_limit(self):
 
2011
        self.overrideEnv('BZR_COLUMNS', '0')
 
2012
        self.assertEqual(None, osutils.terminal_width())
 
2013
 
1992
2014
    def test_falls_back_to_COLUMNS(self):
1993
 
        del os.environ['BZR_COLUMNS']
 
2015
        self.overrideEnv('BZR_COLUMNS', None)
1994
2016
        self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2017
        self.set_fake_tty()
1996
 
        os.environ['COLUMNS'] = '42'
 
2018
        self.overrideEnv('COLUMNS', '42')
1997
2019
        self.assertEqual(42, osutils.terminal_width())
1998
2020
 
1999
2021
    def test_tty_default_without_columns(self):
2000
 
        del os.environ['BZR_COLUMNS']
2001
 
        del os.environ['COLUMNS']
 
2022
        self.overrideEnv('BZR_COLUMNS', None)
 
2023
        self.overrideEnv('COLUMNS', None)
2002
2024
 
2003
2025
        def terminal_size(w, h):
2004
2026
            return 42, 42
2011
2033
        self.assertEqual(42, osutils.terminal_width())
2012
2034
 
2013
2035
    def test_non_tty_default_without_columns(self):
2014
 
        del os.environ['BZR_COLUMNS']
2015
 
        del os.environ['COLUMNS']
 
2036
        self.overrideEnv('BZR_COLUMNS', None)
 
2037
        self.overrideEnv('COLUMNS', None)
2016
2038
        self.replace_stdout(None)
2017
2039
        self.assertEqual(None, osutils.terminal_width())
2018
2040
 
2028
2050
        else:
2029
2051
            self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2052
            del termios.TIOCGWINSZ
2031
 
        del os.environ['BZR_COLUMNS']
2032
 
        del os.environ['COLUMNS']
 
2053
        self.overrideEnv('BZR_COLUMNS', None)
 
2054
        self.overrideEnv('COLUMNS', None)
2033
2055
        # Whatever the result is, if we don't raise an exception, it's ok.
2034
2056
        osutils.terminal_width()
2035
2057
 
 
2058
 
2036
2059
class TestCreationOps(tests.TestCaseInTempDir):
2037
2060
    _test_needs_features = [features.chown_feature]
2038
2061
 
2068
2091
        self.assertEquals(self.uid, s.st_uid)
2069
2092
        self.assertEquals(self.gid, s.st_gid)
2070
2093
 
 
2094
 
2071
2095
class TestGetuserUnicode(tests.TestCase):
2072
2096
 
2073
2097
    def test_ascii_user(self):
2074
 
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
 
2098
        self.overrideEnv('LOGNAME', 'jrandom')
2075
2099
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
2076
2100
 
2077
2101
    def test_unicode_user(self):
2083
2107
                % (osutils.get_user_encoding(),))
2084
2108
        uni_username = u'jrandom' + uni_val
2085
2109
        encoded_username = uni_username.encode(ue)
2086
 
        osutils.set_or_unset_env('LOGNAME', encoded_username)
 
2110
        self.overrideEnv('LOGNAME', encoded_username)
2087
2111
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2112
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2113
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
 
2114
 
 
2115
    def test_no_username_bug_660174(self):
 
2116
        self.requireFeature(features.win32_feature)
 
2117
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
 
2118
            self.overrideEnv(name, None)
 
2119
        self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
 
2120
 
 
2121
 
 
2122
class TestBackupNames(tests.TestCase):
 
2123
 
 
2124
    def setUp(self):
 
2125
        super(TestBackupNames, self).setUp()
 
2126
        self.backups = []
 
2127
 
 
2128
    def backup_exists(self, name):
 
2129
        return name in self.backups
 
2130
 
 
2131
    def available_backup_name(self, name):
 
2132
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2133
        self.backups.append(backup_name)
 
2134
        return backup_name
 
2135
 
 
2136
    def assertBackupName(self, expected, name):
 
2137
        self.assertEqual(expected, self.available_backup_name(name))
 
2138
 
 
2139
    def test_empty(self):
 
2140
        self.assertBackupName('file.~1~', 'file')
 
2141
 
 
2142
    def test_existing(self):
 
2143
        self.available_backup_name('file')
 
2144
        self.available_backup_name('file')
 
2145
        self.assertBackupName('file.~3~', 'file')
 
2146
        # Empty slots are found, this is not a strict requirement and may be
 
2147
        # revisited if we test against all implementations.
 
2148
        self.backups.remove('file.~2~')
 
2149
        self.assertBackupName('file.~2~', 'file')
 
2150
 
 
2151
 
 
2152
class TestFindExecutableInPath(tests.TestCase):
 
2153
 
 
2154
    def test_windows(self):
 
2155
        if sys.platform != 'win32':
 
2156
            raise tests.TestSkipped('test requires win32')
 
2157
        self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
 
2158
        self.assertTrue(
 
2159
            osutils.find_executable_on_path('explorer.exe') is not None)
 
2160
        self.assertTrue(
 
2161
            osutils.find_executable_on_path('EXPLORER.EXE') is not None)
 
2162
        self.assertTrue(
 
2163
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2164
        self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
 
2165
 
 
2166
    def test_other(self):
 
2167
        if sys.platform == 'win32':
 
2168
            raise tests.TestSkipped('test requires non-win32')
 
2169
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
 
2170
        self.assertTrue(
 
2171
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)