test_install_lib.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """Tests for distutils.command.install_data."""
  2. import sys
  3. import os
  4. import importlib.util
  5. import unittest
  6. from distutils.command.install_lib import install_lib
  7. from distutils.extension import Extension
  8. from distutils.tests import support
  9. from distutils.errors import DistutilsOptionError
  10. from test.support import run_unittest
  11. class InstallLibTestCase(support.TempdirManager,
  12. support.LoggingSilencer,
  13. support.EnvironGuard,
  14. unittest.TestCase):
  15. def test_finalize_options(self):
  16. dist = self.create_dist()[1]
  17. cmd = install_lib(dist)
  18. cmd.finalize_options()
  19. self.assertEqual(cmd.compile, 1)
  20. self.assertEqual(cmd.optimize, 0)
  21. # optimize must be 0, 1, or 2
  22. cmd.optimize = 'foo'
  23. self.assertRaises(DistutilsOptionError, cmd.finalize_options)
  24. cmd.optimize = '4'
  25. self.assertRaises(DistutilsOptionError, cmd.finalize_options)
  26. cmd.optimize = '2'
  27. cmd.finalize_options()
  28. self.assertEqual(cmd.optimize, 2)
  29. @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
  30. def test_byte_compile(self):
  31. project_dir, dist = self.create_dist()
  32. os.chdir(project_dir)
  33. cmd = install_lib(dist)
  34. cmd.compile = cmd.optimize = 1
  35. f = os.path.join(project_dir, 'foo.py')
  36. self.write_file(f, '# python file')
  37. cmd.byte_compile([f])
  38. pyc_file = importlib.util.cache_from_source('foo.py', optimization='')
  39. pyc_opt_file = importlib.util.cache_from_source('foo.py',
  40. optimization=cmd.optimize)
  41. self.assertTrue(os.path.exists(pyc_file))
  42. self.assertTrue(os.path.exists(pyc_opt_file))
  43. def test_get_outputs(self):
  44. project_dir, dist = self.create_dist()
  45. os.chdir(project_dir)
  46. os.mkdir('spam')
  47. cmd = install_lib(dist)
  48. # setting up a dist environment
  49. cmd.compile = cmd.optimize = 1
  50. cmd.install_dir = self.mkdtemp()
  51. f = os.path.join(project_dir, 'spam', '__init__.py')
  52. self.write_file(f, '# python package')
  53. cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
  54. cmd.distribution.packages = ['spam']
  55. cmd.distribution.script_name = 'setup.py'
  56. # get_outputs should return 4 elements: spam/__init__.py and .pyc,
  57. # foo.import-tag-abiflags.so / foo.pyd
  58. outputs = cmd.get_outputs()
  59. self.assertEqual(len(outputs), 4, outputs)
  60. def test_get_inputs(self):
  61. project_dir, dist = self.create_dist()
  62. os.chdir(project_dir)
  63. os.mkdir('spam')
  64. cmd = install_lib(dist)
  65. # setting up a dist environment
  66. cmd.compile = cmd.optimize = 1
  67. cmd.install_dir = self.mkdtemp()
  68. f = os.path.join(project_dir, 'spam', '__init__.py')
  69. self.write_file(f, '# python package')
  70. cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
  71. cmd.distribution.packages = ['spam']
  72. cmd.distribution.script_name = 'setup.py'
  73. # get_inputs should return 2 elements: spam/__init__.py and
  74. # foo.import-tag-abiflags.so / foo.pyd
  75. inputs = cmd.get_inputs()
  76. self.assertEqual(len(inputs), 2, inputs)
  77. def test_dont_write_bytecode(self):
  78. # makes sure byte_compile is not used
  79. dist = self.create_dist()[1]
  80. cmd = install_lib(dist)
  81. cmd.compile = 1
  82. cmd.optimize = 1
  83. old_dont_write_bytecode = sys.dont_write_bytecode
  84. sys.dont_write_bytecode = True
  85. try:
  86. cmd.byte_compile([])
  87. finally:
  88. sys.dont_write_bytecode = old_dont_write_bytecode
  89. self.assertIn('byte-compiling is disabled',
  90. self.logs[0][1] % self.logs[0][2])
  91. def test_suite():
  92. return unittest.makeSuite(InstallLibTestCase)
  93. if __name__ == "__main__":
  94. run_unittest(test_suite())