Skip to content

Commit b3df078

Browse files
committed
Updated unit tests
1 parent 451f449 commit b3df078

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

tests/test_cmd2.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_base_py(base_app):
244244

245245
@pytest.mark.skipif(sys.platform == 'win32',
246246
reason="Unit test doesn't work on win32, but feature does")
247-
def test_base_run_python_script(base_app, request):
247+
def test_py_run_script(base_app, request):
248248
test_dir = os.path.dirname(request.module.__file__)
249249
python_script = os.path.join(test_dir, 'script.py')
250250
expected = 'This is a python script running ...'
@@ -253,39 +253,6 @@ def test_base_run_python_script(base_app, request):
253253
assert expected in out
254254

255255

256-
def test_base_run_pyscript(base_app, capsys, request):
257-
test_dir = os.path.dirname(request.module.__file__)
258-
python_script = os.path.join(test_dir, 'script.py')
259-
expected = 'This is a python script running ...'
260-
261-
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
262-
assert expected in out
263-
264-
def test_recursive_run_pyscript_not_allowed(base_app, request):
265-
test_dir = os.path.dirname(request.module.__file__)
266-
python_script = os.path.join(test_dir, 'scripts', 'recursive.py')
267-
expected = 'Recursively entering interactive Python consoles is not allowed.'
268-
269-
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
270-
assert err[0] == expected
271-
272-
def test_run_pyscript_with_nonexist_file(base_app):
273-
python_script = 'does_not_exist.py'
274-
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
275-
assert "Error opening script file" in err[0]
276-
277-
def test_run_pyscript_with_exception(base_app, request):
278-
test_dir = os.path.dirname(request.module.__file__)
279-
python_script = os.path.join(test_dir, 'scripts', 'raises_exception.py')
280-
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
281-
assert err[0].startswith('Traceback')
282-
assert "TypeError: unsupported operand type(s) for +: 'int' and 'str'" in err[-1]
283-
284-
def test_run_pyscript_requires_an_argument(base_app):
285-
out, err = run_cmd(base_app, "run_pyscript")
286-
assert "the following arguments are required: script_path" in err[1]
287-
288-
289256
def test_base_error(base_app):
290257
out, err = run_cmd(base_app, 'meow')
291258
assert "is not a recognized command" in err[0]

tests/test_run_pyscript.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,45 @@ def cmdfinalization_hook(data: plugin.CommandFinalizationData) -> plugin.Command
1515
print(HOOK_OUTPUT)
1616
return data
1717

18+
def test_run_pyscript_base(base_app, request):
19+
test_dir = os.path.dirname(request.module.__file__)
20+
python_script = os.path.join(test_dir, 'script.py')
21+
expected = 'This is a python script running ...'
22+
23+
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
24+
assert expected in out
25+
26+
def test_run_pyscript_recursive_not_allowed(base_app, request):
27+
test_dir = os.path.dirname(request.module.__file__)
28+
python_script = os.path.join(test_dir, 'scripts', 'recursive.py')
29+
expected = 'Recursively entering interactive Python consoles is not allowed.'
30+
31+
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
32+
assert err[0] == expected
33+
34+
def test_run_pyscript_with_nonexist_file(base_app):
35+
python_script = 'does_not_exist.py'
36+
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
37+
assert "Error opening script file" in err[0]
38+
39+
def test_run_pyscript_with_exception(base_app, request):
40+
test_dir = os.path.dirname(request.module.__file__)
41+
python_script = os.path.join(test_dir, 'scripts', 'raises_exception.py')
42+
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
43+
assert err[0].startswith('Traceback')
44+
assert "TypeError: unsupported operand type(s) for +: 'int' and 'str'" in err[-1]
45+
46+
def test_run_pyscript_requires_an_argument(base_app):
47+
out, err = run_cmd(base_app, "run_pyscript")
48+
assert "the following arguments are required: script_path" in err[1]
49+
1850
def test_run_pyscript_help(base_app, request):
1951
test_dir = os.path.dirname(request.module.__file__)
2052
python_script = os.path.join(test_dir, 'pyscript', 'help.py')
2153
out1, err1 = run_cmd(base_app, 'help')
2254
out2, err2 = run_cmd(base_app, 'run_pyscript {}'.format(python_script))
2355
assert out1 and out1 == out2
2456

25-
2657
def test_run_pyscript_dir(base_app, request):
2758
test_dir = os.path.dirname(request.module.__file__)
2859
python_script = os.path.join(test_dir, 'pyscript', 'pyscript_dir.py')
@@ -31,7 +62,6 @@ def test_run_pyscript_dir(base_app, request):
3162
assert out
3263
assert out[0] == "['cmd_echo']"
3364

34-
3565
def test_run_pyscript_stdout_capture(base_app, request):
3666
base_app.register_cmdfinalization_hook(cmdfinalization_hook)
3767
test_dir = os.path.dirname(request.module.__file__)
@@ -54,3 +84,13 @@ def test_run_pyscript_stop(base_app, request):
5484
python_script = os.path.join(test_dir, 'pyscript', 'stop.py')
5585
stop = base_app.onecmd_plus_hooks('run_pyscript {}'.format(python_script))
5686
assert stop
87+
88+
def test_pyscript_deprecated_but_works(base_app, request):
89+
test_dir = os.path.dirname(request.module.__file__)
90+
python_script = os.path.join(test_dir, 'script.py')
91+
expected = 'This is a python script running ...'
92+
93+
out, err = run_cmd(base_app, "pyscript {}".format(python_script))
94+
assert expected in out
95+
assert "pyscript has been renamed and will be removed" in err[0]
96+

0 commit comments

Comments
 (0)