Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
zipdir is now tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
brownhead committed Nov 14, 2013
1 parent f13ca62 commit e126083
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
11 changes: 9 additions & 2 deletions superzippy/zipdir.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

# Copyright (c) 2013 John Sullivan
# Copyright (c) 2013 Other contributers as noted in the CONTRIBUTERS file
#
Expand Down Expand Up @@ -30,6 +28,15 @@
import os.path

def zip_directory(path, output_file, compression = zipfile.ZIP_DEFLATED):
"""
Compresses the directory at ``path`` into a zip file at ``output_file``.
.. note::
Empty directories are not added to the zip file.
"""

with closing(zipfile.ZipFile(output_file, "w", compression)) as f:
for dir_path, dir_names, file_names in os.walk(path):
for i in file_names:
Expand Down
117 changes: 117 additions & 0 deletions tests/test_zipdir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Copyright (c) 2013 John Sullivan
# Copyright (c) 2013 Other contributers as noted in the CONTRIBUTERS file
#
# This file is part of superzippy
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# test helpers
import file_utilities

# external
import pytest

# internal
import superzippy.zipdir

# stdlib
import zipfile
import tempfile
import shutil
import os

class TestZipDir:
good_cases = [
["a", "b", ("a/foo", 1000), ("a/bar", 1000), ("b/baz", 1000)],
["a", ("a/foo", 0)],
[("bar", 1000)],
[]
]

@pytest.fixture
def zip_tree(self, request, test_case):
try:
# We must create these variables up front for our finally
# statement to work correctly.
test_dir = zip_file = unzip_dir = None

# Create some files and directories to zip up
test_dir = file_utilities.create_test_directory(test_case)

# Get a temporary file that will become our zip file
zip_file_handle = tempfile.NamedTemporaryFile(delete = False)
zip_file_handle.close()
zip_file = zip_file_handle.name

# Zip up our directory tree
superzippy.zipdir.zip_directory(test_dir, zip_file)

# Unzip our directory tree
unzip_dir = tempfile.mkdtemp()
with zipfile.ZipFile(zip_file, "r") as f:
f.extractall(unzip_dir)
except:
if test_dir is not None:
shutil.rmtree(test_dir)

if zip_file is not None:
os.remove(zip_file)

if unzip_dir is not None:
shutil.rmtree(unzip_dir)

raise

def cleanup():
shutil.rmtree(test_dir)
os.remove(zip_file)
shutil.rmtree(unzip_dir)
request.addfinalizer(cleanup)

return test_dir, zip_file, unzip_dir

@pytest.mark.parametrize("test_case", good_cases)
def test_existence(self, zip_tree, test_case):
"""
Ensure that all the files that were supposed to get zipped up make it
it into the archive, and ensure that none got in that were not
supposed to.
"""

test_dir, zip_file, unzip_dir = zip_tree

real_contents = file_utilities.get_files(unzip_dir)
expected_contents = file_utilities.get_files(test_dir)

print "real_contents =", real_contents
print "expected_contents =", expected_contents

assert set(real_contents) == set(expected_contents)


@pytest.mark.parametrize("test_case", good_cases)
def test_files_sizes(self, zip_tree, test_case):
"""
Ensures that the files created are the correct size.
"""

test_dir, zip_file, unzip_dir = zip_tree

for i in test_case:
if isinstance(i, tuple):
print "checking size of", i[0]
file_size = os.stat(
os.path.join(test_dir, i[0])).st_size
assert file_size == i[1]

0 comments on commit e126083

Please sign in to comment.