Browse files

with_settings decorator takes context managers

The with_settings() decorator from fabric.decorators takes keyword arguments to
set in the environment, and other context managers, much like the regular
settings context manager does. See issue 458 for more details.
  • Loading branch information...
1 parent df16ff7 commit 0953cc6d16365fec1be92b5e87a00ba92c17649d @rgeoghegan rgeoghegan committed Mar 12, 2012
Showing with 42 additions and 4 deletions.
  1. +2 −2 fabric/decorators.py
  2. +18 −2 tests/test_context_managers.py
  3. +22 −0 tests/test_decorators.py
View
4 fabric/decorators.py
@@ -188,7 +188,7 @@ def inner(*args, **kwargs):
return real_decorator
-def with_settings(**kw_settings):
+def with_settings(*arg_settings, **kw_settings):
"""
Decorator equivalent of ``fabric.context_managers.settings``.
@@ -209,7 +209,7 @@ def foo():
def outer(func):
@wraps(func)
def inner(*args, **kwargs):
- with settings(**kw_settings):
+ with settings(*arg_settings, **kw_settings):
return func(*args, **kwargs)
return _wrap_as_new(func, inner)
return outer
View
20 tests/test_context_managers.py
@@ -2,8 +2,8 @@
from nose.tools import eq_, ok_
-from fabric.state import env
-from fabric.context_managers import cd, settings
+from fabric.state import env, output
+from fabric.context_managers import cd, settings, lcd
#
@@ -75,3 +75,19 @@ def test_settings_with_multiple_kwargs():
eq_(env.testval2, "inner 2")
eq_(env.testval1, "outer 1")
eq_(env.testval2, "outer 2")
+
+def test_settings_with_other_context_managers():
+ """
+ settings() should take other context managers, and use them with other overrided
+ key/value pairs.
+ """
+ env.testval1 = "outer 1"
+ prev_lcwd = env.lcwd
+
+ with settings(lcd("here"), testval1="inner 1"):
+ eq_(env.testval1, "inner 1")
+ ok_(env.lcwd.endswith("here")) # Should be the side-effect of adding cd to settings
+
+ ok_(env.testval1, "outer 1")
+ eq_(env.lcwd, prev_lcwd)
+
View
22 tests/test_decorators.py
@@ -10,6 +10,7 @@
from fabric.state import env
import fabric # for patching fabric.state.xxx
from fabric.tasks import _parallel_tasks, requires_parallel
+from fabric.context_managers import lcd
#
@@ -252,3 +253,24 @@ def some_task():
decorated_task = decorators.with_settings(value=random_return)(some_task)
ok_(some_task(), msg="sanity check")
eq_(random_return, decorated_task())
+
+def test_with_settings_with_other_context_managers():
+ """
+ with_settings() should take other context managers, and use them with other
+ overrided key/value pairs.
+ """
+ env.testval1 = "outer 1"
+ prev_lcwd = env.lcwd
+
+ def some_task():
+ eq_(env.testval1, "inner 1")
+ ok_(env.lcwd.endswith("here")) # Should be the side-effect of adding cd to settings
+
+ decorated_task = decorators.with_settings(
+ lcd("here"),
+ testval1="inner 1"
+ )(some_task)
+ decorated_task()
+
+ ok_(env.testval1, "outer 1")
+ eq_(env.lcwd, prev_lcwd)

0 comments on commit 0953cc6

Please sign in to comment.