summaryrefslogtreecommitdiff
path: root/tests/test_run_pyscript.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2021-03-26 13:56:33 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2021-03-26 15:24:34 -0400
commit62ed8aebf6eefcf68a15fdd4b7a7cd5dfe4c6f6b (patch)
tree1e72725041a8e3f83f31dddbfd564fcd02f27401 /tests/test_run_pyscript.py
parent070262e1f397e2297cdb1ad611db6b6d5bed8830 (diff)
downloadcmd2-git-py_refactor.tar.gz
Renamed use_ipython keyword parameter of cmd2.Cmd.__init__() to include_ipy.py_refactor
Added include_py keyword parameter to cmd2.Cmd.__init__(). If False, then the py command will not be available. Removed ability to run Python commands from the command line with py. Made banners and exit messages of Python and IPython consistent. Changed utils.is_text_file() to raise OSError if file cannot be read.
Diffstat (limited to 'tests/test_run_pyscript.py')
-rw-r--r--tests/test_run_pyscript.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/test_run_pyscript.py b/tests/test_run_pyscript.py
index b8daa24b..02e75ed5 100644
--- a/tests/test_run_pyscript.py
+++ b/tests/test_run_pyscript.py
@@ -141,6 +141,42 @@ def test_run_pyscript_environment(base_app, request):
assert out[0] == "PASSED"
+def test_run_pyscript_self_in_py(base_app, request):
+ test_dir = os.path.dirname(request.module.__file__)
+ python_script = os.path.join(test_dir, 'pyscript', 'self_in_py.py')
+
+ # Set self_in_py to True and make sure we see self
+ base_app.self_in_py = True
+ out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script))
+ assert 'I see self' in out[0]
+
+ # Set self_in_py to False and make sure we can't see self
+ base_app.self_in_py = False
+ out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script))
+ assert 'I do not see self' in out[0]
+
+
+def test_run_pyscript_py_locals(base_app, request):
+ test_dir = os.path.dirname(request.module.__file__)
+ python_script = os.path.join(test_dir, 'pyscript', 'py_locals.py')
+
+ # Make sure pyscripts 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
+
+ # 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'] = []
+
+ run_cmd(base_app, 'run_pyscript {}'.format(python_script))
+
+ # test_var should still exist
+ assert base_app.py_locals['test_var'] == 5
+
+ # my_list should be edited
+ assert base_app.py_locals['my_list'][0] == 2
+
+
def test_run_pyscript_app_echo(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
python_script = os.path.join(test_dir, 'pyscript', 'echo.py')