run_tests.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2021, 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 pkg_resources
  14. import pytest
  15. from PyInstaller import compat
  16. def paths_to_test(include_only=None):
  17. """
  18. If ``include_only`` is falsey, this functions returns paths from all entry points. Otherwise, this parameter
  19. must be a string or sequence of strings. In this case, this function will return *only* paths from entry points
  20. whose ``module_name`` begins with the provided string(s).
  21. """
  22. # Convert a string to a list.
  23. if isinstance(include_only, compat.string_types):
  24. include_only = [include_only]
  25. # Walk through all entry points.
  26. test_path_list = []
  27. for entry_point in pkg_resources.iter_entry_points("pyinstaller40", "tests"):
  28. # Implement ``include_only``.
  29. if (
  30. not include_only # If falsey, include everything,
  31. # Otherwise, include only the specified modules.
  32. or any(entry_point.module_name.startswith(name) for name in include_only)
  33. ):
  34. test_path_list += list(entry_point.load()())
  35. return test_path_list
  36. # Run pytest on all tests registered by the PyInstaller setuptools testing entry point. If provided,
  37. # the ``include_only`` argument is passed to ``path_to_test``.
  38. def run_pytest(*args, **kwargs):
  39. paths = paths_to_test(include_only=kwargs.pop("include_only", None))
  40. # Return an error code if no tests were discovered.
  41. if not paths:
  42. print("Error: no tests discovered.", file=sys.stderr)
  43. # This indicates no tests were discovered; see
  44. # https://docs.pytest.org/en/latest/usage.html#possible-exit-codes.
  45. return 5
  46. else:
  47. # See https://docs.pytest.org/en/latest/usage.html#calling-pytest-from-python-code.
  48. # Omit ``args[0]``, which is the name of this script.
  49. print("pytest " + " ".join([*paths, *args[1:]]))
  50. return pytest.main([*paths, *args[1:]], **kwargs)
  51. if __name__ == "__main__":
  52. # Look only for the ``--include_only`` argument.
  53. parser = argparse.ArgumentParser(description='Run PyInstaller packaging tests.')
  54. parser.add_argument(
  55. "--include_only",
  56. action="append",
  57. help="Only run tests from the specified package.",
  58. )
  59. args, unknown = parser.parse_known_args(sys.argv)
  60. # Convert the parsed args into a dict using ``vars(args)``.
  61. sys.exit(run_pytest(*unknown, **vars(args)))