Permalink
Browse files

Test and implementation

  • Loading branch information...
1 parent e5c06fb commit 9ca78a7f671f06a29a80dde51ed4a7bb7ca876a1 @bitprophet bitprophet committed May 9, 2012
Showing with 29 additions and 5 deletions.
  1. +11 −5 fabric/task_utils.py
  2. +18 −0 tests/test_main.py
View
@@ -1,4 +1,5 @@
from fabric.utils import abort, indent
+from fabric import state
# For attribute tomfoolery
@@ -52,14 +53,19 @@ def merge(hosts, roles, exclude, roledefs):
value = value()
role_hosts += value
+ # Strip whitespace from host strings.
+ cleaned_hosts = [x.strip() for x in list(hosts) + list(role_hosts)]
# Return deduped combo of hosts and role_hosts, preserving order within
# them (vs using set(), which may lose ordering) and skipping hosts to be
# excluded.
- cleaned_hosts = [x.strip() for x in list(hosts) + list(role_hosts)]
- all_hosts = []
- for host in cleaned_hosts:
- if host not in all_hosts and host not in exclude:
- all_hosts.append(host)
+ # But only if the user hasn't indicated they want this behavior disabled.
+ all_hosts = cleaned_hosts
+ if state.env.dedupe_hosts:
+ deduped_hosts = []
+ for host in cleaned_hosts:
+ if host not in deduped_hosts and host not in exclude:
+ deduped_hosts.append(host)
+ all_hosts = deduped_hosts
return all_hosts
View
@@ -11,6 +11,7 @@
from nose.tools import ok_, eq_
from fabric.decorators import hosts, roles, task
+from fabric.context_managers import settings
from fabric.main import (parse_arguments, _escape_split,
load_fabfile as _load_fabfile, list_commands, _task_names,
COMMANDS_HEADER, NESTED_REMINDER)
@@ -149,6 +150,23 @@ def command():
# Not ['a', 'a', 'b', 'c'] or etc
true_eq_hosts(command, ['a', 'b', 'c'], env={'roledefs': fake_roles})
+def test_host_role_merge_deduping_off():
+ """
+ Allow turning deduping off
+ """
+ @roles('r1', 'r2')
+ @hosts('a')
+ def command():
+ pass
+ with settings(dedupe_hosts=False):
+ true_eq_hosts(
+ command,
+ # 'a' 1x host 1x role
+ # 'b' 1x r1 1x r2
+ ['a', 'a', 'b', 'b', 'c'],
+ env={'roledefs': fake_roles}
+ )
+
tuple_roles = {
'r1': ('a', 'b'),

0 comments on commit 9ca78a7

Please sign in to comment.