Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 1 | """Tests for distutils.command.bdist_rpm.""" |
| 2 | |
| 3 | import unittest |
| 4 | import sys |
| 5 | import os |
| 6 | import tempfile |
| 7 | import shutil |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 8 | from test.support import run_unittest |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 9 | |
| 10 | from distutils.core import Distribution |
| 11 | from distutils.command.bdist_rpm import bdist_rpm |
| 12 | from distutils.tests import support |
| 13 | from distutils.spawn import find_executable |
| 14 | from distutils import spawn |
| 15 | from distutils.errors import DistutilsExecError |
| 16 | |
| 17 | SETUP_PY = """\ |
| 18 | from distutils.core import setup |
| 19 | import foo |
| 20 | |
| 21 | setup(name='foo', version='0.1', py_modules=['foo'], |
| 22 | url='xxx', author='xxx', author_email='xxx') |
| 23 | |
| 24 | """ |
| 25 | |
| 26 | class BuildRpmTestCase(support.TempdirManager, |
| 27 | support.LoggingSilencer, |
| 28 | unittest.TestCase): |
| 29 | |
| 30 | def setUp(self): |
Victor Stinner | 22a8cbe | 2011-03-14 20:03:36 -0400 | [diff] [blame] | 31 | try: |
| 32 | sys.executable.encode("UTF-8") |
| 33 | except UnicodeEncodeError: |
| 34 | raise unittest.SkipTest("sys.executable is not encodable to UTF-8") |
| 35 | |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 36 | super(BuildRpmTestCase, self).setUp() |
| 37 | self.old_location = os.getcwd() |
Tarek Ziadé | 430fb63 | 2009-10-18 11:34:51 +0000 | [diff] [blame] | 38 | self.old_sys_argv = sys.argv, sys.argv[:] |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 39 | |
| 40 | def tearDown(self): |
| 41 | os.chdir(self.old_location) |
Tarek Ziadé | 430fb63 | 2009-10-18 11:34:51 +0000 | [diff] [blame] | 42 | sys.argv = self.old_sys_argv[0] |
| 43 | sys.argv[:] = self.old_sys_argv[1] |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 44 | super(BuildRpmTestCase, self).tearDown() |
| 45 | |
| 46 | def test_quiet(self): |
| 47 | |
| 48 | # XXX I am unable yet to make this test work without |
| 49 | # spurious sdtout/stderr output under Mac OS X |
Victor Stinner | 9c3de4a | 2011-08-17 20:49:41 +0200 | [diff] [blame] | 50 | if not sys.platform.startswith('linux'): |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 51 | return |
| 52 | |
| 53 | # this test will run only if the rpm commands are found |
| 54 | if (find_executable('rpm') is None or |
| 55 | find_executable('rpmbuild') is None): |
| 56 | return |
| 57 | |
| 58 | # let's create a package |
| 59 | tmp_dir = self.mkdtemp() |
| 60 | pkg_dir = os.path.join(tmp_dir, 'foo') |
| 61 | os.mkdir(pkg_dir) |
| 62 | self.write_file((pkg_dir, 'setup.py'), SETUP_PY) |
| 63 | self.write_file((pkg_dir, 'foo.py'), '#') |
| 64 | self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py') |
| 65 | self.write_file((pkg_dir, 'README'), '') |
| 66 | |
| 67 | dist = Distribution({'name': 'foo', 'version': '0.1', |
| 68 | 'py_modules': ['foo'], |
| 69 | 'url': 'xxx', 'author': 'xxx', |
| 70 | 'author_email': 'xxx'}) |
| 71 | dist.script_name = 'setup.py' |
| 72 | os.chdir(pkg_dir) |
| 73 | |
| 74 | sys.argv = ['setup.py'] |
| 75 | cmd = bdist_rpm(dist) |
| 76 | cmd.fix_python = True |
| 77 | |
| 78 | # running in quiet mode |
| 79 | cmd.quiet = 1 |
| 80 | cmd.ensure_finalized() |
| 81 | cmd.run() |
| 82 | |
| 83 | dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 84 | self.assertTrue('foo-0.1-1.noarch.rpm' in dist_created) |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 85 | |
Éric Araujo | 73cec21 | 2012-01-15 02:48:55 +0100 | [diff] [blame] | 86 | # bug #2945: upload ignores bdist_rpm files |
| 87 | self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files) |
| 88 | self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files) |
| 89 | |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 90 | def test_no_optimize_flag(self): |
| 91 | |
| 92 | # XXX I am unable yet to make this test work without |
| 93 | # spurious sdtout/stderr output under Mac OS X |
Victor Stinner | 9c3de4a | 2011-08-17 20:49:41 +0200 | [diff] [blame] | 94 | if not sys.platform.startswith('linux'): |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 95 | return |
| 96 | |
| 97 | # http://bugs.python.org/issue1533164 |
| 98 | # this test will run only if the rpm command is found |
| 99 | if (find_executable('rpm') is None or |
| 100 | find_executable('rpmbuild') is None): |
| 101 | return |
| 102 | |
| 103 | # let's create a package that brakes bdist_rpm |
| 104 | tmp_dir = self.mkdtemp() |
| 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 | |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 123 | cmd.quiet = 1 |
| 124 | cmd.ensure_finalized() |
| 125 | cmd.run() |
| 126 | |
| 127 | dist_created = os.listdir(os.path.join(pkg_dir, 'dist')) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 128 | self.assertTrue('foo-0.1-1.noarch.rpm' in dist_created) |
Éric Araujo | 73cec21 | 2012-01-15 02:48:55 +0100 | [diff] [blame] | 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 | |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 134 | os.remove(os.path.join(pkg_dir, 'dist', 'foo-0.1-1.noarch.rpm')) |
| 135 | |
Tarek Ziadé | f072122 | 2009-02-28 10:16:43 +0000 | [diff] [blame] | 136 | def test_suite(): |
| 137 | return unittest.makeSuite(BuildRpmTestCase) |
| 138 | |
| 139 | if __name__ == '__main__': |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 140 | run_unittest(test_suite()) |