From 66ba0fd1754924d374ff30a05ac36da5009d6a1e Mon Sep 17 00:00:00 2001 From: Hongyang Peng Date: Tue, 7 Jan 2025 17:02:28 +0800 Subject: [PATCH] ci:add ci (#472) --- megfile/lib/s3_pipe_handler.py | 8 +-- megfile/smart.py | 2 - tests/lib/test_s3_pipe_handler.py | 3 + tests/lib/test_s3_prefetch_reader.py | 20 +++++++ tests/test_fs_purepath.py | 5 ++ tests/test_pathlike.py | 88 ++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 6 deletions(-) diff --git a/megfile/lib/s3_pipe_handler.py b/megfile/lib/s3_pipe_handler.py index 3542bcdb..64bdb2f4 100644 --- a/megfile/lib/s3_pipe_handler.py +++ b/megfile/lib/s3_pipe_handler.py @@ -34,9 +34,6 @@ def __init__( join_thread: bool = True, profile_name: Optional[str] = None, ): - if mode not in ("rb", "wb"): - raise ValueError("unacceptable mode: %r" % mode) - self._bucket = bucket self._key = key self._mode = mode @@ -45,6 +42,9 @@ def __init__( self._offset = 0 self._profile_name = profile_name + if mode not in ("rb", "wb"): + raise ValueError("unacceptable mode: %r" % mode) + self._exc = None self._pipe = os.pipe() _s3_opened_pipes.append(self._pipe) @@ -76,7 +76,7 @@ def _download_fileobj(self): try: with os.fdopen(self._pipe[1], "wb") as buffer: self._client.download_fileobj(self._bucket, self._key, buffer) - except BrokenPipeError: + except BrokenPipeError: # pragma: no cover if self._fileobj.closed: return raise diff --git a/megfile/smart.py b/megfile/smart.py index b1cc191a..958bdddd 100644 --- a/megfile/smart.py +++ b/megfile/smart.py @@ -285,8 +285,6 @@ def register_copy_func( dst_dict = _copy_funcs.get(src_protocol, {}) dst_dict[dst_protocol] = copy_func _copy_funcs[src_protocol] = dst_dict - except Exception as error: - raise error else: raise ValueError( "Copy Function has already existed: {}->{}".format( diff --git a/tests/lib/test_s3_pipe_handler.py b/tests/lib/test_s3_pipe_handler.py index 5322dc58..a0b3acd2 100644 --- a/tests/lib/test_s3_pipe_handler.py +++ b/tests/lib/test_s3_pipe_handler.py @@ -46,6 +46,9 @@ def test_s3_pipe_handler_read(client): assert reader.tell() == 22 assert reader.read() == b"" + with pytest.raises(ValueError): + S3PipeHandler(BUCKET, KEY, "ab", s3_client=client) + def test_s3_pipe_handler_write(client): with S3PipeHandler(BUCKET, KEY, "wb", s3_client=client) as writer: diff --git a/tests/lib/test_s3_prefetch_reader.py b/tests/lib/test_s3_prefetch_reader.py index 5bd9e231..717fc09b 100644 --- a/tests/lib/test_s3_prefetch_reader.py +++ b/tests/lib/test_s3_prefetch_reader.py @@ -639,3 +639,23 @@ def test_s3_prefetch_reader_no_buffer(client_for_get_object): reader.read() assert list(reader._futures.keys()) == [] + + +def test_s3_prefetch_reader_size(client_for_get_object, mocker): + def fake_fetch_response(*args, **kwargs): + return { + "ContentLength": 123, + "Body": None, + "ETag": "test", + } + + mocker.patch( + "megfile.lib.s3_prefetch_reader.S3PrefetchReader._fetch_response", + fake_fetch_response, + ) + with S3PrefetchReader( + BUCKET, + KEY, + s3_client=client_for_get_object, + ) as reader: + assert reader._content_size == 123 diff --git a/tests/test_fs_purepath.py b/tests/test_fs_purepath.py index bf22f74c..d892b59c 100644 --- a/tests/test_fs_purepath.py +++ b/tests/test_fs_purepath.py @@ -1,3 +1,5 @@ +import pathlib + import pytest from megfile.fs_path import FSPath @@ -59,6 +61,9 @@ def test_operators(): assert FSPath("file://foo") / "bar" / "baz" == FSPath("file://foo/bar/baz") assert FSPath("foo") / "bar" / "baz" == FSPath("foo/bar/baz") assert FSPath("file://foo") / "bar" / "baz" in {FSPath("file://foo/bar/baz")} + assert FSPath("file://foo") / pathlib.Path("bar") / "baz" in { + FSPath("file://foo/bar/baz") + } def test_parts(): diff --git a/tests/test_pathlike.py b/tests/test_pathlike.py index c730a9c9..f59fb949 100644 --- a/tests/test_pathlike.py +++ b/tests/test_pathlike.py @@ -105,3 +105,91 @@ def test_uri_path(mocker): with pytest.raises(ValueError): uri_path.relative_to(1) assert uri_path.resolve() == "fs://test" + + +def test_base_path_attr(mocker): + path = BasePath("/test") + with pytest.raises(NotImplementedError): + path / "test" + + assert path.name == "/test" + + with pytest.raises(NotImplementedError): + path.joinpath("test") + + with pytest.raises(NotImplementedError): + path.parts + + with pytest.raises(NotImplementedError): + path.parents + + with pytest.raises(NotImplementedError): + path.parent + + with pytest.raises(NotImplementedError): + path.is_dir() + + with pytest.raises(NotImplementedError): + path.is_file() + + assert path.is_symlink() is False + + with pytest.raises(NotImplementedError): + path.access(None) + + with pytest.raises(NotImplementedError): + path.exists() + + with pytest.raises(NotImplementedError): + path.listdir() + + with pytest.raises(NotImplementedError): + path.scandir() + + with pytest.raises(NotImplementedError): + path.getsize() + + with pytest.raises(NotImplementedError): + path.getmtime() + + with pytest.raises(NotImplementedError): + path.stat() + + with pytest.raises(NotImplementedError): + path.match(r"*") + + with pytest.raises(NotImplementedError): + path.remove() + + with pytest.raises(NotImplementedError): + path.mkdir() + + with pytest.raises(NotImplementedError): + path.rmdir() + + with pytest.raises(NotImplementedError): + path.open() + + with pytest.raises(NotImplementedError): + path.walk() + + with pytest.raises(NotImplementedError): + path.scan() + + with pytest.raises(NotImplementedError): + path.scan_stat() + + with pytest.raises(NotImplementedError): + path.glob(None) + + with pytest.raises(NotImplementedError): + path.iglob(None) + + with pytest.raises(NotImplementedError): + path.glob_stat(None) + + with pytest.raises(NotImplementedError): + path.load() + + with pytest.raises(NotImplementedError): + path.save(None)