Please review pull request #202: (#3226) Strip whitespace from fact opened by (hkenney)

Description:

Prior to this commit, Facter was not removing trailing or leading
whitespace from facts. This change normalizes all facts by
stripping whitespace. However, since in some custom facts trailing
and leading whitespace may be essential, it is possible to opt
out of stripping the whitespace on certain facts.

  • Opened: Thu May 10 21:49:08 UTC 2012
  • Based on: puppetlabs:master (1bd98fd94b5e577216b80789ee9f933dd5cc74f8)
  • Requested merge: hkenney:ticket/master/3226_strip_whitespace_from_fact (3a84a0e4c4e495b2a18eb06f11ad4a03e4419952)

Diff follows:

diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index a5a2035..4e8236d 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -10,7 +10,7 @@
 
 class Facter::Util::Resolution
   attr_accessor :interpreter, :code, :name, :timeout
-  attr_writer :value, :weight
+  attr_writer :value, :weight, :preserve_whitespace
 
   INTERPRETER = Facter::Util::Config.is_windows? ? "cmd.exe" : "/bin/sh"
 
@@ -225,6 +225,10 @@ def value
     ms = (finishtime - starttime) * 1000
     Facter.show_time "#{self.name}: #{"%.2f" % ms}ms"
 
+    unless @preserve_whitespace
+      result = result.strip if result 
+    end
+    
     return nil if result == ""
     return result
   end
diff --git a/spec/unit/util/resolution_spec.rb b/spec/unit/util/resolution_spec.rb
index 388baac..c5e563f 100755
--- a/spec/unit/util/resolution_spec.rb
+++ b/spec/unit/util/resolution_spec.rb
@@ -167,6 +167,94 @@ def handy_method()
       @resolve.value.should == "foo"
     end
 
+    describe "when dealing with whitespace" do
+      it "should by default strip whitespace" do 
+        @resolve.setcode {'  value  '}
+        @resolve.value.should == 'value' 
+      end 
+      describe "when given a string" do
+        [true, false
+        ].each do |windows| 
+          describe "#{ (windows) ? '' : 'not' } on Windows" do
+            before do
+              Facter::Util::Config.stubs(:is_windows?).returns(windows)
+            end
+            describe "stripping whitespace" do 
+              before do
+                @resolve.preserve_whitespace = false 
+              end 
+              [{:name => 'leading', :result => '  value', :expect => 'value'},
+               {:name => 'trailing', :result => 'value  ', :expect => 'value'}, 
+               {:name => 'internal', :result => 'val  ue', :expect => 'val  ue'},
+               {:name => 'leading and trailing', :result => '  value  ', :expect => 'value'},  
+               {:name => 'leading and internal', :result => '  val  ue', :expect => 'val  ue'}, 
+               {:name => 'trailing and internal', :result => 'val  ue  ', :expect => 'val  ue'}
+              ].each do |scenario|
+                it "should remove outer whitespace when whitespace is #{scenario[:name]}" do 
+                  @resolve.setcode "/bin/foo"
+                  Facter::Util::Resolution.expects(:exec).once.with("/bin/foo").returns scenario[:result]
+                  @resolve.value.should == scenario[:expect] 
+                end 
+              end 
+            end 
+            describe "not stripping whitespace" do
+              before do
+                @resolve.preserve_whitespace = true 
+              end 
+              [{:name => 'leading', :result => '  value', :expect => '  value'}, 
+               {:name => 'trailing', :result => 'value  ', :expect => 'value  '}, 
+               {:name => 'internal', :result => 'val  ue', :expect => 'val  ue'},
+               {:name => 'leading and trailing', :result => '  value  ', :expect => '  value  '},  
+               {:name => 'leading and internal', :result => '  val  ue', :expect => '  val  ue'}, 
+               {:name => 'trailing and internal', :result => 'val  ue  ', :expect => 'val  ue  '}
+              ].each do |scenario|
+                it "should not remove #{scenario[:name]} whitespace" do 
+                  @resolve.setcode "/bin/foo"
+                  Facter::Util::Resolution.expects(:exec).once.with("/bin/foo").returns scenario[:result]
+                  @resolve.value.should == scenario[:expect] 
+                end 
+              end 
+            end 
+          end 
+        end 
+      end 
+      describe "when given a block" do
+        describe "stripping whitespace" do 
+          before do
+            @resolve.preserve_whitespace = false 
+          end 
+          [{:name => 'leading', :result => '  value', :expect => 'value'},
+           {:name => 'trailing', :result => 'value  ', :expect => 'value'}, 
+           {:name => 'internal', :result => 'val  ue', :expect => 'val  ue'},
+           {:name => 'leading and trailing', :result => '  value  ', :expect => 'value'},  
+           {:name => 'leading and internal', :result => '  val  ue', :expect => 'val  ue'}, 
+           {:name => 'trailing and internal', :result => 'val  ue  ', :expect => 'val  ue'}
+          ].each do |scenario|
+            it "should remove outer whitespace when whitespace is #{scenario[:name]}" do 
+              @resolve.setcode {scenario[:result]}
+              @resolve.value.should == scenario[:expect] 
+            end
+          end 
+        end
+        describe "not stripping whitespace" do 
+          before do
+            @resolve.preserve_whitespace = true 
+          end 
+          [{:name => 'leading', :result => '  value', :expect => '  value'}, 
+          {:name => 'trailing', :result => 'value  ', :expect => 'value  '}, 
+          {:name => 'internal', :result => 'val  ue', :expect => 'val  ue'},
+          {:name => 'leading and trailing', :result => '  value  ', :expect => '  value  '},  
+          {:name => 'leading and internal', :result => '  val  ue', :expect => '  val  ue'}, 
+          {:name => 'trailing and internal', :result => 'val  ue  ', :expect => 'val  ue  '}
+         ].each do |scenario|
+            it "should not remove #{scenario[:name]} whitespace" do 
+              @resolve.setcode {scenario[:result]}
+              @resolve.value.should == scenario[:expect] 
+            end
+          end 
+        end 
+      end 
+    end 
     describe "and setcode has not been called" do
       it "should return nil" do
         Facter::Util::Resolution.expects(:exec).with(nil, nil).never

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to