summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-29 13:23:59 -0400
committerDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-29 13:23:59 -0400
commit7e8e66adebfb2bc33da6f074abc54e9bb4fcff1e (patch)
treea52b648438543d75fee9fdd0cb923b1d1adbfc46
parent20e8878663cfdb13778bcadd209f1bb9f4bbc41a (diff)
downloadcliff-tablib-7e8e66adebfb2bc33da6f074abc54e9bb4fcff1e.tar.gz
add tests for App and fix an issue with error handling and clean_up() in python 3.
-rw-r--r--cliff/app.py9
-rw-r--r--tests/test_app.py73
-rw-r--r--tox.ini4
3 files changed, 84 insertions, 2 deletions
diff --git a/cliff/app.py b/cliff/app.py
index 3d80710..3be4d60 100644
--- a/cliff/app.py
+++ b/cliff/app.py
@@ -197,7 +197,6 @@ class App(object):
LOG.exception(err)
raise
LOG.error('ERROR: %s', err)
- finally:
try:
self.clean_up(cmd, result, err)
except Exception as err2:
@@ -205,4 +204,12 @@ class App(object):
LOG.exception(err2)
else:
LOG.error('Could not clean up: %s', err2)
+ else:
+ try:
+ self.clean_up(cmd, result, None)
+ except Exception as err3:
+ if self.options.debug:
+ LOG.exception(err3)
+ else:
+ LOG.error('Could not clean up: %s', err3)
return result
diff --git a/tests/test_app.py b/tests/test_app.py
new file mode 100644
index 0000000..15c5471
--- /dev/null
+++ b/tests/test_app.py
@@ -0,0 +1,73 @@
+from cliff.app import App
+from cliff.command import Command
+from cliff.commandmanager import CommandManager
+
+import mock
+
+
+def make_app():
+ cmd_mgr = CommandManager('cliff.tests')
+ # Register a command that succeeds
+ command = mock.MagicMock(spec=Command)
+ command_inst = mock.MagicMock(spec=Command)
+ command_inst.run.return_value = 0
+ command.return_value = command_inst
+ cmd_mgr.add_command('mock', command)
+ app = App('testing interactive mode',
+ '1',
+ cmd_mgr,
+ stderr=mock.Mock(), # suppress warning messages
+ )
+ return app, command
+
+
+def test_no_args_triggers_interactive_mode():
+ app, command = make_app()
+ app.interact = mock.MagicMock(name='inspect')
+ app.run([])
+ app.interact.assert_called_once_with()
+
+
+def test_initialize_app():
+ app, command = make_app()
+ app.initialize_app = mock.MagicMock(name='initialize_app')
+ app.run(['mock'])
+ app.initialize_app.assert_called_once_with()
+
+
+def test_prepare_to_run_command():
+ app, command = make_app()
+ app.prepare_to_run_command = mock.MagicMock(name='prepare_to_run_command')
+ app.run(['mock'])
+ app.prepare_to_run_command.assert_called_once_with(command())
+
+
+def test_clean_up_success():
+ app, command = make_app()
+ app.clean_up = mock.MagicMock(name='clean_up')
+ app.run(['mock'])
+ app.clean_up.assert_called_once_with(command.return_value, 0, None)
+
+
+def test_clean_up_error():
+ app, command = make_app()
+
+ # Register a command that fails
+ err_command = mock.Mock(name='err_command', spec=Command)
+ err_command_inst = mock.Mock(spec=Command)
+ def raise_exception(*args):
+ #raise RuntimeError('test exception %s' % args[0])
+ raise RuntimeError('test exception')
+ err_command_inst.run = mock.Mock(side_effect=raise_exception)
+ err_command.return_value = err_command_inst
+ app.command_manager.add_command('error', err_command)
+
+ app.clean_up = mock.MagicMock(name='clean_up')
+ app.run(['error'])
+
+ app.clean_up.assert_called_once()
+ call_args = app.clean_up.call_args_list[0]
+ assert call_args == mock.call(mock.ANY, 1, mock.ANY)
+ args, kwargs = call_args
+ assert isinstance(args[2], RuntimeError)
+ assert args[2].args == ('test exception',)
diff --git a/tox.ini b/tox.ini
index 55c9cfc..6d83e1c 100644
--- a/tox.ini
+++ b/tox.ini
@@ -3,4 +3,6 @@ envlist = py27,py32
[testenv]
commands = nosetests -d []
-deps = nose
+deps =
+ nose
+ mock