mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 00:14:41 +01:00
added tests for foreign shells
This commit is contained in:
parent
62624e43a3
commit
1e24bbb2ed
5 changed files with 83 additions and 4 deletions
10
docs/api/foreign_shells.rst
Normal file
10
docs/api/foreign_shells.rst
Normal file
|
@ -0,0 +1,10 @@
|
|||
.. _xonsh_foreign_shells:
|
||||
|
||||
******************************************************
|
||||
Foreign Shell Tools (``xonsh.foreign_shells``)
|
||||
******************************************************
|
||||
|
||||
.. automodule:: xonsh.foreign_shells
|
||||
:members:
|
||||
:undoc-members:
|
||||
:inherited-members:
|
|
@ -51,5 +51,6 @@ For those of you who want the gritty details.
|
|||
lazyjson
|
||||
teepty
|
||||
openpy
|
||||
foriegn_shells
|
||||
main
|
||||
pyghooks
|
||||
|
|
5
tests/bashrc.sh
Normal file
5
tests/bashrc.sh
Normal file
|
@ -0,0 +1,5 @@
|
|||
export EMERALD="SWORD"
|
||||
alias ll='ls -a -lF'
|
||||
alias la='ls -A'
|
||||
export MIGHTY=WARRIOR
|
||||
alias l='ls -CF'
|
56
tests/test_foreign_shells.py
Normal file
56
tests/test_foreign_shells.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
"""Tests foreign shells."""
|
||||
from __future__ import unicode_literals, print_function
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
import nose
|
||||
from nose.plugins.skip import SkipTest
|
||||
from nose.tools import assert_equal, assert_true, assert_false
|
||||
|
||||
from xonsh.foreign_shells import foreign_shell_data, parse_env, parse_aliases
|
||||
|
||||
def test_parse_env():
|
||||
exp = {'X': 'YES', 'Y': 'NO'}
|
||||
s = ('some garbage\n'
|
||||
'__XONSH_ENV_BEG__\n'
|
||||
'Y=NO\n'
|
||||
'X=YES\n'
|
||||
'__XONSH_ENV_END__\n'
|
||||
'more filth')
|
||||
obs = parse_env(s)
|
||||
assert_equal(exp, obs)
|
||||
|
||||
|
||||
def test_parse_aliases():
|
||||
exp = {'x': ['yes', '-1'], 'y': ['echo', 'no']}
|
||||
s = ('some garbage\n'
|
||||
'__XONSH_ALIAS_BEG__\n'
|
||||
"alias x='yes -1'\n"
|
||||
"alias y='echo no'\n"
|
||||
'__XONSH_ALIAS_END__\n'
|
||||
'more filth')
|
||||
obs = parse_aliases(s)
|
||||
assert_equal(exp, obs)
|
||||
|
||||
|
||||
def test_foreign_bash_data():
|
||||
expenv = {"EMERALD": "SWORD", 'MIGHTY': 'WARRIOR'}
|
||||
expaliases = {
|
||||
'l': ['ls', '-CF'],
|
||||
'la': ['ls', '-A'],
|
||||
'll': ['ls', '-a', '-lF'],
|
||||
}
|
||||
rcfile = os.path.join(os.path.dirname(__file__), 'bashrc.sh')
|
||||
try:
|
||||
obsenv, obsaliases = foreign_shell_data('bash', currenv=(),
|
||||
extra_args=('--rcfile', rcfile),
|
||||
safe=False)
|
||||
except (subprocess.CalledProcessError, FileNotFoundError):
|
||||
raise SkipTest
|
||||
for key, expval in expenv.items():
|
||||
yield assert_equal, expval, obsenv.get(key, False)
|
||||
yield assert_equal, expaliases, obsaliases
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
nose.runmodule()
|
|
@ -25,7 +25,8 @@ __XONSH_ALIAS_END__
|
|||
|
||||
@lru_cache()
|
||||
def foreign_shell_data(shell, interactive=True, login=False, envcmd='env',
|
||||
aliascmd='alias', extra_args=(), currenv=None):
|
||||
aliascmd='alias', extra_args=(), currenv=None,
|
||||
safe=True):
|
||||
"""Extracts data from a foreign (non-xonsh) shells. Currently this gets
|
||||
the environment and aliases, but may be extended in the future.
|
||||
|
||||
|
@ -41,10 +42,12 @@ def foreign_shell_data(shell, interactive=True, login=False, envcmd='env',
|
|||
The command to generate environment output with.
|
||||
aliascmd : str, optional
|
||||
The command to generate alais output with.
|
||||
extra_args : list of str, optional
|
||||
extra_args : tuple of str, optional
|
||||
Addtional command line options to pass into the shell.
|
||||
currenv : dict or None, optional
|
||||
currenv : tuple of items or None, optional
|
||||
Manual override for the current environment.
|
||||
safe : bool, optional
|
||||
Flag for whether or not to safely handle exceptions and other errors.
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
@ -54,19 +57,23 @@ def foreign_shell_data(shell, interactive=True, login=False, envcmd='env',
|
|||
Dictionary of shell's alaiases.
|
||||
"""
|
||||
cmd = [shell]
|
||||
cmd.extend(extra_args) # needs to come here for GNU long options
|
||||
if interactive:
|
||||
cmd.append('-i')
|
||||
if login:
|
||||
cmd.append('-l')
|
||||
cmd.extend(extra_args)
|
||||
cmd.append('-c')
|
||||
cmd.append(COMMAND.format(envcmd=envcmd, aliascmd=aliascmd))
|
||||
if currenv is None and hasattr(builtins, '__xonsh_env__'):
|
||||
currenv = builtins.__xonsh_env__.detype()
|
||||
elif currenv is not None:
|
||||
currenv = dict(currenv)
|
||||
try:
|
||||
s = subprocess.check_output(cmd,stderr=subprocess.PIPE, env=currenv,
|
||||
universal_newlines=True)
|
||||
except (subprocess.CalledProcessError, FileNotFoundError):
|
||||
if not safe:
|
||||
raise
|
||||
s = FAILED_COMMAND_STDOUT
|
||||
env = parse_env(s)
|
||||
aliases = parse_aliases(s)
|
||||
|
|
Loading…
Add table
Reference in a new issue