Changeset 1845:442b8ca8a85e


Ignore:
Timestamp:
Jul 11, 2019, 10:33:49 PM (6 weeks ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Tags:
tip
Message:
Use `ftp_host_factory` as context manager

Apart from a dedicated test for `FTPHost.close`, use
`test_base.ftp_host_factory` everywhere as context manager.
That is, use

  with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
      ...

instead of

  host = test_base.ftp_host_factory(scripted_session.factory(script))
  ...
  host.close()
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_host.py

    r1841 r1845  
    8585          # trailing slash to test if it's removed when stored in the
    8686          # `FTPHost` instance.
    87           Call("pwd", result="/home/")
    88         ]
    89         host = test_base.ftp_host_factory(scripted_session.factory(script))
    90         assert host.getcwd() == "/home"
     87          Call("pwd", result="/home/"),
     88          Call("close")
     89        ]
     90        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
     91            assert host.getcwd() == "/home"
    9192
    9293
     
    113114          # due to a session timeout.
    114115          Call("pwd", result=ftplib.error_temp),
    115         ]
    116         host = test_base.ftp_host_factory(scripted_session.factory(script))
    117         with pytest.raises(ftputil.error.TemporaryError):
    118             host.keep_alive()
     116          Call("close")
     117        ]
     118        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
     119            with pytest.raises(ftputil.error.TemporaryError):
     120                host.keep_alive()
    119121
    120122
     
    149151          Call("dir",
    150152               result="drwxr-xr-x   2 45854    200           512 May  4  2000 home"),
    151           Call("cwd", args=("/",))
    152         ]
    153         host = test_base.ftp_host_factory(scripted_session.factory(script))
    154         assert host._stat._allow_parser_switching is True
    155         trivial_parser = TestSetParser.TrivialParser()
    156         host.set_parser(trivial_parser)
    157         stat_result = host.stat("/home")
    158         assert stat_result == trivial_parser.default_stat_result
    159         assert host._stat._allow_parser_switching is False
     153          Call("cwd", args=("/",)),
     154          Call("close")
     155        ]
     156        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
     157            assert host._stat._allow_parser_switching is True
     158            trivial_parser = TestSetParser.TrivialParser()
     159            host.set_parser(trivial_parser)
     160            stat_result = host.stat("/home")
     161            assert stat_result == trivial_parser.default_stat_result
     162            assert host._stat._allow_parser_switching is False
    160163
    161164
     
    186189               args=("SITE CHMOD 0644 nonexistent",)),
    187190          Call("cwd", args=("/",)),
    188         ]
    189         host = test_base.ftp_host_factory(scripted_session.factory(script))
    190         with pytest.raises(ftputil.error.CommandNotImplementedError):
    191             host.chmod("nonexistent", 0o644)
    192         # `CommandNotImplementedError` is a subclass of `PermanentError`.
    193         with pytest.raises(ftputil.error.PermanentError):
    194             host.chmod("nonexistent", 0o644)
     191          Call("close")
     192        ]
     193        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
     194            with pytest.raises(ftputil.error.CommandNotImplementedError):
     195                host.chmod("nonexistent", 0o644)
     196            # `CommandNotImplementedError` is a subclass of `PermanentError`.
     197            with pytest.raises(ftputil.error.PermanentError):
     198                host.chmod("nonexistent", 0o644)
    195199
    196200
     
    216220          Call("close")
    217221        ]
    218         host = test_base.ftp_host_factory(scripted_session.factory(script))
    219         lines = host._dir(host.curdir)
    220         assert lines[0] == "non-recursive listing"
    221         host.close()
     222        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
     223            lines = host._dir(host.curdir)
     224            assert lines[0] == "non-recursive listing"
    222225
    223226    def test_empty_string_instead_of_dot_workaround(self):
     
    227230        an empty string.
    228231        """
    229         dir_result = """\
    230 total 10
    231 lrwxrwxrwx   1 staff          7 Aug 13  2003 bin -> usr/bin
    232 d--x--x--x   2 staff        512 Sep 24  2000 dev
    233 d--x--x--x   3 staff        512 Sep 25  2000 etc
    234 dr-xr-xr-x   3 staff        512 Oct  3  2000 pub
    235 d--x--x--x   5 staff        512 Oct  3  2000 usr"""
     232        dir_result = (
     233          "total 10\n"
     234          "lrwxrwxrwx   1 staff     7 Aug 13  2003 bin -> usr/bin\n"
     235          "d--x--x--x   2 staff   512 Sep 24  2000 dev\n"
     236          "d--x--x--x   3 staff   512 Sep 25  2000 etc\n"
     237          "dr-xr-xr-x   3 staff   512 Oct  3  2000 pub\n"
     238          "d--x--x--x   5 staff   512 Oct  3  2000 usr\n")
    236239        script = [
    237240          Call("__init__"),
     
    243246          Call("close")
    244247        ]
    245         host = test_base.ftp_host_factory(scripted_session.factory(script))
    246         files = host.listdir(host.curdir)
    247         assert files == ["bin", "dev", "etc", "pub", "usr"]
    248         host.close()
     248        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
     249            files = host.listdir(host.curdir)
     250            assert files == ["bin", "dev", "etc", "pub", "usr"]
    249251
    250252
Note: See TracChangeset for help on using the changeset viewer.