source: test/test_error.py

Last change on this file was 1935:38acf28f3905, checked in by Stefan Schwarzer <sschwarzer@…>, 3 months ago
Format docstrings and comments Reformat docstrings and comments to 80 characters. Traditionally, the margin for the code was at 79 columns, so I chose a bit less for the comments. With the switch to Black for formatting the right margin is now at nearly 90 columns, so it makes sense to also allow a greater right margin for docstrings and comments. Lines still don't get too long because the formatted text often starts only in column 5 or 9.
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 unicode
15    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` argument.
36        """
37        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
38            with ftputil.error.ftplib_error_to_ftp_os_error:
39                self.callee()
40        exc = exc_info.value
41        assert not (exc.args and isinstance(exc.args[0], ftplib.error_perm))
42        del exc_info
43
44    def test_ftplib_error_to_ftp_io_error(self):
45        """
46        Ensure the `ftplib` exception isn't used as `FTPIOError` argument.
47        """
48        with pytest.raises(ftputil.error.FTPIOError) as exc_info:
49            with ftputil.error.ftplib_error_to_ftp_io_error:
50                self.callee()
51        exc = exc_info.value
52        assert not (exc.args and isinstance(exc.args[0], ftplib.error_perm))
53        del exc_info
54
55    def test_error_message_reuse(self):
56        """
57        Test if the error message string is retained if the caught exception
58        has more than one element in `args`.
59        """
60        # See ticket #76.
61        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
62            # Format "host:port" doesn't work. The use here is intentional.
63            host = ftputil.FTPHost("localhost:21", "", "")
64        exc = exc_info.value
65        # The error message may be different for different Python versions.
66        assert "No address associated with hostname" in str(
67            exc
68        ) or "Name or service not known" in str(exc)
69        del exc_info
Note: See TracBrowser for help on using the repository browser.