test_bdist_dumb.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """Tests for distutils.command.bdist_dumb."""
  2. import os
  3. import sys
  4. import zipfile
  5. import unittest
  6. from test.support import run_unittest
  7. from distutils.core import Distribution
  8. from distutils.command.bdist_dumb import bdist_dumb
  9. from distutils.tests import support
  10. SETUP_PY = """\
  11. from distutils.core import setup
  12. import foo
  13. setup(name='foo', version='0.1', py_modules=['foo'],
  14. url='xxx', author='xxx', author_email='xxx')
  15. """
  16. try:
  17. import zlib
  18. ZLIB_SUPPORT = True
  19. except ImportError:
  20. ZLIB_SUPPORT = False
  21. class BuildDumbTestCase(support.TempdirManager,
  22. support.LoggingSilencer,
  23. support.EnvironGuard,
  24. unittest.TestCase):
  25. def setUp(self):
  26. super(BuildDumbTestCase, self).setUp()
  27. self.old_location = os.getcwd()
  28. self.old_sys_argv = sys.argv, sys.argv[:]
  29. def tearDown(self):
  30. os.chdir(self.old_location)
  31. sys.argv = self.old_sys_argv[0]
  32. sys.argv[:] = self.old_sys_argv[1]
  33. super(BuildDumbTestCase, self).tearDown()
  34. @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
  35. def test_simple_built(self):
  36. # let's create a simple package
  37. tmp_dir = self.mkdtemp()
  38. pkg_dir = os.path.join(tmp_dir, 'foo')
  39. os.mkdir(pkg_dir)
  40. self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
  41. self.write_file((pkg_dir, 'foo.py'), '#')
  42. self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
  43. self.write_file((pkg_dir, 'README'), '')
  44. dist = Distribution({'name': 'foo', 'version': '0.1',
  45. 'py_modules': ['foo'],
  46. 'url': 'xxx', 'author': 'xxx',
  47. 'author_email': 'xxx'})
  48. dist.script_name = 'setup.py'
  49. os.chdir(pkg_dir)
  50. sys.argv = ['setup.py']
  51. cmd = bdist_dumb(dist)
  52. # so the output is the same no matter
  53. # what is the platform
  54. cmd.format = 'zip'
  55. cmd.ensure_finalized()
  56. cmd.run()
  57. # see what we have
  58. dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
  59. base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name)
  60. self.assertEqual(dist_created, [base])
  61. # now let's check what we have in the zip file
  62. fp = zipfile.ZipFile(os.path.join('dist', base))
  63. try:
  64. contents = fp.namelist()
  65. finally:
  66. fp.close()
  67. contents = sorted(filter(None, map(os.path.basename, contents)))
  68. wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py']
  69. if not sys.dont_write_bytecode:
  70. wanted.append('foo.%s.pyc' % sys.implementation.cache_tag)
  71. self.assertEqual(contents, sorted(wanted))
  72. def test_suite():
  73. return unittest.makeSuite(BuildDumbTestCase)
  74. if __name__ == '__main__':
  75. run_unittest(test_suite())