~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: 2016-04-21 05:06:57 UTC
  • mfrom: (6603.4.1 bzr)
  • Revision ID: pqm@pqm.ubuntu.com-20160421050657-ygnzfybewvudf1j9
(richard-wilbur) Use initial_comment as commit_message for lp_propose.(Shawn
 Wang) (Shawn Wang)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2016 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
 
import stat
25
25
import sys
 
26
import tempfile
26
27
import time
27
28
 
28
29
from bzrlib import (
29
30
    errors,
 
31
    lazy_regex,
30
32
    osutils,
 
33
    symbol_versioning,
31
34
    tests,
32
35
    trace,
33
36
    win32utils,
37
40
    file_utils,
38
41
    test__walkdirs_win32,
39
42
    )
40
 
 
41
 
 
42
 
class _UTF8DirReaderFeature(tests.Feature):
 
43
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
44
 
 
45
 
 
46
class _UTF8DirReaderFeature(features.Feature):
43
47
 
44
48
    def _probe(self):
45
49
        try:
52
56
    def feature_name(self):
53
57
        return 'bzrlib._readdir_pyx'
54
58
 
55
 
UTF8DirReaderFeature = _UTF8DirReaderFeature()
 
59
UTF8DirReaderFeature = features.ModuleAvailableFeature('bzrlib._readdir_pyx')
56
60
 
57
 
term_ios_feature = tests.ModuleAvailableFeature('termios')
 
61
term_ios_feature = features.ModuleAvailableFeature('termios')
58
62
 
59
63
 
60
64
def _already_unicode(s):
95
99
    return scenarios
96
100
 
97
101
 
98
 
def load_tests(basic_tests, module, loader):
99
 
    suite = loader.suiteClass()
100
 
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
101
 
        basic_tests, tests.condition_isinstance(TestDirReader))
102
 
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
103
 
    suite.addTest(remaining_tests)
104
 
    return suite
 
102
load_tests = load_tests_apply_scenarios
105
103
 
106
104
 
107
105
class TestContainsWhitespace(tests.TestCase):
108
106
 
109
107
    def test_contains_whitespace(self):
110
 
        self.failUnless(osutils.contains_whitespace(u' '))
111
 
        self.failUnless(osutils.contains_whitespace(u'hello there'))
112
 
        self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
113
 
        self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
114
 
        self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
115
 
        self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
 
108
        self.assertTrue(osutils.contains_whitespace(u' '))
 
109
        self.assertTrue(osutils.contains_whitespace(u'hello there'))
 
110
        self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
 
111
        self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
 
112
        self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
 
113
        self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
116
114
 
117
115
        # \xa0 is "Non-breaking-space" which on some python locales thinks it
118
116
        # is whitespace, but we do not.
119
 
        self.failIf(osutils.contains_whitespace(u''))
120
 
        self.failIf(osutils.contains_whitespace(u'hellothere'))
121
 
        self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
 
117
        self.assertFalse(osutils.contains_whitespace(u''))
 
118
        self.assertFalse(osutils.contains_whitespace(u'hellothere'))
 
119
        self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
122
120
 
123
121
 
124
122
class TestRename(tests.TestCaseInTempDir):
138
136
        # This should work everywhere
139
137
        self.create_file('a', 'something in a\n')
140
138
        self._fancy_rename('a', 'b')
141
 
        self.failIfExists('a')
142
 
        self.failUnlessExists('b')
 
139
        self.assertPathDoesNotExist('a')
 
140
        self.assertPathExists('b')
143
141
        self.check_file_contents('b', 'something in a\n')
144
142
 
145
143
        self.create_file('a', 'new something in a\n')
152
150
        self.create_file('target', 'data in target\n')
153
151
        self.assertRaises((IOError, OSError), self._fancy_rename,
154
152
                          'missingsource', 'target')
155
 
        self.failUnlessExists('target')
 
153
        self.assertPathExists('target')
156
154
        self.check_file_contents('target', 'data in target\n')
157
155
 
158
156
    def test_fancy_rename_fails_if_source_and_target_missing(self):
163
161
        # Rename should be semi-atomic on all platforms
164
162
        self.create_file('a', 'something in a\n')
165
163
        osutils.rename('a', 'b')
166
 
        self.failIfExists('a')
167
 
        self.failUnlessExists('b')
 
164
        self.assertPathDoesNotExist('a')
 
165
        self.assertPathExists('b')
168
166
        self.check_file_contents('b', 'something in a\n')
169
167
 
170
168
        self.create_file('a', 'new something in a\n')
182
180
        # we can't use failUnlessExists on case-insensitive filesystem
183
181
        # so try to check shape of the tree
184
182
        shape = sorted(os.listdir('.'))
185
 
        self.assertEquals(['A', 'B'], shape)
 
183
        self.assertEqual(['A', 'B'], shape)
 
184
 
 
185
    def test_rename_exception(self):
 
186
        try:
 
187
            osutils.rename('nonexistent_path', 'different_nonexistent_path')
 
188
        except OSError, e:
 
189
            self.assertEqual(e.old_filename, 'nonexistent_path')
 
190
            self.assertEqual(e.new_filename, 'different_nonexistent_path')
 
191
            self.assertTrue('nonexistent_path' in e.strerror)
 
192
            self.assertTrue('different_nonexistent_path' in e.strerror)
186
193
 
187
194
 
188
195
class TestRandChars(tests.TestCase):
215
222
                         (['src'], SRC_FOO_C),
216
223
                         (['src'], 'src'),
217
224
                         ]:
218
 
            self.assert_(osutils.is_inside_any(dirs, fn))
 
225
            self.assertTrue(osutils.is_inside_any(dirs, fn))
219
226
        for dirs, fn in [(['src'], 'srccontrol'),
220
227
                         (['src'], 'srccontrol/foo')]:
221
228
            self.assertFalse(osutils.is_inside_any(dirs, fn))
