Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zipfile.is_zipfile support #230

Merged
merged 4 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions tests/v2_tests/test_s3_zip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import shutil
import tempfile
import zipfile

import pytest
from moto import mock_s3
Expand All @@ -11,18 +12,20 @@

@mock_s3
def test_s3_zip():
bucket = "test-dummy-bucket"
key = "it's me!deadbeef"
secret = "asedf;lkjdf;a'lksjd"
with S3(bucket, create_bucket=True):
with from_url('s3://test-dummy-bucket/base',
aws_access_key_id=key,
aws_secret_access_key=secret) as s3:
assert bucket == s3.bucket
assert '/base' == s3.cwd
assert key == s3.aws_access_key_id
assert secret == s3.aws_secret_access_key
assert s3.endpoint is None
with tempfile.TemporaryDirectory() as d:
zipfilename = os.path.join(d, "test.zip")
_ = ZipForTest(zipfilename)
bucket = "test-dummy-bucket"

with from_url('s3://{}/'.format(bucket),
create_bucket=True) as s3:
assert isinstance(s3, S3)
with open(zipfilename, 'rb') as src,\
s3.open('test.zip', 'wb') as dst:
shutil.copyfileobj(src, dst)

with s3.open('test.zip', 'rb') as fp:
assert zipfile.is_zipfile(fp)


@mock_s3
Expand All @@ -36,7 +39,7 @@ def test_force_type2():
create_bucket=True) as s3:
assert isinstance(s3, S3)
with open(zipfilename, 'rb') as src,\
s3.open('test.zip', 'wb') as dst:
s3.open('test.zip', 'wb') as dst:
shutil.copyfileobj(src, dst)

with open(zipfilename, 'rb') as f1:
Expand Down
12 changes: 11 additions & 1 deletion tests/v2_tests/test_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import sys
import tempfile
import unittest
import zipfile
from datetime import datetime
from zipfile import ZipFile

import pytest
from parameterized import parameterized

from pfio.testing import make_random_str, make_zip
from pfio.testing import ZipForTest, make_random_str, make_zip
from pfio.v2 import ZipFileStat, local

ZIP_TEST_FILENAME_LIST = {
Expand Down Expand Up @@ -847,3 +848,12 @@ def test_isdir(self, path_or_prefix, expected):
def test_isdir_not_exist(self, dir):
with local.open_zip(self.zip_file_name) as z:
self.assertFalse(z.isdir(dir))


def test_is_zipfile():
with tempfile.TemporaryDirectory() as tmpdir:
zipfilename = os.path.join(tmpdir, 'test.zip')
_ = ZipForTest(zipfilename)
with local as fs:
with fs.open(zipfilename, 'rb') as fp:
assert zipfile.is_zipfile(fp)