| 1 |
import os.path |
| 2 |
|
| 3 |
from tempfile import TemporaryDirectory |
| 4 |
|
| 5 |
import IPython.testing.tools as tt |
| 6 |
from IPython.utils.syspathcontext import prepended_to_syspath |
| 7 |
|
| 8 |
ext1_content = """ |
| 9 |
def load_ipython_extension(ip): |
| 10 |
print("Running ext1 load") |
| 11 |
|
| 12 |
def unload_ipython_extension(ip): |
| 13 |
print("Running ext1 unload") |
| 14 |
""" |
| 15 |
|
| 16 |
ext2_content = """ |
| 17 |
def load_ipython_extension(ip): |
| 18 |
print("Running ext2 load") |
| 19 |
""" |
| 20 |
|
| 21 |
ext3_content = """ |
| 22 |
def load_ipython_extension(ip): |
| 23 |
ip2 = get_ipython() |
| 24 |
print(ip is ip2) |
| 25 |
""" |
| 26 |
|
| 27 |
|
| 28 |
def test_extension_loading(): |
| 29 |
em = get_ipython().extension_manager |
| 30 |
with TemporaryDirectory() as td: |
| 31 |
ext1 = os.path.join(td, "ext1.py") |
| 32 |
with open(ext1, "w", encoding="utf-8") as f: |
| 33 |
f.write(ext1_content) |
| 34 |
|
| 35 |
ext2 = os.path.join(td, "ext2.py") |
| 36 |
with open(ext2, "w", encoding="utf-8") as f: |
| 37 |
f.write(ext2_content) |
| 38 |
|
| 39 |
with prepended_to_syspath(td): |
| 40 |
assert "ext1" not in em.loaded |
| 41 |
assert "ext2" not in em.loaded |
| 42 |
|
| 43 |
# Load extension |
| 44 |
with tt.AssertPrints("Running ext1 load"): |
| 45 |
assert em.load_extension("ext1") is None |
| 46 |
assert "ext1" in em.loaded |
| 47 |
|
| 48 |
# Should refuse to load it again |
| 49 |
with tt.AssertNotPrints("Running ext1 load"): |
| 50 |
assert em.load_extension("ext1") == "already loaded" |
| 51 |
|
| 52 |
# Reload |
| 53 |
with tt.AssertPrints("Running ext1 unload"): |
| 54 |
with tt.AssertPrints("Running ext1 load", suppress=False): |
| 55 |
em.reload_extension("ext1") |
| 56 |
|
| 57 |
# Unload |
| 58 |
with tt.AssertPrints("Running ext1 unload"): |
| 59 |
assert em.unload_extension("ext1") is None |
| 60 |
|
| 61 |
# Can't unload again |
| 62 |
with tt.AssertNotPrints("Running ext1 unload"): |
| 63 |
assert em.unload_extension("ext1") == "not loaded" |
| 64 |
assert em.unload_extension("ext2") == "not loaded" |
| 65 |
|
| 66 |
# Load extension 2 |
| 67 |
with tt.AssertPrints("Running ext2 load"): |
| 68 |
assert em.load_extension("ext2") is None |
| 69 |
|
| 70 |
# Can't unload this |
| 71 |
assert em.unload_extension("ext2") == "no unload function" |
| 72 |
|
| 73 |
# But can reload it |
| 74 |
with tt.AssertPrints("Running ext2 load"): |
| 75 |
em.reload_extension("ext2") |
| 76 |
|
| 77 |
|
| 78 |
def test_extension_builtins(): |
| 79 |
em = get_ipython().extension_manager |
| 80 |
with TemporaryDirectory() as td: |
| 81 |
ext3 = os.path.join(td, "ext3.py") |
| 82 |
with open(ext3, "w", encoding="utf-8") as f: |
| 83 |
f.write(ext3_content) |
| 84 |
|
| 85 |
assert "ext3" not in em.loaded |
| 86 |
|
| 87 |
with prepended_to_syspath(td): |
| 88 |
# Load extension |
| 89 |
with tt.AssertPrints("True"): |
| 90 |
assert em.load_extension("ext3") is None |
| 91 |
assert "ext3" in em.loaded |
| 92 |
|
| 93 |
|
| 94 |
def test_non_extension(): |
| 95 |
em = get_ipython().extension_manager |
| 96 |
assert em.load_extension("sys") == "no load function" |