1"""Tests for distutils.command.bdist_rpm."""
2
3import unittest
4import sys
5import os
6import tempfile
7import shutil
8
9from test.test_support import run_unittest
10
11from distutils.core import Distribution
12from distutils.command.bdist_rpm import bdist_rpm
13from distutils.tests import support
14from distutils.spawn import find_executable
15from distutils import spawn
16from distutils.errors import DistutilsExecError
17
18SETUP_PY = """\
19from distutils.core import setup
20import foo
21
22setup(name='foo', version='0.1', py_modules=['foo'],
23      url='xxx', author='xxx', author_email='xxx')
24
25"""
26
27class BuildRpmTestCase(support.TempdirManager,
28                       support.LoggingSilencer,
29                       unittest.TestCase):
30
31    def setUp(self):
32        super(BuildRpmTestCase, self).setUp()
33        self.old_location = os.getcwd()
34        self.old_sys_argv = sys.argv, sys.argv[:]
35
36    def tearDown(self):
37        os.chdir(self.old_location)
38        sys.argv = self.old_sys_argv[0]
39        sys.argv[:] = self.old_sys_argv[1]
40        super(BuildRpmTestCase, self).tearDown()
41
42    def test_quiet(self):
43
44        # XXX I am unable yet to make this test work without
45        # spurious sdtout/stderr output under Mac OS X
46        if sys.platform != 'linux2':
47            return
48
49        # this test will run only if the rpm commands are found
50        if (find_executable('rpm') is None or
51            find_executable('rpmbuild') is None):
52            return
53
54        # let's create a package
55        tmp_dir = self.mkdtemp()
56        pkg_dir = os.path.join(tmp_dir, 'foo')
57        os.mkdir(pkg_dir)
58        self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
59        self.write_file((pkg_dir, 'foo.py'), '#')
60        self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
61        self.write_file((pkg_dir, 'README'), '')
62
63        dist = Distribution({'name': 'foo', 'version': '0.1',
64                             'py_modules': ['foo'],
65                             'url': 'xxx', 'author': 'xxx',
66                             'author_email': 'xxx'})
67        dist.script_name = 'setup.py'
68        os.chdir(pkg_dir)
69
70        sys.argv = ['setup.py']
71        cmd = bdist_rpm(dist)
72        cmd.fix_python = True
73
74        # running in quiet mode
75        cmd.quiet = 1
76        cmd.ensure_finalized()
77        cmd.run()
78
79        dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
80        self.assertTrue('foo-0.1-1.noarch.rpm' in dist_created)
81
82        # bug #2945: upload ignores bdist_rpm files
83        self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files)
84        self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files)
85
86    def test_no_optimize_flag(self):
87
88        # XXX I am unable yet to make this test work without
89        # spurious sdtout/stderr output under Mac OS X
90        if sys.platform != 'linux2':
91            return
92
93        # http://bugs.python.org/issue1533164
94        # this test will run only if the rpm command is found
95        if (find_executable('rpm') is None or
96            find_executable('rpmbuild') is None):
97            return
98
99        # let's create a package that brakes bdist_rpm
100        tmp_dir = self.mkdtemp()
101        pkg_dir = os.path.join(tmp_dir, 'foo')
102        os.mkdir(pkg_dir)
103        self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
104        self.write_file((pkg_dir, 'foo.py'), '#')
105        self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
106        self.write_file((pkg_dir, 'README'), '')
107
108        dist = Distribution({'name': 'foo', 'version': '0.1',
109                             'py_modules': ['foo'],
110                             'url': 'xxx', 'author': 'xxx',
111                             'author_email': 'xxx'})
112        dist.script_name = 'setup.py'
113        os.chdir(pkg_dir)
114
115        sys.argv = ['setup.py']
116        cmd = bdist_rpm(dist)
117        cmd.fix_python = True
118
119        cmd.quiet = 1
120        cmd.ensure_finalized()
121        cmd.run()
122
123        dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
124        self.assertTrue('foo-0.1-1.noarch.rpm' in dist_created)
125
126        # bug #2945: upload ignores bdist_rpm files
127        self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files)
128        self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files)
129
130        os.remove(os.path.join(pkg_dir, 'dist', 'foo-0.1-1.noarch.rpm'))
131
132def test_suite():
133    return unittest.makeSuite(BuildRpmTestCase)
134
135if __name__ == '__main__':
136    run_unittest(test_suite())
137