summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py17
-rw-r--r--tests/test_cmd2.py109
-rw-r--r--tests/test_history.py117
3 files changed, 128 insertions, 115 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 7bc8e7d0..f41afb64 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -28,7 +28,7 @@ except ImportError:
# Help text for base cmd2.Cmd application
BASE_HELP = """Documented commands (type help <topic>):
========================================
-alias help load py quit shell
+alias help load py quit shell
edit history macro pyscript set shortcuts
""" # noqa: W291
@@ -50,7 +50,8 @@ shortcuts List available shortcuts
"""
# Help text for the history command
-HELP_HISTORY = """Usage: history [-h] [-r | -e | -s | -o FILE | -t TRANSCRIPT | -v | -c] [arg]
+HELP_HISTORY = """Usage: history [-h] [-r | -e | -o FILE | -t TRANSCRIPT | -c] [-s] [-x] [-v]
+ [arg]
View, run, edit, save, or clear previously entered commands
@@ -65,13 +66,17 @@ optional arguments:
-h, --help show this help message and exit
-r, --run run selected history items
-e, --edit edit and then run selected history items
- -s, --script output commands in script format
-o, --output-file FILE
- output commands to a script file
+ output commands to a script file, implies -s
-t, --transcript TRANSCRIPT
- output commands and results to a transcript file
- -v, --verbose display history and include expanded commands if they differ from the typed command.
+ output commands and results to a transcript file, implies -s
-c, --clear clear all history
+
+formatting:
+ -s, --script output commands in script format, i.e. without command numbers
+ -x, --expanded output expanded commands instead of entered command
+ -v, --verbose display history and include expanded commands if they differ from the typed command
+
"""
# Output from the shortcuts command with default built-in shortcuts
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index bb415fe5..1b554fef 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -56,10 +56,6 @@ def test_base_help_verbose(base_app):
out = run_cmd(base_app, 'help --verbose')
assert out == expected
-def test_base_help_history(base_app):
- out = run_cmd(base_app, 'help history')
- assert out == normalize(HELP_HISTORY)
-
def test_base_argparse_help(base_app, capsys):
# Verify that "set -h" gives the same output as "help set" and that it starts in a way that makes sense
run_cmd(base_app, 'set -h')
@@ -737,29 +733,6 @@ def test_base_py_interactive(base_app):
m.assert_called_once()
-def test_exclude_from_history(base_app, monkeypatch):
- # Mock out the os.system call so we don't actually open an editor
- m = mock.MagicMock(name='system')
- monkeypatch.setattr("os.system", m)
-
- # Run edit command
- run_cmd(base_app, 'edit')
-
- # Run history command
- run_cmd(base_app, 'history')
-
- # Verify that the history is empty
- out = run_cmd(base_app, 'history')
- assert out == []
-
- # Now run a command which isn't excluded from the history
- run_cmd(base_app, 'help')
- # And verify we have a history now ...
- out = run_cmd(base_app, 'history')
- expected = normalize(""" 1 help""")
- assert out == expected
-
-
def test_base_cmdloop_with_queue():
# Create a cmd2.Cmd() instance and make sure basic settings are like we want for test
app = cmd2.Cmd()
@@ -1946,20 +1919,12 @@ def test_parseline(base_app):
assert line == statement.strip()
-def test_readline_remove_history_item(base_app):
- from cmd2.rl_utils import readline
- assert readline.get_current_history_length() == 0
- readline.add_history('this is a test')
- assert readline.get_current_history_length() == 1
- readline.remove_history_item(0)
- assert readline.get_current_history_length() == 0
-
def test_onecmd_raw_str_continue(base_app):
line = "help"
stop = base_app.onecmd(line)
out = base_app.stdout.getvalue()
assert not stop
- assert out.strip() == BASE_HELP.strip()
+ assert normalize(out) == normalize(BASE_HELP)
def test_onecmd_raw_str_quit(base_app):
line = "quit"
@@ -1969,78 +1934,6 @@ def test_onecmd_raw_str_quit(base_app):
assert out == ''
-@pytest.fixture(scope="session")
-def hist_file():
- fd, filename = tempfile.mkstemp(prefix='hist_file', suffix='.txt')
- os.close(fd)
- yield filename
- # teardown code
- try:
- os.remove(filename)
- except FileNotFoundError:
- pass
-
-def test_existing_history_file(hist_file, capsys):
- import atexit
- import readline
-
- # Create the history file before making cmd2 app
- with open(hist_file, 'w'):
- pass
-
- # Create a new cmd2 app
- app = cmd2.Cmd(persistent_history_file=hist_file)
- out, err = capsys.readouterr()
-
- # Make sure there were no errors
- assert err == ''
-
- # Unregister the call to write_history_file that cmd2 did
- atexit.unregister(readline.write_history_file)
-
- # Remove created history file
- os.remove(hist_file)
-
-
-def test_new_history_file(hist_file, capsys):
- import atexit
- import readline
-
- # Remove any existing history file
- try:
- os.remove(hist_file)
- except OSError:
- pass
-
- # Create a new cmd2 app
- app = cmd2.Cmd(persistent_history_file=hist_file)
- out, err = capsys.readouterr()
-
- # Make sure there were no errors
- assert err == ''
-
- # Unregister the call to write_history_file that cmd2 did
- atexit.unregister(readline.write_history_file)
-
- # Remove created history file
- os.remove(hist_file)
-
-def test_bad_history_file_path(capsys, request):
- # Use a directory path as the history file
- test_dir = os.path.dirname(request.module.__file__)
-
- # Create a new cmd2 app
- app = cmd2.Cmd(persistent_history_file=test_dir)
- out, err = capsys.readouterr()
-
- if sys.platform == 'win32':
- # pyreadline masks the read exception. Therefore the bad path error occurs when trying to write the file.
- assert 'readline cannot write' in err
- else:
- # GNU readline raises an exception upon trying to read the directory as a file
- assert 'readline cannot read' in err
-
-
def test_get_all_commands(base_app):
# Verify that the base app has the expected commands
commands = base_app.get_all_commands()
diff --git a/tests/test_history.py b/tests/test_history.py
index 98d72512..32b507ce 100644
--- a/tests/test_history.py
+++ b/tests/test_history.py
@@ -5,6 +5,7 @@ Test history functions of cmd2
"""
import tempfile
import os
+import sys
import pytest
@@ -17,7 +18,35 @@ except ImportError:
import cmd2
from cmd2 import clipboard
from cmd2 import utils
-from .conftest import run_cmd, normalize
+from .conftest import run_cmd, normalize, HELP_HISTORY
+
+
+def test_base_help_history(base_app):
+ out = run_cmd(base_app, 'help history')
+ assert out == normalize(HELP_HISTORY)
+
+def test_exclude_from_history(base_app, monkeypatch):
+ # Mock out the os.system call so we don't actually open an editor
+ m = mock.MagicMock(name='system')
+ monkeypatch.setattr("os.system", m)
+
+ # Run edit command
+ run_cmd(base_app, 'edit')
+
+ # Run history command
+ run_cmd(base_app, 'history')
+
+ # Verify that the history is empty
+ out = run_cmd(base_app, 'history')
+ assert out == []
+
+ # Now run a command which isn't excluded from the history
+ run_cmd(base_app, 'help')
+ # And verify we have a history now ...
+ out = run_cmd(base_app, 'history')
+ expected = normalize(""" 1 help""")
+ assert out == expected
+
@pytest.fixture
def hist():
@@ -264,3 +293,89 @@ def test_history_script_expanded(base_app):
output = run_cmd(base_app, 'history -sx')
expected = ['alias create s shortcuts', 'shortcuts']
assert output == expected
+
+
+#####
+#
+# readline tests
+#
+#####
+def test_readline_remove_history_item(base_app):
+ from cmd2.rl_utils import readline
+ assert readline.get_current_history_length() == 0
+ readline.add_history('this is a test')
+ assert readline.get_current_history_length() == 1
+ readline.remove_history_item(0)
+ assert readline.get_current_history_length() == 0
+
+
+@pytest.fixture(scope="session")
+def hist_file():
+ fd, filename = tempfile.mkstemp(prefix='hist_file', suffix='.txt')
+ os.close(fd)
+ yield filename
+ # teardown code
+ try:
+ os.remove(filename)
+ except FileNotFoundError:
+ pass
+
+def test_existing_history_file(hist_file, capsys):
+ import atexit
+ import readline
+
+ # Create the history file before making cmd2 app
+ with open(hist_file, 'w'):
+ pass
+
+ # Create a new cmd2 app
+ app = cmd2.Cmd(persistent_history_file=hist_file)
+ out, err = capsys.readouterr()
+
+ # Make sure there were no errors
+ assert err == ''
+
+ # Unregister the call to write_history_file that cmd2 did
+ atexit.unregister(readline.write_history_file)
+
+ # Remove created history file
+ os.remove(hist_file)
+
+def test_new_history_file(hist_file, capsys):
+ import atexit
+ import readline
+
+ # Remove any existing history file
+ try:
+ os.remove(hist_file)
+ except OSError:
+ pass
+
+ # Create a new cmd2 app
+ app = cmd2.Cmd(persistent_history_file=hist_file)
+ out, err = capsys.readouterr()
+
+ # Make sure there were no errors
+ assert err == ''
+
+ # Unregister the call to write_history_file that cmd2 did
+ atexit.unregister(readline.write_history_file)
+
+ # Remove created history file
+ os.remove(hist_file)
+
+def test_bad_history_file_path(capsys, request):
+ # Use a directory path as the history file
+ test_dir = os.path.dirname(request.module.__file__)
+
+ # Create a new cmd2 app
+ app = cmd2.Cmd(persistent_history_file=test_dir)
+ out, err = capsys.readouterr()
+
+ if sys.platform == 'win32':
+ # pyreadline masks the read exception. Therefore the bad path error occurs when trying to write the file.
+ assert 'readline cannot write' in err
+ else:
+ # GNU readline raises an exception upon trying to read the directory as a file
+ assert 'readline cannot read' in err
+