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