Skip to content

Commit 35e084f

Browse files
committed
Added unit tests
1 parent f3325cb commit 35e084f

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

cmd2/cmd2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,11 +3553,13 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
35533553
else:
35543554
fobj.write('{}\n'.format(command.raw))
35553555
try:
3556+
quoted_fname = utils.quote_string(fname)
3557+
35563558
# noinspection PyTypeChecker
3557-
self.do_edit(utils.quote_string(fname))
3559+
self.do_edit(quoted_fname)
35583560

35593561
# noinspection PyTypeChecker
3560-
self.do_run_script(utils.quote_string(fname))
3562+
self.do_run_script(quoted_fname)
35613563
finally:
35623564
os.remove(fname)
35633565
elif args.output_file:

tests/test_cmd2.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,36 @@ def test_edit_file(base_app, request, monkeypatch):
656656
# We think we have an editor, so should expect a Popen call
657657
m.assert_called_once()
658658

659+
def test_edit_file_with_odd_file_names(base_app, monkeypatch):
660+
"""Test editor and file names with various patterns"""
661+
# Mock out the do_shell call to see what args are passed
662+
shell_mock = mock.MagicMock(name='do_shell')
663+
monkeypatch.setattr("cmd2.Cmd.do_shell", shell_mock)
664+
665+
base_app.editor = 'fooedit'
666+
python_script = utils.quote_string('nothingweird.py')
667+
out, err = run_cmd(base_app, "edit {}".format(python_script))
668+
shell_mock.assert_called_once_with('"fooedit" "nothingweird.py"')
669+
shell_mock.reset_mock()
670+
671+
base_app.editor = 'foo edit'
672+
python_script = utils.quote_string('has spaces.py')
673+
out, err = run_cmd(base_app, "edit {}".format(python_script))
674+
shell_mock.assert_called_once_with('"foo edit" "has spaces.py"')
675+
shell_mock.reset_mock()
676+
677+
base_app.editor = '"fooedit"'
678+
python_script = utils.quote_string('"is_double_quoted.py"')
679+
out, err = run_cmd(base_app, "edit {}".format(python_script))
680+
shell_mock.assert_called_once_with('\'"fooedit"\' \'"is_double_quoted.py"\'')
681+
shell_mock.reset_mock()
682+
683+
base_app.editor = "'fooedit'"
684+
python_script = utils.quote_string("'is_single_quoted.py'")
685+
out, err = run_cmd(base_app, "edit {}".format(python_script))
686+
shell_mock.assert_called_once_with('"\'fooedit\'" "\'is_single_quoted.py\'"')
687+
shell_mock.reset_mock()
688+
659689
def test_edit_file_with_spaces(base_app, request, monkeypatch):
660690
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
661691
base_app.editor = 'fooedit'
@@ -986,7 +1016,7 @@ def do_study(self, arg):
9861016
def do_procrastinate(self, arg):
9871017
"""Waste time in your manner of choice."""
9881018
# Pass in a list of tuples for selections
989-
leisure_activity = self.select([('Netflix and chill', 'Netflix'), ('Porn', 'WebSurfing')],
1019+
leisure_activity = self.select([('Netflix and chill', 'Netflix'), ('YouTube', 'WebSurfing')],
9901020
'How would you like to procrastinate? ')
9911021
result = 'Have fun procrasinating with {}!\n'.format(leisure_activity)
9921022
self.stdout.write(result)
@@ -1098,7 +1128,7 @@ def test_select_list_of_tuples(select_app):
10981128
1. Netflix
10991129
2. WebSurfing
11001130
Have fun procrasinating with {}!
1101-
""".format('Porn'))
1131+
""".format('YouTube'))
11021132

11031133
# Make sure our mock was called with the expected arguments
11041134
m.assert_called_once_with('How would you like to procrastinate? ')

tests/test_run_pyscript.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import builtins
77
import os
88

9-
from cmd2 import plugin
9+
from cmd2 import plugin, utils
1010
from .conftest import run_cmd
1111

1212
# Python 3.5 had some regressions in the unitest.mock module, so use 3rd party mock if available
@@ -52,6 +52,27 @@ def test_run_pyscript_with_non_python_file(base_app, request):
5252
out, err = run_cmd(base_app, 'run_pyscript {}'.format(filename))
5353
assert "does not have a .py extension" in err[0]
5454

55+
def test_run_pyscript_with_odd_file_names(base_app):
56+
"""
57+
Pass in file names with various patterns. Since these files don't exist, we will rely
58+
on the error text to make sure the file names were processed correctly.
59+
"""
60+
python_script = utils.quote_string('nothingweird.py')
61+
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
62+
assert "'nothingweird.py'" in err[0]
63+
64+
python_script = utils.quote_string('has spaces.py')
65+
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
66+
assert "'has spaces.py'" in err[0]
67+
68+
python_script = utils.quote_string('"is_double_quoted.py"')
69+
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
70+
assert "'\"is_double_quoted.py\"'" in err[0]
71+
72+
python_script = utils.quote_string("'is_single_quoted.py'")
73+
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
74+
assert "''is_single_quoted.py''" in err[0]
75+
5576
def test_run_pyscript_with_exception(base_app, request):
5677
test_dir = os.path.dirname(request.module.__file__)
5778
python_script = os.path.join(test_dir, 'pyscript', 'raises_exception.py')
@@ -107,4 +128,4 @@ def test_run_pyscript_run(base_app, request):
107128
expected = 'I have been run'
108129

109130
out, err = run_cmd(base_app, "run_pyscript {}".format(python_script))
110-
assert expected in out
131+
assert expected in out

tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ def test_is_quoted_no():
9393
simple_str = "hello world"
9494
assert not cu.is_quoted(simple_str)
9595

96+
def test_quote_string():
97+
my_str = "Hello World"
98+
assert cu.quote_string(my_str) == '"' + my_str + '"'
99+
100+
my_str = "'Hello World'"
101+
assert cu.quote_string(my_str) == '"' + my_str + '"'
102+
103+
my_str = '"Hello World"'
104+
assert cu.quote_string(my_str) == "'" + my_str + "'"
105+
96106
def test_quote_string_if_needed_yes():
97107
my_str = "Hello World"
98108
assert cu.quote_string_if_needed(my_str) == '"' + my_str + '"'

0 commit comments

Comments
 (0)