Show More
@@ -1,65 +1,66 b'' | |||
|
1 | 1 | import tempfile, os |
|
2 | from pathlib import Path | |
|
2 | 3 | |
|
3 | 4 | from traitlets.config.loader import Config |
|
4 | 5 | |
|
5 | 6 | |
|
6 | 7 | def setup_module(): |
|
7 | 8 | ip.magic('load_ext storemagic') |
|
8 | 9 | |
|
9 | 10 | def test_store_restore(): |
|
10 | 11 | assert 'bar' not in ip.user_ns, "Error: some other test leaked `bar` in user_ns" |
|
11 | 12 | assert 'foo' not in ip.user_ns, "Error: some other test leaked `foo` in user_ns" |
|
12 | 13 | assert 'foobar' not in ip.user_ns, "Error: some other test leaked `foobar` in user_ns" |
|
13 | 14 | assert 'foobaz' not in ip.user_ns, "Error: some other test leaked `foobaz` in user_ns" |
|
14 | 15 | ip.user_ns['foo'] = 78 |
|
15 | 16 | ip.magic('alias bar echo "hello"') |
|
16 | 17 | ip.user_ns['foobar'] = 79 |
|
17 | 18 | ip.user_ns['foobaz'] = '80' |
|
18 | 19 | tmpd = tempfile.mkdtemp() |
|
19 | 20 | ip.magic('cd ' + tmpd) |
|
20 | 21 | ip.magic('store foo') |
|
21 | 22 | ip.magic('store bar') |
|
22 | 23 | ip.magic('store foobar foobaz') |
|
23 | 24 | |
|
24 | 25 | # Check storing |
|
25 | 26 | assert ip.db["autorestore/foo"] == 78 |
|
26 | 27 | assert "bar" in ip.db["stored_aliases"] |
|
27 | 28 | assert ip.db["autorestore/foobar"] == 79 |
|
28 | 29 | assert ip.db["autorestore/foobaz"] == "80" |
|
29 | 30 | |
|
30 | 31 | # Remove those items |
|
31 | 32 | ip.user_ns.pop('foo', None) |
|
32 | 33 | ip.user_ns.pop('foobar', None) |
|
33 | 34 | ip.user_ns.pop('foobaz', None) |
|
34 | 35 | ip.alias_manager.undefine_alias('bar') |
|
35 | 36 | ip.magic('cd -') |
|
36 | 37 | ip.user_ns['_dh'][:] = [] |
|
37 | 38 | |
|
38 | 39 | # Check restoring |
|
39 | 40 | ip.magic("store -r foo bar foobar foobaz") |
|
40 | 41 | assert ip.user_ns["foo"] == 78 |
|
41 | 42 | assert ip.alias_manager.is_alias("bar") |
|
42 | 43 | assert ip.user_ns["foobar"] == 79 |
|
43 | 44 | assert ip.user_ns["foobaz"] == "80" |
|
44 | 45 | |
|
45 | 46 | ip.magic("store -r") # restores _dh too |
|
46 |
assert |
|
|
47 | assert any(Path(tmpd).samefile(p) for p in ip.user_ns["_dh"]) | |
|
47 | 48 | |
|
48 | 49 | os.rmdir(tmpd) |
|
49 | 50 | |
|
50 | 51 | def test_autorestore(): |
|
51 | 52 | ip.user_ns['foo'] = 95 |
|
52 | 53 | ip.magic('store foo') |
|
53 | 54 | del ip.user_ns['foo'] |
|
54 | 55 | c = Config() |
|
55 | 56 | c.StoreMagics.autorestore = False |
|
56 | 57 | orig_config = ip.config |
|
57 | 58 | try: |
|
58 | 59 | ip.config = c |
|
59 | 60 | ip.extension_manager.reload_extension("storemagic") |
|
60 | 61 | assert "foo" not in ip.user_ns |
|
61 | 62 | c.StoreMagics.autorestore = True |
|
62 | 63 | ip.extension_manager.reload_extension("storemagic") |
|
63 | 64 | assert ip.user_ns["foo"] == 95 |
|
64 | 65 | finally: |
|
65 | 66 | ip.config = orig_config |
@@ -1,29 +1,29 b'' | |||
|
1 | 1 | #----------------------------------------------------------------------------- |
|
2 | 2 | # Copyright (C) 2012- The IPython Development Team |
|
3 | 3 | # |
|
4 | 4 | # Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | # the file COPYING, distributed as part of this software. |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | from pathlib import Path |
|
9 | 9 | |
|
10 | 10 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
11 | 11 | from IPython.utils.tempdir import TemporaryWorkingDirectory |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | def test_named_file_in_temporary_directory(): |
|
15 | 15 | with NamedFileInTemporaryDirectory('filename') as file: |
|
16 | 16 | name = file.name |
|
17 | 17 | assert not file.closed |
|
18 | 18 | assert Path(name).exists() |
|
19 | 19 | file.write(b'test') |
|
20 | 20 | assert file.closed |
|
21 | 21 | assert not Path(name).exists() |
|
22 | 22 | |
|
23 | 23 | def test_temporary_working_directory(): |
|
24 | 24 | with TemporaryWorkingDirectory() as directory: |
|
25 | directory_path = Path(directory) | |
|
25 | directory_path = Path(directory).resolve() | |
|
26 | 26 | assert directory_path.exists() |
|
27 |
assert Path.cwd() == directory_path |
|
|
27 | assert Path.cwd().resolve() == directory_path | |
|
28 | 28 | assert not directory_path.exists() |
|
29 |
assert Path.cwd() != directory_path |
|
|
29 | assert Path.cwd().resolve() != directory_path |
General Comments 0
You need to be logged in to leave comments.
Login now