2015-11-16 14:04:32 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-03-29 17:49:44 -05:00
|
|
|
"""Testing xonsh import hooks"""
|
2016-08-27 13:51:45 +03:00
|
|
|
import os
|
2016-09-08 21:11:51 -04:00
|
|
|
import builtins
|
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
|
|
|
|
2016-08-10 18:20:54 -07:00
|
|
|
from xonsh import imphooks
|
2017-02-14 21:14:18 +01:00
|
|
|
from xonsh.execer import Execer
|
2016-06-28 20:22:11 -04:00
|
|
|
from xonsh.environ import Env
|
2017-02-14 21:14:18 +01:00
|
|
|
from xonsh.built_ins import unload_builtins
|
2015-03-29 18:25:11 -05:00
|
|
|
|
2017-02-19 13:22:01 -05:00
|
|
|
imphooks.install_import_hooks()
|
2016-08-10 18:20:54 -07:00
|
|
|
|
2015-03-29 18:25:11 -05:00
|
|
|
|
2018-11-19 20:22:18 -05:00
|
|
|
@pytest.fixture(autouse=True)
|
2018-11-19 21:56:58 -05:00
|
|
|
def imp_env(xonsh_builtins):
|
2020-05-05 06:42:28 -04:00
|
|
|
Execer(unload=False)
|
2018-09-13 14:03:35 -04:00
|
|
|
builtins.__xonsh__.env = Env({"PATH": [], "PATHEXT": []})
|
2016-07-01 13:52:44 +03:00
|
|
|
yield
|
|
|
|
unload_builtins()
|
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_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():
|
|
|
|
mod = import_module('sample')
|
|
|
|
loader = mod.__loader__
|
|
|
|
source = loader.get_source('sample')
|
|
|
|
with open(os.path.join(TEST_DIR, 'sample.xsh'), 'rt') as srcfile:
|
|
|
|
assert source == srcfile.read()
|