run_tests.py 2.9 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 sys
  12. import argparse
  13. import pytest
  14. import pkg_resources
  15. from PyInstaller import compat
  16. def paths_to_test(include_only=None):
  17. """If ``include_only`` is falsey, this functions returns paths from all entry
  18. points. Otherwise, this parameter must be a string or sequence of strings.
  19. 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",
  28. "tests"):
  29. # Implement ``include_only``.
  30. if (not include_only # If falsey, include everything,
  31. # Otherwise, include only the specified modules.
  32. or any(entry_point.module_name.startswith(name)
  33. for name in include_only)):
  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
  37. # entry point. If provided, the ``include_only`` argument is passed to
  38. # ``path_to_test``.
  39. def run_pytest(*args, **kwargs):
  40. paths = paths_to_test(include_only=kwargs.pop("include_only", None))
  41. # Return an error code if no tests were discovered.
  42. if not paths:
  43. print("Error: no tests discovered.", file=sys.stderr)
  44. # This indicates no tests were discovered; see
  45. # https://docs.pytest.org/en/latest/usage.html#possible-exit-codes.
  46. return 5
  47. else:
  48. # See
  49. # https://docs.pytest.org/en/latest/usage.html#calling-pytest-from-python-code.
  50. # Omit ``args[0]``, which is the name of this script.
  51. print("pytest " + " ".join([*paths, *args[1:]]))
  52. return pytest.main([*paths, *args[1:]], **kwargs)
  53. if __name__ == "__main__":
  54. # Look only for the ``--include_only`` argument.
  55. parser = argparse.ArgumentParser(
  56. description='Run PyInstaller packaging tests.')
  57. parser.add_argument("--include_only", action="append",
  58. help="Only run tests from the specified package.")
  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)))