test_config_cmd.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Tests for distutils.command.config."""
  2. import os
  3. import sys
  4. from distutils._log import log
  5. from distutils.command.config import config, dump_file
  6. from distutils.tests import missing_compiler_executable, support
  7. import more_itertools
  8. import path
  9. import pytest
  10. @pytest.fixture(autouse=True)
  11. def info_log(request, monkeypatch):
  12. self = request.instance
  13. self._logs = []
  14. monkeypatch.setattr(log, 'info', self._info)
  15. @support.combine_markers
  16. class TestConfig(support.TempdirManager):
  17. def _info(self, msg, *args):
  18. for line in msg.splitlines():
  19. self._logs.append(line)
  20. def test_dump_file(self):
  21. this_file = path.Path(__file__).with_suffix('.py')
  22. with this_file.open(encoding='utf-8') as f:
  23. numlines = more_itertools.ilen(f)
  24. dump_file(this_file, 'I am the header')
  25. assert len(self._logs) == numlines + 1
  26. @pytest.mark.skipif('platform.system() == "Windows"')
  27. def test_search_cpp(self):
  28. cmd = missing_compiler_executable(['preprocessor'])
  29. if cmd is not None:
  30. self.skipTest(f'The {cmd!r} command is not found')
  31. pkg_dir, dist = self.create_dist()
  32. cmd = config(dist)
  33. cmd._check_compiler()
  34. compiler = cmd.compiler
  35. if sys.platform[:3] == "aix" and "xlc" in compiler.preprocessor[0].lower():
  36. self.skipTest(
  37. 'xlc: The -E option overrides the -P, -o, and -qsyntaxonly options'
  38. )
  39. # simple pattern searches
  40. match = cmd.search_cpp(pattern='xxx', body='/* xxx */')
  41. assert match == 0
  42. match = cmd.search_cpp(pattern='_configtest', body='/* xxx */')
  43. assert match == 1
  44. def test_finalize_options(self):
  45. # finalize_options does a bit of transformation
  46. # on options
  47. pkg_dir, dist = self.create_dist()
  48. cmd = config(dist)
  49. cmd.include_dirs = f'one{os.pathsep}two'
  50. cmd.libraries = 'one'
  51. cmd.library_dirs = f'three{os.pathsep}four'
  52. cmd.ensure_finalized()
  53. assert cmd.include_dirs == ['one', 'two']
  54. assert cmd.libraries == ['one']
  55. assert cmd.library_dirs == ['three', 'four']
  56. def test_clean(self):
  57. # _clean removes files
  58. tmp_dir = self.mkdtemp()
  59. f1 = os.path.join(tmp_dir, 'one')
  60. f2 = os.path.join(tmp_dir, 'two')
  61. self.write_file(f1, 'xxx')
  62. self.write_file(f2, 'xxx')
  63. for f in (f1, f2):
  64. assert os.path.exists(f)
  65. pkg_dir, dist = self.create_dist()
  66. cmd = config(dist)
  67. cmd._clean(f1, f2)
  68. for f in (f1, f2):
  69. assert not os.path.exists(f)