root/tags/release2_2/_test_ftp_path.py

Revision 633, 5.1 kB (checked in by schwa, 2 years ago)
Merged in changes from feature branch add_stat_caching:

svn merge -r544:580 svn://ftputil.sschwarzer.net/ftputil/branches/add_stat_caching .

The branch shouldn't be used anymore.
  • Property svn:mime-type set to text/x-python
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 # Copyright (C) 2003-2004, Stefan Schwarzer
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # - Redistributions of source code must retain the above copyright
9 #   notice, this list of conditions and the following disclaimer.
10 #
11 # - Redistributions in binary form must reproduce the above copyright
12 #   notice, this list of conditions and the following disclaimer in the
13 #   documentation and/or other materials provided with the distribution.
14 #
15 # - Neither the name of the above author nor the names of the
16 #   contributors to the software may be used to endorse or promote
17 #   products derived from this software without specific prior written
18 #   permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 # $Id$
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 # mock session, used for testing an inaccessible login directory
49 class SessionWithInaccessibleLoginDirectory(_mock_ftplib.MockSession):
50     def cwd(self, dir):
51         # assume that `dir` is the inaccessible login directory
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         # test a path which isn't there
63         self.failIf(host.path.isdir('notthere'))
64         self.failIf(host.path.isfile('notthere'))
65         self.failIf(host.path.islink('notthere'))
66         # test a directory
67         self.failUnless(host.path.isdir(testdir))
68         self.failIf(host.path.isfile(testdir))
69         self.failIf(host.path.islink(testdir))
70         # test a file
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         # test a link
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         # test a file containing spaces
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         # test a path which isn't there
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         # regular use of `exists`
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         # "abnormal" failure
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
Note: See TracBrowser for help on using the browser.