Debian Codegraph @

tests/test_deepreload.py

raw
1 # -*- coding: utf-8 -*-
2 """Test suite for the deepreload module."""
3
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
6
7 import types
8 from pathlib import Path
9
10 import pytest
11 from tempfile import TemporaryDirectory
12
13 from IPython.lib.deepreload import modules_reloading
14 from IPython.lib.deepreload import reload as dreload
15 from IPython.utils.syspathcontext import prepended_to_syspath
16
17
18 def test_deepreload():
19 "Test that dreload does deep reloads and skips excluded modules."
20 with TemporaryDirectory() as tmpdir:
21 with prepended_to_syspath(tmpdir):
22 tmpdirpath = Path(tmpdir)
23 with open(tmpdirpath / "A.py", "w", encoding="utf-8") as f:
24 f.write("class Object:\n pass\nok = True\n")
25 with open(tmpdirpath / "B.py", "w", encoding="utf-8") as f:
26 f.write("import A\nassert A.ok, 'we are fine'\n")
27 import A
28 import B
29
30 # Test that A is not reloaded.
31 obj = A.Object()
32 dreload(B, exclude=["A"])
33 assert isinstance(obj, A.Object) is True
34
35 # Test that an import failure will not blow-up us.
36 A.ok = False
37 with pytest.raises(AssertionError, match="we are fine"):
38 dreload(B, exclude=["A"])
39 assert len(modules_reloading) == 0
40 assert not A.ok
41
42 # Test that A is reloaded.
43 obj = A.Object()
44 A.ok = False
45 dreload(B)
46 assert A.ok
47 assert isinstance(obj, A.Object) is False
48
49
50 def test_not_module():
51 pytest.raises(TypeError, dreload, "modulename")
52
53
54 def test_not_in_sys_modules():
55 fake_module = types.ModuleType("fake_module")
56 with pytest.raises(ImportError, match="not in sys.modules"):
57 dreload(fake_module)