Changeset 1673:2cb5fa8e9206


Ignore:
Timestamp:
Oct 28, 2017, 5:51:02 PM (14 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
amend_source:
4a3e30553e84b0f388b4ab4845d7b35dfdb91b63
Message:
Add tests for the `_available_child` call

Make the `pwd` method of child sessions raise an exception to test if
it's caught and a new child session created.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_file.py

    r1663 r1673  
    229229        with pytest.raises(ftputil.error.FTPIOError):
    230230            host.open("notthere", "r")
     231
     232
     233class TestAvailableChild(object):
     234
     235    def _failing_pwd(self, exception_class):
     236        """
     237        Return a function that will be used instead of the
     238        `session.pwd` and will raise the exception
     239        `exception_to_raise`.
     240        """
     241        def new_pwd():
     242            raise exception_class("")
     243        return new_pwd
     244
     245    def _test_with_pwd_error(self, exception_class):
     246        """
     247        Test if reusing a child session fails because of
     248        `child_host._session.pwd` raising an exception of type
     249        `exception_class`.
     250        """
     251        host = test_base.ftp_host_factory()
     252        # Implicitly create a child session.
     253        with host.open("/home/older") as _:
     254            pass
     255        assert len(host._children) == 1
     256        # Make sure reusing the previous child session will fail.
     257        host._children[0]._session.pwd = self._failing_pwd(exception_class)
     258        # Try to create a new file. Since `pwd` now raises an
     259        # exception, a new child session should be created.
     260        with host.open("home/older") as _:
     261            pass
     262        assert len(host._children) == 2
     263
     264    def test_pwd_with_error_temp(self):
     265        """
     266        Test if an `error_temp` in `_session.pwd` skips the child
     267        session.
     268        """
     269        self._test_with_pwd_error(ftplib.error_temp)
     270
     271    def test_pwd_with_error_reply(self):
     272        """
     273        Test if an `error_reply` in `_session.pwd` skips the child
     274        session.
     275        """
     276        self._test_with_pwd_error(ftplib.error_reply)
     277
     278    def test_pwd_with_OSError(self):
     279        """
     280        Test if an `OSError` in `_session.pwd` skips the child
     281        session.
     282        """
     283        self._test_with_pwd_error(OSError)
Note: See TracChangeset for help on using the changeset viewer.