def replay(self, merge_envs=DEFAULT_MERGE_ENVS, target=None): """Replays the history specified, returns the history object where the code was executed. Parameters ---------- merge_env : tuple of str or Mappings, optional Describes how to merge the environments, in order of increasing precednce. Available strings are 'replay' and 'native'. The 'replay' env comes from the history file that we are replaying. The 'native' env comes from what this instance of xonsh was started up with. Instead of a string, a dict or other mapping may be passed in as well. Defaults to ('replay', 'native'). target : str, optional Path to new history file. """ shell = builtins.__xonsh_shell__ re_env = self._lj['env'].load() new_env = self._merge_envs(merge_envs, re_env) new_hist = History(env=new_env.detype(), locked=True, ts=[time.time(), None], gc=False, filename=target) with swap(builtins, '__xonsh_env__', new_env), \ swap(builtins, '__xonsh_history__', new_hist): for cmd in self._lj['cmds']: inp = cmd['inp'] shell.default(inp) if builtins.__xonsh_exit__: # prevent premature exit builtins.__xonsh_exit__ = False new_hist.flush(at_exit=True) return new_hist
def test_hist_flush(): FNAME = 'xonsh-SESSIONID.json' FNAME += '.flush' hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS) hf = hist.flush() yield assert_true, hf is None hist.append({'joco': 'still alive'}) hf = hist.flush() yield assert_true, hf is not None while hf.is_alive(): pass with LazyJSON(FNAME) as lj: obs = lj['cmds'][0]['joco'] yield assert_equal, 'still alive', obs os.remove(FNAME)
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 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)
def test_hist_flush(xonsh_builtins): """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() assert hf is None xonsh_builtins.__xonsh_env__['HISTCONTROL'] = set() hist.append({'joco': 'still alive'}) hf = hist.flush() assert hf is not None while hf.is_alive(): pass with LazyJSON(FNAME) as lj: obs = lj['cmds'][0]['joco'] assert 'still alive' == obs os.remove(FNAME)
def test_cmd_field(): FNAME = 'xonsh-SESSIONID.json' FNAME += '.cmdfield' hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS) # in-memory hf = hist.append({'rtn': 1}) yield assert_true, hf is None 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_true, hf is not None yield assert_equal, 1, hist.rtns[0] yield assert_equal, 1, hist.rtns[-1] yield assert_equal, None, hist.outs[-1] os.remove(FNAME)
def test_cmd_field(xonsh_builtins): """Test basic history behavior.""" FNAME = 'xonsh-SESSIONID.json' FNAME += '.cmdfield' hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS) # in-memory xonsh_builtins.__xonsh_env__['HISTCONTROL'] = set() hf = hist.append({'rtn': 1}) assert hf is None assert 1 == hist.rtns[0] assert 1 == hist.rtns[-1] assert None == hist.outs[-1] # slice assert [1] == hist.rtns[:] # on disk hf = hist.flush() assert hf is not None assert 1 == hist.rtns[0] assert 1 == hist.rtns[-1] assert None == hist.outs[-1] os.remove(FNAME)
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)