Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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)
Пример #5
0
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]
Пример #6
0
def cmd2_app():
    app = TabCompleteExample()
    app.stdout = StdSim(app.stdout)
    return app
Пример #7
0
def ac_app():
    app = AutoCompleteTester()
    app.stdout = StdSim(app.stdout)
    return app
Пример #8
0
def ps_echo():
    c = PyscriptCustomNameExample()
    c.stdout = StdSim(c.stdout)
    return c
Пример #9
0
def ps_app():
    c = PyscriptExample()
    c.stdout = StdSim(c.stdout)
    return c
Пример #10
0
def subcommand_app():
    app = SubcommandApp()
    app.stdout = StdSim(app.stdout)
    return app
Пример #11
0
def argparse_app():
    app = ArgparseApp()
    app.stdout = StdSim(app.stdout)
    return app
Пример #12
0
def secondlevel_app():
    app = SecondLevel()
    app.stdout = StdSim(app.stdout)
    return app
Пример #13
0
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
Пример #14
0
def base_app():
    c = cmd2.Cmd()
    c.stdout = StdSim(c.stdout)
    return c
Пример #15
0
def ac_app():
    app = ArgparseCompleterTester()
    app.stdout = StdSim(app.stdout)
    return app