source: test/test_error.py @ 1662:db924600a3c3

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