summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-09-23 22:18:19 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-09-23 22:18:19 -0400
commit265e598594b650936730279506e9f072adf2ea01 (patch)
tree7d6bec563986b4f17326abbebc9ac4fca0dce88f /tests
parentb72edb07d59d45d4006dc596218a52d54396378a (diff)
downloadcmd2-git-265e598594b650936730279506e9f072adf2ea01.tar.gz
Added in_script() and in_pyscript() to cmd2.Cmd class
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test_cmd2.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 94a49a95..25273b4c 100755
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -20,7 +20,7 @@ except ImportError:
from unittest import mock
import cmd2
-from cmd2 import ansi, clipboard, constants, utils
+from cmd2 import ansi, clipboard, constants, plugin, utils
from .conftest import run_cmd, normalize, verify_help_text, HELP_HISTORY
from .conftest import SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG, complete_tester
@@ -461,6 +461,24 @@ def test_relative_run_script_requires_an_argument(base_app):
out, err = run_cmd(base_app, '_relative_run_script')
assert 'Error: the following arguments' in err[1]
+def test_in_script(request):
+ class HookApp(cmd2.Cmd):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.register_cmdfinalization_hook(self.hook)
+
+ def hook(self: cmd2.Cmd, data: plugin.CommandFinalizationData) -> plugin.CommandFinalizationData:
+ if self.in_script():
+ self.poutput("WE ARE IN SCRIPT")
+ return data
+
+ hook_app = HookApp()
+ test_dir = os.path.dirname(request.module.__file__)
+ filename = os.path.join(test_dir, 'script.txt')
+ out, err = run_cmd(hook_app, 'run_script {}'.format(filename))
+
+ assert "WE ARE IN SCRIPT" in out[-1]
+
def test_output_redirection(base_app):
fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt')
os.close(fd)