#6026 - fix tests that failed without zlib
[python.git] / Lib / distutils / tests / test_sdist.py
blobc2feccb3ceade6f118fae5bc43d9fc2b945f8656
1 """Tests for distutils.command.sdist."""
2 import os
3 import unittest
4 import shutil
5 import zipfile
7 # zlib is not used here, but if it's not available
8 # the tests that use zipfile may fail
9 try:
10 import zlib
11 except ImportError:
12 zlib = None
14 from os.path import join
15 import sys
16 import tempfile
17 import warnings
19 from test.test_support import check_warnings
20 from test.test_support import captured_stdout
22 from distutils.command.sdist import sdist
23 from distutils.command.sdist import show_formats
24 from distutils.core import Distribution
25 from distutils.tests.test_config import PyPIRCCommandTestCase
26 from distutils.errors import DistutilsExecError, DistutilsOptionError
27 from distutils.spawn import find_executable
28 from distutils.tests import support
29 from distutils.log import WARN
30 from distutils.archive_util import ARCHIVE_FORMATS
32 SETUP_PY = """
33 from distutils.core import setup
34 import somecode
36 setup(name='fake')
37 """
39 MANIFEST = """\
40 README
41 inroot.txt
42 setup.py
43 data%(sep)sdata.dt
44 scripts%(sep)sscript.py
45 some%(sep)sfile.txt
46 some%(sep)sother_file.txt
47 somecode%(sep)s__init__.py
48 somecode%(sep)sdoc.dat
49 somecode%(sep)sdoc.txt
50 """
52 class SDistTestCase(PyPIRCCommandTestCase):
54 def setUp(self):
55 # PyPIRCCommandTestCase creates a temp dir already
56 # and put it in self.tmp_dir
57 super(SDistTestCase, self).setUp()
58 # setting up an environment
59 self.old_path = os.getcwd()
60 os.mkdir(join(self.tmp_dir, 'somecode'))
61 os.mkdir(join(self.tmp_dir, 'dist'))
62 # a package, and a README
63 self.write_file((self.tmp_dir, 'README'), 'xxx')
64 self.write_file((self.tmp_dir, 'somecode', '__init__.py'), '#')
65 self.write_file((self.tmp_dir, 'setup.py'), SETUP_PY)
66 os.chdir(self.tmp_dir)
68 def tearDown(self):
69 # back to normal
70 os.chdir(self.old_path)
71 super(SDistTestCase, self).tearDown()
73 def get_cmd(self, metadata=None):
74 """Returns a cmd"""
75 if metadata is None:
76 metadata = {'name': 'fake', 'version': '1.0',
77 'url': 'xxx', 'author': 'xxx',
78 'author_email': 'xxx'}
79 dist = Distribution(metadata)
80 dist.script_name = 'setup.py'
81 dist.packages = ['somecode']
82 dist.include_package_data = True
83 cmd = sdist(dist)
84 cmd.dist_dir = 'dist'
85 def _warn(*args):
86 pass
87 cmd.warn = _warn
88 return dist, cmd
90 @unittest.skipUnless(zlib, "requires zlib")
91 def test_prune_file_list(self):
92 # this test creates a package with some vcs dirs in it
93 # and launch sdist to make sure they get pruned
94 # on all systems
96 # creating VCS directories with some files in them
97 os.mkdir(join(self.tmp_dir, 'somecode', '.svn'))
98 self.write_file((self.tmp_dir, 'somecode', '.svn', 'ok.py'), 'xxx')
100 os.mkdir(join(self.tmp_dir, 'somecode', '.hg'))
101 self.write_file((self.tmp_dir, 'somecode', '.hg',
102 'ok'), 'xxx')
104 os.mkdir(join(self.tmp_dir, 'somecode', '.git'))
105 self.write_file((self.tmp_dir, 'somecode', '.git',
106 'ok'), 'xxx')
108 # now building a sdist
109 dist, cmd = self.get_cmd()
111 # zip is available universally
112 # (tar might not be installed under win32)
113 cmd.formats = ['zip']
115 cmd.ensure_finalized()
116 cmd.run()
118 # now let's check what we have
119 dist_folder = join(self.tmp_dir, 'dist')
120 files = os.listdir(dist_folder)
121 self.assertEquals(files, ['fake-1.0.zip'])
123 zip_file = zipfile.ZipFile(join(dist_folder, 'fake-1.0.zip'))
124 try:
125 content = zip_file.namelist()
126 finally:
127 zip_file.close()
129 # making sure everything has been pruned correctly
130 self.assertEquals(len(content), 4)
132 @unittest.skipUnless(zlib, "requires zlib")
133 def test_make_distribution(self):
135 # check if tar and gzip are installed
136 if (find_executable('tar') is None or
137 find_executable('gzip') is None):
138 return
140 # now building a sdist
141 dist, cmd = self.get_cmd()
143 # creating a gztar then a tar
144 cmd.formats = ['gztar', 'tar']
145 cmd.ensure_finalized()
146 cmd.run()
148 # making sure we have two files
149 dist_folder = join(self.tmp_dir, 'dist')
150 result = os.listdir(dist_folder)
151 result.sort()
152 self.assertEquals(result,
153 ['fake-1.0.tar', 'fake-1.0.tar.gz'] )
155 os.remove(join(dist_folder, 'fake-1.0.tar'))
156 os.remove(join(dist_folder, 'fake-1.0.tar.gz'))
158 # now trying a tar then a gztar
159 cmd.formats = ['tar', 'gztar']
161 cmd.ensure_finalized()
162 cmd.run()
164 result = os.listdir(dist_folder)
165 result.sort()
166 self.assertEquals(result,
167 ['fake-1.0.tar', 'fake-1.0.tar.gz'])
169 @unittest.skipUnless(zlib, "requires zlib")
170 def test_add_defaults(self):
172 # http://bugs.python.org/issue2279
174 # add_default should also include
175 # data_files and package_data
176 dist, cmd = self.get_cmd()
178 # filling data_files by pointing files
179 # in package_data
180 dist.package_data = {'': ['*.cfg', '*.dat'],
181 'somecode': ['*.txt']}
182 self.write_file((self.tmp_dir, 'somecode', 'doc.txt'), '#')
183 self.write_file((self.tmp_dir, 'somecode', 'doc.dat'), '#')
185 # adding some data in data_files
186 data_dir = join(self.tmp_dir, 'data')
187 os.mkdir(data_dir)
188 self.write_file((data_dir, 'data.dt'), '#')
189 some_dir = join(self.tmp_dir, 'some')
190 os.mkdir(some_dir)
191 self.write_file((self.tmp_dir, 'inroot.txt'), '#')
192 self.write_file((some_dir, 'file.txt'), '#')
193 self.write_file((some_dir, 'other_file.txt'), '#')
195 dist.data_files = [('data', ['data/data.dt',
196 'inroot.txt',
197 'notexisting']),
198 'some/file.txt',
199 'some/other_file.txt']
201 # adding a script
202 script_dir = join(self.tmp_dir, 'scripts')
203 os.mkdir(script_dir)
204 self.write_file((script_dir, 'script.py'), '#')
205 dist.scripts = [join('scripts', 'script.py')]
207 cmd.formats = ['zip']
208 cmd.use_defaults = True
210 cmd.ensure_finalized()
211 cmd.run()
213 # now let's check what we have
214 dist_folder = join(self.tmp_dir, 'dist')
215 files = os.listdir(dist_folder)
216 self.assertEquals(files, ['fake-1.0.zip'])
218 zip_file = zipfile.ZipFile(join(dist_folder, 'fake-1.0.zip'))
219 try:
220 content = zip_file.namelist()
221 finally:
222 zip_file.close()
224 # making sure everything was added
225 self.assertEquals(len(content), 11)
227 # checking the MANIFEST
228 manifest = open(join(self.tmp_dir, 'MANIFEST')).read()
229 self.assertEquals(manifest, MANIFEST % {'sep': os.sep})
231 @unittest.skipUnless(zlib, "requires zlib")
232 def test_metadata_check_option(self):
233 # testing the `medata-check` option
234 dist, cmd = self.get_cmd(metadata={})
236 # this should raise some warnings !
237 # with the `check` subcommand
238 cmd.ensure_finalized()
239 cmd.run()
240 warnings = self.get_logs(WARN)
241 self.assertEquals(len(warnings), 2)
243 # trying with a complete set of metadata
244 self.clear_logs()
245 dist, cmd = self.get_cmd()
246 cmd.ensure_finalized()
247 cmd.metadata_check = 0
248 cmd.run()
249 warnings = self.get_logs(WARN)
250 self.assertEquals(len(warnings), 0)
252 def test_check_metadata_deprecated(self):
253 # makes sure make_metadata is deprecated
254 dist, cmd = self.get_cmd()
255 with check_warnings() as w:
256 warnings.simplefilter("always")
257 cmd.check_metadata()
258 self.assertEquals(len(w.warnings), 1)
260 def test_show_formats(self):
261 with captured_stdout() as stdout:
262 show_formats()
264 # the output should be a header line + one line per format
265 num_formats = len(ARCHIVE_FORMATS.keys())
266 output = [line for line in stdout.getvalue().split('\n')
267 if line.strip().startswith('--formats=')]
268 self.assertEquals(len(output), num_formats)
270 def test_finalize_options(self):
272 dist, cmd = self.get_cmd()
273 cmd.finalize_options()
275 # default options set by finalize
276 self.assertEquals(cmd.manifest, 'MANIFEST')
277 self.assertEquals(cmd.template, 'MANIFEST.in')
278 self.assertEquals(cmd.dist_dir, 'dist')
280 # formats has to be a string splitable on (' ', ',') or
281 # a stringlist
282 cmd.formats = 1
283 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
284 cmd.formats = ['zip']
285 cmd.finalize_options()
287 # formats has to be known
288 cmd.formats = 'supazipa'
289 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
292 def test_suite():
293 return unittest.makeSuite(SDistTestCase)
295 if __name__ == "__main__":
296 unittest.main(defaultTest="test_suite")