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 (6e7d96738b020fdb7220716fded5c5f633b4864b)
Diff follows:
diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index a5a2035..5ef1e12 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -155,6 +155,10 @@ def limit
@timeout
end
+ def preserve_whitespace
+ @preserve_whitespace = true
+ end
+
# Set our code for returning a value.
def setcode(string = nil, interp = nil, &block)
Facter.warnonce "The interpreter parameter to 'setcode' is deprecated and will be removed in a future version." if interp
@@ -225,6 +229,10 @@ def value
ms = (finishtime - starttime) * 1000
Facter.show_time "#{self.name}: #{"%.2f" % ms}ms"
+ unless @preserve_whitespace
+ result.strip! if result && result.respond_to?(:strip!)
+ 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..e0616e1 100755
--- a/spec/unit/util/resolution_spec.rb
+++ b/spec/unit/util/resolution_spec.rb
@@ -167,6 +167,104 @@ 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
+ [{: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
+ 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
+ [{: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
+ 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.
