xonsh/tests/test_foreign_shells.py

113 lines
2.9 KiB
Python
Raw Normal View History

2015-10-10 19:52:53 -04:00
"""Tests foreign shells."""
2015-10-10 19:52:53 -04:00
import os
import subprocess
import pytest # noqa F401
2015-10-10 19:52:53 -04:00
2022-01-31 21:26:34 +05:30
from xonsh.foreign_shells import foreign_shell_data, parse_aliases, parse_env
2022-03-24 00:46:50 +05:30
from xonsh.pytest.tools import skip_if_on_unix, skip_if_on_windows
2015-10-10 19:52:53 -04:00
2016-07-01 13:35:16 +03:00
2015-10-10 19:52:53 -04:00
def test_parse_env():
2018-08-30 09:18:49 -05:00
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"
)
2015-10-10 19:52:53 -04:00
obs = parse_env(s)
2018-08-30 09:18:49 -05:00
assert exp == obs
2015-10-10 19:52:53 -04:00
2016-06-22 14:15:35 -04:00
def test_parse_env_newline():
2018-08-30 09:18:49 -05:00
exp = {"X": "YES", "Y": "NO", "PROMPT": "why\nme "}
s = (
"some garbage\n"
"__XONSH_ENV_BEG__\n"
"Y=NO\n"
"PROMPT=why\nme \n"
"X=YES\n"
"__XONSH_ENV_END__\n"
"more filth"
)
2016-06-22 14:15:35 -04:00
obs = parse_env(s)
2016-06-25 12:34:55 -04:00
assert exp == obs
2016-06-22 14:15:35 -04:00
def test_parse_env_equals():
2018-08-30 09:18:49 -05:00
exp = {"X": "YES", "Y": "NO", "LS_COLORS": "*.tar=5"}
s = (
"some garbage\n"
"__XONSH_ENV_BEG__\n"
"Y=NO\n"
"LS_COLORS=*.tar=5\n"
"X=YES\n"
"__XONSH_ENV_END__\n"
"more filth"
)
2016-06-22 14:15:35 -04:00
obs = parse_env(s)
2016-06-25 12:34:55 -04:00
assert exp == obs
2016-06-22 14:15:35 -04:00
2015-10-10 19:52:53 -04:00
def test_parse_aliases():
exp = {
"x": ["yes", "-1"],
"y": ["echo", "no"],
"z": ["echo", "True", "&&", "echo", "Next", "||", "echo", "False"],
}
2018-08-30 09:18:49 -05:00
s = (
"some garbage\n"
"__XONSH_ALIAS_BEG__\n"
"alias x='yes -1'\n"
"alias y='echo no'\n"
"alias z='echo True && \\\n echo Next || \\\n echo False'\n" # noqa: E261,W605
2018-08-30 09:18:49 -05:00
"__XONSH_ALIAS_END__\n"
"more filth"
)
2019-02-13 18:49:39 -05:00
obs = parse_aliases(s, "bash")
2018-08-30 09:18:49 -05:00
assert exp == obs
2015-10-10 19:52:53 -04:00
2016-07-01 13:35:16 +03:00
@skip_if_on_windows
2015-10-10 19:52:53 -04:00
def test_foreign_bash_data():
2018-08-30 09:18:49 -05:00
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")
2015-10-10 19:52:53 -04:00
try:
2018-08-30 09:18:49 -05:00
obsenv, obsaliases = foreign_shell_data(
"bash", currenv=(), extra_args=("--rcfile", rcfile), safe=False
)
2015-10-10 19:52:53 -04:00
except (subprocess.CalledProcessError, FileNotFoundError):
2016-06-22 22:57:50 +03:00
return
2015-10-10 19:52:53 -04:00
for key, expval in expenv.items():
2016-06-22 22:57:50 +03:00
assert expval == obsenv.get(key, False)
2015-11-29 17:27:33 -07:00
for key, expval in expaliases.items():
2016-06-22 22:57:50 +03:00
assert expval == obsaliases.get(key, False)
2015-10-10 19:52:53 -04:00
2016-07-01 13:35:16 +03:00
@skip_if_on_unix
def test_foreign_cmd_data():
2018-08-30 09:18:49 -05:00
env = (("ENV_TO_BE_REMOVED", "test"),)
batchfile = os.path.join(os.path.dirname(__file__), "batch.bat")
source_cmd = f'call "{batchfile}"\necho off'
try:
2018-08-30 09:18:49 -05:00
obsenv, _ = foreign_shell_data(
"cmd",
prevcmd=source_cmd,
currenv=env,
interactive=False,
sourcer="call",
envcmd="set",
use_tmpfile=True,
safe=False,
)
except (subprocess.CalledProcessError, FileNotFoundError):
2016-06-22 22:57:50 +03:00
return
2018-08-30 09:18:49 -05:00
assert "ENV_TO_BE_ADDED" in obsenv
assert obsenv["ENV_TO_BE_ADDED"] == "Hallo world"
assert "ENV_TO_BE_REMOVED" not in obsenv