~sschwarzer/ftputil

ftputil/test/test_error.py -rw-r--r-- 2.2 KiB
77f2ca24Stefan Schwarzer Move item "Push to repository" 27 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright (C) 2002-2020, Stefan Schwarzer <sschwarzer@sschwarzer.net>
# and ftputil contributors (see `doc/contributors.txt`)
# See the file LICENSE for licensing terms.

import ftplib
import socket

import pytest

import ftputil.error


class TestFTPErrorArguments:
    """
    The `*Error` constructors should accept either a byte string or a unicode
    string.
    """

    def test_bytestring_argument(self):
        # An umlaut as latin-1 character
        io_error = ftputil.error.FTPIOError(b"\xe4")
        os_error = ftputil.error.FTPOSError(b"\xe4")

    def test_unicode_argument(self):
        # An umlaut as unicode character
        io_error = ftputil.error.FTPIOError("\xe4")
        os_error = ftputil.error.FTPOSError("\xe4")


class TestErrorConversion:
    def callee(self):
        raise ftplib.error_perm()

    def test_ftplib_error_to_ftp_os_error(self):
        """
        Ensure the `ftplib` exception isn't used as `FTPOSError` argument.
        """
        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
            with ftputil.error.ftplib_error_to_ftp_os_error:
                self.callee()
        exc = exc_info.value
        assert not (exc.args and isinstance(exc.args[0], ftplib.error_perm))
        del exc_info

    def test_ftplib_error_to_ftp_io_error(self):
        """
        Ensure the `ftplib` exception isn't used as `FTPIOError` argument.
        """
        with pytest.raises(ftputil.error.FTPIOError) as exc_info:
            with ftputil.error.ftplib_error_to_ftp_io_error:
                self.callee()
        exc = exc_info.value
        assert not (exc.args and isinstance(exc.args[0], ftplib.error_perm))
        del exc_info

    def test_error_message_reuse(self):
        """
        Test if the error message string is retained if the caught exception
        has more than one element in `args`.
        """
        # See ticket #76.
        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
            # Format "host:port" doesn't work. The use here is intentional.
            host = ftputil.FTPHost("localhost:21", "", "")
        exc = exc_info.value
        assert isinstance(exc.__cause__, socket.gaierror)
        assert exc.__cause__.errno == socket.EAI_NONAME
        del exc_info