File: test_imphooks.py

package info (click to toggle)
xonsh 0.13.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,024 kB
  • sloc: python: 46,350; makefile: 136; sh: 41; xml: 17
file content (70 lines) | stat: -rw-r--r-- 1,462 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""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_import_empty():
    from xpack import empty_xsh

    assert empty_xsh


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()