xonsh/tests/aliases/test_source.py

42 lines
1.1 KiB
Python
Raw Normal View History

2016-12-24 16:35:08 +08:00
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)
2018-08-30 09:18:49 -05:00
monkeypatch.setattr(builtins, "open", mocked_open)
2016-12-24 16:35:08 +08:00
def test_source_current_dir(mockopen, monkeypatch):
checker = []
def mocked_execx(src, *args, **kwargs):
checker.append(src.strip())
2018-08-30 09:18:49 -05:00
monkeypatch.setattr(builtins, "execx", mocked_execx)
monkeypatch.setattr(os.path, "isfile", lambda x: True)
source_alias(["foo", "bar"])
assert checker == ["foo", "bar"]
2016-12-24 16:35:08 +08:00
def test_source_path(mockopen, monkeypatch):
checker = []
def mocked_execx(src, *args, **kwargs):
checker.append(src.strip())
2018-08-30 09:18:49 -05:00
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")
2016-12-24 16:45:25 +08:00
assert checker[0].endswith(path_foo)
assert checker[1].endswith(path_bar)