1# -*- coding: utf-8 -*-
2"""Testing xonsh import hooks"""
3import os
4import builtins
5
6import pytest
7
8from xonsh import imphooks
9from xonsh.execer import Execer
10from xonsh.environ import Env
11from xonsh.built_ins import unload_builtins
12
13imphooks.install_import_hooks()
14
15
16@pytest.yield_fixture(autouse=True)
17def imp_env():
18    execer = Execer(unload=False)
19    builtins.__xonsh_env__ = Env({"PATH": [], "PATHEXT": []})
20    yield
21    unload_builtins()
22
23
24def test_import():
25    import sample
26
27    assert "hello mom jawaka\n" == sample.x
28
29
30def test_absolute_import():
31    from xpack import sample
32
33    assert "hello mom jawaka\n" == sample.x
34
35
36def test_relative_import():
37    from xpack import relimp
38
39    assert "hello mom jawaka\n" == relimp.sample.x
40    assert "hello mom jawaka\ndark chest of wonders" == relimp.y
41
42
43def test_sub_import():
44    from xpack.sub import sample
45
46    assert "hello mom jawaka\n" == sample.x
47
48
49TEST_DIR = os.path.dirname(__file__)
50
51
52def test_module_dunder_file_attribute():
53    import sample
54
55    exp = os.path.join(TEST_DIR, "sample.xsh")
56    assert os.path.abspath(sample.__file__) == exp
57
58
59def test_module_dunder_file_attribute_sub():
60    from xpack.sub import sample
61
62    exp = os.path.join(TEST_DIR, "xpack", "sub", "sample.xsh")
63    assert os.path.abspath(sample.__file__) == exp
64