def test_history_transcript(): app = CmdLineApp() app.stdout = StdSim(app.stdout) run_cmd(app, 'orate this is\na /multiline/\ncommand;\n') run_cmd(app, 'speak /tmp/file.txt is not a regex') expected = r"""(Cmd) orate this is > a /multiline/ > command; this is a \/multiline\/ command (Cmd) speak /tmp/file.txt is not a regex \/tmp\/file.txt is not a regex """ # make a tmp file fd, history_fname = tempfile.mkstemp(prefix='', suffix='.txt') os.close(fd) # tell the history command to create a transcript run_cmd(app, 'history -t "{}"'.format(history_fname)) # read in the transcript created by the history command with open(history_fname) as f: xscript = f.read() assert xscript == expected
def test_history_transcript_bad_filename(): app = CmdLineApp() app.stdout = StdSim(app.stdout) run_cmd(app, 'orate this is\na /multiline/\ncommand;\n') run_cmd(app, 'speak /tmp/file.txt is not a regex') expected = r"""(Cmd) orate this is > a /multiline/ > command; this is a \/multiline\/ command (Cmd) speak /tmp/file.txt is not a regex \/tmp\/file.txt is not a regex """ # make a tmp file history_fname = '~/fakedir/this_does_not_exist.txt' # tell the history command to create a transcript run_cmd(app, 'history -t "{}"'.format(history_fname)) # read in the transcript created by the history command with pytest.raises(FileNotFoundError): with open(history_fname) as f: transcript = f.read() assert transcript == expected
def test_commands_at_invocation(): testargs = ["prog", "say hello", "say Gracie", "quit"] expected = "This is an intro banner ...\nhello\nGracie\n" with mock.patch.object(sys, 'argv', testargs): app = CmdLineApp() app.stdout = StdSim(app.stdout) app.cmdloop() out = app.stdout.getvalue() assert out == expected
def run_cmd(app, cmd): """ Clear out and err StdSim buffers, run the command, and return out and err """ saved_sysout = sys.stdout sys.stdout = app.stdout # This will be used to capture app.stdout and sys.stdout copy_cmd_stdout = StdSim(app.stdout) # This will be used to capture sys.stderr copy_stderr = StdSim(sys.stderr) try: app.stdout = copy_cmd_stdout with redirect_stdout(copy_cmd_stdout): with redirect_stderr(copy_stderr): app.onecmd_plus_hooks(cmd) finally: app.stdout = copy_cmd_stdout.inner_stream sys.stdout = saved_sysout out = copy_cmd_stdout.getvalue() err = copy_stderr.getvalue() print(out) print(err) return normalize(out), normalize(err)
def test_history_transcript_bad_path(mocker): app = CmdLineApp() app.stdout = StdSim(app.stdout) run_cmd(app, 'orate this is\na /multiline/\ncommand;\n') run_cmd(app, 'speak /tmp/file.txt is not a regex') # Bad directory history_fname = '~/fakedir/this_does_not_exist.txt' out, err = run_cmd(app, 'history -t "{}"'.format(history_fname)) assert "is not a directory" in err[0] # Cause os.open to fail and make sure error gets printed mock_remove = mocker.patch('builtins.open') mock_remove.side_effect = OSError history_fname = 'outfile.txt' out, err = run_cmd(app, 'history -t "{}"'.format(history_fname)) assert "Error saving transcript file" in err[0]
def cmd2_app(): app = TabCompleteExample() app.stdout = StdSim(app.stdout) return app
def ac_app(): app = AutoCompleteTester() app.stdout = StdSim(app.stdout) return app
def ps_echo(): c = PyscriptCustomNameExample() c.stdout = StdSim(c.stdout) return c
def ps_app(): c = PyscriptExample() c.stdout = StdSim(c.stdout) return c
def subcommand_app(): app = SubcommandApp() app.stdout = StdSim(app.stdout) return app
def argparse_app(): app = ArgparseApp() app.stdout = StdSim(app.stdout) return app
def secondlevel_app(): app = SecondLevel() app.stdout = StdSim(app.stdout) return app
def submenu_app(): app = SubmenuApp() app.stdout = StdSim(app.stdout) second_level_cmd.stdout = StdSim(app.stdout) second_level_b_cmd.stdout = StdSim(app.stdout) return app
def base_app(): c = cmd2.Cmd() c.stdout = StdSim(c.stdout) return c
def ac_app(): app = ArgparseCompleterTester() app.stdout = StdSim(app.stdout) return app