smoke_test.py 3.4 KB

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