1 """Tests for distutils.command.bdist_dumb.""" 2 3 import os 4 import sys 5 import zipfile 6 import unittest 7 from test.test_support import run_unittest 8 9 # zlib is not used here, but if it's not available 10 # test_simple_built will fail 11 try: 12 import zlib 13 except ImportError: 14 zlib = None 15 16 from distutils.core import Distribution 17 from distutils.command.bdist_dumb import bdist_dumb 18 from distutils.tests import support 19 20 SETUP_PY = """\ 21 from distutils.core import setup 22 import foo 23 24 setup(name='foo', version='0.1', py_modules=['foo'], 25 url='xxx', author='xxx', author_email='xxx') 26 27 """ 28 29 class BuildDumbTestCase(support.TempdirManager, 30 support.LoggingSilencer, 31 support.EnvironGuard, 32 unittest.TestCase): 33 34 def setUp(self): 35 super(BuildDumbTestCase, self).setUp() 36 self.old_location = os.getcwd() 37 self.old_sys_argv = sys.argv, sys.argv[:] 38 39 def tearDown(self): 40 os.chdir(self.old_location) 41 sys.argv = self.old_sys_argv[0] 42 sys.argv[:] = self.old_sys_argv[1] 43 super(BuildDumbTestCase, self).tearDown() 44 45 @unittest.skipUnless(zlib, "requires zlib") 46 def test_simple_built(self): 47 48 # let's create a simple package 49 tmp_dir = self.mkdtemp() 50 pkg_dir = os.path.join(tmp_dir, 'foo') 51 os.mkdir(pkg_dir) 52 self.write_file((pkg_dir, 'setup.py'), SETUP_PY) 53 self.write_file((pkg_dir, 'foo.py'), '#') 54 self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py') 55 self.write_file((pkg_dir, 'README'), '') 56 57 dist = Distribution({'name': 'foo', 'version': '0.1', 58 'py_modules': ['foo'], 59 'url': 'xxx', 'author': 'xxx', 60 'author_email': 'xxx'}) 61 dist.script_name = 'setup.py' 62 os.chdir(pkg_dir) 63 64 sys.argv = ['setup.py'] 65 cmd = bdist_dumb(dist) 66 67 # so the output is the same no matter 68 # what is the platform 69 cmd.format = 'zip' 70 71 cmd.ensure_finalized() 72 cmd.run() 73 74 # see what we have 75 dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) 76 base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name) 77 if os.name == 'os2': 78 base = base.replace(':', '-') 79 80 self.assertEqual(dist_created, [base]) 81 82 # now let's check what we have in the zip file 83 fp = zipfile.ZipFile(os.path.join('dist', base)) 84 try: 85 contents = fp.namelist() 86 finally: 87 fp.close() 88 89 contents = sorted(os.path.basename(fn) for fn in contents) 90 wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py'] 91 if not sys.dont_write_bytecode: 92 wanted.append('foo.pyc') 93 self.assertEqual(contents, sorted(wanted)) 94 95 def test_finalize_options(self): 96 pkg_dir, dist = self.create_dist() 97 os.chdir(pkg_dir) 98 cmd = bdist_dumb(dist) 99 self.assertEqual(cmd.bdist_dir, None) 100 cmd.finalize_options() 101 102 # bdist_dir is initialized to bdist_base/dumb if not set 103 base = cmd.get_finalized_command('bdist').bdist_base 104 self.assertEqual(cmd.bdist_dir, os.path.join(base, 'dumb')) 105 106 # the format is set to a default value depending on the os.name 107 default = cmd.default_format[os.name] 108 self.assertEqual(cmd.format, default) 109 110 def test_suite(): 111 return unittest.makeSuite(BuildDumbTestCase) 112 113 if __name__ == '__main__': 114 run_unittest(test_suite()) 115