diff options
Diffstat (limited to 'tests/test_cmd2.py')
-rwxr-xr-x | tests/test_cmd2.py | 26 |
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): |