diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-02-14 16:28:41 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-02-14 16:28:41 -0500 |
commit | 9156618a56d635bb51261d019a3703a1b4e3b588 (patch) | |
tree | 3f35d07cf668ec156dac6e97e612569c83aa36c6 /tests | |
parent | 013b9e0a2c75e17f8aa0e0f7cbe50d84d2f657d8 (diff) | |
download | cmd2-git-9156618a56d635bb51261d019a3703a1b4e3b588.tar.gz |
Fixed bug where pyscripts could edit cmd2.Cmd.py_locals dictionary.
Fixed bug where cmd2 set sys.path[0] for a pyscript to its cwd instead of the script's directory.
Fixed bug where sys.path was not being restored after a pyscript ran.
Setting the following pyscript variables:
__name__: __main__
__file__: script path (as typed)
Removed do_py.run() function since it didn't handle arguments and offered no benefit over run_pyscript.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/pyscript/environment.py | 20 | ||||
-rw-r--r-- | tests/pyscript/recursive.py | 1 | ||||
-rw-r--r-- | tests/pyscript/run.py | 6 | ||||
-rw-r--r-- | tests/pyscript/to_run.py | 2 | ||||
-rwxr-xr-x | tests/test_cmd2.py | 15 | ||||
-rw-r--r-- | tests/test_run_pyscript.py | 9 |
6 files changed, 34 insertions, 19 deletions
diff --git a/tests/pyscript/environment.py b/tests/pyscript/environment.py new file mode 100644 index 00000000..5e4d93d6 --- /dev/null +++ b/tests/pyscript/environment.py @@ -0,0 +1,20 @@ +# flake8: noqa F821 +# Tests that cmd2 populates __name__, __file__, and sets sys.path[0] to our directory +import os +import sys +app.cmd_echo = True + +if __name__ != '__main__': + print("Error: __name__ is: {}".format(__name__)) + quit() + +if __file__ != sys.argv[0]: + print("Error: __file__ is: {}".format(__file__)) + quit() + +our_dir = os.path.dirname(os.path.abspath(__file__)) +if our_dir != sys.path[0]: + print("Error: our_dir is: {}".format(our_dir)) + quit() + +print("PASSED") diff --git a/tests/pyscript/recursive.py b/tests/pyscript/recursive.py index 21550592..7f02bb78 100644 --- a/tests/pyscript/recursive.py +++ b/tests/pyscript/recursive.py @@ -5,6 +5,7 @@ Example demonstrating that calling run_pyscript recursively inside another Python script isn't allowed """ import os +import sys app.cmd_echo = True my_dir = (os.path.dirname(os.path.realpath(sys.argv[0]))) diff --git a/tests/pyscript/run.py b/tests/pyscript/run.py deleted file mode 100644 index 47250a10..00000000 --- a/tests/pyscript/run.py +++ /dev/null @@ -1,6 +0,0 @@ -# flake8: noqa F821 -import os - -app.cmd_echo = True -my_dir = (os.path.dirname(os.path.realpath(sys.argv[0]))) -run(os.path.join(my_dir, 'to_run.py')) diff --git a/tests/pyscript/to_run.py b/tests/pyscript/to_run.py deleted file mode 100644 index b207952d..00000000 --- a/tests/pyscript/to_run.py +++ /dev/null @@ -1,2 +0,0 @@ -# flake8: noqa F821 -print("I have been run") diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 376658e5..41528612 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -205,14 +205,17 @@ 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 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' - # 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' diff --git a/tests/test_run_pyscript.py b/tests/test_run_pyscript.py index d717758c..811fd688 100644 --- a/tests/test_run_pyscript.py +++ b/tests/test_run_pyscript.py @@ -117,10 +117,9 @@ def test_run_pyscript_stop(base_app, request): stop = base_app.onecmd_plus_hooks('run_pyscript {}'.format(python_script)) assert stop -def test_run_pyscript_run(base_app, request): +def test_run_pyscript_environment(base_app, request): test_dir = os.path.dirname(request.module.__file__) - python_script = os.path.join(test_dir, 'pyscript', 'run.py') - expected = 'I have been run' + python_script = os.path.join(test_dir, 'pyscript', 'environment.py') + out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script)) - out, err = run_cmd(base_app, "run_pyscript {}".format(python_script)) - assert expected in out + assert out[0] == "PASSED" |