From 7fe1d67ad5da8b93a55cbfdaed3c9baffc5697e6 2012-06-10 23:43:54 From: MinRK Date: 2012-06-10 23:43:54 Subject: [PATCH] add basic tests for %%file, %%script --- diff --git a/IPython/core/magics/script.py b/IPython/core/magics/script.py index 496251d..907352b 100644 --- a/IPython/core/magics/script.py +++ b/IPython/core/magics/script.py @@ -190,7 +190,7 @@ class ScriptMagics(Magics, Configurable): if args.out: self.shell.user_ns[args.out] = p.stdout if args.err: - self.shell.user_ns[args.out] = p.stderr + self.shell.user_ns[args.err] = p.stderr self.job_manager.new(self._run_script, p, cell) return diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 09d82b1..f22141e 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -28,13 +28,14 @@ from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, line_cell_magic, register_line_magic, register_cell_magic, register_line_cell_magic) -from IPython.core.magics import execution +from IPython.core.magics import execution, script from IPython.nbformat.v3.tests.nbexamples import nb0 from IPython.nbformat import current from IPython.testing import decorators as dec from IPython.testing import tools as tt from IPython.utils import py3compat from IPython.utils.tempdir import TemporaryDirectory +from IPython.utils.process import find_cmd #----------------------------------------------------------------------------- # Test functions begin @@ -560,4 +561,104 @@ class CellMagicTestCase(TestCase): # Check that nothing is registered as 'cellm33' c33 = _ip.find_cell_magic('cellm33') nt.assert_equals(c33, None) + +def test_file(): + """Basic %%file""" + ip = get_ipython() + with TemporaryDirectory() as td: + fname = os.path.join(td, 'file1') + ip.run_cell_magic("file", fname, u'\n'.join([ + 'line1', + 'line2', + ])) + with open(fname) as f: + s = f.read() + nt.assert_in('line1\n', s) + nt.assert_in('line2', s) + +def test_file_unicode(): + """%%file with unicode cell""" + ip = get_ipython() + with TemporaryDirectory() as td: + fname = os.path.join(td, 'file1') + ip.run_cell_magic("file", fname, u'\n'.join([ + u'liné1', + u'liné2', + ])) + with io.open(fname, encoding='utf-8') as f: + s = f.read() + nt.assert_in(u'liné1\n', s) + nt.assert_in(u'liné2', s) + +def test_file_amend(): + """%%file -a amends files""" + ip = get_ipython() + with TemporaryDirectory() as td: + fname = os.path.join(td, 'file2') + ip.run_cell_magic("file", fname, u'\n'.join([ + 'line1', + 'line2', + ])) + ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([ + 'line3', + 'line4', + ])) + with open(fname) as f: + s = f.read() + nt.assert_in('line1\n', s) + nt.assert_in('line3\n', s) + +def test_script_config(): + ip = get_ipython() + ip.config.ScriptMagics.script_magics = ['whoda'] + sm = script.ScriptMagics(shell=ip) + nt.assert_in('whoda', sm.magics['cell']) + +@dec.skip_win32 +def test_script_out(): + ip = get_ipython() + ip.run_cell_magic("script", "--out output sh", "echo 'hi'") + nt.assert_equals(ip.user_ns['output'], 'hi\n') + +@dec.skip_win32 +def test_script_err(): + ip = get_ipython() + ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2") + nt.assert_equals(ip.user_ns['error'], 'hello\n') + +@dec.skip_win32 +def test_script_out_err(): + ip = get_ipython() + ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2") + nt.assert_equals(ip.user_ns['output'], 'hi\n') + nt.assert_equals(ip.user_ns['error'], 'hello\n') + +@dec.skip_win32 +def test_script_bg_out(): + ip = get_ipython() + ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'") + nt.assert_equals(ip.user_ns['output'].read(), 'hi\n') + +@dec.skip_win32 +def test_script_bg_err(): + ip = get_ipython() + ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2") + nt.assert_equals(ip.user_ns['error'].read(), 'hello\n') + +@dec.skip_win32 +def test_script_bg_out_err(): + ip = get_ipython() + ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2") + nt.assert_equals(ip.user_ns['output'].read(), 'hi\n') + nt.assert_equals(ip.user_ns['error'].read(), 'hello\n') + +def test_script_defaults(): + ip = get_ipython() + for cmd in ['sh', 'bash', 'perl', 'ruby']: + try: + find_cmd(cmd) + except Exception: + pass + else: + nt.assert_in(cmd, ip.magics_manager.magics['cell'])