test_build_clib.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Tests for distutils.command.build_clib."""
  2. import unittest
  3. import os
  4. import sys
  5. from test.support import run_unittest, missing_compiler_executable
  6. from distutils.command.build_clib import build_clib
  7. from distutils.errors import DistutilsSetupError
  8. from distutils.tests import support
  9. class BuildCLibTestCase(support.TempdirManager,
  10. support.LoggingSilencer,
  11. unittest.TestCase):
  12. def test_check_library_dist(self):
  13. pkg_dir, dist = self.create_dist()
  14. cmd = build_clib(dist)
  15. # 'libraries' option must be a list
  16. self.assertRaises(DistutilsSetupError, cmd.check_library_list, 'foo')
  17. # each element of 'libraries' must a 2-tuple
  18. self.assertRaises(DistutilsSetupError, cmd.check_library_list,
  19. ['foo1', 'foo2'])
  20. # first element of each tuple in 'libraries'
  21. # must be a string (the library name)
  22. self.assertRaises(DistutilsSetupError, cmd.check_library_list,
  23. [(1, 'foo1'), ('name', 'foo2')])
  24. # library name may not contain directory separators
  25. self.assertRaises(DistutilsSetupError, cmd.check_library_list,
  26. [('name', 'foo1'),
  27. ('another/name', 'foo2')])
  28. # second element of each tuple must be a dictionary (build info)
  29. self.assertRaises(DistutilsSetupError, cmd.check_library_list,
  30. [('name', {}),
  31. ('another', 'foo2')])
  32. # those work
  33. libs = [('name', {}), ('name', {'ok': 'good'})]
  34. cmd.check_library_list(libs)
  35. def test_get_source_files(self):
  36. pkg_dir, dist = self.create_dist()
  37. cmd = build_clib(dist)
  38. # "in 'libraries' option 'sources' must be present and must be
  39. # a list of source filenames
  40. cmd.libraries = [('name', {})]
  41. self.assertRaises(DistutilsSetupError, cmd.get_source_files)
  42. cmd.libraries = [('name', {'sources': 1})]
  43. self.assertRaises(DistutilsSetupError, cmd.get_source_files)
  44. cmd.libraries = [('name', {'sources': ['a', 'b']})]
  45. self.assertEqual(cmd.get_source_files(), ['a', 'b'])
  46. cmd.libraries = [('name', {'sources': ('a', 'b')})]
  47. self.assertEqual(cmd.get_source_files(), ['a', 'b'])
  48. cmd.libraries = [('name', {'sources': ('a', 'b')}),
  49. ('name2', {'sources': ['c', 'd']})]
  50. self.assertEqual(cmd.get_source_files(), ['a', 'b', 'c', 'd'])
  51. def test_build_libraries(self):
  52. pkg_dir, dist = self.create_dist()
  53. cmd = build_clib(dist)
  54. class FakeCompiler:
  55. def compile(*args, **kw):
  56. pass
  57. create_static_lib = compile
  58. cmd.compiler = FakeCompiler()
  59. # build_libraries is also doing a bit of typo checking
  60. lib = [('name', {'sources': 'notvalid'})]
  61. self.assertRaises(DistutilsSetupError, cmd.build_libraries, lib)
  62. lib = [('name', {'sources': list()})]
  63. cmd.build_libraries(lib)
  64. lib = [('name', {'sources': tuple()})]
  65. cmd.build_libraries(lib)
  66. def test_finalize_options(self):
  67. pkg_dir, dist = self.create_dist()
  68. cmd = build_clib(dist)
  69. cmd.include_dirs = 'one-dir'
  70. cmd.finalize_options()
  71. self.assertEqual(cmd.include_dirs, ['one-dir'])
  72. cmd.include_dirs = None
  73. cmd.finalize_options()
  74. self.assertEqual(cmd.include_dirs, [])
  75. cmd.distribution.libraries = 'WONTWORK'
  76. self.assertRaises(DistutilsSetupError, cmd.finalize_options)
  77. @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
  78. def test_run(self):
  79. pkg_dir, dist = self.create_dist()
  80. cmd = build_clib(dist)
  81. foo_c = os.path.join(pkg_dir, 'foo.c')
  82. self.write_file(foo_c, 'int main(void) { return 1;}\n')
  83. cmd.libraries = [('foo', {'sources': [foo_c]})]
  84. build_temp = os.path.join(pkg_dir, 'build')
  85. os.mkdir(build_temp)
  86. cmd.build_temp = build_temp
  87. cmd.build_clib = build_temp
  88. # Before we run the command, we want to make sure
  89. # all commands are present on the system.
  90. ccmd = missing_compiler_executable()
  91. if ccmd is not None:
  92. self.skipTest('The %r command is not found' % ccmd)
  93. # this should work
  94. cmd.run()
  95. # let's check the result
  96. self.assertIn('libfoo.a', os.listdir(build_temp))
  97. def test_suite():
  98. return unittest.makeSuite(BuildCLibTestCase)
  99. if __name__ == "__main__":
  100. run_unittest(test_suite())