227
234
                         (['src/bar.c', 'bla/foo.c'], 'src'),
228
235
                         (['src'], 'src'),
229
236
                         ]:
230
 
            self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
 
237
            self.assertTrue(osutils.is_inside_or_parent_of_any(dirs, fn))
231
238
 
232
239
        for dirs, fn in [(['src'], 'srccontrol'),
233
240
                         (['srccontrol/foo.c'], 'src'),
235
242
            self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
236
243
 
237
244
 
 
245
class TestLstat(tests.TestCaseInTempDir):
 
246
 
 
247
    def test_lstat_matches_fstat(self):
 
248
        # On Windows, lstat and fstat don't always agree, primarily in the
 
249
        # 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
 
250
        # custom implementation.
 
251
        if sys.platform == 'win32':
 
252
            # We only have special lstat/fstat if we have the extension.
 
253
            # Without it, we may end up re-reading content when we don't have
 
254
            # to, but otherwise it doesn't effect correctness.
 
255
            self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
 
256
        f = open('test-file.txt', 'wb')
 
257
        self.addCleanup(f.close)
 
258
        f.write('some content\n')
 
259
        f.flush()
 
260
        self.assertEqualStat(osutils.fstat(f.fileno()),
 
261
                             osutils.lstat('test-file.txt'))
 
262
 
 
263
 
238
264
class TestRmTree(tests.TestCaseInTempDir):
239
265
 
240
266
    def test_rmtree(self):
252
278
 
253
279
        osutils.rmtree('dir')
254
280
 
255
 
        self.failIfExists('dir/file')
256
 
        self.failIfExists('dir')
 
281
        self.assertPathDoesNotExist('dir/file')
 
282
        self.assertPathDoesNotExist('dir')
257
283
 
258
284
 
259
285
class TestDeleteAny(tests.TestCaseInTempDir):
272
298
 
273
299
    def test_file_kind(self):
274
300
        self.build_tree(['file', 'dir/'])
275
 
        self.assertEquals('file', osutils.file_kind('file'))
276
 
        self.assertEquals('directory', osutils.file_kind('dir/'))
 
301
        self.assertEqual('file', osutils.file_kind('file'))
 
302
        self.assertEqual('directory', osutils.file_kind('dir/'))
277
303
        if osutils.has_symlinks():
278
304
            os.symlink('symlink', 'symlink')
279
 
            self.assertEquals('symlink', osutils.file_kind('symlink'))
 
305
            self.assertEqual('symlink', osutils.file_kind('symlink'))
280
306
 
281
307
        # TODO: jam 20060529 Test a block device
282
308
        try:
285
311
            if e.errno not in (errno.ENOENT,):
286
312
                raise
287
313
        else:
288
 
            self.assertEquals('chardev', osutils.file_kind('/dev/null'))
 
314
            self.assertEqual('chardev', osutils.file_kind('/dev/null'))
289
315
 
290
316
        mkfifo = getattr(os, 'mkfifo', None)
291
317
        if mkfifo:
292
318
            mkfifo('fifo')
293
319
            try:
294
 
                self.assertEquals('fifo', osutils.file_kind('fifo'))
 
320
                self.assertEqual('fifo', osutils.file_kind('fifo'))
295
321
            finally:
296
322
                os.remove('fifo')
297
323
 
300
326
            s = socket.socket(AF_UNIX)
301
327
            s.bind('socket')
302
328
            try:
303
 
                self.assertEquals('socket', osutils.file_kind('socket'))
 
329
                self.assertEqual('socket', osutils.file_kind('socket'))
304
330
            finally:
305
331
                os.remove('socket')
306
332
 
411
437
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
412
438
 
413
439
 
 
440
class TestFdatasync(tests.TestCaseInTempDir):
 
441
 
 
442
    def do_fdatasync(self):
 
443
        f = tempfile.NamedTemporaryFile()
 
444
        osutils.fdatasync(f.fileno())
 
445
        f.close()
 
446
 
 
447
    @staticmethod
 
448
    def raise_eopnotsupp(*args, **kwargs):
 
449
        raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
 
450
 
 
451
    @staticmethod
 
452
    def raise_enotsup(*args, **kwargs):
 
453
        raise IOError(errno.ENOTSUP, os.strerror(errno.ENOTSUP))
 
454
 
 
455
    def test_fdatasync_handles_system_function(self):
 
456
        self.overrideAttr(os, "fdatasync")
 
457
        self.do_fdatasync()
 
458
 
 
459
    def test_fdatasync_handles_no_fdatasync_no_fsync(self):
 
460
        self.overrideAttr(os, "fdatasync")
 
461
        self.overrideAttr(os, "fsync")
 
462
        self.do_fdatasync()
 
463
 
 
464
    def test_fdatasync_handles_no_EOPNOTSUPP(self):
 
465
        self.overrideAttr(errno, "EOPNOTSUPP")
 
466
        self.do_fdatasync()
 
467
 
 
468
    def test_fdatasync_catches_ENOTSUP(self):
 
469
        enotsup = getattr(errno, "ENOTSUP", None)
 
470
        if enotsup is None:
 
471
            raise tests.TestNotApplicable("No ENOTSUP on this platform")
 
472
        self.overrideAttr(os, "fdatasync", self.raise_enotsup)
 
473
        self.do_fdatasync()
 
474
 
 
475
    def test_fdatasync_catches_EOPNOTSUPP(self):
 
476
        enotsup = getattr(errno, "EOPNOTSUPP", None)
 
477
        if enotsup is None:
 
478
            raise tests.TestNotApplicable("No EOPNOTSUPP on this platform")
 
479
        self.overrideAttr(os, "fdatasync", self.raise_eopnotsupp)
 
480
        self.do_fdatasync()
 
481
 
 
482
 
414
483
class TestLinks(tests.TestCaseInTempDir):
415
484
 
416
485
    def test_dereference_path(self):
417
 
        self.requireFeature(tests.SymlinkFeature)
 
486
        self.requireFeature(features.SymlinkFeature)
418
487
        cwd = osutils.realpath('.')
419
488
        os.mkdir('bar')
420
489
        bar_path = osutils.pathjoin(cwd, 'bar')
467
536
 
468
537
class TestCanonicalRelPath(tests.TestCaseInTempDir):
469
538
 
470
 
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
 
539
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
471
540
 
472
541
    def test_canonical_relpath_simple(self):
473
542
        f = file('MixedCaseName', 'w')
474
543
        f.close()
475
544
        actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
476
 
        self.failUnlessEqual('work/MixedCaseName', actual)
 
545
        self.assertEqual('work/MixedCaseName', actual)
477
546
 
478
547
    def test_canonical_relpath_missing_tail(self):
479
548
        os.mkdir('MixedCaseParent')
480
549
        actual = osutils.canonical_relpath(self.test_base_dir,
481
550
                                           'mixedcaseparent/nochild')
482
 
        self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
 
551
        self.assertEqual('work/MixedCaseParent/nochild', actual)
483
552
 
484
553
 
485
554
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
529
598
    """Test pumpfile method."""
530
599
 
531
600
    def setUp(self):
532
 
        tests.TestCase.setUp(self)
 
601
        super(TestPumpFile, self).setUp()
533
602
        # create a test datablock
534
603
        self.block_size = 512
535
604
        pattern = '0123456789ABCDEF'
803
872
        self.assertEqual(None, osutils.safe_file_id(None))
804
873
 
805
874
 
 
875
class TestSendAll(tests.TestCase):
 
876
 
 
877
    def test_send_with_disconnected_socket(self):
 
878
        class DisconnectedSocket(object):
 
879
            def __init__(self, err):
 
880
                self.err = err
 
881
            def send(self, content):
 
882
                raise self.err
 
883
            def close(self):
 
884
                pass
 
885
        # All of these should be treated as ConnectionReset
 
886
        errs = []
 
887
        for err_cls in (IOError, socket.error):
 
888
            for errnum in osutils._end_of_stream_errors:
 
889
                errs.append(err_cls(errnum))
 
890
        for err in errs:
 
891
            sock = DisconnectedSocket(err)
 
892
            self.assertRaises(errors.ConnectionReset,
 
893
                osutils.send_all, sock, 'some more content')
 
894
 
 
895
    def test_send_with_no_progress(self):
 
896
        # See https://bugs.launchpad.net/bzr/+bug/1047309
 
897
        # It seems that paramiko can get into a state where it doesn't error,
 
898
        # but it returns 0 bytes sent for requests over and over again.
 
899
        class NoSendingSocket(object):
 
900
            def __init__(self):
 
901
                self.call_count = 0
 
902
            def send(self, bytes):
 
903
                self.call_count += 1
 
904
                if self.call_count > 100:
 
905
                    # Prevent the test suite from hanging
 
906
                    raise RuntimeError('too many calls')
 
907
                return 0
 
908
        sock = NoSendingSocket()
 
909
        self.assertRaises(errors.ConnectionReset,
 
910
                          osutils.send_all, sock, 'content')
 
911
        self.assertEqual(1, sock.call_count)
 
912
 
 
913
 
 
914
class TestPosixFuncs(tests.TestCase):
 
915
    """Test that the posix version of normpath returns an appropriate path
 
916
       when used with 2 leading slashes."""
 
917
 
 
918
    def test_normpath(self):
 
919
        self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
 
920
        self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
 
921
        self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
 
922
 
 
923
 
806
924
class TestWin32Funcs(tests.TestCase):
807
925
    """Test that _win32 versions of os utilities return appropriate paths."""
808
926
 
809
927
    def test_abspath(self):
 
928
        self.requireFeature(features.win32_feature)
810
929
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
811
930
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
812
931
        self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
825
944
                         osutils._win32_pathjoin('path/to', 'C:/foo'))
