Add smoke test suite to verify distribution is installed.
authorBen Finney <[email protected]>
Fri, 10 Jun 2016 16:03:39 +0000 (11 02:03 +1000)
committerBen Finney <[email protected]>
Wed, 15 Jun 2016 12:33:10 +0000 (15 22:33 +1000)
debian/changelog
debian/tests/control [new file with mode: 0644]
debian/tests/smoke-python2 [new file with mode: 0755]
debian/tests/smoke-python3 [new file with mode: 0755]
debian/tests/smoke_test.py [new file with mode: 0644]

index 9d89203..da65ef5 100644 (file)
@@ -60,6 +60,10 @@ python-coverage (4.1+dfsg.1-1) UNRELEASED; urgency=medium
     * Remove the ‘python-coverage’ command from Python 3 package.
       This resolves a conflict between the Python 2 and Python 3 packages.
     * Document that the ‘python-coverage’ command is only for Python 2.
+  * debian/tests/control,
+    debian/tests/smoke-python{2,3}, debian/tests/smoke_test.py:
+    * Add smoke test suite to verify distribution is installed.
+      (Closes: bug#729703)
 
  --
 
diff --git a/debian/tests/control b/debian/tests/control
new file mode 100644 (file)
index 0000000..0e48847
--- /dev/null
@@ -0,0 +1,20 @@
+# debian/tests/control
+# Control file for Debian ‘autopkgtests’.
+# Documentation: ‘/usr/share/doc/autopkgtest/README.package-tests.rst.gz’
+
+Tests: smoke-python2
+Depends:
+    python-pkg-resources,
+    python-coverage
+
+Tests: smoke-python3
+Depends:
+    python3-pkg-resources,
+    python3-coverage
+
+\f
+# Local variables:
+# coding: utf-8
+# mode: conf
+# End:
+# vim: fileencoding=utf-8 filetype=conf :
diff --git a/debian/tests/smoke-python2 b/debian/tests/smoke-python2
new file mode 100755 (executable)
index 0000000..c2af818
--- /dev/null
@@ -0,0 +1,39 @@
+#! /bin/bash
+#
+# debian/tests/smoke-python2
+# Part of Debian ‘python-coverage’ package.
+#
+# Copyright © 2016 Ben Finney <[email protected]>
+# This is free software; you may copy, modify, and/or distribute this work
+# under the terms of the Expat license as published by James Clark.
+# No warranty expressed or implied.
+#
+# Smoke test for package in Python 2 environments.
+
+set -o errexit
+set -o errtrace
+set -o nounset
+
+DISTRIBUTION_NAME=coverage
+MODULE_NAMES=(
+        coverage
+        )
+
+test_opts="--distribution=$DISTRIBUTION_NAME"
+for mod in ${MODULE_NAMES[@]} ; do
+    # Accumulate the module names.
+    test_opts="$test_opts --module=$mod"
+done
+
+for py in $(pyversions -i) ; do
+    printf "Python command: %s\n" $py
+    $py debian/tests/smoke_test.py $test_opts
+    printf "\n"
+done
+
+
+# Local variables:
+# coding: utf-8
+# mode: shell-script
+# End:
+# vim: fileencoding=utf-8 filetype=sh :
diff --git a/debian/tests/smoke-python3 b/debian/tests/smoke-python3
new file mode 100755 (executable)
index 0000000..d789109
--- /dev/null
@@ -0,0 +1,39 @@
+#! /bin/bash
+#
+# debian/tests/smoke-python3
+# Part of Debian ‘python-daemon’ package.
+#
+# Copyright © 2016 Ben Finney <[email protected]>
+# This is free software; you may copy, modify, and/or distribute this work
+# under the terms of the Expat license as published by James Clark.
+# No warranty expressed or implied.
+#
+# Smoke test for package in Python 3 environments.
+
+set -o errexit
+set -o errtrace
+set -o nounset
+
+DISTRIBUTION_NAME=coverage
+MODULE_NAMES=(
+        coverage
+        )
+
+test_opts="--distribution=$DISTRIBUTION_NAME"
+for mod in ${MODULE_NAMES[@]} ; do
+    # Accumulate the module names.
+    test_opts="$test_opts --module=$mod"
+done
+
+for py in $(py3versions -i) ; do
+    printf "Python command: %s\n" $py
+    $py debian/tests/smoke_test.py $test_opts
+    printf "\n"
+done
+
+
+# Local variables:
+# coding: utf-8
+# mode: shell-script
+# End:
+# vim: fileencoding=utf-8 filetype=sh :
diff --git a/debian/tests/smoke_test.py b/debian/tests/smoke_test.py
new file mode 100644 (file)
index 0000000..d4d46a8
--- /dev/null
@@ -0,0 +1,135 @@
+# -*- coding: utf-8 -*-
+#
+# debian/tests/smoke_test.py
+#
+# Copyright © 2016 Ben Finney <[email protected]>
+# This is free software; you may copy, modify, and/or distribute this work
+# under the terms of the Expat license as published by James Clark.
+# No warranty expressed or implied.
+
+""" Post-install Python smoke test for use in Debian autopkgtest.
+
+    Written for both Python 2 and Python 3, to test all installed
+    versions of a package.
+
+    Smoke test the distribution::
+        --distribution=DISTRIBUTION
+
+    Smoke test one or more modules::
+        --module=MODULE_FOO --module=MODULE_BAR --module=MODULE_BAZ
+
+    """
+
+import sys
+import argparse
+import importlib
+import pkg_resources
+
+\f
+def emit_implementation():
+    """ Emit the details of the current Python implementation.
+
+        :return: ``None``.
+
+        """
+    sys.stdout.write(
+            "Interpreter: {command}\n{version}\n".format(
+                command=sys.executable, version=sys.version))
+
+
+def emit_distribution(name):
+    """ Get the distribution `name` and emit its representation.
+
+        :param name: Name of the distribution to retrieve.
+        :return: ``None``.
+
+        """
+    distribution = pkg_resources.get_distribution(name)
+    sys.stdout.write(
+            "Distribution ‘{name}’:\n\t{distribution!r}\n".format(
+                name=name, distribution=distribution))
+
+
+def emit_module(name):
+    """ Import the module `name` and emit the module representation.
+
+        :param name: Full name of the module to import.
+        :return: ``None``.
+
+        """
+    module = importlib.import_module(name)
+    sys.stdout.write(
+            "Package ‘{name}’:\n\t{module!r}\n".format(
+                name=name, module=module))
+
+
+def suite(args):
+    """ Run the full suite of tests.
+
+        :param args: Namespace of arguments parsed from `ArgumentParser`.
+        :return: ``None``.
+
+        """
+    emit_implementation()
+
+    if args.distribution_name:
+        emit_distribution(args.distribution_name)
+
+    for module_name in args.module_names:
+        emit_module(module_name)
+
+\f
+class SmokeTestArgumentParser(argparse.ArgumentParser):
+    """ Command-line argument parser for this program. """
+
+    def __init__(self, *args, **kwargs):
+        super(SmokeTestArgumentParser, self).__init__(*args, **kwargs)
+
+        self.add_argument(
+                '--distribution',
+                dest='distribution_name', type=str,
+                metavar="DISTRIBUTION", help=(
+                    "Test the Python distribution named DISTRIBUTION."))
+        self.add_argument(
+                '--module',
+                dest='module_names', type=str, nargs='+',
+                metavar="MODULE", help=(
+                    "Test the Python module named MODULE."))
+
+
+def main(argv=None):
+    """ Mainline code for this module.
+
+        :param argv: Sequence of all command line arguments.
+            (Default: `sys.argv`)
+        :return: The exit status (integer) for exit from the process.
+
+        """
+    exit_status = 0
+
+    if argv is None:
+        argv = sys.argv
+
+    try:
+        program_name = argv[0]
+        parser = SmokeTestArgumentParser(prog=program_name)
+        args = parser.parse_args(argv[1:])
+
+        suite(args)
+
+    except SystemExit as exc:
+        exit_status = exc.code
+
+    return exit_status
+
+
+if __name__ == "__main__":
+    exit_status = main(sys.argv)
+    sys.exit(exit_status)
+
+\f
+# Local variables:
+# coding: utf-8
+# mode: python
+# End:
+# vim: fileencoding=utf-8 filetype=python :