mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-06 09:20:57 +01:00
Merge pull request #2040 from mitnk/source-file
Minor updates on source alias
This commit is contained in:
commit
d495ca5c40
3 changed files with 45 additions and 3 deletions
0
tests/aliases/__init__.py
Normal file
0
tests/aliases/__init__.py
Normal file
38
tests/aliases/test_source.py
Normal file
38
tests/aliases/test_source.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
import os.path
|
||||
import pytest
|
||||
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import MagicMock
|
||||
from xonsh.aliases import source_alias, builtins
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mockopen(xonsh_builtins, monkeypatch):
|
||||
@contextmanager
|
||||
def mocked_open(fpath, *args, **kwargs):
|
||||
yield MagicMock(read=lambda: fpath)
|
||||
monkeypatch.setattr(builtins, 'open', mocked_open)
|
||||
|
||||
|
||||
def test_source_current_dir(mockopen, monkeypatch):
|
||||
checker = []
|
||||
|
||||
def mocked_execx(src, *args, **kwargs):
|
||||
checker.append(src.strip())
|
||||
monkeypatch.setattr(builtins, 'execx', mocked_execx)
|
||||
monkeypatch.setattr(os.path, 'isfile', lambda x: True)
|
||||
source_alias(['foo', 'bar'])
|
||||
assert checker == ['foo', 'bar']
|
||||
|
||||
|
||||
def test_source_path(mockopen, monkeypatch):
|
||||
checker = []
|
||||
|
||||
def mocked_execx(src, *args, **kwargs):
|
||||
checker.append(src.strip())
|
||||
monkeypatch.setattr(builtins, 'execx', mocked_execx)
|
||||
source_alias(['foo', 'bar'])
|
||||
path_foo = os.path.join('tests', 'bin', 'foo')
|
||||
path_bar = os.path.join('tests', 'bin', 'bar')
|
||||
assert checker[0].endswith(path_foo)
|
||||
assert checker[1].endswith(path_bar)
|
|
@ -256,9 +256,13 @@ def source_alias(args, stdin=None):
|
|||
encoding = env.get('XONSH_ENCODING')
|
||||
errors = env.get('XONSH_ENCODING_ERRORS')
|
||||
for fname in args:
|
||||
if not os.path.isfile(fname):
|
||||
fname = locate_binary(fname)
|
||||
with open(fname, 'r', encoding=encoding, errors=errors) as fp:
|
||||
fpath = fname
|
||||
if not os.path.isfile(fpath):
|
||||
fpath = locate_binary(fname)
|
||||
if fpath is None:
|
||||
print('source: {}: No such file'.format(fname), file=sys.stderr)
|
||||
continue
|
||||
with open(fpath, 'r', encoding=encoding, errors=errors) as fp:
|
||||
src = fp.read()
|
||||
if not src.endswith('\n'):
|
||||
src += '\n'
|
||||
|
|
Loading…
Add table
Reference in a new issue