826
945
        self.assertEqual('path/to/foo',
827
946
                         osutils._win32_pathjoin('path/to/', 'foo'))
828
 
        self.assertEqual('/foo',
 
947
 
 
948
    def test_pathjoin_late_bugfix(self):
 
949
        if sys.version_info < (2, 7, 6):
 
950
            expected = '/foo'
 
951
        else:
 
952
            expected = 'C:/foo'
 
953
        self.assertEqual(expected,
829
954
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
830
 
        self.assertEqual('/foo',
 
955
        self.assertEqual(expected,
831
956
                         osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
832
957
 
833
958
    def test_normpath(self):
853
978
        self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
854
979
 
855
980
    def test_win98_abspath(self):
 
981
        self.requireFeature(features.win32_feature)
856
982
        # absolute path
857
983
        self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
858
984
        self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
861
987
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
862
988
        # relative path
863
989
        cwd = osutils.getcwd().rstrip('/')
864
 
        drive = osutils._nt_splitdrive(cwd)[0]
 
990
        drive = osutils.ntpath.splitdrive(cwd)[0]
865
991
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
866
992
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
867
993
        # unicode path
873
999
    """Test win32 functions that create files."""
874
1000
 
875
1001
    def test_getcwd(self):
876
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1002
        self.requireFeature(features.UnicodeFilenameFeature)
877
1003
        os.mkdir(u'mu-\xb5')
878
1004
        os.chdir(u'mu-\xb5')
879
1005
        # TODO: jam 20060427 This will probably fail on Mac OSX because
909
1035
        b.close()
910
1036
 
911
1037
        osutils._win32_rename('b', 'a')
912
 
        self.failUnlessExists('a')
913
 
        self.failIfExists('b')
 
1038
        self.assertPathExists('a')
 
1039
        self.assertPathDoesNotExist('b')
914
1040
        self.assertFileEqual('baz\n', 'a')
915
1041
 
916
1042
    def test_rename_missing_file(self):
969
1095
    """Test mac special functions that require directories."""
970
1096
 
971
1097
    def test_getcwd(self):
972
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1098
        self.requireFeature(features.UnicodeFilenameFeature)
973
1099
        os.mkdir(u'B\xe5gfors')
974
1100
        os.chdir(u'B\xe5gfors')
975
1101
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
976
1102
 
977
1103
    def test_getcwd_nonnorm(self):
978
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1104
        self.requireFeature(features.UnicodeFilenameFeature)
979
1105
        # Test that _mac_getcwd() will normalize this path
980
1106
        os.mkdir(u'Ba\u030agfors')
981
1107
        os.chdir(u'Ba\u030agfors')
1064
1190
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
1065
1191
 
1066
1192
    def test_walkdirs_os_error(self):
1067
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
 
1193
        # <https://bugs.launchpad.net/bzr/+bug/338653>
1068
1194
        # Pyrex readdir didn't raise useful messages if it had an error
1069
1195
        # reading the directory
1070
1196
        if sys.platform == 'win32':
1071
1197
            raise tests.TestNotApplicable(
1072
1198
                "readdir IOError not tested on win32")
 
1199
        self.requireFeature(features.not_running_as_root)
1073
1200
        os.mkdir("test-unreadable")
1074
1201
        os.chmod("test-unreadable", 0000)
1075
1202
        # must chmod it back so that it can be removed
1078
1205
        # (It would be ok if it happened earlier but at the moment it
1079
1206
        # doesn't.)
1080
1207
        e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
1081
 
        self.assertEquals('./test-unreadable', e.filename)
1082
 
        self.assertEquals(errno.EACCES, e.errno)
 
1208
        self.assertEqual('./test-unreadable', e.filename)
 
1209
        self.assertEqual(errno.EACCES, e.errno)
1083
1210
        # Ensure the message contains the file name
1084
1211
        self.assertContainsRe(str(e), "\./test-unreadable")
1085
1212
 
 
1213
 
 
1214
    def test_walkdirs_encoding_error(self):
 
1215
        # <https://bugs.launchpad.net/bzr/+bug/488519>
 
1216
        # walkdirs didn't raise a useful message when the filenames
 
1217
        # are not using the filesystem's encoding
 
1218
 
 
1219
        # require a bytestring based filesystem
 
1220
        self.requireFeature(features.ByteStringNamedFilesystem)
 
1221
 
 
1222
        tree = [
 
1223
            '.bzr',
 
1224
            '0file',
 
1225
            '1dir/',
 
1226
            '1dir/0file',
 
1227
            '1dir/1dir/',
 
1228
            '1file'
 
1229
            ]
 
1230
 
 
1231
        self.build_tree(tree)
 
1232
 
 
1233
        # rename the 1file to a latin-1 filename
 
1234
        os.rename("./1file", "\xe8file")
 
1235
        if "\xe8file" not in os.listdir("."):
 
1236
            self.skip("Lack filesystem that preserves arbitrary bytes")
 
1237
 
 
1238
        self._save_platform_info()
 
1239
        win32utils.winver = None # Avoid the win32 detection code
 
1240
        osutils._fs_enc = 'UTF-8'
 
1241
 
 
1242
        # this should raise on error
 
1243
        def attempt():
 
1244
            for dirdetail, dirblock in osutils.walkdirs('.'):
 
1245
                pass
 
1246
 
 
1247
        self.assertRaises(errors.BadFilenameEncoding, attempt)
 
1248
 
1086
1249
    def test__walkdirs_utf8(self):
1087
1250
        tree = [
1088
1251
            '.bzr',
1154
1317
        self.requireFeature(UTF8DirReaderFeature)
1155
1318
        self._save_platform_info()
1156
1319
        win32utils.winver = None # Avoid the win32 detection code
1157
 
        osutils._fs_enc = 'UTF-8'
1158
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1320
        osutils._fs_enc = 'utf-8'
 
1321
        self.assertDirReaderIs(
 
1322
            UTF8DirReaderFeature.module.UTF8DirReader)
1159
1323
 
1160
1324
    def test_force_walkdirs_utf8_fs_ascii(self):
1161
1325
        self.requireFeature(UTF8DirReaderFeature)
1162
1326
        self._save_platform_info()
1163
1327
        win32utils.winver = None # Avoid the win32 detection code
1164
 
        osutils._fs_enc = 'US-ASCII'
1165
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1166
 
 
1167
 
    def test_force_walkdirs_utf8_fs_ANSI(self):
1168
 
        self.requireFeature(UTF8DirReaderFeature)
1169
 
        self._save_platform_info()
1170
 
        win32utils.winver = None # Avoid the win32 detection code
1171
 
        osutils._fs_enc = 'ANSI_X3.4-1968'
1172
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1328
        osutils._fs_enc = 'ascii'
 
1329
        self.assertDirReaderIs(
 
1330
            UTF8DirReaderFeature.module.UTF8DirReader)
1173
1331
 
1174
1332
    def test_force_walkdirs_utf8_fs_latin1(self):
1175
1333
        self._save_platform_info()
1176
1334
        win32utils.winver = None # Avoid the win32 detection code
1177
 
        osutils._fs_enc = 'latin1'
 
1335
        osutils._fs_enc = 'iso-8859-1'
1178
1336
        self.assertDirReaderIs(osutils.UnicodeDirReader)
1179
1337
 
1180
1338
    def test_force_walkdirs_utf8_nt(self):
1193
1351
 
1194
1352
    def test_unicode_walkdirs(self):
1195
1353
        """Walkdirs should always return unicode paths."""
1196
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1354
        self.requireFeature(features.UnicodeFilenameFeature)
1197
1355
        name0 = u'0file-\xb6'
1198
1356
        name1 = u'1dir-\u062c\u0648'
1199
1357
        name2 = u'2file-\u0633'
1236
1394
 
1237
1395
        The abspath portion might be in unicode or utf-8
1238
1396
        """
1239
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1397
        self.requireFeature(features.UnicodeFilenameFeature)
1240
1398
        name0 = u'0file-\xb6'
1241
1399
        name1 = u'1dir-\u062c\u0648'
1242
1400
        name2 = u'2file-\u0633'
1297
1455
 
1298
1456
        The abspath portion should be in unicode
1299
1457
        """
1300
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1458
        self.requireFeature(features.UnicodeFilenameFeature)
1301
1459
        # Use the unicode reader. TODO: split into driver-and-driven unit
1302
1460
        # tests.
1303
1461
        self._save_platform_info()
1344
1502
 
1345
1503
    def test__walkdirs_utf8_win32readdir(self):
1346
1504
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1347
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1505
        self.requireFeature(features.UnicodeFilenameFeature)
1348
1506
        from bzrlib._walkdirs_win32 import Win32ReadDir
1349
1507
        self._save_platform_info()
1350
1508
        osutils._selected_dir_reader = Win32ReadDir()
1401
1559
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1402
1560
        """make sure our Stat values are valid"""
1403
1561
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1404
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1562
        self.requireFeature(features.UnicodeFilenameFeature)
1405
1563
        from bzrlib._walkdirs_win32 import Win32ReadDir
1406
1564
        name0u = u'0file-\xb6'
1407
1565
        name0 = name0u.encode('utf8')
1425
1583
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1426
1584
        """make sure our Stat values are valid"""
1427
1585
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1428
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1586
        self.requireFeature(features.UnicodeFilenameFeature)
1429
1587
        from bzrlib._walkdirs_win32 import Win32ReadDir
1430
1588
        name0u = u'0dir-\u062c\u0648'
1431
1589
        name0 = name0u.encode('utf8')
1531
1689
        self.assertEqual(['c'], os.listdir('target/b'))
1532
1690
 
1533
1691
    def test_copy_tree_symlinks(self):
1534
 
        self.requireFeature(tests.SymlinkFeature)
 
1692
        self.requireFeature(features.SymlinkFeature)
1535
1693
        self.build_tree(['source/'])
1536
1694
        os.symlink('a/generic/path', 'source/lnk')
1537
1695
        osutils.copy_tree('source', 'target')
1562
1720
                          ('d', 'source/b', 'target/b'),
1563
1721
                          ('f', 'source/b/c', 'target/b/c'),
1564
1722
                         ], processed_files)
1565
 
        self.failIfExists('target')
 
1723
        self.assertPathDoesNotExist('target')
1566
1724
        if osutils.has_symlinks():
1567
1725
            self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1568
1726
 
1614
1772
        old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1615
1773
        self.assertEqual('foo', old)
1616
1774
        self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1617
 
        self.failIf('BZR_TEST_ENV_VAR' in os.environ)
 
1775
        self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
1618
1776
 
1619
1777
 
1620
1778
class TestSizeShaFile(tests.TestCaseInTempDir):
1671
1829
 
1672
1830
class TestReCompile(tests.TestCase):
1673
1831
 
 
1832
    def _deprecated_re_compile_checked(self, *args, **kwargs):
 
1833
        return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
 
1834
            osutils.re_compile_checked, *args, **kwargs)
 
1835
 
1674
1836
    def test_re_compile_checked(self):
1675
 
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
 
1837
        r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1676
1838
        self.assertTrue(r.match('aaaa'))
1677
1839
        self.assertTrue(r.match('aAaA'))
1678
1840
 
1679
1841
    def test_re_compile_checked_error(self):
1680
1842
        # like https://bugs.launchpad.net/bzr/+bug/251352
 
1843
 
 
1844
        # Due to possible test isolation error, re.compile is not lazy at
 
1845
        # this point. We re-install lazy compile.
 
1846
        lazy_regex.install_lazy_compile()
1681
1847
        err = self.assertRaises(
1682
1848
            errors.BzrCommandError,
1683
 
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
 
1849
            self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1684
1850
        self.assertEqual(
1685
 
            "Invalid regular expression in test case: '*': "
1686
 
            "nothing to repeat",
 
1851
            'Invalid regular expression in test case: '
 
1852
            '"*" nothing to repeat',
1687
1853
            str(err))
1688
1854
 
1689
1855
 
1690
1856
class TestDirReader(tests.TestCaseInTempDir):
1691
1857
 
 
1858
    scenarios = dir_reader_scenarios()
 
1859
 
1692
1860
    # Set by load_tests
1693
1861
    _dir_reader_class = None
1694
1862
    _native_to_unicode = None
1695
1863
 
1696
1864
    def setUp(self):
1697
 
        tests.TestCaseInTempDir.setUp(self)
 
1865
        super(TestDirReader, self).setUp()
1698
1866
        self.overrideAttr(osutils,
1699
1867
                          '_selected_dir_reader', self._dir_reader_class())
1700
1868
 
1794
1962
        return filtered_dirblocks
1795
1963
 
1796
1964
    def test_walk_unicode_tree(self):
1797
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1965
        self.requireFeature(features.UnicodeFilenameFeature)
1798
1966
        tree, expected_dirblocks = self._get_unicode_tree()
1799
1967
        self.build_tree(tree)
1800
1968
        result = list(osutils._walkdirs_utf8('.'))
1801
1969
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1802
1970
 
1803
1971
    def test_symlink(self):
1804
 
        self.requireFeature(tests.SymlinkFeature)
1805
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1972
        self.requireFeature(features.SymlinkFeature)
 
1973
        self.requireFeature(features.UnicodeFilenameFeature)
1806
1974
        target = u'target\N{Euro Sign}'
1807
1975
        link_name = u'l\N{Euro Sign}nk'
1808
1976
        os.symlink(target, link_name)
1826
1994
    But prior python versions failed to properly encode the passed unicode
1827
1995
    string.
1828
1996
    """
1829
 
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
 
1997
    _test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
1830
1998
 
1831
1999
    def setUp(self):
1832
2000
        super(tests.TestCaseInTempDir, self).setUp()
1835
2003
        os.symlink(self.target, self.link)
1836
2004
 
1837
2005
    def test_os_readlink_link_encoding(self):
1838
 
        if sys.version_info < (2, 6):
1839
 
            self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1840
 
        else:
1841
 
            self.assertEquals(self.target,  os.readlink(self.link))
 
2006
        self.assertEqual(self.target,  os.readlink(self.link))
1842
2007
 
1843
2008
    def test_os_readlink_link_decoding(self):
1844
 
        self.assertEquals(self.target.encode(osutils._fs_enc),
 
2009
        self.assertEqual(self.target.encode(osutils._fs_enc),
1845
2010
                          os.readlink(self.link.encode(osutils._fs_enc)))
1846
2011
 
1847
2012
 
1856
2021
        self.assertIsInstance(concurrency, int)
1857
2022
 
1858
2023
    def test_local_concurrency_environment_variable(self):
1859
 
        os.environ['BZR_CONCURRENCY'] = '2'
 
2024
        self.overrideEnv('BZR_CONCURRENCY', '2')
1860
2025
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1861
 
        os.environ['BZR_CONCURRENCY'] = '3'
 
2026
        self.overrideEnv('BZR_CONCURRENCY', '3')
1862
2027
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1863
 
        os.environ['BZR_CONCURRENCY'] = 'foo'
 
2028
        self.overrideEnv('BZR_CONCURRENCY', 'foo')
1864
2029
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1865
2030
 
1866
2031
    def test_option_concurrency(self):
1867
 
        os.environ['BZR_CONCURRENCY'] = '1'
 
2032
        self.overrideEnv('BZR_CONCURRENCY', '1')
1868
2033
        self.run_bzr('rocks --concurrency 42')
1869
 
        # Command line overrides envrionment variable
1870
 
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1871
 
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
 
2034
        # Command line overrides environment variable
 
2035
        self.assertEqual('42', os.environ['BZR_CONCURRENCY'])
 
2036
        self.assertEqual(42, osutils.local_concurrency(use_cache=False))
1872
2037
 
1873
2038
 
1874
2039
class TestFailedToLoadExtension(tests.TestCase):
1887
2052
    def test_failure_to_load(self):
1888
2053
        self._try_loading()
1889
2054
        self.assertLength(1, osutils._extension_load_failures)
1890
 
        self.assertEquals(osutils._extension_load_failures[0],
 
2055
        self.assertEqual(osutils._extension_load_failures[0],
1891
2056
            "No module named _fictional_extension_py")
1892
2057
 
1893
2058
    def test_report_extension_load_failures_no_warning(self):
1910
2075
 
1911
2076
class TestTerminalWidth(tests.TestCase):
1912
2077
 
 
2078
    def setUp(self):
 
2079
        super(TestTerminalWidth, self).setUp()
 
2080
        self._orig_terminal_size_state = osutils._terminal_size_state
 
2081
        self._orig_first_terminal_size = osutils._first_terminal_size
 
2082
        self.addCleanup(self.restore_osutils_globals)
 
2083
        osutils._terminal_size_state = 'no_data'
 
2084
        osutils._first_terminal_size = None
 
2085
 
 
2086
    def restore_osutils_globals(self):
 
2087
        osutils._terminal_size_state = self._orig_terminal_size_state
 
2088
        osutils._first_terminal_size = self._orig_first_terminal_size
 
2089
 
1913
2090
    def replace_stdout(self, new):
1914
2091
        self.overrideAttr(sys, 'stdout', new)
1915
2092
 
1930
2107
    def test_defaults_to_BZR_COLUMNS(self):
1931
2108
        # BZR_COLUMNS is set by the test framework
1932
2109
        self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1933
 
        os.environ['BZR_COLUMNS'] = '12'
 
2110
        self.overrideEnv('BZR_COLUMNS', '12')
1934
2111
        self.assertEqual(12, osutils.terminal_width())
1935
2112
 
 
2113
    def test_BZR_COLUMNS_0_no_limit(self):
 
2114
        self.overrideEnv('BZR_COLUMNS', '0')
 
2115
        self.assertEqual(None, osutils.terminal_width())
 
2116
 
1936
2117
    def test_falls_back_to_COLUMNS(self):
1937
 
        del os.environ['BZR_COLUMNS']
 
2118
        self.overrideEnv('BZR_COLUMNS', None)
1938
2119
        self.assertNotEqual('42', os.environ['COLUMNS'])
1939
2120
        self.set_fake_tty()
1940
 
        os.environ['COLUMNS'] = '42'
 
2121
        self.overrideEnv('COLUMNS', '42')
1941
2122
        self.assertEqual(42, osutils.terminal_width())
1942
2123
 
1943
2124
    def test_tty_default_without_columns(self):
1944
 
        del os.environ['BZR_COLUMNS']
1945
 
        del os.environ['COLUMNS']
 
2125
        self.overrideEnv('BZR_COLUMNS', None)
 
2126
        self.overrideEnv('COLUMNS', None)
1946
2127
 
1947
2128
        def terminal_size(w, h):
1948
2129
            return 42, 42
1955
2136
        self.assertEqual(42, osutils.terminal_width())
1956
2137
 
1957
2138
    def test_non_tty_default_without_columns(self):
1958
 
        del os.environ['BZR_COLUMNS']
1959
 
        del os.environ['COLUMNS']
 
2139
        self.overrideEnv('BZR_COLUMNS', None)
 
2140
        self.overrideEnv('COLUMNS', None)
1960
2141
        self.replace_stdout(None)
1961
2142
        self.assertEqual(None, osutils.terminal_width())
1962
2143
 
1972
2153
        else:
1973
2154
            self.overrideAttr(termios, 'TIOCGWINSZ')
1974
2155
            del termios.TIOCGWINSZ
1975
 
        del os.environ['BZR_COLUMNS']
1976
 
        del os.environ['COLUMNS']
 
2156
        self.overrideEnv('BZR_COLUMNS', None)
 
2157
        self.overrideEnv('COLUMNS', None)
1977
2158
        # Whatever the result is, if we don't raise an exception, it's ok.
1978
2159
        osutils.terminal_width()
1979
2160
 
 
2161
 
1980
2162
class TestCreationOps(tests.TestCaseInTempDir):
1981
2163
    _test_needs_features = [features.chown_feature]
1982
2164
 
1983
2165
    def setUp(self):
1984
 
        tests.TestCaseInTempDir.setUp(self)
 
2166
        super(TestCreationOps, self).setUp()
1985
2167
        self.overrideAttr(os, 'chown', self._dummy_chown)
1986
2168
 
1987
2169
        # params set by call to _dummy_chown
1990
2172
    def _dummy_chown(self, path, uid, gid):
1991
2173
        self.path, self.uid, self.gid = path, uid, gid
1992
2174
 
1993
 
    def test_mkdir_with_ownership_chown(self):
1994
 
        """Ensure that osutils.mkdir_with_ownership chowns correctly with ownership_src.
1995
 
        """
1996
 
        ownsrc = '/'
1997
 
        osutils.mkdir_with_ownership('foo', ownsrc)
1998
 
 
1999
 
        s = os.stat(ownsrc)
2000
 
        self.assertEquals(self.path, 'foo')
2001
 
        self.assertEquals(self.uid, s.st_uid)
2002
 
        self.assertEquals(self.gid, s.st_gid)
2003
 
 
2004
 
    def test_open_with_ownership_chown(self):
2005
 
        """Ensure that osutils.open_with_ownership chowns correctly with ownership_src.
2006
 
        """
2007
 
        ownsrc = '/'
2008
 
        f = osutils.open_with_ownership('foo', 'w', ownership_src=ownsrc)
2009
 
 
2010
 
        # do a test write and close
2011
 
        f.write('hello')
2012
 
        f.close()
2013
 
 
2014
 
        s = os.stat(ownsrc)
2015
 
        self.assertEquals(self.path, 'foo')
2016
 
        self.assertEquals(self.uid, s.st_uid)
2017
 
        self.assertEquals(self.gid, s.st_gid)
2018
 
 
 
2175
    def test_copy_ownership_from_path(self):
 
2176
        """copy_ownership_from_path test with specified src."""
 
2177
        ownsrc = '/'
 
2178
        f = open('test_file', 'wt')
 
2179
        osutils.copy_ownership_from_path('test_file', ownsrc)
 
2180
 
 
2181
        s = os.stat(ownsrc)
 
2182
        self.assertEqual(self.path, 'test_file')
 
2183
        self.assertEqual(self.uid, s.st_uid)
 
2184
        self.assertEqual(self.gid, s.st_gid)
 
2185
 
 
2186
    def test_copy_ownership_nonesrc(self):
 
2187
        """copy_ownership_from_path test with src=None."""
 
2188
        f = open('test_file', 'wt')
 
2189
        # should use parent dir for permissions
 
2190
        osutils.copy_ownership_from_path('test_file')
 
2191
 
 
2192
        s = os.stat('..')
 
2193
        self.assertEqual(self.path, 'test_file')
 
2194
        self.assertEqual(self.uid, s.st_uid)
 
2195
        self.assertEqual(self.gid, s.st_gid)
 
2196
 
 
2197
 
 
2198
class TestPathFromEnviron(tests.TestCase):
 
2199
 
 
2200
    def test_is_unicode(self):
 
2201
        self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
 
2202
        path = osutils.path_from_environ('BZR_TEST_PATH')
 
2203
        self.assertIsInstance(path, unicode)
 
2204
        self.assertEqual(u'./anywhere at all/', path)
 
2205
 
 
2206
    def test_posix_path_env_ascii(self):
 
2207
        self.overrideEnv('BZR_TEST_PATH', '/tmp')
 
2208
        home = osutils._posix_path_from_environ('BZR_TEST_PATH')
 
2209
        self.assertIsInstance(home, unicode)
 
2210
        self.assertEqual(u'/tmp', home)
 
2211
 
 
2212
    def test_posix_path_env_unicode(self):
 
2213
        self.requireFeature(features.ByteStringNamedFilesystem)
 
2214
        self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
 
2215
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
 
2216
        self.assertEqual(u'/home/\xa7test',
 
2217
            osutils._posix_path_from_environ('BZR_TEST_PATH'))
 
2218
        osutils._fs_enc = "iso8859-5"
 
2219
        self.assertEqual(u'/home/\u0407test',
 
2220
            osutils._posix_path_from_environ('BZR_TEST_PATH'))
 
2221
        osutils._fs_enc = "utf-8"
 
2222
        self.assertRaises(errors.BadFilenameEncoding,
 
2223
            osutils._posix_path_from_environ, 'BZR_TEST_PATH')
 
2224
 
 
2225
 
 
2226
class TestGetHomeDir(tests.TestCase):
 
2227
 
 
2228
    def test_is_unicode(self):
 
2229
        home = osutils._get_home_dir()
 
2230
        self.assertIsInstance(home, unicode)
 
2231
 
 
2232
    def test_posix_homeless(self):
 
2233
        self.overrideEnv('HOME', None)
 
2234
        home = osutils._get_home_dir()
 
2235
        self.assertIsInstance(home, unicode)
 
2236
 
 
2237
    def test_posix_home_ascii(self):
 
2238
        self.overrideEnv('HOME', '/home/test')
 
2239
        home = osutils._posix_get_home_dir()
 
2240
        self.assertIsInstance(home, unicode)
 
2241
        self.assertEqual(u'/home/test', home)
 
2242
 
 
2243
    def test_posix_home_unicode(self):
 
2244
        self.requireFeature(features.ByteStringNamedFilesystem)
 
2245
        self.overrideEnv('HOME', '/home/\xa7test')
 
2246
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
 
2247
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
 
2248
        osutils._fs_enc = "iso8859-5"
 
2249
        self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
 
2250
        osutils._fs_enc = "utf-8"
 
2251
        self.assertRaises(errors.BadFilenameEncoding,
 
2252
            osutils._posix_get_home_dir)
 
2253
 
 
2254
 
 
2255
class TestGetuserUnicode(tests.TestCase):
 
2256
 
 
2257
    def test_is_unicode(self):
 
2258
        user = osutils.getuser_unicode()
 
2259
        self.assertIsInstance(user, unicode)
 
2260
 
 
2261
    def envvar_to_override(self):
 
2262
        if sys.platform == "win32":
 
2263
            # Disable use of platform calls on windows so envvar is used
 
2264
            self.overrideAttr(win32utils, 'has_ctypes', False)
 
2265
            return 'USERNAME' # only variable used on windows
 
2266
        return 'LOGNAME' # first variable checked by getpass.getuser()
 
2267
 
 
2268
    def test_ascii_user(self):
 
2269
        self.overrideEnv(self.envvar_to_override(), 'jrandom')
 
2270
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
 
2271
 
 
2272
    def test_unicode_user(self):
 
2273
        ue = osutils.get_user_encoding()
 
2274
        uni_val, env_val = tests.probe_unicode_in_user_encoding()
 
2275
        if uni_val is None:
 
2276
            raise tests.TestSkipped(
 
2277
                'Cannot find a unicode character that works in encoding %s'
 
2278
                % (osutils.get_user_encoding(),))
 
2279
        uni_username = u'jrandom' + uni_val
 
2280
        encoded_username = uni_username.encode(ue)
 
2281
        self.overrideEnv(self.envvar_to_override(), encoded_username)
 
2282
        self.assertEqual(uni_username, osutils.getuser_unicode())
 
2283
 
 
2284
 
 
2285
class TestBackupNames(tests.TestCase):
 
2286
 
 
2287
    def setUp(self):
 
2288
        super(TestBackupNames, self).setUp()
 
2289
        self.backups = []
 
2290
 
 
2291
    def backup_exists(self, name):
 
2292
        return name in self.backups
 
2293
 
 
2294
    def available_backup_name(self, name):
 
2295
        backup_name = osutils.available_backup_name(name, self.backup_exists)
 
2296
        self.backups.append(backup_name)
 
2297
        return backup_name
 
2298
 
 
2299
    def assertBackupName(self, expected, name):
 
2300
        self.assertEqual(expected, self.available_backup_name(name))
 
2301
 
 
2302
    def test_empty(self):
 
2303
        self.assertBackupName('file.~1~', 'file')
 
2304
 
 
2305
    def test_existing(self):
 
2306
        self.available_backup_name('file')
 
2307
        self.available_backup_name('file')
 
2308
        self.assertBackupName('file.~3~', 'file')
 
2309
        # Empty slots are found, this is not a strict requirement and may be
 
2310
        # revisited if we test against all implementations.
 
2311
        self.backups.remove('file.~2~')
 
2312
        self.assertBackupName('file.~2~', 'file')
 
2313
 
 
2314
 
 
2315
class TestFindExecutableInPath(tests.TestCase):
 
2316
 
 
2317
    def test_windows(self):
 
2318
        if sys.platform != 'win32':
 
2319
            raise tests.TestSkipped('test requires win32')
 
2320
        self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
 
2321
        self.assertTrue(
 
2322
            osutils.find_executable_on_path('explorer.exe') is not None)
 
2323
        self.assertTrue(
 
2324
            osutils.find_executable_on_path('EXPLORER.EXE') is not None)
 
2325
        self.assertTrue(
 
2326
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2327
        self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
 
2328
        
 
2329
    def test_windows_app_path(self):
 
2330
        if sys.platform != 'win32':
 
2331
            raise tests.TestSkipped('test requires win32')
 
2332
        # Override PATH env var so that exe can only be found on App Path
 
2333
        self.overrideEnv('PATH', '')
 
2334
        # Internt Explorer is always registered in the App Path
 
2335
        self.assertTrue(osutils.find_executable_on_path('iexplore') is not None)
 
2336
 
 
2337
    def test_other(self):
 
2338
        if sys.platform == 'win32':
 
2339
            raise tests.TestSkipped('test requires non-win32')
 
2340
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
 
2341
        self.assertTrue(
 
2342
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2343
 
 
2344
 
 
2345
class TestEnvironmentErrors(tests.TestCase):
 
2346
    """Test handling of environmental errors"""
 
2347
 
 
2348
    def test_is_oserror(self):
 
2349
        self.assertTrue(osutils.is_environment_error(
 
2350
            OSError(errno.EINVAL, "Invalid parameter")))
 
2351
 
 
2352
    def test_is_ioerror(self):
 
2353
        self.assertTrue(osutils.is_environment_error(
 
2354
            IOError(errno.EINVAL, "Invalid parameter")))
 
2355
 
 
2356
    def test_is_socket_error(self):
 
2357
        self.assertTrue(osutils.is_environment_error(
 
2358
            socket.error(errno.EINVAL, "Invalid parameter")))
 
2359
 
 
2360
    def test_is_select_error(self):
 
2361
        self.assertTrue(osutils.is_environment_error(
 
2362
            select.error(errno.EINVAL, "Invalid parameter")))
 
2363
 
 
2364
    def test_is_pywintypes_error(self):
 
2365
        self.requireFeature(features.pywintypes)
 
2366
        import pywintypes
 
2367
        self.assertTrue(osutils.is_environment_error(
 
2368
            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))