test_bdist_rpm.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """Tests for distutils.command.bdist_rpm."""
  2. import unittest
  3. import sys
  4. import os
  5. from test.support import run_unittest, requires_zlib
  6. from distutils.core import Distribution
  7. from distutils.command.bdist_rpm import bdist_rpm
  8. from distutils.tests import support
  9. from distutils.spawn import find_executable
  10. SETUP_PY = """\
  11. from distutils.core import setup
  12. import foo
  13. setup(name='foo', version='0.1', py_modules=['foo'],
  14. url='xxx', author='xxx', author_email='xxx')
  15. """
  16. class BuildRpmTestCase(support.TempdirManager,
  17. support.EnvironGuard,
  18. support.LoggingSilencer,
  19. unittest.TestCase):
  20. def setUp(self):
  21. try:
  22. sys.executable.encode("UTF-8")
  23. except UnicodeEncodeError:
  24. raise unittest.SkipTest("sys.executable is not encodable to UTF-8")
  25. super(BuildRpmTestCase, self).setUp()
  26. self.old_location = os.getcwd()
  27. self.old_sys_argv = sys.argv, sys.argv[:]
  28. def tearDown(self):
  29. os.chdir(self.old_location)
  30. sys.argv = self.old_sys_argv[0]
  31. sys.argv[:] = self.old_sys_argv[1]
  32. super(BuildRpmTestCase, self).tearDown()
  33. # XXX I am unable yet to make this test work without
  34. # spurious sdtout/stderr output under Mac OS X
  35. @unittest.skipUnless(sys.platform.startswith('linux'),
  36. 'spurious sdtout/stderr output under Mac OS X')
  37. @requires_zlib()
  38. @unittest.skipIf(find_executable('rpm') is None,
  39. 'the rpm command is not found')
  40. @unittest.skipIf(find_executable('rpmbuild') is None,
  41. 'the rpmbuild command is not found')
  42. def test_quiet(self):
  43. # let's create a package
  44. tmp_dir = self.mkdtemp()
  45. os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
  46. pkg_dir = os.path.join(tmp_dir, 'foo')
  47. os.mkdir(pkg_dir)
  48. self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
  49. self.write_file((pkg_dir, 'foo.py'), '#')
  50. self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
  51. self.write_file((pkg_dir, 'README'), '')
  52. dist = Distribution({'name': 'foo', 'version': '0.1',
  53. 'py_modules': ['foo'],
  54. 'url': 'xxx', 'author': 'xxx',
  55. 'author_email': 'xxx'})
  56. dist.script_name = 'setup.py'
  57. os.chdir(pkg_dir)
  58. sys.argv = ['setup.py']
  59. cmd = bdist_rpm(dist)
  60. cmd.fix_python = True
  61. # running in quiet mode
  62. cmd.quiet = 1
  63. cmd.ensure_finalized()
  64. cmd.run()
  65. dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
  66. self.assertIn('foo-0.1-1.noarch.rpm', dist_created)
  67. # bug #2945: upload ignores bdist_rpm files
  68. self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files)
  69. self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files)
  70. # XXX I am unable yet to make this test work without
  71. # spurious sdtout/stderr output under Mac OS X
  72. @unittest.skipUnless(sys.platform.startswith('linux'),
  73. 'spurious sdtout/stderr output under Mac OS X')
  74. @requires_zlib()
  75. # http://bugs.python.org/issue1533164
  76. @unittest.skipIf(find_executable('rpm') is None,
  77. 'the rpm command is not found')
  78. @unittest.skipIf(find_executable('rpmbuild') is None,
  79. 'the rpmbuild command is not found')
  80. def test_no_optimize_flag(self):
  81. # let's create a package that breaks bdist_rpm
  82. tmp_dir = self.mkdtemp()
  83. os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
  84. pkg_dir = os.path.join(tmp_dir, 'foo')
  85. os.mkdir(pkg_dir)
  86. self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
  87. self.write_file((pkg_dir, 'foo.py'), '#')
  88. self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
  89. self.write_file((pkg_dir, 'README'), '')
  90. dist = Distribution({'name': 'foo', 'version': '0.1',
  91. 'py_modules': ['foo'],
  92. 'url': 'xxx', 'author': 'xxx',
  93. 'author_email': 'xxx'})
  94. dist.script_name = 'setup.py'
  95. os.chdir(pkg_dir)
  96. sys.argv = ['setup.py']
  97. cmd = bdist_rpm(dist)
  98. cmd.fix_python = True
  99. cmd.quiet = 1
  100. cmd.ensure_finalized()
  101. cmd.run()
  102. dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
  103. self.assertIn('foo-0.1-1.noarch.rpm', dist_created)
  104. # bug #2945: upload ignores bdist_rpm files
  105. self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files)
  106. self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files)
  107. os.remove(os.path.join(pkg_dir, 'dist', 'foo-0.1-1.noarch.rpm'))
  108. def test_suite():
  109. return unittest.makeSuite(BuildRpmTestCase)
  110. if __name__ == '__main__':
  111. run_unittest(test_suite())