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