Show More
@@ -17,8 +17,6 b' from pathlib import Path' | |||
|
17 | 17 | from textwrap import dedent |
|
18 | 18 | from unittest import TestCase, mock |
|
19 | 19 | |
|
20 | import nose.tools as nt | |
|
21 | ||
|
22 | 20 | import pytest |
|
23 | 21 | |
|
24 | 22 | from IPython import get_ipython |
@@ -47,7 +45,8 b' class DummyMagics(magic.Magics): pass' | |||
|
47 | 45 | |
|
48 | 46 | def test_extract_code_ranges(): |
|
49 | 47 | instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :" |
|
50 |
expected = [ |
|
|
48 | expected = [ | |
|
49 | (0, 1), | |
|
51 | 50 |
|
|
52 | 51 |
|
|
53 | 52 |
|
@@ -56,9 +55,10 b' def test_extract_code_ranges():' | |||
|
56 | 55 |
|
|
57 | 56 |
|
|
58 | 57 |
|
|
59 |
|
|
|
58 | (None, None), | |
|
59 | ] | |
|
60 | 60 | actual = list(code.extract_code_ranges(instr)) |
|
61 |
|
|
|
61 | assert actual == expected | |
|
62 | 62 | |
|
63 | 63 | def test_extract_symbols(): |
|
64 | 64 | source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n""" |
@@ -70,7 +70,7 b' def test_extract_symbols():' | |||
|
70 | 70 | (["class A: pass\n"], ['a']), |
|
71 | 71 | ([], ['z'])] |
|
72 | 72 | for symbols, exp in zip(symbols_args, expected): |
|
73 |
|
|
|
73 | assert code.extract_symbols(source, symbols) == exp | |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | def test_extract_symbols_raises_exception_with_non_python_code(): |
@@ -78,13 +78,13 b' def test_extract_symbols_raises_exception_with_non_python_code():' | |||
|
78 | 78 | "def hello\n" |
|
79 | 79 | "puts 'Hello world'\n" |
|
80 | 80 | "end") |
|
81 |
with |
|
|
81 | with pytest.raises(SyntaxError): | |
|
82 | 82 | code.extract_symbols(source, "hello") |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | def test_magic_not_found(): |
|
86 | 86 | # magic not found raises UsageError |
|
87 |
with |
|
|
87 | with pytest.raises(UsageError): | |
|
88 | 88 | _ip.magic('doesntexist') |
|
89 | 89 | |
|
90 | 90 | # ensure result isn't success when a magic isn't found |
@@ -94,7 +94,7 b' def test_magic_not_found():' | |||
|
94 | 94 | |
|
95 | 95 | def test_cell_magic_not_found(): |
|
96 | 96 | # magic not found raises UsageError |
|
97 |
with |
|
|
97 | with pytest.raises(UsageError): | |
|
98 | 98 | _ip.run_cell_magic('doesntexist', 'line', 'cell') |
|
99 | 99 | |
|
100 | 100 | # ensure result isn't success when a magic isn't found |
@@ -127,7 +127,7 b' def test_config_available_configs():' | |||
|
127 | 127 | |
|
128 | 128 | stdout = captured.stdout |
|
129 | 129 | config_classes = stdout.strip().split('\n')[1:] |
|
130 |
|
|
|
130 | assert config_classes == sorted(set(config_classes)) | |
|
131 | 131 | |
|
132 | 132 | def test_config_print_class(): |
|
133 | 133 | """ test that config with a classname prints the class's options. """ |
@@ -148,15 +148,14 b' def test_rehashx():' | |||
|
148 | 148 | _ip.magic('rehashx') |
|
149 | 149 | # Practically ALL ipython development systems will have more than 10 aliases |
|
150 | 150 | |
|
151 |
|
|
|
151 | assert len(_ip.alias_manager.aliases) > 10 | |
|
152 | 152 | for name, cmd in _ip.alias_manager.aliases: |
|
153 | 153 | # we must strip dots from alias names |
|
154 |
|
|
|
154 | assert "." not in name | |
|
155 | 155 | |
|
156 | 156 | # rehashx must fill up syscmdlist |
|
157 | 157 | scoms = _ip.db['syscmdlist'] |
|
158 |
|
|
|
159 | ||
|
158 | assert len(scoms) > 10 | |
|
160 | 159 | |
|
161 | 160 | |
|
162 | 161 | def test_magic_parse_options(): |
@@ -170,16 +169,17 b' def test_magic_parse_options():' | |||
|
170 | 169 | expected = 'c:x' |
|
171 | 170 | else: |
|
172 | 171 | expected = path |
|
173 |
|
|
|
172 | assert opts["f"] == expected | |
|
173 | ||
|
174 | 174 | |
|
175 | 175 | def test_magic_parse_long_options(): |
|
176 | 176 | """Magic.parse_options can handle --foo=bar long options""" |
|
177 | 177 | ip = get_ipython() |
|
178 | 178 | m = DummyMagics(ip) |
|
179 |
opts, _ = m.parse_options( |
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
179 | opts, _ = m.parse_options("--foo --bar=bubble", "a", "foo", "bar=") | |
|
180 | assert "foo" in opts | |
|
181 | assert "bar" in opts | |
|
182 | assert opts["bar"] == "bubble" | |
|
183 | 183 | |
|
184 | 184 | |
|
185 | 185 | def doctest_hist_f(): |
@@ -263,7 +263,7 b' def doctest_hist_op():' | |||
|
263 | 263 | |
|
264 | 264 | def test_hist_pof(): |
|
265 | 265 | ip = get_ipython() |
|
266 |
ip.run_cell( |
|
|
266 | ip.run_cell("1+2", store_history=True) | |
|
267 | 267 | #raise Exception(ip.history_manager.session_number) |
|
268 | 268 | #raise Exception(list(ip.history_manager._get_range_session())) |
|
269 | 269 | with TemporaryDirectory() as td: |
@@ -279,10 +279,10 b' def test_macro():' | |||
|
279 | 279 | for i, cmd in enumerate(cmds, start=1): |
|
280 | 280 | ip.history_manager.store_inputs(i, cmd) |
|
281 | 281 | ip.magic("macro test 1-3") |
|
282 |
|
|
|
282 | assert ip.user_ns["test"].value == "\n".join(cmds) + "\n" | |
|
283 | 283 | |
|
284 | 284 | # List macros |
|
285 |
|
|
|
285 | assert "test" in ip.magic("macro") | |
|
286 | 286 | |
|
287 | 287 | |
|
288 | 288 | def test_macro_run(): |
@@ -292,7 +292,7 b' def test_macro_run():' | |||
|
292 | 292 | cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"] |
|
293 | 293 | for cmd in cmds: |
|
294 | 294 | ip.run_cell(cmd, store_history=True) |
|
295 |
|
|
|
295 | assert ip.user_ns["test"].value == "a+=1\nprint(a)\n" | |
|
296 | 296 | with tt.AssertPrints("12"): |
|
297 | 297 | ip.run_cell("test") |
|
298 | 298 | with tt.AssertPrints("13"): |
@@ -306,54 +306,59 b' def test_magic_magic():' | |||
|
306 | 306 | ip.magic("magic") |
|
307 | 307 | |
|
308 | 308 | stdout = captured.stdout |
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
|
309 | assert "%magic" in stdout | |
|
310 | assert "IPython" in stdout | |
|
311 | assert "Available" in stdout | |
|
312 | 312 | |
|
313 | 313 | |
|
314 | 314 | @dec.skipif_not_numpy |
|
315 | 315 | def test_numpy_reset_array_undec(): |
|
316 | 316 | "Test '%reset array' functionality" |
|
317 |
_ip.ex( |
|
|
318 |
_ip.ex( |
|
|
319 |
|
|
|
320 |
_ip.magic( |
|
|
321 |
|
|
|
317 | _ip.ex("import numpy as np") | |
|
318 | _ip.ex("a = np.empty(2)") | |
|
319 | assert "a" in _ip.user_ns | |
|
320 | _ip.magic("reset -f array") | |
|
321 | assert "a" not in _ip.user_ns | |
|
322 | ||
|
322 | 323 | |
|
323 | 324 | def test_reset_out(): |
|
324 | 325 | "Test '%reset out' magic" |
|
325 | 326 | _ip.run_cell("parrot = 'dead'", store_history=True) |
|
326 | 327 | # test '%reset -f out', make an Out prompt |
|
327 | 328 | _ip.run_cell("parrot", store_history=True) |
|
328 |
|
|
|
329 |
_ip.magic( |
|
|
330 |
|
|
|
331 |
|
|
|
329 | assert "dead" in [_ip.user_ns[x] for x in ("_", "__", "___")] | |
|
330 | _ip.magic("reset -f out") | |
|
331 | assert "dead" not in [_ip.user_ns[x] for x in ("_", "__", "___")] | |
|
332 | assert len(_ip.user_ns["Out"]) == 0 | |
|
333 | ||
|
332 | 334 | |
|
333 | 335 | def test_reset_in(): |
|
334 | 336 | "Test '%reset in' magic" |
|
335 | 337 | # test '%reset -f in' |
|
336 | 338 | _ip.run_cell("parrot", store_history=True) |
|
337 |
|
|
|
338 |
_ip.magic( |
|
|
339 |
|
|
|
340 |
|
|
|
339 | assert "parrot" in [_ip.user_ns[x] for x in ("_i", "_ii", "_iii")] | |
|
340 | _ip.magic("%reset -f in") | |
|
341 | assert "parrot" not in [_ip.user_ns[x] for x in ("_i", "_ii", "_iii")] | |
|
342 | assert len(set(_ip.user_ns["In"])) == 1 | |
|
343 | ||
|
341 | 344 | |
|
342 | 345 | def test_reset_dhist(): |
|
343 | 346 | "Test '%reset dhist' magic" |
|
344 | 347 | _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing |
|
345 |
_ip.magic( |
|
|
346 |
_ip.magic( |
|
|
347 |
|
|
|
348 |
_ip.magic( |
|
|
349 |
|
|
|
348 | _ip.magic("cd " + os.path.dirname(pytest.__file__)) | |
|
349 | _ip.magic("cd -") | |
|
350 | assert len(_ip.user_ns["_dh"]) > 0 | |
|
351 | _ip.magic("reset -f dhist") | |
|
352 | assert len(_ip.user_ns["_dh"]) == 0 | |
|
350 | 353 | _ip.run_cell("_dh = [d for d in tmp]") #restore |
|
351 | 354 | |
|
355 | ||
|
352 | 356 | def test_reset_in_length(): |
|
353 | 357 | "Test that '%reset in' preserves In[] length" |
|
354 | 358 | _ip.run_cell("print 'foo'") |
|
355 | 359 | _ip.run_cell("reset -f in") |
|
356 |
|
|
|
360 | assert len(_ip.user_ns["In"]) == _ip.displayhook.prompt_count + 1 | |
|
361 | ||
|
357 | 362 | |
|
358 | 363 | class TestResetErrors(TestCase): |
|
359 | 364 | |
@@ -395,7 +400,7 b' def test_tb_syntaxerror():' | |||
|
395 | 400 | sys.stdout = save_stdout |
|
396 | 401 | # trim output, and only check the last line |
|
397 | 402 | last_line = out.rstrip().splitlines()[-1].strip() |
|
398 |
|
|
|
403 | assert last_line == "SyntaxError: invalid syntax" | |
|
399 | 404 | |
|
400 | 405 | |
|
401 | 406 | def test_time(): |
@@ -448,18 +453,21 b' def test_multiline_time():' | |||
|
448 | 453 | a = "ho" |
|
449 | 454 | b = "hey" |
|
450 | 455 | a+b |
|
451 |
""" |
|
|
452 | nt.assert_equal(ip.user_ns_hidden['_'], 'hohey') | |
|
456 | """ | |
|
457 | ) | |
|
458 | ) | |
|
459 | assert ip.user_ns_hidden["_"] == "hohey" | |
|
460 | ||
|
453 | 461 | |
|
454 | 462 | def test_time_local_ns(): |
|
455 | 463 | """ |
|
456 | 464 | Test that local_ns is actually global_ns when running a cell magic |
|
457 | 465 | """ |
|
458 | 466 | ip = get_ipython() |
|
459 | ip.run_cell("%%time\n" | |
|
460 | "myvar = 1") | |
|
461 |
|
|
|
462 | del ip.user_ns['myvar'] | |
|
467 | ip.run_cell("%%time\n" "myvar = 1") | |
|
468 | assert ip.user_ns["myvar"] == 1 | |
|
469 | del ip.user_ns["myvar"] | |
|
470 | ||
|
463 | 471 | |
|
464 | 472 | def test_doctest_mode(): |
|
465 | 473 | "Toggle doctest_mode twice, it should be a no-op and run without error" |
@@ -472,8 +480,8 b' def test_parse_options():' | |||
|
472 | 480 | # These are only the most minimal of tests, more should be added later. At |
|
473 | 481 | # the very least we check that basic text/unicode calls work OK. |
|
474 | 482 | m = DummyMagics(_ip) |
|
475 |
|
|
|
476 |
|
|
|
483 | assert m.parse_options("foo", "")[1] == "foo" | |
|
484 | assert m.parse_options("foo", "")[1] == "foo" | |
|
477 | 485 | |
|
478 | 486 | |
|
479 | 487 | def test_parse_options_preserve_non_option_string(): |
@@ -482,8 +490,8 b' def test_parse_options_preserve_non_option_string():' | |||
|
482 | 490 | opts, stmt = m.parse_options( |
|
483 | 491 | " -n1 -r 13 _ = 314 + foo", "n:r:", preserve_non_opts=True |
|
484 | 492 | ) |
|
485 |
|
|
|
486 |
|
|
|
493 | assert opts == {"n": "1", "r": "13"} | |
|
494 | assert stmt == "_ = 314 + foo" | |
|
487 | 495 | |
|
488 | 496 | |
|
489 | 497 | def test_run_magic_preserve_code_block(): |
@@ -501,13 +509,13 b' def test_dirops():' | |||
|
501 | 509 | ipdir = os.path.realpath(_ip.ipython_dir) |
|
502 | 510 | try: |
|
503 | 511 | _ip.magic('cd "%s"' % ipdir) |
|
504 |
|
|
|
512 | assert curpath() == ipdir | |
|
505 | 513 | _ip.magic('cd -') |
|
506 |
|
|
|
514 | assert curpath() == startdir | |
|
507 | 515 | _ip.magic('pushd "%s"' % ipdir) |
|
508 |
|
|
|
516 | assert curpath() == ipdir | |
|
509 | 517 | _ip.magic('popd') |
|
510 |
|
|
|
518 | assert curpath() == startdir | |
|
511 | 519 | finally: |
|
512 | 520 | os.chdir(startdir) |
|
513 | 521 | |
@@ -534,7 +542,7 b' def test_xmode():' | |||
|
534 | 542 | xmode = _ip.InteractiveTB.mode |
|
535 | 543 | for i in range(4): |
|
536 | 544 | _ip.magic("xmode") |
|
537 |
|
|
|
545 | assert _ip.InteractiveTB.mode == xmode | |
|
538 | 546 | |
|
539 | 547 | def test_reset_hard(): |
|
540 | 548 | monitor = [] |
@@ -547,9 +555,9 b' def test_reset_hard():' | |||
|
547 | 555 | _ip.user_ns["a"] = A() |
|
548 | 556 | _ip.run_cell("a") |
|
549 | 557 | |
|
550 |
|
|
|
558 | assert monitor == [] | |
|
551 | 559 | _ip.magic("reset -f") |
|
552 |
|
|
|
560 | assert monitor == [1] | |
|
553 | 561 | |
|
554 | 562 | class TestXdel(tt.TempFileMixin): |
|
555 | 563 | def test_xdel(self): |
@@ -566,12 +574,12 b' class TestXdel(tt.TempFileMixin):' | |||
|
566 | 574 | _ip.run_cell("a") |
|
567 | 575 | |
|
568 | 576 | monitor = _ip.user_ns["A"].monitor |
|
569 |
|
|
|
577 | assert monitor == [] | |
|
570 | 578 | |
|
571 | 579 | _ip.magic("xdel a") |
|
572 | 580 | |
|
573 | 581 | # Check that a's __del__ method has been called. |
|
574 |
|
|
|
582 | assert monitor == [1] | |
|
575 | 583 | |
|
576 | 584 | def doctest_who(): |
|
577 | 585 | """doctest for %who |
@@ -661,11 +669,12 b' def test_timeit_special_syntax():' | |||
|
661 | 669 | ip.user_ns['lmagic_out'] = line |
|
662 | 670 | |
|
663 | 671 | # line mode test |
|
664 |
_ip.run_line_magic( |
|
|
665 |
|
|
|
672 | _ip.run_line_magic("timeit", "-n1 -r1 %lmagic my line") | |
|
673 | assert _ip.user_ns["lmagic_out"] == "my line" | |
|
666 | 674 | # cell mode test |
|
667 |
_ip.run_cell_magic( |
|
|
668 |
|
|
|
675 | _ip.run_cell_magic("timeit", "-n1 -r1", "%lmagic my line2") | |
|
676 | assert _ip.user_ns["lmagic_out"] == "my line2" | |
|
677 | ||
|
669 | 678 | |
|
670 | 679 | def test_timeit_return(): |
|
671 | 680 | """ |
@@ -688,7 +697,7 b' def test_timeit_return_quiet():' | |||
|
688 | 697 | assert (res is not None) |
|
689 | 698 | |
|
690 | 699 | def test_timeit_invalid_return(): |
|
691 |
with |
|
|
700 | with pytest.raises(SyntaxError): | |
|
692 | 701 | _ip.run_line_magic('timeit', 'return') |
|
693 | 702 | |
|
694 | 703 | @dec.skipif(execution.profile is None) |
@@ -700,17 +709,19 b' def test_prun_special_syntax():' | |||
|
700 | 709 | ip.user_ns['lmagic_out'] = line |
|
701 | 710 | |
|
702 | 711 | # line mode test |
|
703 |
_ip.run_line_magic( |
|
|
704 |
|
|
|
712 | _ip.run_line_magic("prun", "-q %lmagic my line") | |
|
713 | assert _ip.user_ns["lmagic_out"] == "my line" | |
|
705 | 714 | # cell mode test |
|
706 |
_ip.run_cell_magic( |
|
|
707 |
|
|
|
715 | _ip.run_cell_magic("prun", "-q", "%lmagic my line2") | |
|
716 | assert _ip.user_ns["lmagic_out"] == "my line2" | |
|
717 | ||
|
708 | 718 | |
|
709 | 719 | @dec.skipif(execution.profile is None) |
|
710 | 720 | def test_prun_quotes(): |
|
711 | 721 | "Test that prun does not clobber string escapes (GH #1302)" |
|
712 | 722 | _ip.magic(r"prun -q x = '\t'") |
|
713 |
|
|
|
723 | assert _ip.user_ns["x"] == "\t" | |
|
724 | ||
|
714 | 725 | |
|
715 | 726 | def test_extension(): |
|
716 | 727 | # Debugging information for failures of this test |
@@ -719,14 +730,14 b' def test_extension():' | |||
|
719 | 730 | print(' ', p) |
|
720 | 731 | print('CWD', os.getcwd()) |
|
721 | 732 | |
|
722 |
|
|
|
733 | pytest.raises(ImportError, _ip.magic, "load_ext daft_extension") | |
|
723 | 734 | daft_path = os.path.join(os.path.dirname(__file__), "daft_extension") |
|
724 | 735 | sys.path.insert(0, daft_path) |
|
725 | 736 | try: |
|
726 | 737 | _ip.user_ns.pop('arq', None) |
|
727 | 738 | invalidate_caches() # Clear import caches |
|
728 | 739 | _ip.magic("load_ext daft_extension") |
|
729 |
|
|
|
740 | assert _ip.user_ns["arq"] == 185 | |
|
730 | 741 | _ip.magic("unload_ext daft_extension") |
|
731 | 742 | assert 'arq' not in _ip.user_ns |
|
732 | 743 | finally: |
@@ -736,7 +747,7 b' def test_extension():' | |||
|
736 | 747 | def test_notebook_export_json(): |
|
737 | 748 | _ip = get_ipython() |
|
738 | 749 | _ip.history_manager.reset() # Clear any existing history. |
|
739 |
cmds = [ |
|
|
750 | cmds = ["a=1", "def b():\n return a**2", "print('noΓ«l, Γ©tΓ©', b())"] | |
|
740 | 751 | for i, cmd in enumerate(cmds, start=1): |
|
741 | 752 | _ip.history_manager.store_inputs(i, cmd) |
|
742 | 753 | with TemporaryDirectory() as td: |
@@ -797,11 +808,11 b' class CellMagicTestCase(TestCase):' | |||
|
797 | 808 | |
|
798 | 809 | def check_ident(self, magic): |
|
799 | 810 | # Manually called, we get the result |
|
800 |
out = _ip.run_cell_magic(magic, |
|
|
801 | nt.assert_equal(out, ('a','b')) | |
|
811 | out = _ip.run_cell_magic(magic, "a", "b") | |
|
812 | assert out == ("a", "b") | |
|
802 | 813 | # Via run_cell, it goes into the user's namespace via displayhook |
|
803 |
_ip.run_cell( |
|
|
804 |
|
|
|
814 | _ip.run_cell("%%" + magic + " c\nd\n") | |
|
815 | assert _ip.user_ns["_"] == ("c", "d\n") | |
|
805 | 816 | |
|
806 | 817 | def test_cell_magic_func_deco(self): |
|
807 | 818 | "Cell magic using simple decorator" |
@@ -844,7 +855,7 b' class CellMagicTestCase(TestCase):' | |||
|
844 | 855 | self.check_ident('cellm4') |
|
845 | 856 | # Check that nothing is registered as 'cellm33' |
|
846 | 857 | c33 = _ip.find_cell_magic('cellm33') |
|
847 |
|
|
|
858 | assert c33 == None | |
|
848 | 859 | |
|
849 | 860 | def test_file(): |
|
850 | 861 | """Basic %%writefile""" |
@@ -856,8 +867,9 b' def test_file():' | |||
|
856 | 867 | 'line2', |
|
857 | 868 | ])) |
|
858 | 869 | s = Path(fname).read_text() |
|
859 |
|
|
|
860 |
|
|
|
870 | assert "line1\n" in s | |
|
871 | assert "line2" in s | |
|
872 | ||
|
861 | 873 | |
|
862 | 874 | @dec.skip_win32 |
|
863 | 875 | def test_file_single_quote(): |
@@ -870,8 +882,9 b' def test_file_single_quote():' | |||
|
870 | 882 | 'line2', |
|
871 | 883 | ])) |
|
872 | 884 | s = Path(fname).read_text() |
|
873 |
|
|
|
874 |
|
|
|
885 | assert "line1\n" in s | |
|
886 | assert "line2" in s | |
|
887 | ||
|
875 | 888 | |
|
876 | 889 | @dec.skip_win32 |
|
877 | 890 | def test_file_double_quote(): |
@@ -884,8 +897,9 b' def test_file_double_quote():' | |||
|
884 | 897 | 'line2', |
|
885 | 898 | ])) |
|
886 | 899 | s = Path(fname).read_text() |
|
887 |
|
|
|
888 |
|
|
|
900 | assert "line1\n" in s | |
|
901 | assert "line2" in s | |
|
902 | ||
|
889 | 903 | |
|
890 | 904 | def test_file_var_expand(): |
|
891 | 905 | """%%writefile $filename""" |
@@ -898,8 +912,9 b' def test_file_var_expand():' | |||
|
898 | 912 | 'line2', |
|
899 | 913 | ])) |
|
900 | 914 | s = Path(fname).read_text() |
|
901 |
|
|
|
902 |
|
|
|
915 | assert "line1\n" in s | |
|
916 | assert "line2" in s | |
|
917 | ||
|
903 | 918 | |
|
904 | 919 | def test_file_unicode(): |
|
905 | 920 | """%%writefile with unicode cell""" |
@@ -912,8 +927,9 b' def test_file_unicode():' | |||
|
912 | 927 | ])) |
|
913 | 928 | with io.open(fname, encoding='utf-8') as f: |
|
914 | 929 | s = f.read() |
|
915 |
|
|
|
916 |
|
|
|
930 | assert "linΓ©1\n" in s | |
|
931 | assert "linΓ©2" in s | |
|
932 | ||
|
917 | 933 | |
|
918 | 934 | def test_file_amend(): |
|
919 | 935 | """%%writefile -a amends files""" |
@@ -929,8 +945,9 b' def test_file_amend():' | |||
|
929 | 945 | 'line4', |
|
930 | 946 | ])) |
|
931 | 947 | s = Path(fname).read_text() |
|
932 |
|
|
|
933 |
|
|
|
948 | assert "line1\n" in s | |
|
949 | assert "line3\n" in s | |
|
950 | ||
|
934 | 951 | |
|
935 | 952 | def test_file_spaces(): |
|
936 | 953 | """%%file with spaces in filename""" |
@@ -942,14 +959,16 b' def test_file_spaces():' | |||
|
942 | 959 | 'line2', |
|
943 | 960 | ])) |
|
944 | 961 | s = Path(fname).read_text() |
|
945 |
|
|
|
946 |
|
|
|
962 | assert "line1\n" in s | |
|
963 | assert "line2" in s | |
|
964 | ||
|
947 | 965 | |
|
948 | 966 | def test_script_config(): |
|
949 | 967 | ip = get_ipython() |
|
950 | 968 | ip.config.ScriptMagics.script_magics = ['whoda'] |
|
951 | 969 | sm = script.ScriptMagics(shell=ip) |
|
952 |
|
|
|
970 | assert "whoda" in sm.magics["cell"] | |
|
971 | ||
|
953 | 972 | |
|
954 | 973 | @dec.skip_iptest_but_not_pytest |
|
955 | 974 | @dec.skip_win32 |
@@ -962,7 +981,8 b' def test_script_out():' | |||
|
962 | 981 | ip = get_ipython() |
|
963 | 982 | ip.run_cell_magic("script", "--out output sh", "echo 'hi'") |
|
964 | 983 | assert asyncio.get_event_loop().is_running() is False |
|
965 |
|
|
|
984 | assert ip.user_ns["output"] == "hi\n" | |
|
985 | ||
|
966 | 986 | |
|
967 | 987 | @dec.skip_iptest_but_not_pytest |
|
968 | 988 | @dec.skip_win32 |
@@ -974,7 +994,7 b' def test_script_err():' | |||
|
974 | 994 | assert asyncio.get_event_loop().is_running() is False |
|
975 | 995 | ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2") |
|
976 | 996 | assert asyncio.get_event_loop().is_running() is False |
|
977 |
|
|
|
997 | assert ip.user_ns["error"] == "hello\n" | |
|
978 | 998 | |
|
979 | 999 | |
|
980 | 1000 | @dec.skip_iptest_but_not_pytest |
@@ -988,8 +1008,8 b' def test_script_out_err():' | |||
|
988 | 1008 | ip.run_cell_magic( |
|
989 | 1009 | "script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2" |
|
990 | 1010 | ) |
|
991 |
|
|
|
992 |
|
|
|
1011 | assert ip.user_ns["output"] == "hi\n" | |
|
1012 | assert ip.user_ns["error"] == "hello\n" | |
|
993 | 1013 | |
|
994 | 1014 | |
|
995 | 1015 | @dec.skip_iptest_but_not_pytest |
@@ -1000,10 +1020,11 b' def test_script_out_err():' | |||
|
1000 | 1020 | async def test_script_bg_out(): |
|
1001 | 1021 | ip = get_ipython() |
|
1002 | 1022 | ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'") |
|
1003 |
|
|
|
1023 | assert (await ip.user_ns["output"].read()) == b"hi\n" | |
|
1004 | 1024 | ip.user_ns["output"].close() |
|
1005 | 1025 | asyncio.get_event_loop().stop() |
|
1006 | 1026 | |
|
1027 | ||
|
1007 | 1028 | @dec.skip_iptest_but_not_pytest |
|
1008 | 1029 | @dec.skip_win32 |
|
1009 | 1030 | @pytest.mark.skipif( |
@@ -1012,7 +1033,7 b' async def test_script_bg_out():' | |||
|
1012 | 1033 | async def test_script_bg_err(): |
|
1013 | 1034 | ip = get_ipython() |
|
1014 | 1035 | ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2") |
|
1015 |
|
|
|
1036 | assert (await ip.user_ns["error"].read()) == b"hello\n" | |
|
1016 | 1037 | ip.user_ns["error"].close() |
|
1017 | 1038 | |
|
1018 | 1039 | |
@@ -1026,8 +1047,8 b' async def test_script_bg_out_err():' | |||
|
1026 | 1047 | ip.run_cell_magic( |
|
1027 | 1048 | "script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2" |
|
1028 | 1049 | ) |
|
1029 |
|
|
|
1030 |
|
|
|
1050 | assert (await ip.user_ns["output"].read()) == b"hi\n" | |
|
1051 | assert (await ip.user_ns["error"].read()) == b"hello\n" | |
|
1031 | 1052 | ip.user_ns["output"].close() |
|
1032 | 1053 | ip.user_ns["error"].close() |
|
1033 | 1054 | |
@@ -1040,7 +1061,7 b' def test_script_defaults():' | |||
|
1040 | 1061 | except Exception: |
|
1041 | 1062 | pass |
|
1042 | 1063 | else: |
|
1043 |
|
|
|
1064 | assert cmd in ip.magics_manager.magics["cell"] | |
|
1044 | 1065 | |
|
1045 | 1066 | |
|
1046 | 1067 | @magics_class |
@@ -1060,19 +1081,20 b' def test_line_cell_info():' | |||
|
1060 | 1081 | """%%foo and %foo magics are distinguishable to inspect""" |
|
1061 | 1082 | ip = get_ipython() |
|
1062 | 1083 | ip.magics_manager.register(FooFoo) |
|
1063 |
oinfo = ip.object_inspect( |
|
|
1064 |
|
|
|
1065 |
|
|
|
1084 | oinfo = ip.object_inspect("foo") | |
|
1085 | assert oinfo["found"] is True | |
|
1086 | assert oinfo["ismagic"] is True | |
|
1087 | ||
|
1088 | oinfo = ip.object_inspect("%%foo") | |
|
1089 | assert oinfo["found"] is True | |
|
1090 | assert oinfo["ismagic"] is True | |
|
1091 | assert oinfo["docstring"] == FooFoo.cell_foo.__doc__ | |
|
1066 | 1092 | |
|
1067 |
oinfo = ip.object_inspect( |
|
|
1068 |
|
|
|
1069 |
|
|
|
1070 |
|
|
|
1093 | oinfo = ip.object_inspect("%foo") | |
|
1094 | assert oinfo["found"] is True | |
|
1095 | assert oinfo["ismagic"] is True | |
|
1096 | assert oinfo["docstring"] == FooFoo.line_foo.__doc__ | |
|
1071 | 1097 | |
|
1072 | oinfo = ip.object_inspect('%foo') | |
|
1073 | nt.assert_true(oinfo['found']) | |
|
1074 | nt.assert_true(oinfo['ismagic']) | |
|
1075 | nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__) | |
|
1076 | 1098 | |
|
1077 | 1099 | def test_multiple_magics(): |
|
1078 | 1100 | ip = get_ipython() |
@@ -1080,9 +1102,10 b' def test_multiple_magics():' | |||
|
1080 | 1102 | foo2 = FooFoo(ip) |
|
1081 | 1103 | mm = ip.magics_manager |
|
1082 | 1104 | mm.register(foo1) |
|
1083 |
|
|
|
1105 | assert mm.magics["line"]["foo"].__self__ is foo1 | |
|
1084 | 1106 | mm.register(foo2) |
|
1085 |
|
|
|
1107 | assert mm.magics["line"]["foo"].__self__ is foo2 | |
|
1108 | ||
|
1086 | 1109 | |
|
1087 | 1110 | def test_alias_magic(): |
|
1088 | 1111 | """Test %alias_magic.""" |
@@ -1090,48 +1113,49 b' def test_alias_magic():' | |||
|
1090 | 1113 | mm = ip.magics_manager |
|
1091 | 1114 | |
|
1092 | 1115 | # Basic operation: both cell and line magics are created, if possible. |
|
1093 |
ip.run_line_magic( |
|
|
1094 |
|
|
|
1095 |
|
|
|
1116 | ip.run_line_magic("alias_magic", "timeit_alias timeit") | |
|
1117 | assert "timeit_alias" in mm.magics["line"] | |
|
1118 | assert "timeit_alias" in mm.magics["cell"] | |
|
1096 | 1119 | |
|
1097 | 1120 | # --cell is specified, line magic not created. |
|
1098 |
ip.run_line_magic( |
|
|
1099 |
|
|
|
1100 |
|
|
|
1121 | ip.run_line_magic("alias_magic", "--cell timeit_cell_alias timeit") | |
|
1122 | assert "timeit_cell_alias" not in mm.magics["line"] | |
|
1123 | assert "timeit_cell_alias" in mm.magics["cell"] | |
|
1101 | 1124 | |
|
1102 | 1125 | # Test that line alias is created successfully. |
|
1103 |
ip.run_line_magic( |
|
|
1104 | nt.assert_equal(ip.run_line_magic('env', ''), | |
|
1105 | ip.run_line_magic('env_alias', '')) | |
|
1126 | ip.run_line_magic("alias_magic", "--line env_alias env") | |
|
1127 | assert ip.run_line_magic("env", "") == ip.run_line_magic("env_alias", "") | |
|
1106 | 1128 | |
|
1107 | 1129 | # Test that line alias with parameters passed in is created successfully. |
|
1108 | ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3')) | |
|
1109 | nt.assert_in('history_alias', mm.magics['line']) | |
|
1130 | ip.run_line_magic( | |
|
1131 | "alias_magic", "--line history_alias history --params " + shlex.quote("3") | |
|
1132 | ) | |
|
1133 | assert "history_alias" in mm.magics["line"] | |
|
1110 | 1134 | |
|
1111 | 1135 | |
|
1112 | 1136 | def test_save(): |
|
1113 | 1137 | """Test %save.""" |
|
1114 | 1138 | ip = get_ipython() |
|
1115 | 1139 | ip.history_manager.reset() # Clear any existing history. |
|
1116 |
cmds = [ |
|
|
1140 | cmds = ["a=1", "def b():\n return a**2", "print(a, b())"] | |
|
1117 | 1141 | for i, cmd in enumerate(cmds, start=1): |
|
1118 | 1142 | ip.history_manager.store_inputs(i, cmd) |
|
1119 | 1143 | with TemporaryDirectory() as tmpdir: |
|
1120 | 1144 | file = os.path.join(tmpdir, "testsave.py") |
|
1121 | 1145 | ip.run_line_magic("save", "%s 1-10" % file) |
|
1122 | 1146 | content = Path(file).read_text() |
|
1123 |
|
|
|
1124 |
|
|
|
1147 | assert content.count(cmds[0]) == 1 | |
|
1148 | assert "coding: utf-8" in content | |
|
1125 | 1149 | ip.run_line_magic("save", "-a %s 1-10" % file) |
|
1126 | 1150 | content = Path(file).read_text() |
|
1127 |
|
|
|
1128 |
|
|
|
1151 | assert content.count(cmds[0]) == 2 | |
|
1152 | assert "coding: utf-8" in content | |
|
1129 | 1153 | |
|
1130 | 1154 | |
|
1131 | 1155 | def test_save_with_no_args(): |
|
1132 | 1156 | ip = get_ipython() |
|
1133 | 1157 | ip.history_manager.reset() # Clear any existing history. |
|
1134 |
cmds = [ |
|
|
1158 | cmds = ["a=1", "def b():\n return a**2", "print(a, b())", "%save"] | |
|
1135 | 1159 | for i, cmd in enumerate(cmds, start=1): |
|
1136 | 1160 | ip.history_manager.store_inputs(i, cmd) |
|
1137 | 1161 | |
@@ -1148,7 +1172,7 b' def test_save_with_no_args():' | |||
|
1148 | 1172 | print(a, b()) |
|
1149 | 1173 | """ |
|
1150 | 1174 | ) |
|
1151 |
|
|
|
1175 | assert content == expected_content | |
|
1152 | 1176 | |
|
1153 | 1177 | |
|
1154 | 1178 | def test_store(): |
@@ -1157,17 +1181,17 b' def test_store():' | |||
|
1157 | 1181 | ip.run_line_magic('load_ext', 'storemagic') |
|
1158 | 1182 | |
|
1159 | 1183 | # make sure the storage is empty |
|
1160 |
ip.run_line_magic( |
|
|
1161 |
ip.user_ns[ |
|
|
1162 |
ip.run_line_magic( |
|
|
1163 |
ip.user_ns[ |
|
|
1164 |
ip.run_line_magic( |
|
|
1165 |
|
|
|
1184 | ip.run_line_magic("store", "-z") | |
|
1185 | ip.user_ns["var"] = 42 | |
|
1186 | ip.run_line_magic("store", "var") | |
|
1187 | ip.user_ns["var"] = 39 | |
|
1188 | ip.run_line_magic("store", "-r") | |
|
1189 | assert ip.user_ns["var"] == 42 | |
|
1166 | 1190 | |
|
1167 |
ip.run_line_magic( |
|
|
1168 |
ip.user_ns[ |
|
|
1169 |
ip.run_line_magic( |
|
|
1170 |
|
|
|
1191 | ip.run_line_magic("store", "-d var") | |
|
1192 | ip.user_ns["var"] = 39 | |
|
1193 | ip.run_line_magic("store", "-r") | |
|
1194 | assert ip.user_ns["var"] == 39 | |
|
1171 | 1195 | |
|
1172 | 1196 | |
|
1173 | 1197 | def _run_edit_test(arg_s, exp_filename=None, |
@@ -1181,27 +1205,27 b' def _run_edit_test(arg_s, exp_filename=None,' | |||
|
1181 | 1205 | filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call) |
|
1182 | 1206 | |
|
1183 | 1207 | if exp_filename is not None: |
|
1184 |
|
|
|
1208 | assert exp_filename == filename | |
|
1185 | 1209 | if exp_contents is not None: |
|
1186 | 1210 | with io.open(filename, 'r', encoding='utf-8') as f: |
|
1187 | 1211 | contents = f.read() |
|
1188 |
|
|
|
1212 | assert exp_contents == contents | |
|
1189 | 1213 | if exp_lineno != -1: |
|
1190 |
|
|
|
1214 | assert exp_lineno == lineno | |
|
1191 | 1215 | if exp_is_temp is not None: |
|
1192 |
|
|
|
1216 | assert exp_is_temp == is_temp | |
|
1193 | 1217 | |
|
1194 | 1218 | |
|
1195 | 1219 | def test_edit_interactive(): |
|
1196 | 1220 | """%edit on interactively defined objects""" |
|
1197 | 1221 | ip = get_ipython() |
|
1198 | 1222 | n = ip.execution_count |
|
1199 |
ip.run_cell( |
|
|
1223 | ip.run_cell("def foo(): return 1", store_history=True) | |
|
1200 | 1224 | |
|
1201 | 1225 | try: |
|
1202 | 1226 | _run_edit_test("foo") |
|
1203 | 1227 | except code.InteractivelyDefined as e: |
|
1204 |
|
|
|
1228 | assert e.index == n | |
|
1205 | 1229 | else: |
|
1206 | 1230 | raise AssertionError("Should have raised InteractivelyDefined") |
|
1207 | 1231 | |
@@ -1210,7 +1234,7 b' def test_edit_cell():' | |||
|
1210 | 1234 | """%edit [cell id]""" |
|
1211 | 1235 | ip = get_ipython() |
|
1212 | 1236 | |
|
1213 |
ip.run_cell( |
|
|
1237 | ip.run_cell("def foo(): return 1", store_history=True) | |
|
1214 | 1238 | |
|
1215 | 1239 | # test |
|
1216 | 1240 | _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True) |
@@ -1234,17 +1258,18 b' def test_ls_magic():' | |||
|
1234 | 1258 | lsmagic = ip.magic('lsmagic') |
|
1235 | 1259 | with warnings.catch_warnings(record=True) as w: |
|
1236 | 1260 | j = json_formatter(lsmagic) |
|
1237 |
|
|
|
1238 |
|
|
|
1261 | assert sorted(j) == ["cell", "line"] | |
|
1262 | assert w == [] # no warnings | |
|
1263 | ||
|
1239 | 1264 | |
|
1240 | 1265 | def test_strip_initial_indent(): |
|
1241 | 1266 | def sii(s): |
|
1242 | 1267 | lines = s.splitlines() |
|
1243 | 1268 | return '\n'.join(code.strip_initial_indent(lines)) |
|
1244 | 1269 | |
|
1245 |
|
|
|
1246 |
|
|
|
1247 |
|
|
|
1270 | assert sii(" a = 1\nb = 2") == "a = 1\nb = 2" | |
|
1271 | assert sii(" a\n b\nc") == "a\n b\nc" | |
|
1272 | assert sii("a\n b") == "a\n b" | |
|
1248 | 1273 | |
|
1249 | 1274 | def test_logging_magic_quiet_from_arg(): |
|
1250 | 1275 | _ip.config.LoggingMagics.quiet = False |
@@ -1336,6 +1361,6 b' def test_run_module_from_import_hook():' | |||
|
1336 | 1361 | _ip.run_cell("import my_tmp") |
|
1337 | 1362 | |
|
1338 | 1363 | output = "Loaded my_tmp\nI just ran a script\nLoaded my_tmp\n" |
|
1339 |
|
|
|
1364 | assert output == captured.stdout | |
|
1340 | 1365 | |
|
1341 | 1366 | sys.meta_path.pop(0) |
General Comments 0
You need to be logged in to leave comments.
Login now