~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2012-01-27 18:41:20 UTC
  • mto: This revision was merged to the branch mainline in revision 6449.
  • Revision ID: jelmer@samba.org-20120127184120-osvbbiwijy58hsah
remove unnecessary code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2015 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
23
23
import select
24
24
import socket
25
25
import sys
26
 
import tempfile
27
26
import time
28
27
 
29
28
from bzrlib import (
182
181
        shape = sorted(os.listdir('.'))
183
182
        self.assertEquals(['A', 'B'], shape)
184
183
 
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)
193
 
 
194
184
 
195
185
class TestRandChars(tests.TestCase):
196
186
 
437
427
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
438
428
 
439
429
 
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
 
 
483
430
class TestLinks(tests.TestCaseInTempDir):
484
431
 
485
432
    def test_dereference_path(self):
598
545
    """Test pumpfile method."""
599
546
 
600
547
    def setUp(self):
601
 
        super(TestPumpFile, self).setUp()
 
548
        tests.TestCase.setUp(self)
602
549
        # create a test datablock
603
550
        self.block_size = 512
604
551
        pattern = '0123456789ABCDEF'
872
819
        self.assertEqual(None, osutils.safe_file_id(None))
873
820
 
874
821
 
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
822
class TestPosixFuncs(tests.TestCase):
915
823
    """Test that the posix version of normpath returns an appropriate path
916
824
       when used with 2 leading slashes."""
925
833
    """Test that _win32 versions of os utilities return appropriate paths."""
926
834
 
927
835
    def test_abspath(self):
928
 
        self.requireFeature(features.win32_feature)
929
836
        self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
930
837
        self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
931
838
        self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
944
851
                         osutils._win32_pathjoin('path/to', 'C:/foo'))
945
852
        self.assertEqual('path/to/foo',
946
853
                         osutils._win32_pathjoin('path/to/', '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,
 
854
        self.assertEqual('/foo',
954
855
                         osutils._win32_pathjoin('C:/path/to/', '/foo'))
955
 
        self.assertEqual(expected,
 
856
        self.assertEqual('/foo',
956
857
                         osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
957
858
 
958
859
    def test_normpath(self):
978
879
        self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
979
880
 
980
881
    def test_win98_abspath(self):
981
 
        self.requireFeature(features.win32_feature)
982
882
        # absolute path
983
883
        self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
984
884
        self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
1862
1762
    _native_to_unicode = None
1863
1763
 
1864
1764
    def setUp(self):
1865
 
        super(TestDirReader, self).setUp()
 
1765
        tests.TestCaseInTempDir.setUp(self)
1866
1766
        self.overrideAttr(osutils,
1867
1767
                          '_selected_dir_reader', self._dir_reader_class())
1868
1768
 
2076
1976
class TestTerminalWidth(tests.TestCase):
2077
1977
 
2078
1978
    def setUp(self):
2079
 
        super(TestTerminalWidth, self).setUp()
 
1979
        tests.TestCase.setUp(self)
2080
1980
        self._orig_terminal_size_state = osutils._terminal_size_state
2081
1981
        self._orig_first_terminal_size = osutils._first_terminal_size
2082
1982
        self.addCleanup(self.restore_osutils_globals)
2163
2063
    _test_needs_features = [features.chown_feature]
2164
2064
 
2165
2065
    def setUp(self):
2166
 
        super(TestCreationOps, self).setUp()
 
2066
        tests.TestCaseInTempDir.setUp(self)
2167
2067
        self.overrideAttr(os, 'chown', self._dummy_chown)
2168
2068
 
2169
2069
        # params set by call to _dummy_chown
2195
2095
        self.assertEquals(self.gid, s.st_gid)
2196
2096
 
2197
2097
 
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
2098
class TestGetuserUnicode(tests.TestCase):
2256
2099
 
2257
2100
    def test_is_unicode(self):
2325
2168
        self.assertTrue(
2326
2169
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2327
2170
        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
2171
 
2337
2172
    def test_other(self):
2338
2173
        if sys.platform == 'win32':