]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/distutils/tests/test_sdist.py
1 """Tests for distutils.command.sdist."""
8 # zlib is not used here, but if it's not available
9 # the tests that use zipfile may fail
18 UID_GID_SUPPORT
= True
20 UID_GID_SUPPORT
= False
22 from os
.path
import join
27 from test
.test_support
import captured_stdout
, check_warnings
, run_unittest
29 from distutils
.command
.sdist
import sdist
, show_formats
30 from distutils
.core
import Distribution
31 from distutils
.tests
.test_config
import PyPIRCCommandTestCase
32 from distutils
.errors
import DistutilsExecError
, DistutilsOptionError
33 from distutils
.spawn
import find_executable
34 from distutils
.tests
import support
35 from distutils
.log
import WARN
36 from distutils
.archive_util
import ARCHIVE_FORMATS
39 from distutils.core import setup
46 # file GENERATED by distutils, do NOT edit
51 scripts%(sep)sscript.py
53 some%(sep)sother_file.txt
54 somecode%(sep)s__init__.py
55 somecode%(sep)sdoc.dat
56 somecode%(sep)sdoc.txt
59 class SDistTestCase(PyPIRCCommandTestCase
):
62 # PyPIRCCommandTestCase creates a temp dir already
63 # and put it in self.tmp_dir
64 super(SDistTestCase
, self
).setUp()
65 # setting up an environment
66 self
.old_path
= os
.getcwd()
67 os
.mkdir(join(self
.tmp_dir
, 'somecode'))
68 os
.mkdir(join(self
.tmp_dir
, 'dist'))
69 # a package, and a README
70 self
.write_file((self
.tmp_dir
, 'README'), 'xxx')
71 self
.write_file((self
.tmp_dir
, 'somecode', '__init__.py'), '#')
72 self
.write_file((self
.tmp_dir
, 'setup.py'), SETUP_PY
)
73 os
.chdir(self
.tmp_dir
)
77 os
.chdir(self
.old_path
)
78 super(SDistTestCase
, self
).tearDown()
80 def get_cmd(self
, metadata
=None):
83 metadata
= {'name': 'fake', 'version': '1.0',
84 'url': 'xxx', 'author': 'xxx',
85 'author_email': 'xxx'}
86 dist
= Distribution(metadata
)
87 dist
.script_name
= 'setup.py'
88 dist
.packages
= ['somecode']
89 dist
.include_package_data
= True
97 @unittest.skipUnless(zlib
, "requires zlib")
98 def test_prune_file_list(self
):
99 # this test creates a package with some vcs dirs in it
100 # and launch sdist to make sure they get pruned
103 # creating VCS directories with some files in them
104 os
.mkdir(join(self
.tmp_dir
, 'somecode', '.svn'))
105 self
.write_file((self
.tmp_dir
, 'somecode', '.svn', 'ok.py'), 'xxx')
107 os
.mkdir(join(self
.tmp_dir
, 'somecode', '.hg'))
108 self
.write_file((self
.tmp_dir
, 'somecode', '.hg',
111 os
.mkdir(join(self
.tmp_dir
, 'somecode', '.git'))
112 self
.write_file((self
.tmp_dir
, 'somecode', '.git',
115 # now building a sdist
116 dist
, cmd
= self
.get_cmd()
118 # zip is available universally
119 # (tar might not be installed under win32)
120 cmd
.formats
= ['zip']
122 cmd
.ensure_finalized()
125 # now let's check what we have
126 dist_folder
= join(self
.tmp_dir
, 'dist')
127 files
= os
.listdir(dist_folder
)
128 self
.assertEqual(files
, ['fake-1.0.zip'])
130 zip_file
= zipfile
.ZipFile(join(dist_folder
, 'fake-1.0.zip'))
132 content
= zip_file
.namelist()
136 # making sure everything has been pruned correctly
137 self
.assertEqual(len(content
), 4)
139 @unittest.skipUnless(zlib
, "requires zlib")
140 def test_make_distribution(self
):
142 # check if tar and gzip are installed
143 if (find_executable('tar') is None or
144 find_executable('gzip') is None):
147 # now building a sdist
148 dist
, cmd
= self
.get_cmd()
150 # creating a gztar then a tar
151 cmd
.formats
= ['gztar', 'tar']
152 cmd
.ensure_finalized()
155 # making sure we have two files
156 dist_folder
= join(self
.tmp_dir
, 'dist')
157 result
= os
.listdir(dist_folder
)
159 self
.assertEqual(result
, ['fake-1.0.tar', 'fake-1.0.tar.gz'] )
161 os
.remove(join(dist_folder
, 'fake-1.0.tar'))
162 os
.remove(join(dist_folder
, 'fake-1.0.tar.gz'))
164 # now trying a tar then a gztar
165 cmd
.formats
= ['tar', 'gztar']
167 cmd
.ensure_finalized()
170 result
= os
.listdir(dist_folder
)
172 self
.assertEqual(result
, ['fake-1.0.tar', 'fake-1.0.tar.gz'])
174 @unittest.skipUnless(zlib
, "requires zlib")
175 def test_add_defaults(self
):
177 # http://bugs.python.org/issue2279
179 # add_default should also include
180 # data_files and package_data
181 dist
, cmd
= self
.get_cmd()
183 # filling data_files by pointing files
185 dist
.package_data
= {'': ['*.cfg', '*.dat'],
186 'somecode': ['*.txt']}
187 self
.write_file((self
.tmp_dir
, 'somecode', 'doc.txt'), '#')
188 self
.write_file((self
.tmp_dir
, 'somecode', 'doc.dat'), '#')
190 # adding some data in data_files
191 data_dir
= join(self
.tmp_dir
, 'data')
193 self
.write_file((data_dir
, 'data.dt'), '#')
194 some_dir
= join(self
.tmp_dir
, 'some')
196 self
.write_file((self
.tmp_dir
, 'inroot.txt'), '#')
197 self
.write_file((some_dir
, 'file.txt'), '#')
198 self
.write_file((some_dir
, 'other_file.txt'), '#')
200 dist
.data_files
= [('data', ['data/data.dt',
204 'some/other_file.txt']
207 script_dir
= join(self
.tmp_dir
, 'scripts')
209 self
.write_file((script_dir
, 'script.py'), '#')
210 dist
.scripts
= [join('scripts', 'script.py')]
212 cmd
.formats
= ['zip']
213 cmd
.use_defaults
= True
215 cmd
.ensure_finalized()
218 # now let's check what we have
219 dist_folder
= join(self
.tmp_dir
, 'dist')
220 files
= os
.listdir(dist_folder
)
221 self
.assertEqual(files
, ['fake-1.0.zip'])
223 zip_file
= zipfile
.ZipFile(join(dist_folder
, 'fake-1.0.zip'))
225 content
= zip_file
.namelist()
229 # making sure everything was added
230 self
.assertEqual(len(content
), 11)
232 # checking the MANIFEST
233 f
= open(join(self
.tmp_dir
, 'MANIFEST'))
236 self
.assertEqual(manifest
, MANIFEST
% {'sep': os
.sep
})
240 @unittest.skipUnless(zlib
, "requires zlib")
241 def test_metadata_check_option(self
):
242 # testing the `medata-check` option
243 dist
, cmd
= self
.get_cmd(metadata
={})
245 # this should raise some warnings !
246 # with the `check` subcommand
247 cmd
.ensure_finalized()
249 warnings
= self
.get_logs(WARN
)
250 self
.assertEqual(len(warnings
), 2)
252 # trying with a complete set of metadata
254 dist
, cmd
= self
.get_cmd()
255 cmd
.ensure_finalized()
256 cmd
.metadata_check
= 0
258 warnings
= self
.get_logs(WARN
)
259 self
.assertEqual(len(warnings
), 0)
261 def test_check_metadata_deprecated(self
):
262 # makes sure make_metadata is deprecated
263 dist
, cmd
= self
.get_cmd()
264 with
check_warnings() as w
:
265 warnings
.simplefilter("always")
267 self
.assertEqual(len(w
.warnings
), 1)
269 def test_show_formats(self
):
270 with
captured_stdout() as stdout
:
273 # the output should be a header line + one line per format
274 num_formats
= len(ARCHIVE_FORMATS
.keys())
275 output
= [line
for line
in stdout
.getvalue().split('\n')
276 if line
.strip().startswith('--formats=')]
277 self
.assertEqual(len(output
), num_formats
)
279 def test_finalize_options(self
):
281 dist
, cmd
= self
.get_cmd()
282 cmd
.finalize_options()
284 # default options set by finalize
285 self
.assertEqual(cmd
.manifest
, 'MANIFEST')
286 self
.assertEqual(cmd
.template
, 'MANIFEST.in')
287 self
.assertEqual(cmd
.dist_dir
, 'dist')
289 # formats has to be a string splitable on (' ', ',') or
292 self
.assertRaises(DistutilsOptionError
, cmd
.finalize_options
)
293 cmd
.formats
= ['zip']
294 cmd
.finalize_options()
296 # formats has to be known
297 cmd
.formats
= 'supazipa'
298 self
.assertRaises(DistutilsOptionError
, cmd
.finalize_options
)
300 @unittest.skipUnless(zlib
, "requires zlib")
301 @unittest.skipUnless(UID_GID_SUPPORT
, "Requires grp and pwd support")
302 def test_make_distribution_owner_group(self
):
304 # check if tar and gzip are installed
305 if (find_executable('tar') is None or
306 find_executable('gzip') is None):
309 # now building a sdist
310 dist
, cmd
= self
.get_cmd()
312 # creating a gztar and specifying the owner+group
313 cmd
.formats
= ['gztar']
314 cmd
.owner
= pwd
.getpwuid(0)[0]
315 cmd
.group
= grp
.getgrgid(0)[0]
316 cmd
.ensure_finalized()
319 # making sure we have the good rights
320 archive_name
= join(self
.tmp_dir
, 'dist', 'fake-1.0.tar.gz')
321 archive
= tarfile
.open(archive_name
)
323 for member
in archive
.getmembers():
324 self
.assertEqual(member
.uid
, 0)
325 self
.assertEqual(member
.gid
, 0)
329 # building a sdist again
330 dist
, cmd
= self
.get_cmd()
333 cmd
.formats
= ['gztar']
334 cmd
.ensure_finalized()
337 # making sure we have the good rights
338 archive_name
= join(self
.tmp_dir
, 'dist', 'fake-1.0.tar.gz')
339 archive
= tarfile
.open(archive_name
)
341 # note that we are not testing the group ownership here
342 # because, depending on the platforms and the container
345 for member
in archive
.getmembers():
346 self
.assertEqual(member
.uid
, os
.getuid())
350 @unittest.skipUnless(zlib
, "requires zlib")
351 def test_get_file_list(self
):
352 # make sure MANIFEST is recalculated
353 dist
, cmd
= self
.get_cmd()
355 # filling data_files by pointing files in package_data
356 dist
.package_data
= {'somecode': ['*.txt']}
357 self
.write_file((self
.tmp_dir
, 'somecode', 'doc.txt'), '#')
358 cmd
.ensure_finalized()
361 f
= open(cmd
.manifest
)
363 manifest
= [line
.strip() for line
in f
.read().split('\n')
364 if line
.strip() != '']
368 self
.assertEqual(len(manifest
), 5)
371 self
.write_file((self
.tmp_dir
, 'somecode', 'doc2.txt'), '#')
373 # make sure build_py is reinitialized, like a fresh run
374 build_py
= dist
.get_command_obj('build_py')
375 build_py
.finalized
= False
376 build_py
.ensure_finalized()
380 f
= open(cmd
.manifest
)
382 manifest2
= [line
.strip() for line
in f
.read().split('\n')
383 if line
.strip() != '']
387 # do we have the new file in MANIFEST ?
388 self
.assertEqual(len(manifest2
), 6)
389 self
.assertIn('doc2.txt', manifest2
[-1])
391 @unittest.skipUnless(zlib
, "requires zlib")
392 def test_manifest_marker(self
):
393 # check that autogenerated MANIFESTs have a marker
394 dist
, cmd
= self
.get_cmd()
395 cmd
.ensure_finalized()
398 f
= open(cmd
.manifest
)
400 manifest
= [line
.strip() for line
in f
.read().split('\n')
401 if line
.strip() != '']
405 self
.assertEqual(manifest
[0],
406 '# file GENERATED by distutils, do NOT edit')
408 @unittest.skipUnless(zlib
, "requires zlib")
409 def test_manual_manifest(self
):
410 # check that a MANIFEST without a marker is left alone
411 dist
, cmd
= self
.get_cmd()
412 cmd
.ensure_finalized()
413 self
.write_file((self
.tmp_dir
, cmd
.manifest
), 'README.manual')
416 f
= open(cmd
.manifest
)
418 manifest
= [line
.strip() for line
in f
.read().split('\n')
419 if line
.strip() != '']
423 self
.assertEqual(manifest
, ['README.manual'])
426 return unittest
.makeSuite(SDistTestCase
)
428 if __name__
== "__main__":
429 run_unittest(test_suite())