From 0ef44790bde586cba3d8fdfb902762e5244fc221 2015-04-03 17:56:52 From: Min RK Date: 2015-04-03 17:56:52 Subject: [PATCH] remove testing.tools.monkeypatch use mock.patch.object instead part 1 of reducing dependence on testing.tools --- diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index 2316a3d..267a8f3 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -23,6 +23,11 @@ import tempfile import textwrap import unittest +try: + from unittest.mock import patch +except ImportError: + from mock import patch + import nose.tools as nt from nose import SkipTest @@ -448,7 +453,7 @@ class TestMagicRunWithPackage(unittest.TestCase): def with_fake_debugger(func): @functools.wraps(func) def wrapper(*args, **kwds): - with tt.monkeypatch(debugger.Pdb, 'run', staticmethod(eval)): + with patch.object(debugger.Pdb, 'run', staticmethod(eval)): return func(*args, **kwds) return wrapper diff --git a/IPython/html/widgets/tests/test_interaction.py b/IPython/html/widgets/tests/test_interaction.py index d209fd3..999c5a3 100644 --- a/IPython/html/widgets/tests/test_interaction.py +++ b/IPython/html/widgets/tests/test_interaction.py @@ -5,10 +5,12 @@ from __future__ import print_function -from collections import OrderedDict +try: + from unittest.mock import patch +except ImportError: + from mock import patch import nose.tools as nt -import IPython.testing.tools as tt from IPython.kernel.comm import Comm from IPython.html import widgets @@ -354,7 +356,7 @@ def test_priority(): @nt.with_setup(clear_display) def test_decorator_kwarg(): - with tt.monkeypatch(interaction, 'display', record_display): + with patch.object(interaction, 'display', record_display): @interact(a=5) def foo(a): pass @@ -373,7 +375,7 @@ def test_interact_instancemethod(): f = Foo() - with tt.monkeypatch(interaction, 'display', record_display): + with patch.object(interaction, 'display', record_display): g = interact(f.show, x=(1,10)) nt.assert_equal(len(displayed), 1) w = displayed[0].children[0] @@ -384,7 +386,7 @@ def test_interact_instancemethod(): @nt.with_setup(clear_display) def test_decorator_no_call(): - with tt.monkeypatch(interaction, 'display', record_display): + with patch.object(interaction, 'display', record_display): @interact def foo(a='default'): pass @@ -399,7 +401,7 @@ def test_decorator_no_call(): def test_call_interact(): def foo(a='default'): pass - with tt.monkeypatch(interaction, 'display', record_display): + with patch.object(interaction, 'display', record_display): ifoo = interact(foo) nt.assert_equal(len(displayed), 1) w = displayed[0].children[0] @@ -412,7 +414,7 @@ def test_call_interact(): def test_call_interact_kwargs(): def foo(a='default'): pass - with tt.monkeypatch(interaction, 'display', record_display): + with patch.object(interaction, 'display', record_display): ifoo = interact(foo, a=10) nt.assert_equal(len(displayed), 1) w = displayed[0].children[0] @@ -425,7 +427,7 @@ def test_call_interact_kwargs(): def test_call_decorated_on_trait_change(): """test calling @interact decorated functions""" d = {} - with tt.monkeypatch(interaction, 'display', record_display): + with patch.object(interaction, 'display', record_display): @interact def foo(a='default'): d['a'] = a @@ -449,7 +451,7 @@ def test_call_decorated_on_trait_change(): def test_call_decorated_kwargs_on_trait_change(): """test calling @interact(foo=bar) decorated functions""" d = {} - with tt.monkeypatch(interaction, 'display', record_display): + with patch.object(interaction, 'display', record_display): @interact(a='kwarg') def foo(a='default'): d['a'] = a diff --git a/IPython/lib/tests/test_latextools.py b/IPython/lib/tests/test_latextools.py index 5f4cd27..418af98 100644 --- a/IPython/lib/tests/test_latextools.py +++ b/IPython/lib/tests/test_latextools.py @@ -1,18 +1,18 @@ # encoding: utf-8 """Tests for IPython.utils.path.py""" -#----------------------------------------------------------------------------- -# Copyright (C) 2008-2011 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. + +try: + from unittest.mock import patch +except ImportError: + from mock import patch import nose.tools as nt from IPython.lib import latextools from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib -from IPython.testing.tools import monkeypatch from IPython.utils.process import FindCmdError @@ -29,7 +29,7 @@ def check_latex_to_png_dvipng_fails_when_no_cmd(command): if arg == command: raise FindCmdError - with monkeypatch(latextools, "find_cmd", mock_find_cmd): + with patch.object(latextools, "find_cmd", mock_find_cmd): nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True), None) @@ -46,7 +46,7 @@ def test_latex_to_png_dvipng_runs(): for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]: yield (latextools.latex_to_png_dvipng, s, wrap) - with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): + with patch.object(latextools, "kpsewhich", mock_kpsewhich): yield (latextools.latex_to_png_dvipng, s, wrap) @skipif_not_matplotlib @@ -61,7 +61,7 @@ def test_latex_to_png_mpl_runs(): for (s, wrap) in [("$x^2$", False), ("x^2", True)]: yield (latextools.latex_to_png_mpl, s, wrap) - with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): + with patch.object(latextools, "kpsewhich", mock_kpsewhich): yield (latextools.latex_to_png_mpl, s, wrap) @skipif_not_matplotlib @@ -78,7 +78,7 @@ def test_genelatex_no_wrap(): assert False, ("kpsewhich should not be called " "(called with {0})".format(filename)) - with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): + with patch.object(latextools, "kpsewhich", mock_kpsewhich): nt.assert_equals( '\n'.join(latextools.genelatex("body text", False)), r'''\documentclass{article} @@ -100,7 +100,7 @@ def test_genelatex_wrap_with_breqn(): nt.assert_equals(filename, "breqn.sty") return "path/to/breqn.sty" - with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): + with patch.object(latextools, "kpsewhich", mock_kpsewhich): nt.assert_equals( '\n'.join(latextools.genelatex("x^2", True)), r'''\documentclass{article} @@ -125,7 +125,7 @@ def test_genelatex_wrap_without_breqn(): nt.assert_equals(filename, "breqn.sty") return None - with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): + with patch.object(latextools, "kpsewhich", mock_kpsewhich): nt.assert_equals( '\n'.join(latextools.genelatex("x^2", True)), r'''\documentclass{article} diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index b92489c..84f200a 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -444,17 +444,6 @@ def make_tempfile(name): os.unlink(name) -@contextmanager -def monkeypatch(obj, name, attr): - """ - Context manager to replace attribute named `name` in `obj` with `attr`. - """ - orig = getattr(obj, name) - setattr(obj, name, attr) - yield - setattr(obj, name, orig) - - def help_output_test(subcommand=''): """test that `ipython [subcommand] -h` works""" cmd = get_ipython_cmd() + [subcommand, '-h'] diff --git a/jupyter_console/tests/test_image_handler.py b/jupyter_console/tests/test_image_handler.py index 11e3852..f91de61 100644 --- a/jupyter_console/tests/test_image_handler.py +++ b/jupyter_console/tests/test_image_handler.py @@ -1,15 +1,16 @@ -#----------------------------------------------------------------------------- -# Copyright (C) 2012 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. import os import sys import unittest import base64 +try: + from unittest.mock import patch +except ImportError: + from mock import patch + from IPython.kernel import KernelClient from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell from IPython.utils.tempdir import TemporaryDirectory @@ -54,7 +55,7 @@ class ZMQTerminalInteractiveShellTestCase(unittest.TestCase): open_called_with.append(arg) return Struct(show=lambda: show_called_with.append(None)) - with monkeypatch(PIL.Image, 'open', fake_open): + with patch.object(PIL.Image, 'open', fake_open): self.shell.handle_image_PIL(self.data, self.mime) self.assertEqual(len(open_called_with), 1)