run_tests.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2023, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. # -----------------------------------------------------------------------------
  11. import argparse
  12. import sys
  13. import pytest
  14. from PyInstaller.compat import importlib_metadata
  15. def paths_to_test(include_only=None):
  16. """
  17. If ``include_only`` is falsey, this functions returns paths from all entry points. Otherwise, this parameter
  18. must be a string or sequence of strings. In this case, this function will return *only* paths from entry points
  19. whose ``module_name`` begins with the provided string(s).
  20. """
  21. # Convert a string to a list.
  22. if isinstance(include_only, str):
  23. include_only = [include_only]
  24. # Walk through all entry points.
  25. test_path_list = []
  26. for entry_point in importlib_metadata.entry_points(group="pyinstaller40", name="tests"):
  27. # Implement ``include_only``.
  28. if (
  29. not include_only # If falsey, include everything,
  30. # Otherwise, include only the specified modules.
  31. or any(entry_point.module.startswith(name) for name in include_only)
  32. ):
  33. test_path_list += list(entry_point.load()())
  34. return test_path_list
  35. # Run pytest on all tests registered by the PyInstaller setuptools testing entry point. If provided,
  36. # the ``include_only`` argument is passed to ``path_to_test``.
  37. def run_pytest(*args, **kwargs):
  38. paths = paths_to_test(include_only=kwargs.pop("include_only", None))
  39. # Return an error code if no tests were discovered.
  40. if not paths:
  41. print("Error: no tests discovered.", file=sys.stderr)
  42. # This indicates no tests were discovered; see
  43. # https://docs.pytest.org/en/latest/usage.html#possible-exit-codes.
  44. return 5
  45. else:
  46. # See https://docs.pytest.org/en/latest/usage.html#calling-pytest-from-python-code.
  47. # Omit ``args[0]``, which is the name of this script.
  48. print("pytest " + " ".join([*paths, *args[1:]]))
  49. return pytest.main([*paths, *args[1:]], **kwargs)
  50. if __name__ == "__main__":
  51. # Look only for the ``--include_only`` argument.
  52. parser = argparse.ArgumentParser(description='Run PyInstaller packaging tests.')
  53. parser.add_argument(
  54. "--include_only",
  55. action="append",
  56. help="Only run tests from the specified package.",
  57. )
  58. args, unknown = parser.parse_known_args(sys.argv)
  59. # Convert the parsed args into a dict using ``vars(args)``.
  60. sys.exit(run_pytest(*unknown, **vars(args)))