smoke_test.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- coding: utf-8 -*-
  2. #
  3. # debian/tests/smoke_test.py
  4. #
  5. # Copyright © 2016 Ben Finney <bignose@debian.org>
  6. # This is free software; you may copy, modify, and/or distribute this work
  7. # under the terms of the GNU General Public License, version 3 or later.
  8. # No warranty expressed or implied.
  9. # See the file ‘/usr/share/common-licenses/GPL-3’ for details.
  10. """ Post-install Python smoke test for use in Debian autopkgtest.
  11. Written for both Python 2 and Python 3, to test all installed
  12. versions of a package.
  13. Smoke test the distribution::
  14. --distribution=DISTRIBUTION
  15. Smoke test one or more modules::
  16. --module=MODULE_FOO --module=MODULE_BAR --module=MODULE_BAZ
  17. """
  18. import sys
  19. import argparse
  20. import importlib
  21. import pkg_resources
  22. def emit_implementation():
  23. """ Emit the details of the current Python implementation.
  24. :return: ``None``.
  25. """
  26. sys.stdout.write(
  27. "Interpreter: {command}\n{version}\n".format(
  28. command=sys.executable, version=sys.version))
  29. def emit_distribution(name):
  30. """ Get the distribution `name` and emit its representation.
  31. :param name: Name of the distribution to retrieve.
  32. :return: ``None``.
  33. """
  34. distribution = pkg_resources.get_distribution(name)
  35. sys.stdout.write(
  36. "Distribution ‘{name}’:\n\t{distribution!r}\n".format(
  37. name=name, distribution=distribution))
  38. def emit_module(name):
  39. """ Import the module `name` and emit the module representation.
  40. :param name: Full name of the module to import.
  41. :return: ``None``.
  42. """
  43. module = importlib.import_module(name)
  44. sys.stdout.write(
  45. "Package ‘{name}’:\n\t{module!r}\n".format(
  46. name=name, module=module))
  47. def suite(args):
  48. """ Run the full suite of tests.
  49. :param args: Namespace of arguments parsed from `ArgumentParser`.
  50. :return: ``None``.
  51. """
  52. emit_implementation()
  53. if args.distribution_name:
  54. emit_distribution(args.distribution_name)
  55. for module_name in args.module_names:
  56. emit_module(module_name)
  57. class SmokeTestArgumentParser(argparse.ArgumentParser):
  58. """ Command-line argument parser for this program. """
  59. def __init__(self, *args, **kwargs):
  60. super(SmokeTestArgumentParser, self).__init__(*args, **kwargs)
  61. self.add_argument(
  62. '--distribution',
  63. dest='distribution_name', type=str,
  64. metavar="DISTRIBUTION", help=(
  65. "Test the Python distribution named DISTRIBUTION."))
  66. self.add_argument(
  67. '--module',
  68. dest='module_names', type=str, nargs='+',
  69. metavar="MODULE", help=(
  70. "Test the Python module named MODULE."))
  71. def main(argv=None):
  72. """ Mainline code for this module.
  73. :param argv: Sequence of all command line arguments.
  74. (Default: `sys.argv`)
  75. :return: The exit status (integer) for exit from the process.
  76. """
  77. exit_status = 0
  78. if argv is None:
  79. argv = sys.argv
  80. try:
  81. program_name = argv[0]
  82. parser = SmokeTestArgumentParser(prog=program_name)
  83. args = parser.parse_args(argv[1:])
  84. suite(args)
  85. except SystemExit as exc:
  86. exit_status = exc.code
  87. return exit_status
  88. if __name__ == "__main__":
  89. exit_status = main(sys.argv)
  90. sys.exit(exit_status)
  91. # Local variables:
  92. # coding: utf-8
  93. # mode: python
  94. # End:
  95. # vim: fileencoding=utf-8 filetype=python :