File: test_error.py

package info (click to toggle)
python-ftputil 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 848 kB
  • sloc: python: 3,308; makefile: 3
file content (91 lines) | stat: -rw-r--r-- 3,004 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# encoding: utf-8
# Copyright (C) 2002-2016, Stefan Schwarzer <sschwarzer@sschwarzer.net>
# and ftputil contributors (see `doc/contributors.txt`)
# See the file LICENSE for licensing terms.

from __future__ import unicode_literals

import ftplib

import pytest

import ftputil.error


class TestFTPErrorArguments(object):
    """
    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(object):

    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_os_error_non_ascii_server_message(self):
        """
        Test that we don't get a `UnicodeDecodeError` if the server
        sends a message containing non-ASCII characters.
        """
        # See ticket #77.
        message = \
          ftputil.tool.as_bytes(
            "Não é possível criar um arquivo já existente.")
        with pytest.raises(ftputil.error.PermanentError):
            with ftputil.error.ftplib_error_to_ftp_os_error:
                raise ftplib.error_perm(message)

    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.
            host = ftputil.FTPHost("localhost:21", "", "")
        exc = exc_info.value
        # The error message may be different for different Python
        # versions.
        assert (
          "No address associated with hostname" in str(exc) or
          "Name or service not known" in str(exc))
        del exc_info