summaryrefslogtreecommitdiff
path: root/Lib/test/test_zipfile.py
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2013-10-22 04:09:28 +0200
committerChristian Tismer <tismer@stackless.com>2013-10-22 04:09:28 +0200
commit410d931a177af3253b7a360dc3d04f1ba57c294c (patch)
tree51a27517beff501d45e369442d3e2f166b85d2c0 /Lib/test/test_zipfile.py
parent8ed30c15e8491ba70ee1cc89acec569c72b2ce26 (diff)
downloadcpython-git-410d931a177af3253b7a360dc3d04f1ba57c294c.tar.gz
add filtering of individual files to PyZipFile
changed output of debug messages to say "path" or "file" extended test for filtering certain files in a package added test for filtering files in a python dir (no package)
Diffstat (limited to 'Lib/test/test_zipfile.py')
-rw-r--r--Lib/test/test_zipfile.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index f57da5f94d..7249b13d4c 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -604,12 +604,21 @@ class PyZipFileTests(unittest.TestCase):
reportStr = reportSIO.getvalue()
self.assertTrue('SyntaxError' in reportStr)
- # then check that the filter works
+ # then check that the filter works on the whole package
with captured_stdout() as reportSIO:
zipfp.writepy(packagedir, filterfunc=lambda whatever: False)
reportStr = reportSIO.getvalue()
self.assertTrue('SyntaxError' not in reportStr)
+ # then check that the filter works on individual files
+ with captured_stdout() as reportSIO:
+ zipfp.writepy(packagedir, filterfunc=lambda fn:
+ 'bad' not in fn)
+ reportStr = reportSIO.getvalue()
+ if reportStr:
+ print(reportStr)
+ self.assertTrue('SyntaxError' not in reportStr)
+
def test_write_with_optimization(self):
import email
packagedir = os.path.dirname(email.__file__)
@@ -649,6 +658,26 @@ class PyZipFileTests(unittest.TestCase):
finally:
shutil.rmtree(TESTFN2)
+ def test_write_python_directory_filtered(self):
+ os.mkdir(TESTFN2)
+ try:
+ with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
+ fp.write("print(42)\n")
+
+ with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
+ fp.write("print(42 * 42)\n")
+
+ with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
+ zipfp.writepy(TESTFN2, filterfunc=lambda fn:
+ not fn.endswith('mod2.py'))
+
+ names = zipfp.namelist()
+ self.assertCompiledIn('mod1.py', names)
+ self.assertNotIn('mod2.py', names)
+
+ finally:
+ shutil.rmtree(TESTFN2)
+
def test_write_non_pyfile(self):
with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
with open(TESTFN, 'w') as f: