summaryrefslogtreecommitdiff
path: root/tests/test_cmd2.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2020-02-20 20:36:33 -0700
committerkotfu <kotfu@kotfu.net>2020-02-20 20:36:33 -0700
commitf0c3cbffa35dfe26b8ac54ea1450ed50ba787c9a (patch)
tree03b0e85aaaccc0706caf5658446dda0a5a13cd9f /tests/test_cmd2.py
parent1e57b0c451de23338378f44088118648848cc82c (diff)
parent22de85832e877b5b360eeacd4b71e00f69bf00e1 (diff)
downloadcmd2-git-f0c3cbffa35dfe26b8ac54ea1450ed50ba787c9a.tar.gz
Merge branch 'master' into api_docs
# Conflicts: # CHANGELOG.md # cmd2/__init__.py # cmd2/decorators.py # docs/api/utility_functions.rst
Diffstat (limited to 'tests/test_cmd2.py')
-rwxr-xr-xtests/test_cmd2.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 376658e5..2db52d39 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, plugin, utils, COMMAND_NAME
+from cmd2 import ansi, clipboard, constants, exceptions, plugin, utils, COMMAND_NAME
from .conftest import (run_cmd, normalize, verify_help_text, HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT,
SHOW_LONG, complete_tester, odd_file_names)
@@ -205,14 +205,24 @@ def test_base_shell(base_app, monkeypatch):
def test_base_py(base_app):
- # Create a variable and make sure we can see it
- out, err = run_cmd(base_app, 'py qqq=3')
- assert not out
+ # Make sure py can't edit Cmd.py_locals. It used to be that cmd2 was passing its py_locals
+ # dictionary to the py environment instead of a shallow copy.
+ base_app.py_locals['test_var'] = 5
+ out, err = run_cmd(base_app, 'py del[locals()["test_var"]]')
+ assert not out and not err
+ assert base_app.py_locals['test_var'] == 5
- out, err = run_cmd(base_app, 'py print(qqq)')
- assert out[0].rstrip() == '3'
+ out, err = run_cmd(base_app, 'py print(test_var)')
+ assert out[0].rstrip() == '5'
+
+ # Place an editable object in py_locals. Since we make a shallow copy of py_locals,
+ # this object should be editable from the py environment.
+ base_app.py_locals['my_list'] = []
+ out, err = run_cmd(base_app, 'py my_list.append(2)')
+ assert not out and not err
+ assert base_app.py_locals['my_list'][0] == 2
- # Add a more complex statement
+ # Try a print statement
out, err = run_cmd(base_app, 'py print("spaces" + " in this " + "command")')
assert out[0].rstrip() == 'spaces in this command'
@@ -1248,7 +1258,7 @@ def multiline_app():
return app
def test_multiline_complete_empty_statement_raises_exception(multiline_app):
- with pytest.raises(cmd2.EmptyStatement):
+ with pytest.raises(exceptions.EmptyStatement):
multiline_app._complete_statement('')
def test_multiline_complete_statement_without_terminator(multiline_app):