Changeset 2035:d1192e577ef2


Ignore:
Timestamp:
Jan 28, 2021, 12:14:14 PM (10 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Fix `test_host.py` for Python 3.9

Add an optional argument `encoding` argument to the `as_bytes` helper
function and use this argument in `TestAcceptEitherUnicodeOrBytes`.

The code in `scripted_session` uses the path encoding of the `ftplib`
module in the standard library, whereas the `as_bytes` helper function
in `test_host.py` by default uses the ftputil path encoding default.

Therefore, the tests worked for Python 3.8 and before, but not for
Python 3.9 with the changed path encoding default.

ticket: 143
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_host.py

    r2034 r2035  
    4646
    4747
    48 def as_bytes(string):
    49     return string.encode(ftputil.path_encoding.DEFAULT_ENCODING)
     48def as_bytes(string, encoding=ftputil.path_encoding.DEFAULT_ENCODING):
     49    return string.encode(encoding)
    5050
    5151
     
    818818        # It's possible to mix argument types, as for `os.rename`.
    819819        path_as_str = "/ä"
    820         path_as_bytes = as_bytes(path_as_str)
     820        path_as_bytes = as_bytes(
     821            path_as_str, ftputil.path_encoding.FTPLIB_DEFAULT_ENCODING
     822        )
    821823        paths = [path_as_str, path_as_bytes]
    822824        for source_path, target_path in itertools.product(paths, paths):
     825            # Uses ftplib default encoding
    823826            session_factory = scripted_session.factory(script)
    824827            with test_base.ftp_host_factory(session_factory) as host:
     
    860863        session_factory = scripted_session.factory(script)
    861864        with test_base.ftp_host_factory(session_factory) as host:
    862             items = host.listdir(as_bytes("ä"))
    863             assert items == [as_bytes("ö"), as_bytes("o")]
     865            items = host.listdir(
     866                as_bytes("ä", ftputil.path_encoding.FTPLIB_DEFAULT_ENCODING)
     867            )
     868            assert items == [
     869                as_bytes("ö", ftputil.path_encoding.FTPLIB_DEFAULT_ENCODING),
     870                as_bytes("o"),
     871            ]
    864872
    865873    def test_chmod(self):
     
    884892        session_factory = scripted_session.factory(script)
    885893        with test_base.ftp_host_factory(session_factory) as host:
    886             host.chmod(as_bytes(path), 0o755)
     894            host.chmod(
     895                as_bytes(path, ftputil.path_encoding.FTPLIB_DEFAULT_ENCODING), 0o755
     896            )
    887897
    888898    def _test_method_with_single_path_argument(self, method_name, path, script):
     
    896906        with test_base.ftp_host_factory(session_factory) as host:
    897907            method = getattr(host, method_name)
    898             method(as_bytes(path))
     908            method(as_bytes(path, ftputil.path_encoding.FTPLIB_DEFAULT_ENCODING))
    899909
    900910    def test_chdir(self):
     
    11261136        session_factory = scripted_session.factory(script)
    11271137        with test_base.ftp_host_factory(session_factory) as host:
    1128             result = list(host.walk(as_bytes("/ä")))
     1138            result = list(
     1139                host.walk(as_bytes("/ä", ftputil.path_encoding.FTPLIB_DEFAULT_ENCODING))
     1140            )
    11291141
    11301142
Note: See TracChangeset for help on using the changeset viewer.