Skip to content

Commit a949834

Browse files
committed
Added more unit tests for py and pyscript commands
1 parent 79d5b67 commit a949834

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ News
44
0.7.4
55
-----
66

7-
*Release date: TBD*
7+
*Release date: 2017-07-TBD*
88

99
* Bug fixes
1010
* Fixed a couple bugs in interacting with pastebuffer/clipboard on macOS and Linux
@@ -18,8 +18,10 @@ News
1818
* These also strongly felt out of place
1919
* ``load`` and ``_relative_load`` now require a file path
2020
* ``edit`` and ``save`` now use a temporary file if a file path isn't provided
21-
* Load command has better error checking and reporting
22-
* Clipboard copy and paste functionality is now handled by the ``pyperclip`` module
21+
* ``load`` command has better error checking and reporting
22+
* Clipboard copy and paste functionality is now handled by the **pyperclip** module
23+
* NOTE: This adds an additional required 3rd-party dependency
24+
* Added a lot of unit tests
2325

2426
0.7.3
2527
-----

tests/scripts/raises_exception.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
"""
4+
Example demonstrating what happens when a Python script raises an exception
5+
"""
6+
1 + 'blue'

tests/scripts/recursive.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
"""
4+
Example demonstrating that running a Python script recursively inside another Python script isn't allowed
5+
"""
6+
cmd('pyscript ../script.py')

tests/test_cmd2.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@ def test_base_run_pyscript(base_app, capsys, request):
142142
out, err = capsys.readouterr()
143143
assert out == expected
144144

145+
def test_recursive_pyscript_not_allowed(base_app, capsys, request):
146+
test_dir = os.path.dirname(request.module.__file__)
147+
python_script = os.path.join(test_dir, 'scripts', 'recursive.py')
148+
expected = 'ERROR: Recursively entering interactive Python consoles is not allowed.\n'
149+
150+
run_cmd(base_app, "pyscript {}".format(python_script))
151+
out, err = capsys.readouterr()
152+
assert err == expected
153+
154+
def test_pyscript_with_nonexist_file(base_app, capsys):
155+
python_script = 'does_not_exist.py'
156+
run_cmd(base_app, "pyscript {}".format(python_script))
157+
out, err = capsys.readouterr()
158+
assert err.startswith('ERROR: [Errno 2] No such file or directory:')
159+
160+
def test_pyscript_with_exception(base_app, capsys, request):
161+
test_dir = os.path.dirname(request.module.__file__)
162+
python_script = os.path.join(test_dir, 'scripts', 'raises_exception.py')
163+
run_cmd(base_app, "pyscript {}".format(python_script))
164+
out, err = capsys.readouterr()
165+
assert err.startswith('Traceback')
166+
assert err.endswith("TypeError: unsupported operand type(s) for +: 'int' and 'str'\n")
167+
168+
def test_pyscript_requires_an_argument(base_app, capsys):
169+
run_cmd(base_app, "pyscript")
170+
out, err = capsys.readouterr()
171+
assert err.startswith('ERROR: pyscript command requires at least 1 argument ...')
172+
145173

146174
def test_base_error(base_app):
147175
out = run_cmd(base_app, 'meow')
@@ -957,3 +985,21 @@ def test_select_uneven_list_of_tuples(select_app):
957985

958986
# And verify the expected output to stdout
959987
assert out == expected
988+
989+
@pytest.fixture
990+
def noarglist_app():
991+
cmd2.set_use_arg_list(False)
992+
app = cmd2.Cmd()
993+
app.stdout = StdOut()
994+
return app
995+
996+
def test_pyscript_with_noarglist(noarglist_app, capsys, request):
997+
test_dir = os.path.dirname(request.module.__file__)
998+
python_script = os.path.join(test_dir, '..', 'examples', 'scripts', 'arg_printer.py')
999+
expected = """Running Python script 'arg_printer.py' which was called with 2 arguments
1000+
arg 1: 'foo'
1001+
arg 2: 'bar'
1002+
"""
1003+
run_cmd(noarglist_app, 'pyscript {} foo bar'.format(python_script))
1004+
out, err = capsys.readouterr()
1005+
assert out == expected

0 commit comments

Comments
 (0)