test_build_clib.py 4.2 KB

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