Skip to content

Commit

Permalink
Don't barf when settings(nonexistent_key=value).
Browse files Browse the repository at this point in the history
Fixes fabric#316

Conflicts:

	tests/test_context_managers.py
  • Loading branch information
bitprophet committed Mar 20, 2011
1 parent 09ca296 commit 5695925
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/changes/0.9.5.rst
Expand Up @@ -29,7 +29,9 @@ Bugfixes
* :issue:`287`: Fix bug in password prompt causing occasional tracebacks.
Thanks to Antti Kaihola for the catch and Rick Harding for testing the
proposed solution.

* :issue:`316`: Use of `~fabric.context_managers.settings` with key names not
previously set in ``env`` no longer raises KeyErrors. Whoops. Thanks to Adam
Ernst for the catch.

Documentation updates
=====================
Expand Down
3 changes: 2 additions & 1 deletion fabric/context_managers.py
Expand Up @@ -86,7 +86,8 @@ def _setenv(**kwargs):
"""
previous = {}
for key, value in kwargs.iteritems():
previous[key] = env[key]
if key in env:
previous[key] = env[key]
env[key] = value
try:
yield
Expand Down
36 changes: 36 additions & 0 deletions tests/test_context_managers.py
Expand Up @@ -38,3 +38,39 @@ def test_cwd_with_absolute_paths():
eq_(env.cwd, absolute)
with cd(additional):
eq_(env.cwd, existing + '/' + additional)


#
# settings()
#

def test_setting_new_env_dict_key_should_not_raise_keyerror():
"""
Using settings() with a previously nonexistent key should not error
"""
# Nose has no obvious way to assert a NON-RAISED exception :( so ye olde
# "if it runs it passes" will have to do.
with settings(thiskeyreallyshouldnotexist='value'):
pass


def test_settings():
"""
settings() should temporarily override env dict with given key/value pair
"""
env.testval = "outer value"
with settings(testval="inner value"):
eq_(env.testval, "inner value")
eq_(env.testval, "outer value")

def test_settings_with_multiple_kwargs():
"""
settings() should temporarily override env dict with given key/value pairS
"""
env.testval1 = "outer 1"
env.testval2 = "outer 2"
with settings(testval1="inner 1", testval2="inner 2"):
eq_(env.testval1, "inner 1")
eq_(env.testval2, "inner 2")
eq_(env.testval1, "outer 1")
eq_(env.testval2, "outer 2")

0 comments on commit 5695925

Please sign in to comment.