|
21 | 21 |
|
22 | 22 | import cmd2 |
23 | 23 | from cmd2 import ansi, clipboard, constants, plugin, utils, COMMAND_NAME |
24 | | -from .conftest import run_cmd, normalize, verify_help_text, HELP_HISTORY |
25 | | -from .conftest import SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG, complete_tester |
| 24 | +from .conftest import (run_cmd, normalize, verify_help_text, HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, |
| 25 | + SHOW_LONG, complete_tester, odd_file_names) |
26 | 26 |
|
27 | 27 | def CreateOutsimApp(): |
28 | 28 | c = cmd2.Cmd() |
@@ -431,31 +431,15 @@ def test_relative_run_script(base_app, request): |
431 | 431 | assert script_out == manual_out |
432 | 432 | assert script_err == manual_err |
433 | 433 |
|
434 | | -def test_relative_run_script_with_odd_file_names(base_app, monkeypatch): |
| 434 | +@pytest.mark.parametrize('file_name', odd_file_names) |
| 435 | +def test_relative_run_script_with_odd_file_names(base_app, file_name, monkeypatch): |
435 | 436 | """Test file names with various patterns""" |
436 | 437 | # Mock out the do_run_script call to see what args are passed to it |
437 | 438 | run_script_mock = mock.MagicMock(name='do_run_script') |
438 | 439 | monkeypatch.setattr("cmd2.Cmd.do_run_script", run_script_mock) |
439 | 440 |
|
440 | | - file_name = utils.quote_string('nothingweird.txt') |
441 | | - out, err = run_cmd(base_app, "_relative_run_script {}".format(file_name)) |
442 | | - run_script_mock.assert_called_once_with('"nothingweird.txt"') |
443 | | - run_script_mock.reset_mock() |
444 | | - |
445 | | - file_name = utils.quote_string('has spaces.txt') |
446 | | - out, err = run_cmd(base_app, "_relative_run_script {}".format(file_name)) |
447 | | - run_script_mock.assert_called_once_with('"has spaces.txt"') |
448 | | - run_script_mock.reset_mock() |
449 | | - |
450 | | - file_name = utils.quote_string('"is_double_quoted.txt"') |
451 | | - out, err = run_cmd(base_app, "_relative_run_script {}".format(file_name)) |
452 | | - run_script_mock.assert_called_once_with('\'"is_double_quoted.txt"\'') |
453 | | - run_script_mock.reset_mock() |
454 | | - |
455 | | - file_name = utils.quote_string("'is_single_quoted.txt'") |
456 | | - out, err = run_cmd(base_app, "_relative_run_script {}".format(file_name)) |
457 | | - run_script_mock.assert_called_once_with('"\'is_single_quoted.txt\'"') |
458 | | - run_script_mock.reset_mock() |
| 441 | + run_cmd(base_app, "_relative_run_script {}".format(utils.quote_string(file_name))) |
| 442 | + run_script_mock.assert_called_once_with(utils.quote_string(file_name)) |
459 | 443 |
|
460 | 444 | def test_relative_run_script_requires_an_argument(base_app): |
461 | 445 | out, err = run_cmd(base_app, '_relative_run_script') |
@@ -715,35 +699,17 @@ def test_edit_file(base_app, request, monkeypatch): |
715 | 699 | # We think we have an editor, so should expect a Popen call |
716 | 700 | m.assert_called_once() |
717 | 701 |
|
718 | | -def test_edit_file_with_odd_file_names(base_app, monkeypatch): |
| 702 | +@pytest.mark.parametrize('file_name', odd_file_names) |
| 703 | +def test_edit_file_with_odd_file_names(base_app, file_name, monkeypatch): |
719 | 704 | """Test editor and file names with various patterns""" |
720 | 705 | # Mock out the do_shell call to see what args are passed to it |
721 | 706 | shell_mock = mock.MagicMock(name='do_shell') |
722 | 707 | monkeypatch.setattr("cmd2.Cmd.do_shell", shell_mock) |
723 | 708 |
|
724 | 709 | base_app.editor = 'fooedit' |
725 | 710 | file_name = utils.quote_string('nothingweird.py') |
726 | | - out, err = run_cmd(base_app, "edit {}".format(file_name)) |
727 | | - shell_mock.assert_called_once_with('"fooedit" "nothingweird.py"') |
728 | | - shell_mock.reset_mock() |
729 | | - |
730 | | - base_app.editor = 'foo edit' |
731 | | - file_name = utils.quote_string('has spaces.py') |
732 | | - out, err = run_cmd(base_app, "edit {}".format(file_name)) |
733 | | - shell_mock.assert_called_once_with('"foo edit" "has spaces.py"') |
734 | | - shell_mock.reset_mock() |
735 | | - |
736 | | - base_app.editor = '"fooedit"' |
737 | | - file_name = utils.quote_string('"is_double_quoted.py"') |
738 | | - out, err = run_cmd(base_app, "edit {}".format(file_name)) |
739 | | - shell_mock.assert_called_once_with('\'"fooedit"\' \'"is_double_quoted.py"\'') |
740 | | - shell_mock.reset_mock() |
741 | | - |
742 | | - base_app.editor = "'fooedit'" |
743 | | - file_name = utils.quote_string("'is_single_quoted.py'") |
744 | | - out, err = run_cmd(base_app, "edit {}".format(file_name)) |
745 | | - shell_mock.assert_called_once_with('"\'fooedit\'" "\'is_single_quoted.py\'"') |
746 | | - shell_mock.reset_mock() |
| 711 | + run_cmd(base_app, "edit {}".format(utils.quote_string(file_name))) |
| 712 | + shell_mock.assert_called_once_with('"fooedit" {}'.format(utils.quote_string(file_name))) |
747 | 713 |
|
748 | 714 | def test_edit_file_with_spaces(base_app, request, monkeypatch): |
749 | 715 | # Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock |
@@ -2386,14 +2352,29 @@ def test_startup_script(request): |
2386 | 2352 | startup_script = os.path.join(test_dir, '.cmd2rc') |
2387 | 2353 | app = cmd2.Cmd(allow_cli_args=False, startup_script=startup_script) |
2388 | 2354 | assert len(app._startup_commands) == 1 |
2389 | | - assert app._startup_commands[0] == "run_script '{}'".format(startup_script) |
| 2355 | + assert app._startup_commands[0] == "run_script {}".format(utils.quote_string(startup_script)) |
2390 | 2356 | app._startup_commands.append('quit') |
2391 | 2357 | app.cmdloop() |
2392 | 2358 | out, err = run_cmd(app, 'alias list') |
2393 | 2359 | assert len(out) > 1 |
2394 | 2360 | assert 'alias create ls' in out[0] |
2395 | 2361 |
|
2396 | 2362 |
|
| 2363 | +@pytest.mark.parametrize('startup_script', odd_file_names) |
| 2364 | +def test_startup_script_with_odd_file_names(startup_script): |
| 2365 | + """Test file names with various patterns""" |
| 2366 | + # Mock os.path.exists to trick cmd2 into adding this script to its startup commands |
| 2367 | + saved_exists = os.path.exists |
| 2368 | + os.path.exists = mock.MagicMock(name='exists', return_value=True) |
| 2369 | + |
| 2370 | + app = cmd2.Cmd(allow_cli_args=False, startup_script=startup_script) |
| 2371 | + assert len(app._startup_commands) == 1 |
| 2372 | + assert app._startup_commands[0] == "run_script {}".format(utils.quote_string(os.path.abspath(startup_script))) |
| 2373 | + |
| 2374 | + # Restore os.path.exists |
| 2375 | + os.path.exists = saved_exists |
| 2376 | + |
| 2377 | + |
2397 | 2378 | def test_transcripts_at_init(): |
2398 | 2379 | transcript_files = ['foo', 'bar'] |
2399 | 2380 | app = cmd2.Cmd(allow_cli_args=False, transcript_files=transcript_files) |
|
0 commit comments