##// END OF EJS Templates
add basic tests for %%file, %%script
MinRK -
Show More
@@ -190,7 +190,7 b' class ScriptMagics(Magics, Configurable):'
190 if args.out:
190 if args.out:
191 self.shell.user_ns[args.out] = p.stdout
191 self.shell.user_ns[args.out] = p.stdout
192 if args.err:
192 if args.err:
193 self.shell.user_ns[args.out] = p.stderr
193 self.shell.user_ns[args.err] = p.stderr
194 self.job_manager.new(self._run_script, p, cell)
194 self.job_manager.new(self._run_script, p, cell)
195 return
195 return
196
196
@@ -28,13 +28,14 b' from IPython.core.magic import (Magics, magics_class, line_magic,'
28 cell_magic, line_cell_magic,
28 cell_magic, line_cell_magic,
29 register_line_magic, register_cell_magic,
29 register_line_magic, register_cell_magic,
30 register_line_cell_magic)
30 register_line_cell_magic)
31 from IPython.core.magics import execution
31 from IPython.core.magics import execution, script
32 from IPython.nbformat.v3.tests.nbexamples import nb0
32 from IPython.nbformat.v3.tests.nbexamples import nb0
33 from IPython.nbformat import current
33 from IPython.nbformat import current
34 from IPython.testing import decorators as dec
34 from IPython.testing import decorators as dec
35 from IPython.testing import tools as tt
35 from IPython.testing import tools as tt
36 from IPython.utils import py3compat
36 from IPython.utils import py3compat
37 from IPython.utils.tempdir import TemporaryDirectory
37 from IPython.utils.tempdir import TemporaryDirectory
38 from IPython.utils.process import find_cmd
38
39
39 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
40 # Test functions begin
41 # Test functions begin
@@ -560,4 +561,104 b' class CellMagicTestCase(TestCase):'
560 # Check that nothing is registered as 'cellm33'
561 # Check that nothing is registered as 'cellm33'
561 c33 = _ip.find_cell_magic('cellm33')
562 c33 = _ip.find_cell_magic('cellm33')
562 nt.assert_equals(c33, None)
563 nt.assert_equals(c33, None)
564
565 def test_file():
566 """Basic %%file"""
567 ip = get_ipython()
568 with TemporaryDirectory() as td:
569 fname = os.path.join(td, 'file1')
570 ip.run_cell_magic("file", fname, u'\n'.join([
571 'line1',
572 'line2',
573 ]))
574 with open(fname) as f:
575 s = f.read()
576 nt.assert_in('line1\n', s)
577 nt.assert_in('line2', s)
578
579 def test_file_unicode():
580 """%%file with unicode cell"""
581 ip = get_ipython()
582 with TemporaryDirectory() as td:
583 fname = os.path.join(td, 'file1')
584 ip.run_cell_magic("file", fname, u'\n'.join([
585 u'linΓ©1',
586 u'linΓ©2',
587 ]))
588 with io.open(fname, encoding='utf-8') as f:
589 s = f.read()
590 nt.assert_in(u'linΓ©1\n', s)
591 nt.assert_in(u'linΓ©2', s)
592
593 def test_file_amend():
594 """%%file -a amends files"""
595 ip = get_ipython()
596 with TemporaryDirectory() as td:
597 fname = os.path.join(td, 'file2')
598 ip.run_cell_magic("file", fname, u'\n'.join([
599 'line1',
600 'line2',
601 ]))
602 ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([
603 'line3',
604 'line4',
605 ]))
606 with open(fname) as f:
607 s = f.read()
608 nt.assert_in('line1\n', s)
609 nt.assert_in('line3\n', s)
610
563
611
612 def test_script_config():
613 ip = get_ipython()
614 ip.config.ScriptMagics.script_magics = ['whoda']
615 sm = script.ScriptMagics(shell=ip)
616 nt.assert_in('whoda', sm.magics['cell'])
617
618 @dec.skip_win32
619 def test_script_out():
620 ip = get_ipython()
621 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
622 nt.assert_equals(ip.user_ns['output'], 'hi\n')
623
624 @dec.skip_win32
625 def test_script_err():
626 ip = get_ipython()
627 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
628 nt.assert_equals(ip.user_ns['error'], 'hello\n')
629
630 @dec.skip_win32
631 def test_script_out_err():
632 ip = get_ipython()
633 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
634 nt.assert_equals(ip.user_ns['output'], 'hi\n')
635 nt.assert_equals(ip.user_ns['error'], 'hello\n')
636
637 @dec.skip_win32
638 def test_script_bg_out():
639 ip = get_ipython()
640 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
641 nt.assert_equals(ip.user_ns['output'].read(), 'hi\n')
642
643 @dec.skip_win32
644 def test_script_bg_err():
645 ip = get_ipython()
646 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
647 nt.assert_equals(ip.user_ns['error'].read(), 'hello\n')
648
649 @dec.skip_win32
650 def test_script_bg_out_err():
651 ip = get_ipython()
652 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
653 nt.assert_equals(ip.user_ns['output'].read(), 'hi\n')
654 nt.assert_equals(ip.user_ns['error'].read(), 'hello\n')
655
656 def test_script_defaults():
657 ip = get_ipython()
658 for cmd in ['sh', 'bash', 'perl', 'ruby']:
659 try:
660 find_cmd(cmd)
661 except Exception:
662 pass
663 else:
664 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
General Comments 0
You need to be logged in to leave comments. Login now