source: test/test_error.py @ 1719:560867ae70f0

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