2015-11-16 14:04:32 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-08-23 16:50:54 -04:00
|
|
|
"""Tests the xonsh replay functionality."""
|
|
|
|
from __future__ import unicode_literals, print_function
|
|
|
|
import os
|
|
|
|
import builtins
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
2016-06-22 23:20:37 +03:00
|
|
|
import pytest
|
2015-08-23 16:50:54 -04:00
|
|
|
|
|
|
|
from xonsh.tools import swap
|
|
|
|
from xonsh.shell import Shell
|
|
|
|
from xonsh.replay import Replayer
|
|
|
|
|
2016-06-28 13:16:12 +03:00
|
|
|
from tools import skip_if_on_darwin
|
2015-10-20 17:53:12 -04:00
|
|
|
|
2016-06-28 13:47:35 +03:00
|
|
|
SHELL = Shell({'PATH': []})
|
|
|
|
HISTDIR = os.path.join(os.path.dirname(__file__), 'histories')
|
2015-08-23 16:50:54 -04:00
|
|
|
|
|
|
|
|
2016-06-28 13:16:12 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def re_file():
|
2016-06-28 13:47:35 +03:00
|
|
|
return 'echo.json'
|
2015-08-23 16:50:54 -04:00
|
|
|
|
2016-06-28 13:16:12 +03:00
|
|
|
|
|
|
|
@pytest.yield_fixture
|
2016-06-28 13:47:35 +03:00
|
|
|
def replay(re_file, xonsh_builtins):
|
|
|
|
xonsh_builtins.__xonsh_env__['__xonsh_shell__'] = SHELL
|
2016-06-28 13:16:12 +03:00
|
|
|
xonsh_builtins.__xonsh_env__['__xonsh_exit__'] = False
|
2016-06-28 13:47:35 +03:00
|
|
|
f = os.path.join(HISTDIR, re_file)
|
2016-06-28 13:16:12 +03:00
|
|
|
r = Replayer(f)
|
2016-06-28 13:47:35 +03:00
|
|
|
print("file:", f)
|
|
|
|
print('replayer:', r)
|
2016-06-28 13:16:12 +03:00
|
|
|
hist = r.replay()
|
2016-06-28 13:47:35 +03:00
|
|
|
print('hist:', hist)
|
2016-06-28 13:16:12 +03:00
|
|
|
yield hist
|
2015-08-23 16:50:54 -04:00
|
|
|
fname = hist.filename
|
|
|
|
del hist
|
|
|
|
if os.path.isfile(fname):
|
|
|
|
os.remove(fname)
|
|
|
|
|
2015-08-23 17:00:26 -04:00
|
|
|
|
2016-06-28 13:16:12 +03:00
|
|
|
@pytest.mark.parametrize('re_file, expected_len', [
|
|
|
|
('echo.json', 2),
|
2016-06-28 13:47:35 +03:00
|
|
|
('simple-python.json', 4),
|
2016-06-28 13:16:12 +03:00
|
|
|
])
|
2016-06-28 13:47:35 +03:00
|
|
|
def test_replay(expected_len, replay, xonsh_builtins):
|
|
|
|
assert len(replay) == expected_len
|
2016-06-28 13:16:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
# def run_replay(re_file):
|
|
|
|
# with swap(builtins, '__xonsh_shell__', SHELL):
|
|
|
|
# with swap(builtins, '__xonsh_exit__', False):
|
|
|
|
# r = Replayer(re_file)
|
|
|
|
# hist = r.replay()
|
|
|
|
# return hist
|
|
|
|
|
|
|
|
|
|
|
|
# def cleanup_replay(hist):
|
|
|
|
# fname = hist.filename
|
|
|
|
# del hist
|
|
|
|
# if os.path.isfile(fname):
|
|
|
|
# os.remove(fname)
|
|
|
|
|
|
|
|
|
|
|
|
# @contextmanager
|
|
|
|
# def a_replay(re_file):
|
|
|
|
# hist = run_replay(re_file)
|
|
|
|
# yield hist
|
|
|
|
# cleanup_replay(hist)
|
|
|
|
|
2015-08-23 16:50:54 -04:00
|
|
|
|
2016-06-28 13:16:12 +03:00
|
|
|
# @skip_if_on_darwin
|
|
|
|
# def test_echo():
|
|
|
|
# f = os.path.join(HISTDIR, 'echo.json')
|
|
|
|
# with a_replay(f) as hist:
|
|
|
|
# assert 2 == len(hist)
|
2015-08-23 16:50:54 -04:00
|
|
|
|
|
|
|
|
2016-06-28 13:16:12 +03:00
|
|
|
# @skip_if_on_darwin
|
|
|
|
# def test_reecho():
|
|
|
|
# f = os.path.join(HISTDIR, 'echo.json')
|
|
|
|
# with a_replay(f) as hist:
|
|
|
|
# assert 2 == len(hist)
|
2015-08-23 17:00:26 -04:00
|
|
|
|
2015-08-23 16:50:54 -04:00
|
|
|
|
2016-06-28 13:16:12 +03:00
|
|
|
# @skip_if_on_darwin
|
|
|
|
# def test_simple_python():
|
|
|
|
# f = os.path.join(HISTDIR, 'simple-python.json')
|
|
|
|
# with a_replay(f) as hist:
|
|
|
|
# assert 4 == len(hist)
|
|
|
|
# assert "print('The Turtles')" == hist.inps[0].strip()
|