1"""Tests for distutils.command.bdist_dumb."""
2
3import os
4import sys
5import zipfile
6import unittest
7from test.support import run_unittest
8
9from distutils.core import Distribution
10from distutils.command.bdist_dumb import bdist_dumb
11from distutils.tests import support
12
13SETUP_PY = """\
14from distutils.core import setup
15import foo
16
17setup(name='foo', version='0.1', py_modules=['foo'],
18      url='xxx', author='xxx', author_email='xxx')
19
20"""
21
22try:
23    import zlib
24    ZLIB_SUPPORT = True
25except ImportError:
26    ZLIB_SUPPORT = False
27
28
29class 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_SUPPORT, 'Need zlib support to run')
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
78        self.assertEqual(dist_created, [base])
79
80        # now let's check what we have in the zip file
81        fp = zipfile.ZipFile(os.path.join('dist', base))
82        try:
83            contents = fp.namelist()
84        finally:
85            fp.close()
86
87        contents = sorted(filter(None, map(os.path.basename, contents)))
88        wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py']
89        if not sys.dont_write_bytecode:
90            wanted.append('foo.%s.pyc' % sys.implementation.cache_tag)
91        self.assertEqual(contents, sorted(wanted))
92
93def test_suite():
94    return unittest.makeSuite(BuildDumbTestCase)
95
96if __name__ == '__main__':
97    run_unittest(test_suite())
98