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