Changeset 1884:a3f58fa3920e


Ignore:
Timestamp:
Jan 1, 2020, 7:22:31 PM (3 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Test if `FTPHost.path` methods accept `PathLike` objects

The tests are simplified by adding a `path_converter` class variable
that either returns a bytes/str path directly (in
`TestAcceptEitherBytesOrStr`) or wraps them in `Path` objects (in
`TestAcceptEitherBytesOrStrFromPath`).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_path.py

    r1882 r1884  
    377377
    378378
    379 class TestAcceptEitherBytesOrUnicode:
     379class TestAcceptEitherBytesOrStr:
     380
     381    # Use path arguments directly
     382    path_converter = staticmethod(lambda path: path)
     383
    380384    def _test_method_string_types(self, method, path):
    381385        expected_type = type(path)
    382         assert isinstance(method(path), expected_type)
     386        path_converter = self.path_converter
     387        assert isinstance(method(path_converter(path)), expected_type)
    383388
    384389    def test_methods_that_take_and_return_one_string(self):
     
    406411        """Test whether the methods accept byte and unicode strings."""
    407412        as_bytes = ftputil.tool.as_bytes
     413        path_converter = self.path_converter
    408414        script = [
    409415            Call("__init__"),
     
    505511            # `isabs`
    506512            assert not host.path.isabs("ä")
    507             assert not host.path.isabs(as_bytes("ä"))
     513            assert not host.path.isabs(path_converter(as_bytes("ä")))
    508514            # `exists`
    509             assert host.path.exists("ä")
    510             assert host.path.exists(as_bytes("ä"))
     515            assert host.path.exists(path_converter("ä"))
     516            assert host.path.exists(path_converter(as_bytes("ä")))
    511517            # `isdir`, `isfile`, `islink`
    512             assert host.path.isdir("ä")
    513             assert host.path.isdir(as_bytes("ä"))
    514             assert host.path.isfile("ö")
    515             assert host.path.isfile(as_bytes("ö"))
    516             assert host.path.islink("ü")
    517             assert host.path.islink(as_bytes("ü"))
     518            assert host.path.isdir(path_converter("ä"))
     519            assert host.path.isdir(path_converter(as_bytes("ä")))
     520            assert host.path.isfile(path_converter("ö"))
     521            assert host.path.isfile(path_converter(as_bytes("ö")))
     522            assert host.path.islink(path_converter("ü"))
     523            assert host.path.islink(path_converter(as_bytes("ü")))
    518524
    519525    def test_getmtime(self):
     
    523529        """
    524530        as_bytes = ftputil.tool.as_bytes
     531        path_converter = self.path_converter
    525532        now = datetime.datetime.now()
    526533        script = [
     
    539546            Call("close"),
    540547        ]
    541         with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
    542             host.stat_cache.disable()
    543             expected_mtime = now.timestamp()
    544             # We don't care about the _exact_ time, so don't bother with
    545             # timezone differences. Instead, do a simple sanity check.
    546             day = 24 * 60 * 60  # seconds
    547             mtime_makes_sense = (
    548                 lambda mtime: expected_mtime - day <= mtime <= expected_mtime + day
    549             )
    550             assert mtime_makes_sense(host.path.getmtime("ä"))
    551             assert mtime_makes_sense(host.path.getmtime(as_bytes("ä")))
     548        expected_mtime = now.timestamp()
     549        # We don't care about the _exact_ time, so don't bother with
     550        # timezone differences. Instead, do a simple sanity check.
     551        day = 24 * 60 * 60  # seconds
     552        mtime_makes_sense = (
     553            lambda mtime: expected_mtime - day <= mtime <= expected_mtime + day
     554        )
     555        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
     556            host.stat_cache.disable()
     557            assert mtime_makes_sense(host.path.getmtime(path_converter(("ä"))))
     558            assert mtime_makes_sense(host.path.getmtime(path_converter(as_bytes("ä"))))
    552559
    553560    def test_getsize(self):
     
    556563        """
    557564        as_bytes = ftputil.tool.as_bytes
     565        path_converter = self.path_converter
    558566        now = datetime.datetime.now()
    559567        script = [
     
    582590        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
    583591            host.stat_cache.disable()
    584             assert host.path.getsize("ä") == 512
    585             assert host.path.getsize(as_bytes("ä")) == 512
     592            assert host.path.getsize(path_converter("ä")) == 512
     593            assert host.path.getsize(path_converter(as_bytes("ä"))) == 512
    586594
    587595    def test_walk(self):
    588596        """Test whether `FTPHost.path.walk` accepts bytes and unicode paths."""
    589597        as_bytes = ftputil.tool.as_bytes
     598        path_converter = self.path_converter
    590599        now = datetime.datetime.now()
    591600        script = [
     
    632641        with test_base.ftp_host_factory(scripted_session.factory(script)) as host:
    633642            host.stat_cache.disable()
    634             host.path.walk("ä", func=noop, arg=None)
    635             host.path.walk(as_bytes("ä"), func=noop, arg=None)
     643            host.path.walk(path_converter("ä"), func=noop, arg=None)
     644            host.path.walk(path_converter(as_bytes("ä")), func=noop, arg=None)
     645
     646
     647class Path:
     648    def __init__(self, path):
     649        self.path = path
     650
     651    def __fspath__(self):
     652        return self.path
     653
     654
     655class TestAcceptEitherBytesOrStrFromPath(TestAcceptEitherBytesOrStr):
     656
     657    # Take path arguments from `Path(...)` objects
     658    path_converter = Path
Note: See TracChangeset for help on using the changeset viewer.