← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~benji/lpsetup/bug-1017973-add-smoke-tests into lp:lpsetup

 

Benji York has proposed merging lp:~benji/lpsetup/bug-1017973-add-smoke-tests into lp:lpsetup.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #1017973 in lpsetup: "--help smoke tests"
  https://bugs.launchpad.net/lpsetup/+bug/1017973

For more details, see:
https://code.launchpad.net/~benji/lpsetup/bug-1017973-add-smoke-tests/+merge/112141

This branch adds a basic smoke test for the subcommands by running each with --help and asserting that no exceptions are raised and a non-error (zero) exit code is returned.

A little refactoring was required to get the tests to work, which was discussed with Francesco by way of mid-implementation call.

Lint: pocketlint reports no lint

Tests: the obvious test was added and I also tested the test by introducing SyntaxError and NameError in subcommand code and verified that the new test failed.
-- 
https://code.launchpad.net/~benji/lpsetup/bug-1017973-add-smoke-tests/+merge/112141
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~benji/lpsetup/bug-1017973-add-smoke-tests into lp:lpsetup.
=== modified file 'lpsetup/argparser.py'
--- lpsetup/argparser.py	2012-03-30 11:02:11 +0000
+++ lpsetup/argparser.py	2012-06-26 15:23:24 +0000
@@ -474,7 +474,4 @@
             args = [getattr(namespace, i) for i in arg_names]
             step_runner = getattr(
                 self, 'call_' + step_name, default_step_runner)
-            try:
-                step_runner(namespace, step, args)
-            except subprocess.CalledProcessError as err:
-                return err
+            step_runner(namespace, step, args)

=== modified file 'lpsetup/cli.py'
--- lpsetup/cli.py	2012-06-22 20:50:41 +0000
+++ lpsetup/cli.py	2012-06-26 15:23:24 +0000
@@ -9,6 +9,7 @@
     'main',
     ]
 
+import sys
 from lpsetup import (
     __doc__ as description,
     argparser,
@@ -24,17 +25,26 @@
     version,
     )
 
+subcommands = [
+    ('install', install.SubCommand),
+    ('inithost', inithost.SubCommand),
+    ('get', get.SubCommand),
+    ('update', update.SubCommand),
+    ('lxc-install', lxcinstall.SubCommand),
+    ('branch', branch.SubCommand),
+    ('version', version.SubCommand),
+    ]
 
-def main():
+def main(args=None):
+    if args is None:
+        args = sys.argv[1:];
     parser = argparser.ArgumentParser(description=description)
-    parser.register_subcommand('install', install.SubCommand)
-    parser.register_subcommand('inithost', inithost.SubCommand)
-    parser.register_subcommand('get', get.SubCommand)
-    parser.register_subcommand('update', update.SubCommand)
-    parser.register_subcommand('lxc-install', lxcinstall.SubCommand)
-    parser.register_subcommand('branch', branch.SubCommand)
-    parser.register_subcommand('version', version.SubCommand)
-    args = parser.parse_args()
+    for subcommand, callable in subcommands:
+        parser.register_subcommand(subcommand, callable)
+    try:
+        args = parser.parse_args(args)
+    except SystemExit:
+        return 0
     try:
         return args.main(args)
     except (exceptions.ExecutionError, KeyboardInterrupt) as err:

=== added file 'lpsetup/tests/subcommands/test_smoke.py'
--- lpsetup/tests/subcommands/test_smoke.py	1970-01-01 00:00:00 +0000
+++ lpsetup/tests/subcommands/test_smoke.py	2012-06-26 15:23:24 +0000
@@ -0,0 +1,21 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Smoke tests for the sub commands."""
+
+import unittest
+
+from lpsetup.cli import (
+    main,
+    subcommands,
+    )
+
+
+class SmokeTest(unittest.TestCase):
+    """Tests which are intended to do a quick check for broken subcommands."""
+
+    def test_subcommand_smoke_via_help(self):
+        # Perform a basic smoke test by running each subcommand's --help
+        # function and verify that a non-error exit code is returned.
+        for subcommand, callable in subcommands:
+            self.assertEqual(main([subcommand, '--help']), 0)

=== modified file 'lpsetup/tests/test_argparser.py'
--- lpsetup/tests/test_argparser.py	2012-05-22 09:36:57 +0000
+++ lpsetup/tests/test_argparser.py	2012-06-26 15:23:24 +0000
@@ -4,6 +4,7 @@
 
 """Tests for the argparser module."""
 
+import subprocess
 import unittest
 
 from lpsetup import argparser
@@ -163,10 +164,8 @@
     def test_failing_step(self):
         # Ensure the steps execution is stopped if a step raises
         # `subprocess.CalledProcessError`.
-        with capture_output() as output:
-            error = self.parse_and_call_main('--foo eggs')
-        self.assertEqual(1, error.returncode)
-        self.check_output(['step1 received eggs'], output)
+        with self.assertRaises(subprocess.CalledProcessError):
+            self.parse_and_call_main('--foo eggs')
 
 
 class DynamicStepsBasedSubCommandTest(SubCommandTestMixin, unittest.TestCase):