source: test/test_error.py

Last change on this file was 1901:616e46ca84e0, checked in by Stefan Schwarzer <sschwarzer@…>, 2 weeks ago
Simplify conversion of original exception In Python 3, exception messages are always unicode strings (`str`), so special handling for byte string messages is no longer necessary. When I entered ticket #132, I was under the impression that `original_exception` was used for exeption chaining and that it could be removed now. However, `original_exception` is only used to apply `str(original_exception)` and this use makes sense.
File size: 2.3 KB
Line 
1# Copyright (C) 2002-2020, 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    def callee(self):
31        raise ftplib.error_perm()
32
33    def test_ftplib_error_to_ftp_os_error(self):
34        """
35        Ensure the `ftplib` exception isn't used as `FTPOSError`
36        argument.
37        """
38        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
39            with ftputil.error.ftplib_error_to_ftp_os_error:
40                self.callee()
41        exc = exc_info.value
42        assert not (exc.args and isinstance(exc.args[0], ftplib.error_perm))
43        del exc_info
44
45    def test_ftplib_error_to_ftp_io_error(self):
46        """
47        Ensure the `ftplib` exception isn't used as `FTPIOError`
48        argument.
49        """
50        with pytest.raises(ftputil.error.FTPIOError) as exc_info:
51            with ftputil.error.ftplib_error_to_ftp_io_error:
52                self.callee()
53        exc = exc_info.value
54        assert not (exc.args and isinstance(exc.args[0], ftplib.error_perm))
55        del exc_info
56
57    def test_error_message_reuse(self):
58        """
59        Test if the error message string is retained if the caught
60        exception has more than one element in `args`.
61        """
62        # See ticket #76.
63        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
64            # Format "host:port" doesn't work. The use here is intentional.
65            host = ftputil.FTPHost("localhost:21", "", "")
66        exc = exc_info.value
67        # The error message may be different for different Python
68        # versions.
69        assert "No address associated with hostname" in str(
70            exc
71        ) or "Name or service not known" in str(exc)
72        del exc_info
Note: See TracBrowser for help on using the repository browser.