xonsh/tests/test_imphooks.py

72 lines
1.4 KiB
Python
Raw Normal View History

2015-03-29 17:49:44 -05:00
"""Testing xonsh import hooks"""
2016-08-27 13:51:45 +03:00
import os
2020-02-11 13:37:20 -05:00
from importlib import import_module
2016-09-08 21:11:51 -04:00
2016-07-01 13:52:44 +03:00
import pytest
2015-03-29 17:49:44 -05:00
from xonsh import imphooks
2015-03-29 18:25:11 -05:00
2018-11-19 20:22:18 -05:00
@pytest.fixture(autouse=True)
def imp_env(xession):
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-08 04:03:22 +05:30
xession.env.update({"PATH": [], "PATHEXT": []})
Refactor: make session loading explicit (#4540) * Refactor: Don't modify XSH in Execer This is not an invariant change - the __del__ method of the execer now does not unload the session. This is probably what we want - the session should be the final arbiter of when it goes out of scope, and we might need an explicit mechanism to handle this. * Refactor: make import hook Execer explicit This is ugly for now, but helps raise the global state modifications to the surface * Style: run black * Refactor: add `update_cache` to `CommandsCache` Previously a lot of internal usage of `all_commands` was for the updating side-effect. Now we make that a separate routine. * Refactor: remove args from XonshSession constructor * Refactor: move non stateful XonshSession methods to __init__ * Refactor: don't create custom class for namespace * Refactor: auto-generate set of default builtins * Refactor: set session attributes to None in constructor * Refactor: don't test hasattr for known attribute * Refactor: add methods to restore and disable Python exit * Refactor: add initial value for XSH.aliases * Refactor: don't default getattr for known attribute * Refactor: make _lastflush a closer and move to `load()` * Fix: catch $path even if empty. This shouldn't ever actually manifest itself, but whilst we're here! * Refactor: support existing usage of install_import_hooks * Style: run black * Refactor: remove unneeded import * Docs: add news item * Fix: news item * Refactor: remove unused arg * docs: fix news item * fix: no duplicate calls add helpers properties to completion-context * refactor: importing a module should not affect the session the user has to have XSH loaded before * fix: todo item for testing imphooks Co-authored-by: Noortheen Raja <jnoortheen@gmail.com>
2021-12-21 07:37:53 +00:00
imphooks.install_import_hooks(xession.execer)
2016-07-01 13:52:44 +03:00
yield
2016-06-21 11:43:08 -04:00
2015-03-29 17:49:44 -05:00
2015-03-29 18:29:56 -05:00
def test_import():
2016-07-01 13:52:44 +03:00
import sample
2018-08-30 09:18:49 -05:00
assert "hello mom jawaka\n" == sample.x
2016-07-01 13:52:44 +03:00
2015-03-29 18:25:11 -05:00
def test_import_empty():
from xpack import empty_xsh
assert empty_xsh
2015-03-29 18:25:11 -05:00
def test_absolute_import():
2016-07-01 13:52:44 +03:00
from xpack import sample
2018-08-30 09:18:49 -05:00
assert "hello mom jawaka\n" == sample.x
2016-07-01 13:52:44 +03:00
2015-03-29 18:25:11 -05:00
2015-03-29 18:29:56 -05:00
def test_relative_import():
2016-07-01 13:52:44 +03:00
from xpack import relimp
2018-08-30 09:18:49 -05:00
assert "hello mom jawaka\n" == relimp.sample.x
assert "hello mom jawaka\ndark chest of wonders" == relimp.y
2016-07-01 13:52:44 +03:00
2015-03-29 18:29:56 -05:00
2015-03-29 22:53:33 -05:00
def test_sub_import():
2016-07-01 13:52:44 +03:00
from xpack.sub import sample
2018-08-30 09:18:49 -05:00
assert "hello mom jawaka\n" == sample.x
2016-08-27 13:51:45 +03:00
TEST_DIR = os.path.dirname(__file__)
def test_module_dunder_file_attribute():
import sample
2018-08-30 09:18:49 -05:00
exp = os.path.join(TEST_DIR, "sample.xsh")
2016-08-28 09:57:31 -04:00
assert os.path.abspath(sample.__file__) == exp
2016-08-27 13:51:45 +03:00
def test_module_dunder_file_attribute_sub():
from xpack.sub import sample
2018-08-30 09:18:49 -05:00
exp = os.path.join(TEST_DIR, "xpack", "sub", "sample.xsh")
2016-08-28 09:57:31 -04:00
assert os.path.abspath(sample.__file__) == exp
2020-02-11 13:37:20 -05:00
def test_get_source():
2020-08-26 10:10:59 -05:00
mod = import_module("sample")
2020-02-11 13:37:20 -05:00
loader = mod.__loader__
2020-08-26 10:10:59 -05:00
source = loader.get_source("sample")
with open(os.path.join(TEST_DIR, "sample.xsh")) as srcfile:
2020-02-11 13:37:20 -05:00
assert source == srcfile.read()