mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
Merge pull request #2434 from xonsh/no_rc_for_scripts
Don't load xonshrc is xonsh is running a script
This commit is contained in:
commit
88647b4a8f
3 changed files with 56 additions and 5 deletions
13
news/no_rc_for_scripts.rst
Normal file
13
news/no_rc_for_scripts.rst
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
**Added:** None
|
||||||
|
|
||||||
|
**Changed:**
|
||||||
|
|
||||||
|
* When xonsh is used to run an ``xsh`` script, the ``xonshrc`` is not loaded
|
||||||
|
|
||||||
|
**Deprecated:** None
|
||||||
|
|
||||||
|
**Removed:** None
|
||||||
|
|
||||||
|
**Fixed:** None
|
||||||
|
|
||||||
|
**Security:** None
|
|
@ -8,6 +8,7 @@ import os.path
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import xonsh.main
|
import xonsh.main
|
||||||
|
from xonsh.main import XonshMode
|
||||||
from xonsh.environ import Env
|
from xonsh.environ import Env
|
||||||
import pytest
|
import pytest
|
||||||
from tools import TEST_DIR
|
from tools import TEST_DIR
|
||||||
|
@ -50,14 +51,48 @@ def test_premain_D(shell):
|
||||||
assert (builtins.__xonsh_env__.get('TEST2') == 'LOL')
|
assert (builtins.__xonsh_env__.get('TEST2') == 'LOL')
|
||||||
|
|
||||||
|
|
||||||
def test_premain_custom_rc(shell, tmpdir):
|
def test_premain_custom_rc(shell, tmpdir, monkeypatch):
|
||||||
|
monkeypatch.setattr(sys.stdin, 'isatty', lambda: True)
|
||||||
builtins.__xonsh_env__ = Env(XONSH_CACHE_SCRIPTS=False)
|
builtins.__xonsh_env__ = Env(XONSH_CACHE_SCRIPTS=False)
|
||||||
f = tmpdir.join('wakkawakka')
|
f = tmpdir.join('wakkawakka')
|
||||||
f.write("print('hi')")
|
f.write("print('hi')")
|
||||||
xonsh.main.premain(['--rc', f.strpath])
|
args = xonsh.main.premain(['--rc', f.strpath])
|
||||||
|
assert args.mode == XonshMode.interactive
|
||||||
assert f.strpath in builtins.__xonsh_env__.get('XONSHRC')
|
assert f.strpath in builtins.__xonsh_env__.get('XONSHRC')
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_rc_with_script(shell, tmpdir):
|
||||||
|
args = xonsh.main.premain(['tests/sample.xsh'])
|
||||||
|
assert not (args.mode == XonshMode.interactive)
|
||||||
|
|
||||||
|
|
||||||
|
def test_force_interactive_rc_with_script(shell, tmpdir):
|
||||||
|
args = xonsh.main.premain(['-i', 'tests/sample.xsh'])
|
||||||
|
assert builtins.__xonsh_env__.get('XONSH_INTERACTIVE')
|
||||||
|
|
||||||
|
|
||||||
|
def test_force_interactive_custom_rc_with_script(shell, tmpdir):
|
||||||
|
"""Calling a custom RC file on a script-call with the interactive flag
|
||||||
|
should run interactively
|
||||||
|
"""
|
||||||
|
builtins.__xonsh_env__ = Env(XONSH_CACHE_SCRIPTS=False)
|
||||||
|
f = tmpdir.join('wakkawakka')
|
||||||
|
f.write("print('hi')")
|
||||||
|
args = xonsh.main.premain(['-i', '--rc', f.strpath, 'tests/sample.xsh'])
|
||||||
|
assert args.mode == XonshMode.interactive
|
||||||
|
assert f.strpath in builtins.__xonsh_env__.get('XONSHRC')
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_rc_with_script(shell, tmpdir):
|
||||||
|
"""Calling a custom RC file on a script-call without the interactive flag
|
||||||
|
should not run interactively
|
||||||
|
"""
|
||||||
|
f = tmpdir.join('wakkawakka')
|
||||||
|
f.write("print('hi')")
|
||||||
|
args = xonsh.main.premain(['--rc', f.strpath, 'tests/sample.xsh'])
|
||||||
|
assert not (args.mode == XonshMode.interactive)
|
||||||
|
|
||||||
|
|
||||||
def test_premain_no_rc(shell, tmpdir):
|
def test_premain_no_rc(shell, tmpdir):
|
||||||
xonsh.main.premain(['--no-rc'])
|
xonsh.main.premain(['--no-rc'])
|
||||||
assert not builtins.__xonsh_env__.get('XONSHRC')
|
assert not builtins.__xonsh_env__.get('XONSHRC')
|
||||||
|
|
|
@ -218,7 +218,7 @@ class XonshMode(enum.Enum):
|
||||||
interactive = 3
|
interactive = 3
|
||||||
|
|
||||||
|
|
||||||
def start_services(shell_kwargs):
|
def start_services(shell_kwargs, args):
|
||||||
"""Starts up the essential services in the proper order.
|
"""Starts up the essential services in the proper order.
|
||||||
This returns the environment instance as a convenience.
|
This returns the environment instance as a convenience.
|
||||||
"""
|
"""
|
||||||
|
@ -236,6 +236,9 @@ def start_services(shell_kwargs):
|
||||||
env = builtins.__xonsh_env__
|
env = builtins.__xonsh_env__
|
||||||
rc = shell_kwargs.get('rc', None)
|
rc = shell_kwargs.get('rc', None)
|
||||||
rc = env.get('XONSHRC') if rc is None else rc
|
rc = env.get('XONSHRC') if rc is None else rc
|
||||||
|
if args.mode != XonshMode.interactive and not args.force_interactive:
|
||||||
|
# Don't load xonshrc if not interactive shell
|
||||||
|
rc = None
|
||||||
events.on_pre_rc.fire()
|
events.on_pre_rc.fire()
|
||||||
xonshrc_context(rcfiles=rc, execer=execer, ctx=ctx, env=env, login=login)
|
xonshrc_context(rcfiles=rc, execer=execer, ctx=ctx, env=env, login=login)
|
||||||
events.on_post_rc.fire()
|
events.on_post_rc.fire()
|
||||||
|
@ -288,11 +291,11 @@ def premain(argv=None):
|
||||||
args.mode = XonshMode.interactive
|
args.mode = XonshMode.interactive
|
||||||
shell_kwargs['completer'] = True
|
shell_kwargs['completer'] = True
|
||||||
shell_kwargs['login'] = True
|
shell_kwargs['login'] = True
|
||||||
env = start_services(shell_kwargs)
|
env = start_services(shell_kwargs, args)
|
||||||
env['XONSH_LOGIN'] = shell_kwargs['login']
|
env['XONSH_LOGIN'] = shell_kwargs['login']
|
||||||
if args.defines is not None:
|
if args.defines is not None:
|
||||||
env.update([x.split('=', 1) for x in args.defines])
|
env.update([x.split('=', 1) for x in args.defines])
|
||||||
env['XONSH_INTERACTIVE'] = args.force_interactive
|
env['XONSH_INTERACTIVE'] = args.force_interactive or (args.mode == XonshMode.interactive)
|
||||||
if ON_WINDOWS:
|
if ON_WINDOWS:
|
||||||
setup_win_unicode_console(env.get('WIN_UNICODE_CONSOLE', True))
|
setup_win_unicode_console(env.get('WIN_UNICODE_CONSOLE', True))
|
||||||
return args
|
return args
|
||||||
|
|
Loading…
Add table
Reference in a new issue