77from os .path import splitdrive
88import warnings
99
10+ from distutils import archive_util
1011from distutils .archive_util import (check_archive_formats , make_tarball ,
1112 make_zipfile , make_archive ,
1213 ARCHIVE_FORMATS )
1314from distutils .spawn import find_executable , spawn
1415from distutils .tests import support
15- from test .support import check_warnings , run_unittest
16+ from test .support import check_warnings , run_unittest , patch
1617
1718try :
1819 import zipfile
1920 ZIP_SUPPORT = True
2021except ImportError :
2122 ZIP_SUPPORT = find_executable ('zip' )
2223
24+ try :
25+ import zlib
26+ ZLIB_SUPPORT = True
27+ except ImportError :
28+ ZLIB_SUPPORT = False
29+
30+
2331class ArchiveUtilTestCase (support .TempdirManager ,
2432 support .LoggingSilencer ,
2533 unittest .TestCase ):
2634
35+ @unittest .skipUnless (ZLIB_SUPPORT , 'Need zlib support to run' )
2736 def test_make_tarball (self ):
2837 # creating something to tar
2938 tmpdir = self .mkdtemp ()
@@ -84,8 +93,9 @@ def _create_files(self):
8493 base_name = os .path .join (tmpdir2 , 'archive' )
8594 return tmpdir , tmpdir2 , base_name
8695
87- @unittest .skipUnless (find_executable ('tar' ) and find_executable ('gzip' ),
88- 'Need the tar command to run' )
96+ @unittest .skipUnless (find_executable ('tar' ) and find_executable ('gzip' )
97+ and ZLIB_SUPPORT ,
98+ 'Need the tar, gzip and zlib command to run' )
8999 def test_tarfile_vs_tar (self ):
90100 tmpdir , tmpdir2 , base_name = self ._create_files ()
91101 old_dir = os .getcwd ()
@@ -169,7 +179,8 @@ def test_compress_deprecated(self):
169179 self .assertTrue (not os .path .exists (tarball ))
170180 self .assertEqual (len (w .warnings ), 1 )
171181
172- @unittest .skipUnless (ZIP_SUPPORT , 'Need zip support to run' )
182+ @unittest .skipUnless (ZIP_SUPPORT and ZLIB_SUPPORT ,
183+ 'Need zip and zlib support to run' )
173184 def test_make_zipfile (self ):
174185 # creating something to tar
175186 tmpdir = self .mkdtemp ()
@@ -182,6 +193,29 @@ def test_make_zipfile(self):
182193
183194 # check if the compressed tarball was created
184195 tarball = base_name + '.zip'
196+ self .assertTrue (os .path .exists (tarball ))
197+
198+ @unittest .skipUnless (ZIP_SUPPORT , 'Need zip support to run' )
199+ def test_make_zipfile_no_zlib (self ):
200+ patch (self , archive_util .zipfile , 'zlib' , None ) # force zlib ImportError
201+
202+ called = []
203+ zipfile_class = zipfile .ZipFile
204+ def fake_zipfile (* a , ** kw ):
205+ if kw .get ('compression' , None ) == zipfile .ZIP_STORED :
206+ called .append ((a , kw ))
207+ return zipfile_class (* a , ** kw )
208+
209+ patch (self , archive_util .zipfile , 'ZipFile' , fake_zipfile )
210+
211+ # create something to tar and compress
212+ tmpdir , tmpdir2 , base_name = self ._create_files ()
213+ make_zipfile (base_name , tmpdir )
214+
215+ tarball = base_name + '.zip'
216+ self .assertEqual (called ,
217+ [((tarball , "w" ), {'compression' : zipfile .ZIP_STORED })])
218+ self .assertTrue (os .path .exists (tarball ))
185219
186220 def test_check_archive_formats (self ):
187221 self .assertEqual (check_archive_formats (['gztar' , 'xxx' , 'zip' ]),
0 commit comments