def test_base_history(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') out = run_cmd(base_app, 'history') expected = normalize(""" -------------------------[1] help -------------------------[2] shortcuts """) assert out == expected out = run_cmd(base_app, 'history he') expected = normalize(""" -------------------------[1] help """) assert out == expected out = run_cmd(base_app, 'history sh') expected = normalize(""" -------------------------[2] shortcuts """) assert out == expected
def test_set_not_supported(base_app, capsys): run_cmd(base_app, 'set qqq True') out, err = capsys.readouterr() expected = normalize(""" EXCEPTION of type 'LookupError' occurred with message: 'Parameter 'qqq' not supported (type 'show' for list of parameters).' To enable full traceback, run the following command: 'set debug true' """) assert normalize(str(err)) == expected
def test_load_with_empty_args(base_app, capsys): # The way the load command works, we can't directly capture its stdout or stderr run_cmd(base_app, 'load') out, err = capsys.readouterr() # The load command requires a file path argument, so we should get an error message expected = normalize("""ERROR: load command requires a file path:\n""") assert normalize(str(err)) == expected assert base_app.cmdqueue == []
def test_send_to_paste_buffer(base_app): # Test writing to the PasteBuffer/Clipboard run_cmd(base_app, 'help >') expected = normalize(BASE_HELP) assert normalize(cmd2.get_paste_buffer()) == expected # Test appending to the PasteBuffer/Clipboard run_cmd(base_app, 'help history >>') expected = normalize(BASE_HELP + '\n' + HELP_HISTORY) assert normalize(cmd2.get_paste_buffer()) == expected
def run_submenu_cmd(app, second_level_app, cmd): """ Clear StdSim buffers, run the command, extract the buffer contents.""" app.stdout.clear() second_level_app.stdout.clear() app.onecmd_plus_hooks(cmd) out1 = app.stdout.getvalue() out2 = second_level_app.stdout.getvalue() app.stdout.clear() second_level_app.stdout.clear() return normalize(out1), normalize(out2)
def test_pipe_to_shell(base_app): # Get help on help and pipe it's output to the input of the word count shell command out = run_cmd(base_app, 'help help | wc') if sys.platform == "win32": expected = normalize("1 11 71") else: expected = normalize("1 11 70") assert out[0].strip() == expected[0].strip()
def _get_transcript_blocks(transcript): cmd = None expected = '' for line in transcript.splitlines(): if line.startswith('(Cmd) '): if cmd is not None: yield cmd, normalize(expected) cmd = line[6:] expected = '' else: expected += line + '\n' yield cmd, normalize(expected)
def test_base_load_default_file(base_app, capsys): # TODO: Make sure to remove the 'command.txt' file in case it exists # The way the load command works, we can't directly capture its stdout or stderr run_cmd(base_app, 'load') out, err = capsys.readouterr() # The default file 'command.txt' doesn't exist, so we should get an error message expected = normalize("""ERROR: Problem accessing script from command.txt: [Errno 2] No such file or directory: 'command.txt.txt' To enable full traceback, run the following command: 'set debug true' """) assert normalize(str(err)) == expected
def test_optparser(_cmdline_app, capsys): run_cmd(_cmdline_app, 'say -h') out, err = capsys.readouterr() expected = normalize(""" Repeats what you tell me to. Usage: speak [options] (text to say) Options: -h, --help show this help message and exit -p, --piglatin atinLay -s, --shout N00B EMULATION MODE -r REPEAT, --repeat=REPEAT output [n] times""") # NOTE: For some reason this extra cast to str is required for Python 2.7 but not 3.x assert normalize(str(out)) == expected
def test_optparser_nosuchoption(_cmdline_app, capsys): run_cmd(_cmdline_app, 'say -a') out, err = capsys.readouterr() expected = normalize(""" no such option: -a Repeats what you tell me to. Usage: speak [options] (text to say) Options: -h, --help show this help message and exit -p, --piglatin atinLay -s, --shout N00B EMULATION MODE -r REPEAT, --repeat=REPEAT output [n] times""") assert normalize(str(out)) == expected
def test_exclude_from_history(abbrev_app, monkeypatch): # Run all variants of run run_cmd(abbrev_app, 'run') run_cmd(abbrev_app, 'ru') run_cmd(abbrev_app, 'r') # Mock out the os.system call so we don't actually open an editor m = mock.MagicMock(name='system') monkeypatch.setattr("os.system", m) # Run all variants of edit run_cmd(abbrev_app, 'edit') run_cmd(abbrev_app, 'edi') run_cmd(abbrev_app, 'ed') # Run all variants of history run_cmd(abbrev_app, 'history') run_cmd(abbrev_app, 'histor') run_cmd(abbrev_app, 'histo') run_cmd(abbrev_app, 'hist') run_cmd(abbrev_app, 'his') run_cmd(abbrev_app, 'hi') # Verify that the history is empty out = run_cmd(abbrev_app, 'history') assert out == [] # Now run a command which isn't excluded from the history run_cmd(abbrev_app, 'help') # And verify we have a history now ... out = run_cmd(abbrev_app, 'history') expected = normalize("""-------------------------[1] help""") assert out == expected
def test_optarser_correct_args_with_quotes_and_midline_options(_cmdline_app): out = run_cmd( _cmdline_app, "speak 'This is a' -s test of the emergency broadcast system!") expected = normalize( """THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM!""") assert out == expected
def test_base_invalid_option(base_app, capsys): run_cmd(base_app, 'show -z') out, err = capsys.readouterr() show_help = run_cmd(base_app, 'help show') expected = ['no such option: -z'] expected.extend(show_help) # 'show -h' is the same as 'help show', other than whitespace differences of an extra newline present in 'help show' assert normalize(str(out)) == expected
def test_history_with_integer_argument(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') out = run_cmd(base_app, 'history 1') expected = normalize(""" -------------------------[1] help """) assert out == expected
def test_history_script_format(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') out = run_cmd(base_app, 'history -s') expected = normalize(""" help shortcuts """) assert out == expected
def test_base_list(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') out = run_cmd(base_app, 'list') expected = normalize(""" -------------------------[2] shortcuts """) assert out == expected
def test_history_with_span_index_error(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'help history') run_cmd(base_app, '!ls -hal :') out = run_cmd(base_app, 'history "hal :"') expected = normalize(""" -------------------------[3] !ls -hal : """) assert out == expected
def test_edit_no_editor(base_app, capsys): # Purposely set the editor to None base_app.editor = None # Make sure we get an exception, but cmd2 handles it run_cmd(base_app, 'edit') out, err = capsys.readouterr() expected = _expected_no_editor_error() assert normalize(str(err)) == expected
def test_base_set(base_app): out = run_cmd(base_app, 'set quiet True') expected = normalize(""" quiet - was: False now: True """) assert out == expected out = run_cmd(base_app, 'show quiet') assert out == ['quiet: True']
def test_input_redirection(base_app, request): test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'redirect.txt') # NOTE: File 'redirect.txt" contains 1 word "history" # Verify that redirecting input from a file works out = run_cmd(base_app, 'help < {}'.format(filename)) expected = normalize(HELP_HISTORY) assert out == expected
def test_base_timing(base_app, capsys): out = run_cmd(base_app, 'set timing True') expected = normalize("""timing - was: False now: True """) assert out == expected out, err = capsys.readouterr() if sys.platform == 'win32': assert out.startswith('Elapsed: 0:00:00') else: assert out.startswith('Elapsed: 0:00:00.0')
def _expected_no_editor_error(): expected_exception = 'OSError' if six.PY2: expected_exception = 'EnvironmentError' expected_text = normalize(""" EXCEPTION of type '{}' occured with message: 'Please use 'set editor' to specify your text editing program of choice.' To enable full traceback, run the following command: 'set debug true' """.format(expected_exception)) return expected_text
def test_output_redirection(base_app): # TODO: Use a temporary directory/file for this file filename = 'out.txt' # Verify that writing to a file works run_cmd(base_app, 'help > {}'.format(filename)) expected = normalize(BASE_HELP) with open(filename) as f: content = normalize(f.read()) assert content == expected # Verify that appending to a file also works run_cmd(base_app, 'help history >> {}'.format(filename)) expected = normalize(BASE_HELP + '\n' + HELP_HISTORY) with open(filename) as f: content = normalize(f.read()) assert content == expected # Delete file that was created os.remove(filename)
def test_list_with_integer_span(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') run_cmd(base_app, 'help list') out = run_cmd(base_app, 'list 1..2') expected = normalize(""" -------------------------[1] help -------------------------[2] shortcuts """) assert out == expected
def test_history_with_string_argument(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') run_cmd(base_app, 'help history') out = run_cmd(base_app, 'history help') expected = normalize(""" -------------------------[1] help -------------------------[3] help history """) assert out == expected
def _expected_no_editor_error(): expected_exception = 'OSError' # If using Python 2 or PyPy (either 2 or 3), expect a different exception than with Python 3 if six.PY2 or hasattr(sys, "pypy_translation_info"): expected_exception = 'EnvironmentError' expected_text = normalize(""" EXCEPTION of type '{}' occurred with message: 'Please use 'set editor' to specify your text editing program of choice.' To enable full traceback, run the following command: 'set debug true' """.format(expected_exception)) return expected_text
def test_history_with_span_start(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') run_cmd(base_app, 'help history') out = run_cmd(base_app, 'history 2:') expected = normalize(""" -------------------------[2] shortcuts -------------------------[3] help history """) assert out == expected
def test_pipe_to_shell(base_app): if sys.platform == "win32": # Windows # Get help menu and pipe it's output to the sort shell command out = run_cmd(base_app, 'help | sort') expected = ['', '', '_relative_load edit history py quit save shell show', '========================================', 'cmdenvironment help load pyscript run set shortcuts', 'Documented commands (type help <topic>):'] assert out == expected else: # Mac and Linux # Get help on help and pipe it's output to the input of the word count shell command out = run_cmd(base_app, 'help help | wc') # Mac and Linux wc behave the same when piped from shell, but differently when piped stdin from file directly if sys.platform == 'darwin': expected = normalize("1 11 70") else: expected = normalize("1 11 70") assert out[0].strip() == expected[0].strip()
def test_history_with_span_end(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') run_cmd(base_app, 'help history') out = run_cmd(base_app, 'history :2') expected = normalize(""" -------------------------[1] help -------------------------[2] shortcuts """) assert out == expected
def test_list_with_string_argument(base_app): run_cmd(base_app, 'help') run_cmd(base_app, 'shortcuts') run_cmd(base_app, 'help list') out = run_cmd(base_app, 'list help') expected = normalize(""" -------------------------[1] help -------------------------[3] help list """) assert out == expected