From 6b3d90328f60803ec39ec7989f938ec8943964c4 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Thu, 15 Mar 2012 14:52:32 -0700 Subject: [PATCH] More test coverage. --- pecan/commands/base.py | 12 ++++---- pecan/commands/create.py | 4 +-- pecan/tests/test_commands.py | 53 ++++++++++++++++++++++++++++++++++++ setup.py | 8 +++--- 4 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 pecan/tests/test_commands.py diff --git a/pecan/commands/base.py b/pecan/commands/base.py index b997d9f..2bb2581 100644 --- a/pecan/commands/base.py +++ b/pecan/commands/base.py @@ -4,14 +4,13 @@ import argparse import logging import sys from warnings import warn -from pecan import load_app log = logging.getLogger(__name__) class HelpfulArgumentParser(argparse.ArgumentParser): - def error(self, message): + def error(self, message): # pragma: nocover """error(message: string) Prints a usage message incorporating the message to stderr and @@ -38,7 +37,7 @@ class CommandManager(object): try: cmd = ep.load() assert hasattr(cmd, 'run') - except Exception, e: + except Exception, e: # pragma: nocover warn("Unable to load plugin %s: %s" % (ep, e), RuntimeWarning) continue self.add({ep.name: cmd}) @@ -77,7 +76,7 @@ class CommandRunner(object): self.commands[ns.command_name]().run(ns) @classmethod - def handle_command_line(cls): + def handle_command_line(cls): # pragma: nocover runner = CommandRunner() runner.run(sys.argv[1:]) @@ -86,10 +85,10 @@ class CommandRunner(object): try: dist = pkg_resources.get_distribution('Pecan') if os.path.dirname(os.path.dirname(__file__)) == dist.location: - return dist.version + return dist.version # pragma: nocover else: return '(development)' - except: + except: # pragma: nocover return '(development)' @property @@ -114,6 +113,7 @@ class BaseCommand(object): self.args = args def load_app(self): + from pecan import load_app if not os.path.isfile(self.args.config_file): raise RuntimeError('`%s` is not a file.' % self.args.config_file) return load_app(self.args.config_file) diff --git a/pecan/commands/create.py b/pecan/commands/create.py index 508f766..df84f01 100644 --- a/pecan/commands/create.py +++ b/pecan/commands/create.py @@ -23,7 +23,7 @@ class ScaffoldManager(object): try: cmd = ep.load() assert hasattr(cmd, 'copy_to') - except Exception, e: + except Exception, e: # pragma: nocover warn( "Unable to load scaffold %s: %s" % (ep, e), RuntimeWarning ) @@ -55,6 +55,6 @@ class CreateCommand(BaseCommand): def run(self, args): super(CreateCommand, self).run(args) - CreateCommand.manager.scaffolds[args.template_name]().copy_to( + self.manager.scaffolds[args.template_name]().copy_to( args.project_name ) diff --git a/pecan/tests/test_commands.py b/pecan/tests/test_commands.py new file mode 100644 index 0000000..5d390d8 --- /dev/null +++ b/pecan/tests/test_commands.py @@ -0,0 +1,53 @@ +import unittest + + +class TestCommandManager(unittest.TestCase): + + def test_commands(self): + from pecan.commands import ServeCommand, ShellCommand, CreateCommand + from pecan.commands.base import CommandManager + m = CommandManager() + assert m.commands['serve'] == ServeCommand + assert m.commands['shell'] == ShellCommand + assert m.commands['create'] == CreateCommand + + +class TestCommandRunner(unittest.TestCase): + + def test_commands(self): + from pecan.commands import ( + ServeCommand, ShellCommand, CreateCommand, CommandRunner + ) + runner = CommandRunner() + assert runner.commands['serve'] == ServeCommand + assert runner.commands['shell'] == ShellCommand + assert runner.commands['create'] == CreateCommand + + def test_run(self): + from pecan.commands import CommandRunner + runner = CommandRunner() + with self.assertRaises(RuntimeError): + runner.run(['serve', 'missing_file.py']) + + +class TestCreateCommand(unittest.TestCase): + + def test_run(self): + from pecan.commands import CreateCommand + + class FakeArg(object): + project_name = 'default' + template_name = 'default' + + class FakeScaffold(object): + def copy_to(self, project_name): + assert project_name == 'default' + + class FakeManager(object): + scaffolds = { + 'default': FakeScaffold + } + + c = CreateCommand() + c.manager = FakeManager() + c.run(FakeArg()) diff --git a/setup.py b/setup.py index f6141b0..6e6f2d0 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,5 @@ import sys -from setuptools import setup, command, find_packages +from setuptools import setup, find_packages from setuptools.command.test import test as TestCommand version = '0.1.0' @@ -17,15 +17,15 @@ requirements = [ ] try: - import json + import json # noqa except: try: - import simplejson + import simplejson # noqa except: requirements.append("simplejson >= 2.1.1") try: - import argparse + import argparse # noqa except: requirements.append('argparse')