xonsh/tests/test_imphooks.py
Noorhteen Raja NJ 7c4e207abd
update test xsh usage (#4581)
* todo

* test: remove usage of DummyEnv and setting .env attribute on xession fixture

one step closer to making too much of tweaking to xession during tests

* test: fix tests vox and gitstatus-prompt

* docs: update test-fixture usage

* fix: import flake8 error

* test: remove direct access to XSH in tests

* test: remove usage of XSH in test files

* todo

* test: use tmp-dir to create stubs

* refactor: use fixture factory to mock out XonshSession

* refactor: remove direct access of XSH from functions

* refactor: remove direct access of XSH from functions

* fix: qa checks

* refactor: rename variables to match their values

* test: update failing tests because it had no PATH set previously

* fix: remove builtins usage from pyghooks.py

* style:

* refactor: update tests to use fixtures

* fix: env varialbe is setup per function

some tests accidentally update the env variables and that is leaking
into next tests

* fix: failing vox tests

* test: set commands_cache per test

* test: fix failing tests

* fix: failing tests on linux

ptk-highlight

* fix: failing tests on Windows

cwd-prompt

* test: copy env as to not affect original object

* fix: lazily evaluate cmds-cache in pyghooks

* test: fix failing tests

* fix: qa errors import

* test: set commands-cache per test

while caching path results

* test: speedup test_thread_local_swap

* fix: failing tests on windows

* refactor: Execer doesn't control session

* refactor: XSH.unload will take care of reversing builtins attrs set

* test: use env.update over monkeypatch

* Revert "test: use env.update over monkeypatch"

This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-07 17:33:22 -05:00

64 lines
1.3 KiB
Python

"""Testing xonsh import hooks"""
import os
from importlib import import_module
import pytest
from xonsh import imphooks
@pytest.fixture(autouse=True)
def imp_env(xession):
xession.env.update({"PATH": [], "PATHEXT": []})
imphooks.install_import_hooks(xession.execer)
yield
def test_import():
import sample
assert "hello mom jawaka\n" == sample.x
def test_absolute_import():
from xpack import sample
assert "hello mom jawaka\n" == sample.x
def test_relative_import():
from xpack import relimp
assert "hello mom jawaka\n" == relimp.sample.x
assert "hello mom jawaka\ndark chest of wonders" == relimp.y
def test_sub_import():
from xpack.sub import sample
assert "hello mom jawaka\n" == sample.x
TEST_DIR = os.path.dirname(__file__)
def test_module_dunder_file_attribute():
import sample
exp = os.path.join(TEST_DIR, "sample.xsh")
assert os.path.abspath(sample.__file__) == exp
def test_module_dunder_file_attribute_sub():
from xpack.sub import sample
exp = os.path.join(TEST_DIR, "xpack", "sub", "sample.xsh")
assert os.path.abspath(sample.__file__) == exp
def test_get_source():
mod = import_module("sample")
loader = mod.__loader__
source = loader.get_source("sample")
with open(os.path.join(TEST_DIR, "sample.xsh")) as srcfile:
assert source == srcfile.read()