blob: 8248c08b19b7191a2871d85fb42536d499bfdc02 [file] [log] [blame]
Tarek Ziadé73347352009-02-28 10:08:02 +00001"""Tests for distutils.command.bdist_rpm."""
2
3import unittest
4import sys
5import os
6import tempfile
7import shutil
8
Georg Brandl28aeb0d2010-02-06 23:53:52 +00009from test.test_support import run_unittest
10
Serhiy Storchaka59addd62016-10-23 22:54:43 +030011try:
12 import zlib
13except ImportError:
14 zlib = None
15
Tarek Ziadé73347352009-02-28 10:08:02 +000016from 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,
R David Murrayece9d5a2014-09-30 20:57:24 -040033 support.EnvironGuard,
Tarek Ziadé73347352009-02-28 10:08:02 +000034 support.LoggingSilencer,
35 unittest.TestCase):
36
37 def setUp(self):
38 super(BuildRpmTestCase, self).setUp()
39 self.old_location = os.getcwd()
Tarek Ziadé2b06d422009-10-18 09:28:26 +000040 self.old_sys_argv = sys.argv, sys.argv[:]
Tarek Ziadé73347352009-02-28 10:08:02 +000041
42 def tearDown(self):
43 os.chdir(self.old_location)
Tarek Ziadé2b06d422009-10-18 09:28:26 +000044 sys.argv = self.old_sys_argv[0]
45 sys.argv[:] = self.old_sys_argv[1]
Tarek Ziadé73347352009-02-28 10:08:02 +000046 super(BuildRpmTestCase, self).tearDown()
47
Serhiy Storchaka57bc6da2013-12-18 16:45:37 +020048 # 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')
Serhiy Storchaka59addd62016-10-23 22:54:43 +030052 @unittest.skipUnless(zlib, "requires zlib")
Serhiy Storchaka57bc6da2013-12-18 16:45:37 +020053 @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')
Tarek Ziadé73347352009-02-28 10:08:02 +000057 def test_quiet(self):
Tarek Ziadé73347352009-02-28 10:08:02 +000058 # let's create a package
59 tmp_dir = self.mkdtemp()
R David Murrayece9d5a2014-09-30 20:57:24 -040060 os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
Tarek Ziadé73347352009-02-28 10:08:02 +000061 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'))
Serhiy Storchaka25a23ef2013-11-17 00:29:27 +020085 self.assertIn('foo-0.1-1.noarch.rpm', dist_created)
Tarek Ziadé73347352009-02-28 10:08:02 +000086
Éric Araujod673b622012-02-26 01:16:47 +010087 # 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
Serhiy Storchaka57bc6da2013-12-18 16:45:37 +020091 # 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')
Serhiy Storchaka59addd62016-10-23 22:54:43 +030095 @unittest.skipUnless(zlib, "requires zlib")
Serhiy Storchaka57bc6da2013-12-18 16:45:37 +020096 # 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')
Tarek Ziadé73347352009-02-28 10:08:02 +0000101 def test_no_optimize_flag(self):
Martin Panter88e42062016-12-18 05:27:49 +0000102 # let's create a package that breaks bdist_rpm
Tarek Ziadé73347352009-02-28 10:08:02 +0000103 tmp_dir = self.mkdtemp()
R David Murrayece9d5a2014-09-30 20:57:24 -0400104 os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
Tarek Ziadé73347352009-02-28 10:08:02 +0000105 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
Tarek Ziadé73347352009-02-28 10:08:02 +0000123 cmd.quiet = 1
124 cmd.ensure_finalized()
125 cmd.run()
126
127 dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
Serhiy Storchaka25a23ef2013-11-17 00:29:27 +0200128 self.assertIn('foo-0.1-1.noarch.rpm', dist_created)
Éric Araujod673b622012-02-26 01:16:47 +0100129
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
Tarek Ziadé73347352009-02-28 10:08:02 +0000134 os.remove(os.path.join(pkg_dir, 'dist', 'foo-0.1-1.noarch.rpm'))
135
Tarek Ziadé73347352009-02-28 10:08:02 +0000136def test_suite():
137 return unittest.makeSuite(BuildRpmTestCase)
138
139if __name__ == '__main__':
Georg Brandl28aeb0d2010-02-06 23:53:52 +0000140 run_unittest(test_suite())