diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-07-01 18:40:47 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-07-01 18:40:47 -0400 |
commit | a949834c4c55b4fdbec32cba09e5fe5f5bdc15fa (patch) | |
tree | f9a22ae2c9db372a3d9154e430fd99a86e177a25 /tests | |
parent | 79d5b675ab52a0522462e0ccfdab236787f3dac0 (diff) | |
download | cmd2-git-a949834c4c55b4fdbec32cba09e5fe5f5bdc15fa.tar.gz |
Added more unit tests for py and pyscript commands
Diffstat (limited to 'tests')
-rw-r--r-- | tests/scripts/raises_exception.py | 6 | ||||
-rw-r--r-- | tests/scripts/recursive.py | 6 | ||||
-rw-r--r-- | tests/test_cmd2.py | 46 |
3 files changed, 58 insertions, 0 deletions
diff --git a/tests/scripts/raises_exception.py b/tests/scripts/raises_exception.py new file mode 100644 index 00000000..738edaf2 --- /dev/null +++ b/tests/scripts/raises_exception.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python +# coding=utf-8 +""" +Example demonstrating what happens when a Python script raises an exception +""" +1 + 'blue' diff --git a/tests/scripts/recursive.py b/tests/scripts/recursive.py new file mode 100644 index 00000000..84f445bb --- /dev/null +++ b/tests/scripts/recursive.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python +# coding=utf-8 +""" +Example demonstrating that running a Python script recursively inside another Python script isn't allowed +""" +cmd('pyscript ../script.py') diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 7e9769e3..1bed3c07 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -142,6 +142,34 @@ def test_base_run_pyscript(base_app, capsys, request): out, err = capsys.readouterr() assert out == expected +def test_recursive_pyscript_not_allowed(base_app, capsys, request): + test_dir = os.path.dirname(request.module.__file__) + python_script = os.path.join(test_dir, 'scripts', 'recursive.py') + expected = 'ERROR: Recursively entering interactive Python consoles is not allowed.\n' + + run_cmd(base_app, "pyscript {}".format(python_script)) + out, err = capsys.readouterr() + assert err == expected + +def test_pyscript_with_nonexist_file(base_app, capsys): + python_script = 'does_not_exist.py' + run_cmd(base_app, "pyscript {}".format(python_script)) + out, err = capsys.readouterr() + assert err.startswith('ERROR: [Errno 2] No such file or directory:') + +def test_pyscript_with_exception(base_app, capsys, request): + test_dir = os.path.dirname(request.module.__file__) + python_script = os.path.join(test_dir, 'scripts', 'raises_exception.py') + run_cmd(base_app, "pyscript {}".format(python_script)) + out, err = capsys.readouterr() + assert err.startswith('Traceback') + assert err.endswith("TypeError: unsupported operand type(s) for +: 'int' and 'str'\n") + +def test_pyscript_requires_an_argument(base_app, capsys): + run_cmd(base_app, "pyscript") + out, err = capsys.readouterr() + assert err.startswith('ERROR: pyscript command requires at least 1 argument ...') + def test_base_error(base_app): out = run_cmd(base_app, 'meow') @@ -957,3 +985,21 @@ Charm us with the {}... # And verify the expected output to stdout assert out == expected + +@pytest.fixture +def noarglist_app(): + cmd2.set_use_arg_list(False) + app = cmd2.Cmd() + app.stdout = StdOut() + return app + +def test_pyscript_with_noarglist(noarglist_app, capsys, request): + test_dir = os.path.dirname(request.module.__file__) + python_script = os.path.join(test_dir, '..', 'examples', 'scripts', 'arg_printer.py') + expected = """Running Python script 'arg_printer.py' which was called with 2 arguments +arg 1: 'foo' +arg 2: 'bar' +""" + run_cmd(noarglist_app, 'pyscript {} foo bar'.format(python_script)) + out, err = capsys.readouterr() + assert out == expected |