2015-11-16 14:04:32 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-10-10 19:52:53 -04:00
|
|
|
"""Tests foreign shells."""
|
|
|
|
from __future__ import unicode_literals, print_function
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2020-05-05 06:42:28 -04:00
|
|
|
import pytest # noqa F401
|
2016-07-01 13:35:16 +03:00
|
|
|
from tools import skip_if_on_windows, skip_if_on_unix
|
2015-10-10 19:52:53 -04:00
|
|
|
|
|
|
|
from xonsh.foreign_shells import foreign_shell_data, parse_env, parse_aliases
|
|
|
|
|
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():
|
2018-08-30 09:18:49 -05:00
|
|
|
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"
|
|
|
|
)
|
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
|
2016-04-18 00:41:11 +02:00
|
|
|
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 = 'call "{}"\necho off'.format(batchfile)
|
2016-04-18 00:41:11 +02:00
|
|
|
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,
|
|
|
|
)
|
2016-04-18 00:41:11 +02:00
|
|
|
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
|