From 0365d0ecb2c6c7dd2bd3947cd01504aa71130dac Mon Sep 17 00:00:00 2001
From: Jesse Wolfe <jes5199@gmail.com>
Date: Tue, 10 Nov 2009 02:58:02 -0800
Subject: [PATCH/puppet 1/1] Fixing #2789 puppetrun fails without --tag

Puppet::Transaction was handling "tags" strings differently depending on
whether they came in from Puppet[:tags] or another source.
This was causing puppetrun's tags to be misparsed if there was not
exactly one --tag parameter.

I've moved the code to Util::Tagging.

Signed-off-by: Jesse Wolfe <jes5199@gmail.com>
---
 lib/puppet/transaction.rb  |   19 +++++++++----------
 lib/puppet/util/tagging.rb |   22 ++++++++++++++++++++--
 spec/unit/transaction.rb   |   10 ++++++++++
 3 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb
index d04856d..893f773 100644
--- a/lib/puppet/transaction.rb
+++ b/lib/puppet/transaction.rb
@@ -2,6 +2,7 @@
 # and performs them
 
 require 'puppet'
+require 'puppet/util/tagging'
 
 module Puppet
 class Transaction
@@ -18,6 +19,7 @@ class Transaction
     attr_reader :events
 
     include Puppet::Util
+    include Puppet::Util::Tagging
 
     # Add some additional times for reporting
     def addtimes(hash)
@@ -601,20 +603,17 @@ class Transaction
     # The tags we should be checking.
     def tags
         unless defined? @tags
-            tags = Puppet[:tags]
-            if tags.nil? or tags == ""
-                @tags = []
-            else
-                @tags = tags.split(/\s*,\s*/)
-            end
+            self.tags = Puppet[:tags]
         end
 
-        @tags
+        super
     end
 
-    def tags=(tags)
-        tags = [tags] unless tags.is_a?(Array)
-        @tags = tags
+    def handle_qualified_tags( qualified )
+        # The default behavior of Puppet::Util::Tagging is
+        # to split qualified tags into parts. That would cause
+        # qualified tags to match too broadly here.
+        return
     end
 
     # Is this resource tagged appropriately?
diff --git a/lib/puppet/util/tagging.rb b/lib/puppet/util/tagging.rb
index f421d18..b244a7a 100644
--- a/lib/puppet/util/tagging.rb
+++ b/lib/puppet/util/tagging.rb
@@ -16,8 +16,7 @@ module Puppet::Util::Tagging
             @tags << tag unless @tags.include?(tag)
         end
 
-        # LAK:NOTE See http://snurl.com/21zf8  [groups_google_com]
-        qualified.collect { |name| x = name.split("::") }.flatten.each { |tag| @tags << tag unless @tags.include?(tag) }
+        handle_qualified_tags( qualified )
     end
 
     # Are we tagged with the provided tag?
@@ -32,8 +31,27 @@ module Puppet::Util::Tagging
         @tags.dup
     end
 
+    def tags=(tags)
+        @tags = []
+
+        return if tags.nil? or tags == ""
+
+        if tags.is_a?(String)
+            tags = tags.strip.split(/\s*,\s*/)
+        end
+
+        tags.each do |t|
+            tag(t)
+        end
+    end
+
     private
 
+    def handle_qualified_tags( qualified )
+        # LAK:NOTE See http://snurl.com/21zf8  [groups_google_com]
+        qualified.collect { |name| x = name.split("::") }.flatten.each { |tag| @tags << tag unless @tags.include?(tag) }
+    end
+
     def valid_tag?(tag)
         tag =~ /^\w[-\w:.]*$/
     end
diff --git a/spec/unit/transaction.rb b/spec/unit/transaction.rb
index 7966c7a..5474c38 100755
--- a/spec/unit/transaction.rb
+++ b/spec/unit/transaction.rb
@@ -108,4 +108,14 @@ describe Puppet::Transaction, " when determining tags" do
         @transaction.tags = "one::two"
         @transaction.tags.should == %w{one::two}
     end
+
+    it "should accept a comma-delimited string" do
+        @transaction.tags = "one, two"
+        @transaction.tags.should == %w{one two}
+    end
+
+    it "should accept an empty string" do
+        @transaction.tags = ""
+        @transaction.tags.should == []
+    end
 end
-- 
1.6.3.3


