Debian Codegraph @

tests/test_storemagic.py

raw
1 import tempfile, os
2 from pathlib import Path
3
4 from traitlets.config.loader import Config
5
6
7 def setup_module():
8 ip.run_line_magic("load_ext", "storemagic")
9
10
11 def test_store_restore():
12 assert "bar" not in ip.user_ns, "Error: some other test leaked `bar` in user_ns"
13 assert "foo" not in ip.user_ns, "Error: some other test leaked `foo` in user_ns"
14 assert (
15 "foobar" not in ip.user_ns
16 ), "Error: some other test leaked `foobar` in user_ns"
17 assert (
18 "foobaz" not in ip.user_ns
19 ), "Error: some other test leaked `foobaz` in user_ns"
20 ip.user_ns["foo"] = 78
21 ip.run_line_magic("alias", 'bar echo "hello"')
22 ip.user_ns["foobar"] = 79
23 ip.user_ns["foobaz"] = "80"
24 tmpd = tempfile.mkdtemp()
25 ip.run_line_magic("cd", tmpd)
26 ip.run_line_magic("store", "foo")
27 ip.run_line_magic("store", "bar")
28 ip.run_line_magic("store", "foobar foobaz")
29
30 # Check storing
31 assert ip.db["autorestore/foo"] == 78
32 assert "bar" in ip.db["stored_aliases"]
33 assert ip.db["autorestore/foobar"] == 79
34 assert ip.db["autorestore/foobaz"] == "80"
35
36 # Remove those items
37 ip.user_ns.pop("foo", None)
38 ip.user_ns.pop("foobar", None)
39 ip.user_ns.pop("foobaz", None)
40 ip.alias_manager.undefine_alias("bar")
41 ip.run_line_magic("cd", "-")
42 ip.user_ns["_dh"][:] = []
43
44 # Check restoring
45 ip.run_line_magic("store", "-r foo bar foobar foobaz")
46 assert ip.user_ns["foo"] == 78
47 assert ip.alias_manager.is_alias("bar")
48 assert ip.user_ns["foobar"] == 79
49 assert ip.user_ns["foobaz"] == "80"
50
51 ip.run_line_magic("store", "-r") # restores _dh too
52 assert any(Path(tmpd).samefile(p) for p in ip.user_ns["_dh"])
53
54 os.rmdir(tmpd)
55
56
57 def test_autorestore():
58 ip.user_ns["foo"] = 95
59 ip.run_line_magic("store", "foo")
60 del ip.user_ns["foo"]
61 c = Config()
62 c.StoreMagics.autorestore = False
63 orig_config = ip.config
64 try:
65 ip.config = c
66 ip.extension_manager.reload_extension("storemagic")
67 assert "foo" not in ip.user_ns
68 c.StoreMagics.autorestore = True
69 ip.extension_manager.reload_extension("storemagic")
70 assert ip.user_ns["foo"] == 95
71 finally:
72 ip.config = orig_config