5 new commits in pytest:
https://bitbucket.org/hpk42/pytest/changeset/cdf022434243/ changeset: cdf022434243 user: schmir date: 2012-01-04 12:40:57 summary: test that a second undo doesn't change sys.path also use a 'mp' funcarg that restores sys.path and the current working directory in preparation for the monkeypatch.chdir method. affected #: 1 file diff -r e3065c26d6f5757e9516f8fcd69bb07844612c56 -r cdf02243424336364da819db2a2239f373be1ca9 testing/test_monkeypatch.py --- a/testing/test_monkeypatch.py +++ b/testing/test_monkeypatch.py @@ -2,6 +2,17 @@ import pytest from _pytest.monkeypatch import monkeypatch as MonkeyPatch +def pytest_funcarg__mp(request): + cwd = os.getcwd() + sys_path = list(sys.path) + + def cleanup(): + sys.path[:] = sys_path + os.chdir(cwd) + + request.addfinalizer(cleanup) + return MonkeyPatch() + def test_setattr(): class A: x = 1 @@ -144,19 +155,20 @@ res = reprec.countoutcomes() assert tuple(res) == (1, 0, 0), res -def test_syspath_prepend(): +def test_syspath_prepend(mp): old = list(sys.path) - try: - monkeypatch = MonkeyPatch() - monkeypatch.syspath_prepend('world') - monkeypatch.syspath_prepend('hello') - assert sys.path[0] == "hello" - assert sys.path[1] == "world" - monkeypatch.undo() - assert sys.path == old - monkeypatch.undo() - assert sys.path == old - finally: - sys.path[:] = old + mp.syspath_prepend('world') + mp.syspath_prepend('hello') + assert sys.path[0] == "hello" + assert sys.path[1] == "world" + mp.undo() + assert sys.path == old + mp.undo() + assert sys.path == old - +def test_syspath_prepend_double_undo(mp): + mp.syspath_prepend('hello world') + mp.undo() + sys.path.append('more hello world') + mp.undo() + assert sys.path[-1] == 'more hello world' https://bitbucket.org/hpk42/pytest/changeset/2e1969572830/ changeset: 2e1969572830 user: schmir date: 2012-01-04 12:42:23 summary: make sure calling undo a second time doesn't change sys.path affected #: 1 file diff -r cdf02243424336364da819db2a2239f373be1ca9 -r 2e1969572830eefd87c486a6a6a7b91702aa6bfa _pytest/monkeypatch.py --- a/_pytest/monkeypatch.py +++ b/_pytest/monkeypatch.py @@ -104,3 +104,4 @@ self._setitem[:] = [] if hasattr(self, '_savesyspath'): sys.path[:] = self._savesyspath + del self._savesyspath https://bitbucket.org/hpk42/pytest/changeset/03972d3a10e6/ changeset: 03972d3a10e6 user: schmir date: 2012-01-04 12:43:19 summary: add monkeypatch.chdir method affected #: 2 files diff -r 2e1969572830eefd87c486a6a6a7b91702aa6bfa -r 03972d3a10e6234ac72823fbe1a3b1c8240ca769 _pytest/monkeypatch.py --- a/_pytest/monkeypatch.py +++ b/_pytest/monkeypatch.py @@ -30,6 +30,7 @@ def __init__(self): self._setattr = [] self._setitem = [] + self._cwd = None def setattr(self, obj, name, value, raising=True): """ set attribute ``name`` on ``obj`` to ``value``, by default @@ -83,6 +84,14 @@ self._savesyspath = sys.path[:] sys.path.insert(0, str(path)) + def chdir(self, path): + if self._cwd is None: + self._cwd = os.getcwd() + if hasattr(path, "chdir"): + path.chdir() + else: + os.chdir(path) + def undo(self): """ undo previous changes. This call consumes the undo stack. Calling it a second time has no effect unless @@ -105,3 +114,7 @@ if hasattr(self, '_savesyspath'): sys.path[:] = self._savesyspath del self._savesyspath + + if self._cwd is not None: + os.chdir(self._cwd) + self._cwd = None diff -r 2e1969572830eefd87c486a6a6a7b91702aa6bfa -r 03972d3a10e6234ac72823fbe1a3b1c8240ca769 testing/test_monkeypatch.py --- a/testing/test_monkeypatch.py +++ b/testing/test_monkeypatch.py @@ -172,3 +172,24 @@ sys.path.append('more hello world') mp.undo() assert sys.path[-1] == 'more hello world' + +def test_chdir_with_path_local(mp, tmpdir): + mp.chdir(tmpdir) + assert os.getcwd() == tmpdir.strpath + +def test_chdir_with_str(mp, tmpdir): + mp.chdir(tmpdir.strpath) + assert os.getcwd() == tmpdir.strpath + +def test_chdir_undo(mp, tmpdir): + cwd = os.getcwd() + mp.chdir(tmpdir) + mp.undo() + assert os.getcwd() == cwd + +def test_chdir_double_undo(mp, tmpdir): + mp.chdir(tmpdir.strpath) + mp.undo() + tmpdir.chdir() + mp.undo() + assert os.getcwd() == tmpdir.strpath https://bitbucket.org/hpk42/pytest/changeset/a6fd5823d699/ changeset: a6fd5823d699 user: schmir date: 2012-01-06 15:25:57 summary: update documentation for the new monkeypatch.chdir method affected #: 2 files diff -r 03972d3a10e6234ac72823fbe1a3b1c8240ca769 -r a6fd5823d699b65eb75db11f69ca47cdac0fff3f CHANGELOG --- a/CHANGELOG +++ b/CHANGELOG @@ -9,6 +9,7 @@ test directory was renamed and some pyc/__pycache__ remain - fix issue106: allow parametrize to be applied multiple times e.g. from module, class and at function level. +- add chdir method to monkeypatch funcarg Changes between 2.2.0 and 2.2.1 ---------------------------------------- diff -r 03972d3a10e6234ac72823fbe1a3b1c8240ca769 -r a6fd5823d699b65eb75db11f69ca47cdac0fff3f _pytest/monkeypatch.py --- a/_pytest/monkeypatch.py +++ b/_pytest/monkeypatch.py @@ -13,6 +13,7 @@ monkeypatch.setenv(name, value, prepend=False) monkeypatch.delenv(name, value, raising=True) monkeypatch.syspath_prepend(path) + monkeypatch.chdir(path) All modifications will be undone after the requesting test function has finished. The ``raising`` @@ -85,6 +86,9 @@ sys.path.insert(0, str(path)) def chdir(self, path): + """ change the current working directory to the specified path + path can be a string or a py.path.local object + """ if self._cwd is None: self._cwd = os.getcwd() if hasattr(path, "chdir"): https://bitbucket.org/hpk42/pytest/changeset/bbf6380d2776/ changeset: bbf6380d2776 user: hpk42 date: 2012-01-06 21:37:18 summary: bump version, mention "mp" also in the docs and changelog affected #: 4 files diff -r a6fd5823d699b65eb75db11f69ca47cdac0fff3f -r bbf6380d27762cbaaf38f09d41ac214686c1a606 CHANGELOG --- a/CHANGELOG +++ b/CHANGELOG @@ -10,6 +10,8 @@ - fix issue106: allow parametrize to be applied multiple times e.g. from module, class and at function level. - add chdir method to monkeypatch funcarg +- add "mp" funcargs as a shortcut for "monkeypatch" +- fix crash resulting from calling monkeypatch undo a second time Changes between 2.2.0 and 2.2.1 ---------------------------------------- diff -r a6fd5823d699b65eb75db11f69ca47cdac0fff3f -r bbf6380d27762cbaaf38f09d41ac214686c1a606 _pytest/__init__.py --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.2.2.dev5' +__version__ = '2.2.2.dev6' diff -r a6fd5823d699b65eb75db11f69ca47cdac0fff3f -r bbf6380d27762cbaaf38f09d41ac214686c1a606 doc/monkeypatch.txt --- a/doc/monkeypatch.txt +++ b/doc/monkeypatch.txt @@ -14,14 +14,14 @@ .. _`monkeypatch blog post`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/ - -Simple example: patching ``os.path.expanduser`` +Simple example: monkeypatching functions --------------------------------------------------- -If, for instance, you want to pretend that ``os.expanduser`` returns a certain +If you want to pretend that ``os.expanduser`` returns a certain directory, you can use the :py:meth:`monkeypatch.setattr` method to patch this function before calling into a function which uses it:: + # content of test_module.py import os.path def getssh(): # pseudo application code return os.path.join(os.path.expanduser("~admin"), '.ssh') @@ -33,22 +33,20 @@ x = getssh() assert x == '/abc/.ssh' -After the test function finishes the ``os.path.expanduser`` modification -will be undone. +Here our test function monkeypatches ``os.path.expanduser`` and +then calls into an function that calls it. After the test function +finishes the ``os.path.expanduser`` modification will be undone. -.. background check: - $ py.test - =========================== test session starts ============================ - platform darwin -- Python 2.7.1 -- pytest-2.2.1 - collecting ... collected 0 items - - ============================= in 0.00 seconds ============================= +.. note:: + + As with version 2.2.2 there is a ``mp`` function argument + which is a shortcut for "monkeypatch". Method reference of the monkeypatch function argument ----------------------------------------------------- .. autoclass:: monkeypatch - :members: setattr, delattr, setitem, delitem, setenv, delenv, syspath_prepend, undo + :members: setattr, delattr, setitem, delitem, setenv, delenv, syspath_prepend, chdir, undo ``monkeypatch.setattr/delattr/delitem/delenv()`` all by default raise an Exception if the target does not exist. diff -r a6fd5823d699b65eb75db11f69ca47cdac0fff3f -r bbf6380d27762cbaaf38f09d41ac214686c1a606 setup.py --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ name='pytest', description='py.test: simple powerful testing with Python', long_description = long_description, - version='2.2.2.dev5', + version='2.2.2.dev6', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], Repository URL: https://bitbucket.org/hpk42/pytest/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. _______________________________________________ py-svn mailing list py-svn@codespeak.net http://codespeak.net/mailman/listinfo/py-svn