1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2005-2015 Canonical Ltd
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
181
182
shape = sorted(os.listdir('.'))
182
183
self.assertEquals(['A', 'B'], shape)
185
def test_rename_exception(self):
187
osutils.rename('nonexistent_path', 'different_nonexistent_path')
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)
185
195
class TestRandChars(tests.TestCase):
427
437
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
440
class TestFdatasync(tests.TestCaseInTempDir):
442
def do_fdatasync(self):
443
f = tempfile.NamedTemporaryFile()
444
osutils.fdatasync(f.fileno())
448
def raise_eopnotsupp(*args, **kwargs):
449
raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
452
def raise_enotsup(*args, **kwargs):
453
raise IOError(errno.ENOTSUP, os.strerror(errno.ENOTSUP))
455
def test_fdatasync_handles_system_function(self):
456
self.overrideAttr(os, "fdatasync")
459
def test_fdatasync_handles_no_fdatasync_no_fsync(self):
460
self.overrideAttr(os, "fdatasync")
461
self.overrideAttr(os, "fsync")
464
def test_fdatasync_handles_no_EOPNOTSUPP(self):
465
self.overrideAttr(errno, "EOPNOTSUPP")
468
def test_fdatasync_catches_ENOTSUP(self):
469
enotsup = getattr(errno, "ENOTSUP", None)
471
raise tests.TestNotApplicable("No ENOTSUP on this platform")
472
self.overrideAttr(os, "fdatasync", self.raise_enotsup)
475
def test_fdatasync_catches_EOPNOTSUPP(self):
476
enotsup = getattr(errno, "EOPNOTSUPP", None)
478
raise tests.TestNotApplicable("No EOPNOTSUPP on this platform")
479
self.overrideAttr(os, "fdatasync", self.raise_eopnotsupp)
430
483
class TestLinks(tests.TestCaseInTempDir):
432
485
def test_dereference_path(self):
545
598
"""Test pumpfile method."""
548
tests.TestCase.setUp(self)
601
super(TestPumpFile, self).setUp()
549
602
# create a test datablock
550
603
self.block_size = 512
551
604
pattern = '0123456789ABCDEF'
819
872
self.assertEqual(None, osutils.safe_file_id(None))
875
class TestSendAll(tests.TestCase):
877
def test_send_with_disconnected_socket(self):
878
class DisconnectedSocket(object):
879
def __init__(self, err):
881
def send(self, content):
885
# All of these should be treated as ConnectionReset
887
for err_cls in (IOError, socket.error):
888
for errnum in osutils._end_of_stream_errors:
889
errs.append(err_cls(errnum))
891
sock = DisconnectedSocket(err)
892
self.assertRaises(errors.ConnectionReset,
893
osutils.send_all, sock, 'some more content')
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):
902
def send(self, bytes):
904
if self.call_count > 100:
905
# Prevent the test suite from hanging
906
raise RuntimeError('too many calls')
908
sock = NoSendingSocket()
909
self.assertRaises(errors.ConnectionReset,
910
osutils.send_all, sock, 'content')
911
self.assertEqual(1, sock.call_count)
822
914
class TestPosixFuncs(tests.TestCase):
823
915
"""Test that the posix version of normpath returns an appropriate path
824
916
when used with 2 leading slashes."""
833
925
"""Test that _win32 versions of os utilities return appropriate paths."""
835
927
def test_abspath(self):
928
self.requireFeature(features.win32_feature)
836
929
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
837
930
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
838
931
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
851
944
osutils._win32_pathjoin('path/to', 'C:/foo'))
852
945
self.assertEqual('path/to/foo',
853
946
osutils._win32_pathjoin('path/to/', 'foo'))
854
self.assertEqual('/foo',
948
def test_pathjoin_late_bugfix(self):
949
if sys.version_info < (2, 7, 6):
953
self.assertEqual(expected,
855
954
osutils._win32_pathjoin('C:/path/to/', '/foo'))
856
self.assertEqual('/foo',
955
self.assertEqual(expected,
857
956
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
859
958
def test_normpath(self):
879
978
self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
881
980
def test_win98_abspath(self):
981
self.requireFeature(features.win32_feature)
883
983
self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
884
984
self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
1762
1862
_native_to_unicode = None
1764
1864
def setUp(self):
1765
tests.TestCaseInTempDir.setUp(self)
1865
super(TestDirReader, self).setUp()
1766
1866
self.overrideAttr(osutils,
1767
1867
'_selected_dir_reader', self._dir_reader_class())
1976
2076
class TestTerminalWidth(tests.TestCase):
1978
2078
def setUp(self):
1979
tests.TestCase.setUp(self)
2079
super(TestTerminalWidth, self).setUp()
1980
2080
self._orig_terminal_size_state = osutils._terminal_size_state
1981
2081
self._orig_first_terminal_size = osutils._first_terminal_size
1982
2082
self.addCleanup(self.restore_osutils_globals)
2063
2163
_test_needs_features = [features.chown_feature]
2065
2165
def setUp(self):
2066
tests.TestCaseInTempDir.setUp(self)
2166
super(TestCreationOps, self).setUp()
2067
2167
self.overrideAttr(os, 'chown', self._dummy_chown)
2069
2169
# params set by call to _dummy_chown
2225
2325
self.assertTrue(
2226
2326
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2227
2327
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
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)
2229
2337
def test_other(self):
2230
2338
if sys.platform == 'win32':