summaryrefslogtreecommitdiff
path: root/tests/test_plugin.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-14 18:12:39 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-14 18:12:39 -0400
commit85dbe035114330a15abd5af6e098ef6948fa6cb0 (patch)
treee087dfa160eba882dcbd623609cab763efbd1ebc /tests/test_plugin.py
parent3329e5fec21f2cc4479bb3358170e2226885ab3b (diff)
parentddd07f9cd6d72baca1232ae98856cf3b3d564706 (diff)
downloadcmd2-git-85dbe035114330a15abd5af6e098ef6948fa6cb0.tar.gz
Merge branch 'master' into history_fixes
Diffstat (limited to 'tests/test_plugin.py')
-rw-r--r--tests/test_plugin.py47
1 files changed, 32 insertions, 15 deletions
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index 242b0d25..f7065db5 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -2,12 +2,17 @@
# flake8: noqa E302
"""
Test plugin infrastructure and hooks.
-
-Copyright 2018 Jared Crapo <jared@kotfu.net>
-Released under MIT license, see LICENSE file
"""
+import sys
+
import pytest
+# Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available
+try:
+ import mock
+except ImportError:
+ from unittest import mock
+
import cmd2
from cmd2 import plugin
@@ -265,21 +270,27 @@ def test_register_preloop_hook_with_return_annotation():
app.register_preloop_hook(app.prepost_hook_with_wrong_return_annotation)
def test_preloop_hook(capsys):
- app = PluggedApp()
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog", "say hello", 'quit']
+
+ with mock.patch.object(sys, 'argv', testargs):
+ app = PluggedApp()
+
app.register_preloop_hook(app.prepost_hook_one)
- app.cmdqueue.append('say hello')
- app.cmdqueue.append('quit')
app.cmdloop()
out, err = capsys.readouterr()
assert out == 'one\nhello\n'
assert not err
def test_preloop_hooks(capsys):
- app = PluggedApp()
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog", "say hello", 'quit']
+
+ with mock.patch.object(sys, 'argv', testargs):
+ app = PluggedApp()
+
app.register_preloop_hook(app.prepost_hook_one)
app.register_preloop_hook(app.prepost_hook_two)
- app.cmdqueue.append('say hello')
- app.cmdqueue.append('quit')
app.cmdloop()
out, err = capsys.readouterr()
assert out == 'one\ntwo\nhello\n'
@@ -296,21 +307,27 @@ def test_register_postloop_hook_with_wrong_return_annotation():
app.register_postloop_hook(app.prepost_hook_with_wrong_return_annotation)
def test_postloop_hook(capsys):
- app = PluggedApp()
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog", "say hello", 'quit']
+
+ with mock.patch.object(sys, 'argv', testargs):
+ app = PluggedApp()
+
app.register_postloop_hook(app.prepost_hook_one)
- app.cmdqueue.append('say hello')
- app.cmdqueue.append('quit')
app.cmdloop()
out, err = capsys.readouterr()
assert out == 'hello\none\n'
assert not err
def test_postloop_hooks(capsys):
- app = PluggedApp()
+ # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args
+ testargs = ["prog", "say hello", 'quit']
+
+ with mock.patch.object(sys, 'argv', testargs):
+ app = PluggedApp()
+
app.register_postloop_hook(app.prepost_hook_one)
app.register_postloop_hook(app.prepost_hook_two)
- app.cmdqueue.append('say hello')
- app.cmdqueue.append('quit')
app.cmdloop()
out, err = capsys.readouterr()
assert out == 'hello\none\ntwo\n'