test_deepreload.py
57 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
Bradley M. Froehle
|
r6534 | # -*- coding: utf-8 -*- | ||
Bradley M. Froehle
|
r6533 | """Test suite for the deepreload module.""" | ||
Thomas Kluyver
|
r22971 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Bradley M. Froehle
|
r6534 | |||
Nikita Kniazev
|
r27044 | import types | ||
dswij
|
r26042 | from pathlib import Path | ||
Bradley M. Froehle
|
r6535 | |||
Matthias Bussonnier
|
r27509 | import pytest | ||
from tempfile import TemporaryDirectory | ||||
from IPython.lib.deepreload import modules_reloading | ||||
from IPython.lib.deepreload import reload as dreload | ||||
Bradley M. Froehle
|
r6541 | from IPython.utils.syspathcontext import prepended_to_syspath | ||
Bradley M. Froehle
|
r6533 | |||
dswij
|
r26043 | |||
Bradley M. Froehle
|
r6535 | def test_deepreload(): | ||
"Test that dreload does deep reloads and skips excluded modules." | ||||
with TemporaryDirectory() as tmpdir: | ||||
Bradley M. Froehle
|
r6541 | with prepended_to_syspath(tmpdir): | ||
dswij
|
r26042 | tmpdirpath = Path(tmpdir) | ||
gousaiyang
|
r27495 | with open(tmpdirpath / "A.py", "w", encoding="utf-8") as f: | ||
Nikita Kniazev
|
r27044 | f.write("class Object:\n pass\nok = True\n") | ||
gousaiyang
|
r27495 | with open(tmpdirpath / "B.py", "w", encoding="utf-8") as f: | ||
Nikita Kniazev
|
r27044 | f.write("import A\nassert A.ok, 'we are fine'\n") | ||
Bradley M. Froehle
|
r6541 | import A | ||
import B | ||||
# Test that A is not reloaded. | ||||
obj = A.Object() | ||||
dswij
|
r26043 | dreload(B, exclude=["A"]) | ||
Samuel Gaist
|
r26913 | assert isinstance(obj, A.Object) is True | ||
Bradley M. Froehle
|
r6541 | |||
Nikita Kniazev
|
r27044 | # Test that an import failure will not blow-up us. | ||
A.ok = False | ||||
with pytest.raises(AssertionError, match="we are fine"): | ||||
dreload(B, exclude=["A"]) | ||||
assert len(modules_reloading) == 0 | ||||
assert not A.ok | ||||
Bradley M. Froehle
|
r6541 | # Test that A is reloaded. | ||
obj = A.Object() | ||||
Nikita Kniazev
|
r27044 | A.ok = False | ||
Bradley M. Froehle
|
r6541 | dreload(B) | ||
Nikita Kniazev
|
r27044 | assert A.ok | ||
Samuel Gaist
|
r26913 | assert isinstance(obj, A.Object) is False | ||
Nikita Kniazev
|
r27044 | |||
def test_not_module(): | ||||
pytest.raises(TypeError, dreload, "modulename") | ||||
def test_not_in_sys_modules(): | ||||
fake_module = types.ModuleType("fake_module") | ||||
with pytest.raises(ImportError, match="not in sys.modules"): | ||||
dreload(fake_module) | ||||