This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
brownhead
committed
Nov 14, 2013
1 parent
f13ca62
commit e126083
Showing
2 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |