mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +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
|
lazyjson
|
||||||
teepty
|
teepty
|
||||||
openpy
|
openpy
|
||||||
|
foriegn_shells
|
||||||
main
|
main
|
||||||
pyghooks
|
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()
|
@lru_cache()
|
||||||
def foreign_shell_data(shell, interactive=True, login=False, envcmd='env',
|
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
|
"""Extracts data from a foreign (non-xonsh) shells. Currently this gets
|
||||||
the environment and aliases, but may be extended in the future.
|
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.
|
The command to generate environment output with.
|
||||||
aliascmd : str, optional
|
aliascmd : str, optional
|
||||||
The command to generate alais output with.
|
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.
|
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.
|
Manual override for the current environment.
|
||||||
|
safe : bool, optional
|
||||||
|
Flag for whether or not to safely handle exceptions and other errors.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
|
@ -54,19 +57,23 @@ def foreign_shell_data(shell, interactive=True, login=False, envcmd='env',
|
||||||
Dictionary of shell's alaiases.
|
Dictionary of shell's alaiases.
|
||||||
"""
|
"""
|
||||||
cmd = [shell]
|
cmd = [shell]
|
||||||
|
cmd.extend(extra_args) # needs to come here for GNU long options
|
||||||
if interactive:
|
if interactive:
|
||||||
cmd.append('-i')
|
cmd.append('-i')
|
||||||
if login:
|
if login:
|
||||||
cmd.append('-l')
|
cmd.append('-l')
|
||||||
cmd.extend(extra_args)
|
|
||||||
cmd.append('-c')
|
cmd.append('-c')
|
||||||
cmd.append(COMMAND.format(envcmd=envcmd, aliascmd=aliascmd))
|
cmd.append(COMMAND.format(envcmd=envcmd, aliascmd=aliascmd))
|
||||||
if currenv is None and hasattr(builtins, '__xonsh_env__'):
|
if currenv is None and hasattr(builtins, '__xonsh_env__'):
|
||||||
currenv = builtins.__xonsh_env__.detype()
|
currenv = builtins.__xonsh_env__.detype()
|
||||||
|
elif currenv is not None:
|
||||||
|
currenv = dict(currenv)
|
||||||
try:
|
try:
|
||||||
s = subprocess.check_output(cmd,stderr=subprocess.PIPE, env=currenv,
|
s = subprocess.check_output(cmd,stderr=subprocess.PIPE, env=currenv,
|
||||||
universal_newlines=True)
|
universal_newlines=True)
|
||||||
except (subprocess.CalledProcessError, FileNotFoundError):
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
||||||
|
if not safe:
|
||||||
|
raise
|
||||||
s = FAILED_COMMAND_STDOUT
|
s = FAILED_COMMAND_STDOUT
|
||||||
env = parse_env(s)
|
env = parse_env(s)
|
||||||
aliases = parse_aliases(s)
|
aliases = parse_aliases(s)
|
||||||
|
|
Loading…
Add table
Reference in a new issue