summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r--tests/test_cmd2.py114
1 files changed, 104 insertions, 10 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index b973fdf5..25d1db3f 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -231,7 +231,7 @@ def test_pyscript_with_nonexist_file(base_app, capsys):
python_script = 'does_not_exist.py'
run_cmd(base_app, "pyscript {}".format(python_script))
out, err = capsys.readouterr()
- assert err.startswith("EXCEPTION of type 'FileNotFoundError' occurred with message:")
+ assert "Error opening script file" in err
def test_pyscript_with_exception(base_app, capsys, request):
test_dir = os.path.dirname(request.module.__file__)
@@ -426,7 +426,7 @@ def test_history_run_all_commands(base_app):
out = run_cmd(base_app, 'history -r')
# this should generate an error, but we don't currently have a way to
# capture stderr in these tests. So we assume that if we got nothing on
- # standard out, that the error occured because if the commaned executed
+ # standard out, that the error occurred because if the command executed
# then we should have a list of shortcuts in our output
assert out == []
@@ -435,6 +435,23 @@ def test_history_run_one_command(base_app):
output = run_cmd(base_app, 'history -r 1')
assert output == expected
+def test_history_clear(base_app):
+ # Add commands to history
+ run_cmd(base_app, 'help')
+ run_cmd(base_app, 'alias')
+
+ # Make sure history has items
+ out = run_cmd(base_app, 'history')
+ assert out
+
+ # Clear the history
+ run_cmd(base_app, 'history --clear')
+
+ # Make sure history is empty
+ out = run_cmd(base_app, 'history')
+ assert out == []
+
+
def test_base_load(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'script.txt')
@@ -457,8 +474,7 @@ def test_load_with_empty_args(base_app, capsys):
out, err = capsys.readouterr()
# The load command requires a file path argument, so we should get an error message
- expected = normalize("""ERROR: load command requires a file path:\n""")
- assert normalize(str(err)) == expected
+ assert "load command requires a file path" in str(err)
assert base_app.cmdqueue == []
@@ -468,10 +484,18 @@ def test_load_with_nonexistent_file(base_app, capsys):
out, err = capsys.readouterr()
# The load command requires a path to an existing file
- assert str(err).startswith("ERROR")
- assert "does not exist or is not a file" in str(err)
+ assert "does not exist" in str(err)
assert base_app.cmdqueue == []
+def test_load_with_directory(base_app, capsys, request):
+ test_dir = os.path.dirname(request.module.__file__)
+
+ # The way the load command works, we can't directly capture its stdout or stderr
+ run_cmd(base_app, 'load {}'.format(test_dir))
+ out, err = capsys.readouterr()
+
+ assert "is not a file" in str(err)
+ assert base_app.cmdqueue == []
def test_load_with_empty_file(base_app, capsys, request):
test_dir = os.path.dirname(request.module.__file__)
@@ -481,8 +505,7 @@ def test_load_with_empty_file(base_app, capsys, request):
run_cmd(base_app, 'load {}'.format(filename))
out, err = capsys.readouterr()
- # The load command requires non-empty scripts files
- assert str(err).startswith("ERROR")
+ # The load command requires non-empty script files
assert "is empty" in str(err)
assert base_app.cmdqueue == []
@@ -724,7 +747,7 @@ def test_pipe_to_shell(base_app, capsys):
out, err = capsys.readouterr()
# Unfortunately with the improved way of piping output to a subprocess, there isn't any good way of getting
- # access to the output produced by that subprocess within a unit test, but we can verify that no error occured
+ # access to the output produced by that subprocess within a unit test, but we can verify that no error occurred
assert not err
def test_pipe_to_shell_error(base_app, capsys):
@@ -1225,7 +1248,7 @@ Other
================================================================================
alias Define or display aliases
help List available commands with "help" or detailed help with "help cmd".
-history View, run, edit, and save previously entered commands.
+history View, run, edit, save, or clear previously entered commands.
load Runs commands in script file that is encoded as either ASCII or UTF-8 text.
py Invoke python command, shell, or script
pyscript Runs a python script file inside the console
@@ -1802,3 +1825,74 @@ def test_onecmd_raw_str_quit(base_app):
assert stop
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