support.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Support code for distutils test cases."""
  2. import itertools
  3. import os
  4. import pathlib
  5. import shutil
  6. import sys
  7. import sysconfig
  8. import tempfile
  9. from distutils.core import Distribution
  10. import pytest
  11. from more_itertools import always_iterable
  12. @pytest.mark.usefixtures('distutils_managed_tempdir')
  13. class TempdirManager:
  14. """
  15. Mix-in class that handles temporary directories for test cases.
  16. """
  17. def mkdtemp(self):
  18. """Create a temporary directory that will be cleaned up.
  19. Returns the path of the directory.
  20. """
  21. d = tempfile.mkdtemp()
  22. self.tempdirs.append(d)
  23. return d
  24. def write_file(self, path, content='xxx'):
  25. """Writes a file in the given path.
  26. path can be a string or a sequence.
  27. """
  28. pathlib.Path(*always_iterable(path)).write_text(content, encoding='utf-8')
  29. def create_dist(self, pkg_name='foo', **kw):
  30. """Will generate a test environment.
  31. This function creates:
  32. - a Distribution instance using keywords
  33. - a temporary directory with a package structure
  34. It returns the package directory and the distribution
  35. instance.
  36. """
  37. tmp_dir = self.mkdtemp()
  38. pkg_dir = os.path.join(tmp_dir, pkg_name)
  39. os.mkdir(pkg_dir)
  40. dist = Distribution(attrs=kw)
  41. return pkg_dir, dist
  42. class DummyCommand:
  43. """Class to store options for retrieval via set_undefined_options()."""
  44. def __init__(self, **kwargs):
  45. vars(self).update(kwargs)
  46. def ensure_finalized(self):
  47. pass
  48. def copy_xxmodule_c(directory):
  49. """Helper for tests that need the xxmodule.c source file.
  50. Example use:
  51. def test_compile(self):
  52. copy_xxmodule_c(self.tmpdir)
  53. self.assertIn('xxmodule.c', os.listdir(self.tmpdir))
  54. If the source file can be found, it will be copied to *directory*. If not,
  55. the test will be skipped. Errors during copy are not caught.
  56. """
  57. shutil.copy(_get_xxmodule_path(), os.path.join(directory, 'xxmodule.c'))
  58. def _get_xxmodule_path():
  59. source_name = 'xxmodule.c' if sys.version_info > (3, 9) else 'xxmodule-3.8.c'
  60. return os.path.join(os.path.dirname(__file__), source_name)
  61. def fixup_build_ext(cmd):
  62. """Function needed to make build_ext tests pass.
  63. When Python was built with --enable-shared on Unix, -L. is not enough to
  64. find libpython<blah>.so, because regrtest runs in a tempdir, not in the
  65. source directory where the .so lives.
  66. When Python was built with in debug mode on Windows, build_ext commands
  67. need their debug attribute set, and it is not done automatically for
  68. some reason.
  69. This function handles both of these things. Example use:
  70. cmd = build_ext(dist)
  71. support.fixup_build_ext(cmd)
  72. cmd.ensure_finalized()
  73. Unlike most other Unix platforms, Mac OS X embeds absolute paths
  74. to shared libraries into executables, so the fixup is not needed there.
  75. """
  76. if os.name == 'nt':
  77. cmd.debug = sys.executable.endswith('_d.exe')
  78. elif sysconfig.get_config_var('Py_ENABLE_SHARED'):
  79. # To further add to the shared builds fun on Unix, we can't just add
  80. # library_dirs to the Extension() instance because that doesn't get
  81. # plumbed through to the final compiler command.
  82. runshared = sysconfig.get_config_var('RUNSHARED')
  83. if runshared is None:
  84. cmd.library_dirs = ['.']
  85. else:
  86. if sys.platform == 'darwin':
  87. cmd.library_dirs = []
  88. else:
  89. name, equals, value = runshared.partition('=')
  90. cmd.library_dirs = [d for d in value.split(os.pathsep) if d]
  91. def combine_markers(cls):
  92. """
  93. pytest will honor markers as found on the class, but when
  94. markers are on multiple subclasses, only one appears. Use
  95. this decorator to combine those markers.
  96. """
  97. cls.pytestmark = [
  98. mark
  99. for base in itertools.chain([cls], cls.__bases__)
  100. for mark in getattr(base, 'pytestmark', [])
  101. ]
  102. return cls