test_extension.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Tests for distutils.extension."""
  2. import os
  3. import pathlib
  4. import warnings
  5. from distutils.extension import Extension, read_setup_file
  6. import pytest
  7. from test.support.warnings_helper import check_warnings
  8. class TestExtension:
  9. def test_read_setup_file(self):
  10. # trying to read a Setup file
  11. # (sample extracted from the PyGame project)
  12. setup = os.path.join(os.path.dirname(__file__), 'Setup.sample')
  13. exts = read_setup_file(setup)
  14. names = [ext.name for ext in exts]
  15. names.sort()
  16. # here are the extensions read_setup_file should have created
  17. # out of the file
  18. wanted = [
  19. '_arraysurfarray',
  20. '_camera',
  21. '_numericsndarray',
  22. '_numericsurfarray',
  23. 'base',
  24. 'bufferproxy',
  25. 'cdrom',
  26. 'color',
  27. 'constants',
  28. 'display',
  29. 'draw',
  30. 'event',
  31. 'fastevent',
  32. 'font',
  33. 'gfxdraw',
  34. 'image',
  35. 'imageext',
  36. 'joystick',
  37. 'key',
  38. 'mask',
  39. 'mixer',
  40. 'mixer_music',
  41. 'mouse',
  42. 'movie',
  43. 'overlay',
  44. 'pixelarray',
  45. 'pypm',
  46. 'rect',
  47. 'rwobject',
  48. 'scrap',
  49. 'surface',
  50. 'surflock',
  51. 'time',
  52. 'transform',
  53. ]
  54. assert names == wanted
  55. def test_extension_init(self):
  56. # the first argument, which is the name, must be a string
  57. with pytest.raises(TypeError):
  58. Extension(1, [])
  59. ext = Extension('name', [])
  60. assert ext.name == 'name'
  61. # the second argument, which is the list of files, must
  62. # be an iterable of strings or PathLike objects, and not a string
  63. with pytest.raises(TypeError):
  64. Extension('name', 'file')
  65. with pytest.raises(TypeError):
  66. Extension('name', ['file', 1])
  67. ext = Extension('name', ['file1', 'file2'])
  68. assert ext.sources == ['file1', 'file2']
  69. ext = Extension('name', [pathlib.Path('file1'), pathlib.Path('file2')])
  70. assert ext.sources == ['file1', 'file2']
  71. # any non-string iterable of strings or PathLike objects should work
  72. ext = Extension('name', ('file1', 'file2')) # tuple
  73. assert ext.sources == ['file1', 'file2']
  74. ext = Extension('name', {'file1', 'file2'}) # set
  75. assert sorted(ext.sources) == ['file1', 'file2']
  76. ext = Extension('name', iter(['file1', 'file2'])) # iterator
  77. assert ext.sources == ['file1', 'file2']
  78. ext = Extension('name', [pathlib.Path('file1'), 'file2']) # mixed types
  79. assert ext.sources == ['file1', 'file2']
  80. # others arguments have defaults
  81. for attr in (
  82. 'include_dirs',
  83. 'define_macros',
  84. 'undef_macros',
  85. 'library_dirs',
  86. 'libraries',
  87. 'runtime_library_dirs',
  88. 'extra_objects',
  89. 'extra_compile_args',
  90. 'extra_link_args',
  91. 'export_symbols',
  92. 'swig_opts',
  93. 'depends',
  94. ):
  95. assert getattr(ext, attr) == []
  96. assert ext.language is None
  97. assert ext.optional is None
  98. # if there are unknown keyword options, warn about them
  99. with check_warnings() as w:
  100. warnings.simplefilter('always')
  101. ext = Extension('name', ['file1', 'file2'], chic=True)
  102. assert len(w.warnings) == 1
  103. assert str(w.warnings[0].message) == "Unknown Extension options: 'chic'"