diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-26 23:09:02 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-26 23:09:02 -0400 |
commit | 6a4eee4d850b9af719a8192a3ac8ad51f6601ee2 (patch) | |
tree | 2297297c3954e22fd51c08a2fe00364a09ddad7e /tests/test_cmd2.py | |
parent | eff66eac71d1e7ccf13b782ed22b36dd86751540 (diff) | |
parent | 2315338a5986b1d2fa64d0b6cc15b2659e80c3ee (diff) | |
download | cmd2-git-6a4eee4d850b9af719a8192a3ac8ad51f6601ee2.tar.gz |
Merge branch 'macro' into argparse_conversion
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 223 |
1 files changed, 195 insertions, 28 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 8011ba30..7e2c9fc6 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1766,6 +1766,36 @@ def test_poutput_color_never(base_app): assert out == expected +# These are invalid names for aliases and macros +invalid_command_name = [ + '""', # Blank name + '!no_shortcut', + '">"', + '"no>pe"', + '"no spaces"', + '"nopipe|"', + '"noterm;"', + 'noembedded"quotes', +] + +def test_get_alias_names(base_app): + assert len(base_app.aliases) == 0 + run_cmd(base_app, 'alias create fake pyscript') + run_cmd(base_app, 'alias create ls !ls -hal') + assert len(base_app.aliases) == 2 + assert sorted(base_app.get_alias_names()) == ['fake', 'ls'] + +def test_get_macro_names(base_app): + assert len(base_app.macros) == 0 + run_cmd(base_app, 'macro create foo !echo foo') + run_cmd(base_app, 'macro create bar !echo bar') + assert len(base_app.macros) == 2 + assert sorted(base_app.get_macro_names()) == ['bar', 'foo'] + +def test_alias_no_subcommand(base_app, capsys): + out = run_cmd(base_app, 'alias') + assert "Usage: alias [-h]" in out[0] + def test_alias_create(base_app, capsys): # Create the alias out = run_cmd(base_app, 'alias create fake pyscript') @@ -1784,7 +1814,25 @@ def test_alias_create(base_app, capsys): out = run_cmd(base_app, 'alias list fake') assert out == normalize('alias create fake pyscript') -def test_alias_create_with_quotes(base_app, capsys): +def test_alias_quoted_name(base_app, capsys): + """Demonstrate that names can be quoted in alias commands because they will be stripped""" + # Create the alias + out = run_cmd(base_app, 'alias create "fake" pyscript') + + # The quotes on names are stripped + assert out == normalize("Alias 'fake' created") + + # Look up the new alias and quote the name + out = run_cmd(base_app, 'alias list "fake"') + assert out == normalize('alias create fake pyscript') + + # Delete the alias using quotes + out = run_cmd(base_app, 'alias delete "fake"') + assert out == normalize("Alias 'fake' deleted") + +def test_alias_create_with_quoted_value(base_app, capsys): + """Demonstrate that quotes in alias value will be preserved (except for redirectors)""" + # Create the alias out = run_cmd(base_app, 'alias create fake help ">" "out file.txt"') assert out == normalize("Alias 'fake' created") @@ -1793,16 +1841,7 @@ def test_alias_create_with_quotes(base_app, capsys): out = run_cmd(base_app, 'alias list fake') assert out == normalize('alias create fake help > "out file.txt"') -@pytest.mark.parametrize('alias_name', [ - '""', # Blank name - '!no_shortcut', - '">"', - '"no>pe"', - '"no spaces"', - '"nopipe|"', - '"noterm;"', - 'noembedded"quotes', -]) +@pytest.mark.parametrize('alias_name', invalid_command_name) def test_alias_create_invalid_name(base_app, alias_name, capsys): run_cmd(base_app, 'alias create {} help'.format(alias_name)) out, err = capsys.readouterr() @@ -1813,27 +1852,13 @@ def test_alias_create_with_macro_name(base_app, capsys): run_cmd(base_app, 'macro create {} help'.format(macro)) run_cmd(base_app, 'alias create {} help'.format(macro)) out, err = capsys.readouterr() - assert "cannot have the same name" in err - -@pytest.mark.parametrize('alias_target', [ - '""', # Blank name - '">"', - '"no>pe"', - '"no spaces"', - '"nopipe|"', - '"noterm;"', - 'noembedded"quotes', -]) -def test_alias_create_with_invalid_command(base_app, alias_target, capsys): - run_cmd(base_app, 'alias create my_alias {}'.format(alias_target)) - out, err = capsys.readouterr() - assert "Invalid alias target" in err + assert "Aliases cannot have the same name as a macro" in err def test_alias_list_invalid_alias(base_app, capsys): # Look up invalid alias out = run_cmd(base_app, 'alias list invalid') out, err = capsys.readouterr() - assert "not found" in err + assert "Alias 'invalid' not found" in err def test_alias_delete(base_app): # Create an alias @@ -1850,7 +1875,11 @@ def test_alias_delete_all(base_app): def test_alias_delete_non_existing(base_app, capsys): run_cmd(base_app, 'alias delete fake') out, err = capsys.readouterr() - assert "does not exist" in err + assert "Alias 'fake' does not exist" in err + +def test_alias_delete_no_name(base_app, capsys): + out = run_cmd(base_app, 'alias delete') + assert "Usage: alias delete" in out[0] def test_multiple_aliases(base_app): alias1 = 'h1' @@ -1865,6 +1894,144 @@ def test_multiple_aliases(base_app): expected = normalize(BASE_HELP_VERBOSE) assert out == expected +def test_macro_no_subcommand(base_app, capsys): + out = run_cmd(base_app, 'macro') + assert "Usage: macro [-h]" in out[0] + +def test_macro_create(base_app, capsys): + # Create the macro + out = run_cmd(base_app, 'macro create fake pyscript') + assert out == normalize("Macro 'fake' created") + + # Use the macro + run_cmd(base_app, 'fake') + out, err = capsys.readouterr() + assert "pyscript command requires at least 1 argument" in err + + # See a list of macros + out = run_cmd(base_app, 'macro list') + assert out == normalize('macro create fake pyscript') + + # Look up the new macro + out = run_cmd(base_app, 'macro list fake') + assert out == normalize('macro create fake pyscript') + +def test_macro_create_quoted_name(base_app, capsys): + """Demonstrate that names can be quoted in macro commands because they will be stripped""" + # Create the macro + out = run_cmd(base_app, 'macro create "fake" pyscript') + + # The quotes on names are stripped + assert out == normalize("Macro 'fake' created") + + # Look up the new macro and quote the name + out = run_cmd(base_app, 'macro list "fake"') + assert out == normalize('macro create fake pyscript') + + # Delete the macro using quotes + out = run_cmd(base_app, 'macro delete "fake"') + assert out == normalize("Macro 'fake' deleted") + +def test_macro_create_with_quoted_value(base_app, capsys): + """Demonstrate that quotes in macro value will be preserved (except for redirectors)""" + # Create the macro + out = run_cmd(base_app, 'macro create fake help ">" "out file.txt"') + assert out == normalize("Macro 'fake' created") + + # Look up the new macro (Only the redirector should be unquoted) + out = run_cmd(base_app, 'macro list fake') + assert out == normalize('macro create fake help > "out file.txt"') + +@pytest.mark.parametrize('macro_name', invalid_command_name) +def test_macro_create_invalid_name(base_app, macro_name, capsys): + run_cmd(base_app, 'macro create {} help'.format(macro_name)) + out, err = capsys.readouterr() + assert "Invalid macro name" in err + +def test_macro_create_with_alias_name(base_app, capsys): + macro = "my_macro" + run_cmd(base_app, 'alias create {} help'.format(macro)) + run_cmd(base_app, 'macro create {} help'.format(macro)) + out, err = capsys.readouterr() + assert "Macros cannot have the same name as an alias" in err + +def test_macro_create_with_command_name(base_app, capsys): + macro = "my_macro" + run_cmd(base_app, 'macro create help stuff') + out, err = capsys.readouterr() + assert "Macros cannot have the same name as a command" in err + +def test_macro_create_with_args(base_app, capsys): + # Create the macro + out = run_cmd(base_app, 'macro create fake {1} {2}') + assert out == normalize("Macro 'fake' created") + + # Run the macro + out = run_cmd(base_app, 'fake help -v') + expected = normalize(BASE_HELP_VERBOSE) + assert out == expected + +def test_macro_create_with_escaped_args(base_app, capsys): + # Create the macro + out = run_cmd(base_app, 'macro create fake help {{1}}') + assert out == normalize("Macro 'fake' created") + + # Run the macro + out = run_cmd(base_app, 'fake') + assert 'No help on {1}' in out[0] + +def test_macro_create_with_missing_arg_nums(base_app, capsys): + # Create the macro + run_cmd(base_app, 'macro create fake help {1} {3}') + out, err = capsys.readouterr() + assert "Not all numbers between 1 and 3" in err + +def test_macro_create_with_invalid_arg_num(base_app, capsys): + # Create the macro + run_cmd(base_app, 'macro create fake help {1} {-1} {0}') + out, err = capsys.readouterr() + assert "Argument numbers must be greater than 0" in err + +def test_macro_list_invalid_macro(base_app, capsys): + # Look up invalid macro + out = run_cmd(base_app, 'macro list invalid') + out, err = capsys.readouterr() + assert "Macro 'invalid' not found" in err + +def test_macro_delete(base_app): + # Create an macro + run_cmd(base_app, 'macro create fake pyscript') + + # Delete the macro + out = run_cmd(base_app, 'macro delete fake') + assert out == normalize("Macro 'fake' deleted") + +def test_macro_delete_all(base_app): + out = run_cmd(base_app, 'macro delete --all') + assert out == normalize("All macros deleted") + +def test_macro_delete_non_existing(base_app, capsys): + run_cmd(base_app, 'macro delete fake') + out, err = capsys.readouterr() + assert "Macro 'fake' does not exist" in err + +def test_macro_delete_no_name(base_app, capsys): + out = run_cmd(base_app, 'macro delete') + assert "Usage: macro delete" in out[0] + +def test_multiple_macros(base_app): + macro1 = 'h1' + macro2 = 'h2' + run_cmd(base_app, 'macro create {} help'.format(macro1)) + run_cmd(base_app, 'macro create {} help -v'.format(macro2)) + out = run_cmd(base_app, macro1) + expected = normalize(BASE_HELP) + assert out == expected + + out = run_cmd(base_app, macro2) + expected = normalize(BASE_HELP_VERBOSE) + assert out == expected + def test_ppaged(base_app): msg = 'testing...' |