| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
import ftplib |
|---|
| 35 |
import unittest |
|---|
| 36 |
|
|---|
| 37 |
import _mock_ftplib |
|---|
| 38 |
import _test_base |
|---|
| 39 |
import ftp_error |
|---|
| 40 |
import ftputil |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
class FailingFTPHost(ftputil.FTPHost): |
|---|
| 44 |
def _dir(self, path): |
|---|
| 45 |
raise ftp_error.FTPOSError("simulate a failure, e. g. timeout") |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
class SessionWithInaccessibleLoginDirectory(_mock_ftplib.MockSession): |
|---|
| 50 |
def cwd(self, dir): |
|---|
| 51 |
|
|---|
| 52 |
raise ftplib.error_perm("can't change into this directory") |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
class TestPath(unittest.TestCase): |
|---|
| 56 |
"""Test operations in `FTPHost.path`.""" |
|---|
| 57 |
def test_regular_isdir_isfile_islink(self): |
|---|
| 58 |
"""Test regular `FTPHost._Path.isdir/isfile/islink`.""" |
|---|
| 59 |
testdir = '/home/sschwarzer' |
|---|
| 60 |
host = _test_base.ftp_host_factory() |
|---|
| 61 |
host.chdir(testdir) |
|---|
| 62 |
|
|---|
| 63 |
self.failIf(host.path.isdir('notthere')) |
|---|
| 64 |
self.failIf(host.path.isfile('notthere')) |
|---|
| 65 |
self.failIf(host.path.islink('notthere')) |
|---|
| 66 |
|
|---|
| 67 |
self.failUnless(host.path.isdir(testdir)) |
|---|
| 68 |
self.failIf(host.path.isfile(testdir)) |
|---|
| 69 |
self.failIf(host.path.islink(testdir)) |
|---|
| 70 |
|
|---|
| 71 |
testfile = '/home/sschwarzer/index.html' |
|---|
| 72 |
self.failIf(host.path.isdir(testfile)) |
|---|
| 73 |
self.failUnless(host.path.isfile(testfile)) |
|---|
| 74 |
self.failIf(host.path.islink(testfile)) |
|---|
| 75 |
|
|---|
| 76 |
testlink = '/home/sschwarzer/osup' |
|---|
| 77 |
self.failIf(host.path.isdir(testlink)) |
|---|
| 78 |
self.failIf(host.path.isfile(testlink)) |
|---|
| 79 |
self.failUnless(host.path.islink(testlink)) |
|---|
| 80 |
|
|---|
| 81 |
def test_workaround_for_spaces(self): |
|---|
| 82 |
"""Test whether the workaround for space-containing paths is used.""" |
|---|
| 83 |
testdir = '/home/sschwarzer' |
|---|
| 84 |
host = _test_base.ftp_host_factory() |
|---|
| 85 |
host.chdir(testdir) |
|---|
| 86 |
|
|---|
| 87 |
testfile = '/home/dir with spaces/file with spaces' |
|---|
| 88 |
self.failIf(host.path.isdir(testfile)) |
|---|
| 89 |
self.failUnless(host.path.isfile(testfile)) |
|---|
| 90 |
self.failIf(host.path.islink(testfile)) |
|---|
| 91 |
|
|---|
| 92 |
def test_inaccessible_home_directory_and_whitespace_workaround(self): |
|---|
| 93 |
"Test combination of inaccessible home directory + whitespace in path." |
|---|
| 94 |
host = _test_base.ftp_host_factory( |
|---|
| 95 |
session_factory=SessionWithInaccessibleLoginDirectory) |
|---|
| 96 |
self.assertRaises(ftp_error.InaccessibleLoginDirError, |
|---|
| 97 |
host._dir, '/home dir') |
|---|
| 98 |
|
|---|
| 99 |
def test_abnormal_isdir_isfile_islink(self): |
|---|
| 100 |
"""Test abnormal `FTPHost._Path.isdir/isfile/islink`.""" |
|---|
| 101 |
testdir = '/home/sschwarzer' |
|---|
| 102 |
host = _test_base.ftp_host_factory(ftp_host_class=FailingFTPHost) |
|---|
| 103 |
host.chdir(testdir) |
|---|
| 104 |
|
|---|
| 105 |
self.assertRaises(ftp_error.FTPOSError, host.path.isdir, "index.html") |
|---|
| 106 |
self.assertRaises(ftp_error.FTPOSError, host.path.isfile, "index.html") |
|---|
| 107 |
self.assertRaises(ftp_error.FTPOSError, host.path.islink, "index.html") |
|---|
| 108 |
|
|---|
| 109 |
def test_exists(self): |
|---|
| 110 |
"""Test if "abnormal" FTP errors come through `path.exists`.""" |
|---|
| 111 |
|
|---|
| 112 |
testdir = '/home/sschwarzer' |
|---|
| 113 |
host = _test_base.ftp_host_factory() |
|---|
| 114 |
host.chdir(testdir) |
|---|
| 115 |
self.assertEqual(host.path.exists("index.html"), True) |
|---|
| 116 |
self.assertEqual(host.path.exists("notthere"), False) |
|---|
| 117 |
|
|---|
| 118 |
host = _test_base.ftp_host_factory(ftp_host_class=FailingFTPHost) |
|---|
| 119 |
self.assertRaises(ftp_error.FTPOSError, host.path.exists, "index.html") |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
if __name__ == '__main__': |
|---|
| 123 |
unittest.main() |
|---|
| 124 |
|
|---|