source: test/test_error.py @ 1718:8bed138bc404

Last change on this file since 1718:8bed138bc404 was 1718:8bed138bc404, checked in by Stefan Schwarzer <sschwarzer@…>, 6 months ago
Don't inherit from `object` This is no longer needed because we're targeting Python 3 only now.
File size: 2.9 KB
Line 
1# encoding: utf-8
2# Copyright (C) 2002-2018, Stefan Schwarzer <sschwarzer@sschwarzer.net>
3# and ftputil contributors (see `doc/contributors.txt`)
4# See the file LICENSE for licensing terms.
5
6import ftplib
7
8import pytest
9
10import ftputil.error
11
12
13class TestFTPErrorArguments:
14    """
15    The `*Error` constructors should accept either a byte string or a
16    unicode string.
17    """
18
19    def test_bytestring_argument(self):
20        # An umlaut as latin-1 character
21        io_error = ftputil.error.FTPIOError(b"\xe4")
22        os_error = ftputil.error.FTPOSError(b"\xe4")
23
24    def test_unicode_argument(self):
25        # An umlaut as unicode character
26        io_error = ftputil.error.FTPIOError("\xe4")
27        os_error = ftputil.error.FTPOSError("\xe4")
28
29
30class TestErrorConversion:
31
32    def callee(self):
33        raise ftplib.error_perm()
34
35    def test_ftplib_error_to_ftp_os_error(self):
36        """
37        Ensure the `ftplib` exception isn't used as `FTPOSError`
38        argument.
39        """
40        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
41            with ftputil.error.ftplib_error_to_ftp_os_error:
42                self.callee()
43        exc = exc_info.value
44        assert not (exc.args and 
45                    isinstance(exc.args[0], ftplib.error_perm))
46        del exc_info
47
48    def test_ftplib_error_to_ftp_os_error_non_ascii_server_message(self):
49        """
50        Test that we don't get a `UnicodeDecodeError` if the server
51        sends a message containing non-ASCII characters.
52        """
53        # See ticket #77.
54        message = \
55          ftputil.tool.as_bytes(
56            "Não é possível criar um arquivo já existente.")
57        with pytest.raises(ftputil.error.PermanentError):
58            with ftputil.error.ftplib_error_to_ftp_os_error:
59                raise ftplib.error_perm(message)
60
61    def test_ftplib_error_to_ftp_io_error(self):
62        """
63        Ensure the `ftplib` exception isn't used as `FTPIOError`
64        argument.
65        """
66        with pytest.raises(ftputil.error.FTPIOError) as exc_info:
67            with ftputil.error.ftplib_error_to_ftp_io_error:
68                self.callee()
69        exc = exc_info.value
70        assert not (exc.args and
71                    isinstance(exc.args[0], ftplib.error_perm))
72        del exc_info
73
74    def test_error_message_reuse(self):
75        """
76        Test if the error message string is retained if the caught
77        exception has more than one element in `args`.
78        """
79        # See ticket #76.
80        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
81            # Format "host:port" doesn't work.
82            host = ftputil.FTPHost("localhost:21", "", "")
83        exc = exc_info.value
84        # The error message may be different for different Python
85        # versions.
86        assert (
87          "No address associated with hostname" in str(exc) or
88          "Name or service not known" in str(exc))
89        del exc_info
Note: See TracBrowser for help on using the repository browser.