Skip to content

Commit 3bc87c3

Browse files
committed
Added unit tests
1 parent 35e084f commit 3bc87c3

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

cmd2/cmd2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,6 +3553,7 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
35533553
else:
35543554
fobj.write('{}\n'.format(command.raw))
35553555
try:
3556+
# Handle potential edge case where the temp file needs to be quoted on the command line
35563557
quoted_fname = utils.quote_string(fname)
35573558

35583559
# noinspection PyTypeChecker

tests/test_cmd2.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,33 @@ def test_relative_run_script(base_app, request):
427427
assert script_out == manual_out
428428
assert script_err == manual_err
429429

430+
def test_relative_run_script_with_odd_file_names(base_app, monkeypatch):
431+
"""Test file names with various patterns"""
432+
# Mock out the do_run_script call to see what args are passed
433+
run_script_mock = mock.MagicMock(name='do_run_script')
434+
monkeypatch.setattr("cmd2.Cmd.do_run_script", run_script_mock)
435+
436+
file_name = utils.quote_string('nothingweird.txt')
437+
out, err = run_cmd(base_app, "run_script {}".format(file_name))
438+
run_script_mock.assert_called_once_with('"nothingweird.txt"')
439+
run_script_mock.reset_mock()
440+
441+
file_name = utils.quote_string('has spaces.txt')
442+
out, err = run_cmd(base_app, "run_script {}".format(file_name))
443+
run_script_mock.assert_called_once_with('"has spaces.txt"')
444+
run_script_mock.reset_mock()
445+
446+
file_name = utils.quote_string('"is_double_quoted.txt"')
447+
out, err = run_cmd(base_app, "run_script {}".format(file_name))
448+
run_script_mock.assert_called_once_with('\'"is_double_quoted.txt"\'')
449+
run_script_mock.reset_mock()
450+
451+
file_name = utils.quote_string("'is_single_quoted.txt'")
452+
out, err = run_cmd(base_app, "run_script {}".format(file_name))
453+
run_script_mock.assert_called_once_with('"\'is_single_quoted.txt\'"')
454+
run_script_mock.reset_mock()
455+
456+
430457
def test_relative_run_script_requires_an_argument(base_app):
431458
out, err = run_cmd(base_app, '_relative_run_script')
432459
assert 'Error: the following arguments' in err[1]
@@ -663,26 +690,26 @@ def test_edit_file_with_odd_file_names(base_app, monkeypatch):
663690
monkeypatch.setattr("cmd2.Cmd.do_shell", shell_mock)
664691

665692
base_app.editor = 'fooedit'
666-
python_script = utils.quote_string('nothingweird.py')
667-
out, err = run_cmd(base_app, "edit {}".format(python_script))
693+
file_name = utils.quote_string('nothingweird.py')
694+
out, err = run_cmd(base_app, "edit {}".format(file_name))
668695
shell_mock.assert_called_once_with('"fooedit" "nothingweird.py"')
669696
shell_mock.reset_mock()
670697

671698
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))
699+
file_name = utils.quote_string('has spaces.py')
700+
out, err = run_cmd(base_app, "edit {}".format(file_name))
674701
shell_mock.assert_called_once_with('"foo edit" "has spaces.py"')
675702
shell_mock.reset_mock()
676703

677704
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))
705+
file_name = utils.quote_string('"is_double_quoted.py"')
706+
out, err = run_cmd(base_app, "edit {}".format(file_name))
680707
shell_mock.assert_called_once_with('\'"fooedit"\' \'"is_double_quoted.py"\'')
681708
shell_mock.reset_mock()
682709

683710
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))
711+
file_name = utils.quote_string("'is_single_quoted.py'")
712+
out, err = run_cmd(base_app, "edit {}".format(file_name))
686713
shell_mock.assert_called_once_with('"\'fooedit\'" "\'is_single_quoted.py\'"')
687714
shell_mock.reset_mock()
688715

0 commit comments

Comments
 (0)