diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 2 | ||||
-rw-r--r-- | tests/pyscript/py_locals.py | 5 | ||||
-rw-r--r-- | tests/pyscript/self_in_py.py | 6 | ||||
-rwxr-xr-x | tests/test_cmd2.py | 52 | ||||
-rw-r--r-- | tests/test_run_pyscript.py | 36 |
5 files changed, 58 insertions, 43 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 5ed185a3..c605a73e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -167,7 +167,7 @@ def run_cmd(app, cmd): @fixture def base_app(): - return cmd2.Cmd() + return cmd2.Cmd(include_py=True, include_ipy=True) # These are odd file names for testing quoting of them diff --git a/tests/pyscript/py_locals.py b/tests/pyscript/py_locals.py new file mode 100644 index 00000000..16cb6926 --- /dev/null +++ b/tests/pyscript/py_locals.py @@ -0,0 +1,5 @@ +# flake8: noqa F821 +# Tests how much a pyscript can affect cmd2.Cmd.py_locals + +del [locals()["test_var"]] +my_list.append(2) diff --git a/tests/pyscript/self_in_py.py b/tests/pyscript/self_in_py.py new file mode 100644 index 00000000..f0f6271a --- /dev/null +++ b/tests/pyscript/self_in_py.py @@ -0,0 +1,6 @@ +# flake8: noqa F821 +# Tests self_in_py in pyscripts +if 'self' in globals(): + print("I see self") +else: + print("I do not see self") diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 44d2b304..43c937ae 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -265,39 +265,6 @@ def test_shell_manual_call(base_app): base_app.do_shell(cmd) -def test_base_py(base_app): - # 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(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 - - # Try a print statement - out, err = run_cmd(base_app, 'py print("spaces" + " in this " + "command")') - assert out[0].rstrip() == 'spaces in this command' - - # Set self_in_py to True and make sure we see self - base_app.self_in_py = True - out, err = run_cmd(base_app, 'py print(self)') - assert 'cmd2.cmd2.Cmd object' 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, 'py print(self)') - assert "NameError: name 'self' is not defined" in err - - def test_base_error(base_app): out, err = run_cmd(base_app, 'meow') assert "is not a recognized command" in err[0] @@ -336,15 +303,15 @@ def test_run_script_with_empty_args(base_app): assert "the following arguments are required" in err[1] -def test_run_script_with_nonexistent_file(base_app, capsys): +def test_run_script_with_invalid_file(base_app, request): + # Path does not exist out, err = run_cmd(base_app, 'run_script does_not_exist.txt') - assert "does not exist" in err[0] + assert "Problem accessing script from " in err[0] - -def test_run_script_with_directory(base_app, request): + # Path is a directory test_dir = os.path.dirname(request.module.__file__) out, err = run_cmd(base_app, 'run_script {}'.format(test_dir)) - assert "is not a file" in err[0] + assert "Problem accessing script from " in err[0] def test_run_script_with_empty_file(base_app, request): @@ -1562,12 +1529,12 @@ def test_commandresult_falsy(commandresult_app): def test_is_text_file_bad_input(base_app): # Test with a non-existent file - file_is_valid = utils.is_text_file('does_not_exist.txt') - assert not file_is_valid + with pytest.raises(OSError): + utils.is_text_file('does_not_exist.txt') # Test with a directory - dir_is_valid = utils.is_text_file('.') - assert not dir_is_valid + with pytest.raises(OSError): + utils.is_text_file('.') def test_eof(base_app): @@ -2270,6 +2237,7 @@ def test_get_all_commands(base_app): 'eof', 'help', 'history', + 'ipy', 'macro', 'py', 'quit', 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') |