Esempio n. 1
0
def test_histcontrol():
    """Test HISTCONTROL=ignoredups,ignoreerr"""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.append'
    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)

    with mock_xonsh_env({'HISTCONTROL': 'ignoredups,ignoreerr'}):
        yield assert_equal, len(hist.buffer), 0

        # An error, buffer remains empty
        hist.append({'inp': 'ls foo', 'rtn': 2})
        yield assert_equal, len(hist.buffer), 0

        # Success
        hist.append({'inp': 'ls foobazz', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 1
        yield assert_equal, 'ls foobazz', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Error
        hist.append({'inp': 'ls foo', 'rtn': 2})
        yield assert_equal, len(hist.buffer), 1
        yield assert_equal, 'ls foobazz', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # File now exists, success
        hist.append({'inp': 'ls foo', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 2
        yield assert_equal, 'ls foo', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Success
        hist.append({'inp': 'ls', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 3
        yield assert_equal, 'ls', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Dup
        hist.append({'inp': 'ls', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 3

        # Success
        hist.append({'inp': '/bin/ls', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 4
        yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Error
        hist.append({'inp': 'ls bazz', 'rtn': 1})
        yield assert_equal, len(hist.buffer), 4
        yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Error
        hist.append({'inp': 'ls bazz', 'rtn': -1})
        yield assert_equal, len(hist.buffer), 4
        yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

    os.remove(FNAME)
Esempio n. 2
0
def test_histcontrol():
    """Test HISTCONTROL=ignoredups,ignoreerr"""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.append'
    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)

    with mock_xonsh_env({'HISTCONTROL': 'ignoredups,ignoreerr'}):
        yield assert_equal, len(hist.buffer), 0

        # An error, buffer remains empty
        hist.append({'inp': 'ls foo', 'rtn': 2})
        yield assert_equal, len(hist.buffer), 0

        # Success
        hist.append({'inp': 'ls foobazz', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 1
        yield assert_equal, 'ls foobazz', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Error
        hist.append({'inp': 'ls foo', 'rtn': 2})
        yield assert_equal, len(hist.buffer), 1
        yield assert_equal, 'ls foobazz', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # File now exists, success
        hist.append({'inp': 'ls foo', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 2
        yield assert_equal, 'ls foo', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Success
        hist.append({'inp': 'ls', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 3
        yield assert_equal, 'ls', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Dup
        hist.append({'inp': 'ls', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 3

        # Success
        hist.append({'inp': '/bin/ls', 'rtn': 0})
        yield assert_equal, len(hist.buffer), 4
        yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Error
        hist.append({'inp': 'ls bazz', 'rtn': 1})
        yield assert_equal, len(hist.buffer), 4
        yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

        # Error
        hist.append({'inp': 'ls bazz', 'rtn': -1})
        yield assert_equal, len(hist.buffer), 4
        yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
        yield assert_equal, 0, hist.buffer[-1]['rtn']

    os.remove(FNAME)
Esempio n. 3
0
def check_load_static_config(s, exp, loaded):
    env = {'XONSH_SHOW_TRACEBACK': False}
    with tempfile.NamedTemporaryFile() as f, mock_xonsh_env(env):
        f.write(s)
        f.flush()
        conf = load_static_config(env, f.name)
    assert_equal(exp, conf)
    assert_equal(env['LOADED_CONFIG'], loaded)
Esempio n. 4
0
def test_man_completion():
    if ON_WINDOWS:
        raise SkipTest
    with mock_xonsh_env({}):
        man_completer = ManCompleter()
        completions = man_completer.option_complete('--', 'yes')
    assert_true('--version' in completions)
    assert_true('--help' in completions)
Esempio n. 5
0
def test_hist_append():
    """Verify appending to the history works."""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.append'
    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
    with mock_xonsh_env({'HISTCONTROL': set()}):
        hf = hist.append({'joco': 'still alive'})
    yield assert_is_none, hf
    yield assert_equal, 'still alive', hist.buffer[0]['joco']
    os.remove(FNAME)
Esempio n. 6
0
def test_hist_append():
    """Verify appending to the history works."""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.append'
    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
    with mock_xonsh_env({'HISTCONTROL': set()}):
        hf = hist.append({'joco': 'still alive'})
    yield assert_is_none, hf
    yield assert_equal, 'still alive', hist.buffer[0]['joco']
    os.remove(FNAME)
Esempio n. 7
0
def test_hist_flush():
    """Verify explicit flushing of the history works."""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.flush'
    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
    hf = hist.flush()
    yield assert_is_none, hf
    with mock_xonsh_env({'HISTCONTROL': set()}):
        hist.append({'joco': 'still alive'})
    hf = hist.flush()
    yield assert_is_not_none, hf
    while hf.is_alive():
        pass
    with LazyJSON(FNAME) as lj:
        obs = lj['cmds'][0]['joco']
    yield assert_equal, 'still alive', obs
    os.remove(FNAME)
Esempio n. 8
0
def test_hist_flush():
    """Verify explicit flushing of the history works."""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.flush'
    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
    hf = hist.flush()
    yield assert_is_none, hf
    with mock_xonsh_env({'HISTCONTROL': set()}):
        hist.append({'joco': 'still alive'})
    hf = hist.flush()
    yield assert_is_not_none, hf
    while hf.is_alive():
        pass
    with LazyJSON(FNAME) as lj:
        obs = lj['cmds'][0]['joco']
    yield assert_equal, 'still alive', obs
    os.remove(FNAME)
Esempio n. 9
0
def test_cmd_field():
    """Test basic history behavior."""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.cmdfield'
    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
    # in-memory
    with mock_xonsh_env({'HISTCONTROL': set()}):
        hf = hist.append({'rtn': 1})
    yield assert_is_none, hf
    yield assert_equal, 1, hist.rtns[0]
    yield assert_equal, 1, hist.rtns[-1]
    yield assert_equal, None, hist.outs[-1]
    # slice
    yield assert_equal, [1], hist.rtns[:]
    # on disk
    hf = hist.flush()
    yield assert_is_not_none, hf
    yield assert_equal, 1, hist.rtns[0]
    yield assert_equal, 1, hist.rtns[-1]
    yield assert_equal, None, hist.outs[-1]
    os.remove(FNAME)
Esempio n. 10
0
def test_cmd_field():
    """Test basic history behavior."""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.cmdfield'
    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
    # in-memory
    with mock_xonsh_env({'HISTCONTROL': set()}):
        hf = hist.append({'rtn': 1})
    yield assert_is_none, hf
    yield assert_equal, 1, hist.rtns[0]
    yield assert_equal, 1, hist.rtns[-1]
    yield assert_equal, None, hist.outs[-1]
    # slice
    yield assert_equal, [1], hist.rtns[:]
    # on disk
    hf = hist.flush()
    yield assert_is_not_none, hf
    yield assert_equal, 1, hist.rtns[0]
    yield assert_equal, 1, hist.rtns[-1]
    yield assert_equal, None, hist.outs[-1]
    os.remove(FNAME)
Esempio n. 11
0
def test_sub_import():
    with mock_xonsh_env({}):
        from xpack.sub import sample
        assert_equal('hello mom jawaka\n', sample.x)
Esempio n. 12
0
def test_import():
    with mock_xonsh_env({}):
        import sample
        assert_equal('hello mom jawaka\n', sample.x)
Esempio n. 13
0
def test_relative_import():
    with mock_xonsh_env({}):
        from xpack import relimp
        assert_equal('hello mom jawaka\n', relimp.sample.x)
        assert_equal('hello mom jawaka\ndark chest of wonders', relimp.y)
Esempio n. 14
0
def test_sub_import():
    with mock_xonsh_env({}):
        from xpack.sub import sample
        assert_equal('hello mom jawaka\n', sample.x)
Esempio n. 15
0
def test_relative_import():
    with mock_xonsh_env({}):
        from xpack import relimp
        assert_equal('hello mom jawaka\n', relimp.sample.x)
        assert_equal('hello mom jawaka\ndark chest of wonders', relimp.y)
Esempio n. 16
0
def test_import():
    with mock_xonsh_env({}):
        import sample
        assert_equal('hello mom jawaka\n', sample.x)
Esempio n. 17
0
def test_show_cmd():
    """Verify that CLI history commands work."""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.show_cmd'
    cmds = ['ls', 'cat hello kitty', 'abc', 'def', 'touch me', 'grep from me']

    def format_hist_line(idx, cmd):
        """Construct a history output line."""
        return ' {:d}  {:s}\n'.format(idx, cmd)

    def run_show_cmd(hist_args, commands, base_idx=0, step=1):
        """Run and evaluate the output of the given show command."""
        stdout.seek(0, io.SEEK_SET)
        stdout.truncate()
        history._main(hist, hist_args)
        stdout.seek(0, io.SEEK_SET)
        hist_lines = stdout.readlines()
        yield assert_equal, len(commands), len(hist_lines)
        for idx, (cmd, actual) in enumerate(zip(commands, hist_lines)):
            expected = format_hist_line(base_idx + idx * step, cmd)
            yield assert_equal, expected, actual

    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
    stdout = io.StringIO()
    saved_stdout = sys.stdout
    sys.stdout = stdout

    with mock_xonsh_env({'HISTCONTROL': set()}):
        for cmd in cmds:  # populate the shell history
            hist.append({'inp': cmd, 'rtn': 0})

        # Verify an implicit "show" emits the entire history.
        for x in run_show_cmd([], cmds):
            yield x

        # Verify an explicit "show" with no qualifiers emits the entire history.
        for x in run_show_cmd(['show'], cmds):
            yield x

        # Verify an explicit "show" with a reversed qualifier emits the entire
        # history in reverse order.
        for x in run_show_cmd(['show', '-r'], list(reversed(cmds)),
                              len(cmds) - 1, -1):
            yield x

        # Verify that showing a specific history entry relative to the start of the
        # history works.
        for x in run_show_cmd(['show', '0'], [cmds[0]], 0):
            yield x
        for x in run_show_cmd(['show', '1'], [cmds[1]], 1):
            yield x

        # Verify that showing a specific history entry relative to the end of the
        # history works.
        for x in run_show_cmd(['show', '-2'], [cmds[-2]], len(cmds) - 2):
            yield x

        # Verify that showing a history range relative to the start of the
        # history works.
        for x in run_show_cmd(['show', '0:2'], cmds[0:2], 0):
            yield x
        for x in run_show_cmd(['show', '1::2'], cmds[1::2], 1, 2):
            yield x

        # Verify that showing a history range relative to the end of the
        # history works.
        for x in run_show_cmd(['show', '-2:'], cmds[-2:], len(cmds) - 2):
            yield x
        for x in run_show_cmd(['show', '-4:-2'], cmds[-4:-2], len(cmds) - 4):
            yield x

    sys.stdout = saved_stdout
    os.remove(FNAME)
Esempio n. 18
0
def test_show_cmd():
    """Verify that CLI history commands work."""
    FNAME = 'xonsh-SESSIONID.json'
    FNAME += '.show_cmd'
    cmds = ['ls', 'cat hello kitty', 'abc', 'def', 'touch me', 'grep from me']

    def format_hist_line(idx, cmd):
        """Construct a history output line."""
        return ' {:d}  {:s}\n'.format(idx, cmd)

    def run_show_cmd(hist_args, commands, base_idx=0, step=1):
        """Run and evaluate the output of the given show command."""
        stdout.seek(0, io.SEEK_SET)
        stdout.truncate()
        history._main(hist, hist_args)
        stdout.seek(0, io.SEEK_SET)
        hist_lines = stdout.readlines()
        yield assert_equal, len(commands), len(hist_lines)
        for idx, (cmd, actual) in enumerate(zip(commands, hist_lines)):
            expected = format_hist_line(base_idx + idx * step, cmd)
            yield assert_equal, expected, actual

    hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
    stdout = io.StringIO()
    saved_stdout = sys.stdout
    sys.stdout = stdout

    with mock_xonsh_env({'HISTCONTROL': set()}):
        for cmd in cmds:  # populate the shell history
            hist.append({'inp': cmd, 'rtn': 0})

        # Verify an implicit "show" emits the entire history.
        for x in run_show_cmd([], cmds):
            yield x

        # Verify an explicit "show" with no qualifiers emits the entire history.
        for x in run_show_cmd(['show'], cmds):
            yield x

        # Verify an explicit "show" with a reversed qualifier emits the entire
        # history in reverse order.
        for x in run_show_cmd(['show', '-r'], list(reversed(cmds)),
                              len(cmds) - 1, -1):
            yield x

        # Verify that showing a specific history entry relative to the start of the
        # history works.
        for x in run_show_cmd(['show', '0'], [cmds[0]], 0):
            yield x
        for x in run_show_cmd(['show', '1'], [cmds[1]], 1):
            yield x

        # Verify that showing a specific history entry relative to the end of the
        # history works.
        for x in run_show_cmd(['show', '-2'], [cmds[-2]], len(cmds) - 2):
            yield x

        # Verify that showing a history range relative to the start of the
        # history works.
        for x in run_show_cmd(['show', '0:2'], cmds[0:2], 0):
            yield x
        for x in run_show_cmd(['show', '1::2'], cmds[1::2], 1, 2):
            yield x

        # Verify that showing a history range relative to the end of the
        # history works.
        for x in run_show_cmd(['show', '-2:'], cmds[-2:], len(cmds) - 2):
            yield x
        for x in run_show_cmd(['show', '-4:-2'], cmds[-4:-2], len(cmds) - 4):
            yield x

    sys.stdout = saved_stdout
    os.remove(FNAME)