Codebase list facter / 3a84a0e
(#3226) Strip whitespace from fact 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. Hailee Kenney 12 years ago
2 changed file(s) with 93 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
99
1010 class Facter::Util::Resolution
1111 attr_accessor :interpreter, :code, :name, :timeout
12 attr_writer :value, :weight
12 attr_writer :value, :weight, :preserve_whitespace
1313
1414 INTERPRETER = Facter::Util::Config.is_windows? ? "cmd.exe" : "/bin/sh"
1515
224224 ms = (finishtime - starttime) * 1000
225225 Facter.show_time "#{self.name}: #{"%.2f" % ms}ms"
226226
227 unless @preserve_whitespace
228 result = result.strip if result
229 end
230
227231 return nil if result == ""
228232 return result
229233 end
166166 @resolve.value.should == "foo"
167167 end
168168
169 describe "when dealing with whitespace" do
170 it "should by default strip whitespace" do
171 @resolve.setcode {' value '}
172 @resolve.value.should == 'value'
173 end
174 describe "when given a string" do
175 [true, false
176 ].each do |windows|
177 describe "#{ (windows) ? '' : 'not' } on Windows" do
178 before do
179 Facter::Util::Config.stubs(:is_windows?).returns(windows)
180 end
181 describe "stripping whitespace" do
182 before do
183 @resolve.preserve_whitespace = false
184 end
185 [{:name => 'leading', :result => ' value', :expect => 'value'},
186 {:name => 'trailing', :result => 'value ', :expect => 'value'},
187 {:name => 'internal', :result => 'val ue', :expect => 'val ue'},
188 {:name => 'leading and trailing', :result => ' value ', :expect => 'value'},
189 {:name => 'leading and internal', :result => ' val ue', :expect => 'val ue'},
190 {:name => 'trailing and internal', :result => 'val ue ', :expect => 'val ue'}
191 ].each do |scenario|
192 it "should remove outer whitespace when whitespace is #{scenario[:name]}" do
193 @resolve.setcode "/bin/foo"
194 Facter::Util::Resolution.expects(:exec).once.with("/bin/foo").returns scenario[:result]
195 @resolve.value.should == scenario[:expect]
196 end
197 end
198 end
199 describe "not stripping whitespace" do
200 before do
201 @resolve.preserve_whitespace = true
202 end
203 [{:name => 'leading', :result => ' value', :expect => ' value'},
204 {:name => 'trailing', :result => 'value ', :expect => 'value '},
205 {:name => 'internal', :result => 'val ue', :expect => 'val ue'},
206 {:name => 'leading and trailing', :result => ' value ', :expect => ' value '},
207 {:name => 'leading and internal', :result => ' val ue', :expect => ' val ue'},
208 {:name => 'trailing and internal', :result => 'val ue ', :expect => 'val ue '}
209 ].each do |scenario|
210 it "should not remove #{scenario[:name]} whitespace" do
211 @resolve.setcode "/bin/foo"
212 Facter::Util::Resolution.expects(:exec).once.with("/bin/foo").returns scenario[:result]
213 @resolve.value.should == scenario[:expect]
214 end
215 end
216 end
217 end
218 end
219 end
220 describe "when given a block" do
221 describe "stripping whitespace" do
222 before do
223 @resolve.preserve_whitespace = false
224 end
225 [{:name => 'leading', :result => ' value', :expect => 'value'},
226 {:name => 'trailing', :result => 'value ', :expect => 'value'},
227 {:name => 'internal', :result => 'val ue', :expect => 'val ue'},
228 {:name => 'leading and trailing', :result => ' value ', :expect => 'value'},
229 {:name => 'leading and internal', :result => ' val ue', :expect => 'val ue'},
230 {:name => 'trailing and internal', :result => 'val ue ', :expect => 'val ue'}
231 ].each do |scenario|
232 it "should remove outer whitespace when whitespace is #{scenario[:name]}" do
233 @resolve.setcode {scenario[:result]}
234 @resolve.value.should == scenario[:expect]
235 end
236 end
237 end
238 describe "not stripping whitespace" do
239 before do
240 @resolve.preserve_whitespace = true
241 end
242 [{:name => 'leading', :result => ' value', :expect => ' value'},
243 {:name => 'trailing', :result => 'value ', :expect => 'value '},
244 {:name => 'internal', :result => 'val ue', :expect => 'val ue'},
245 {:name => 'leading and trailing', :result => ' value ', :expect => ' value '},
246 {:name => 'leading and internal', :result => ' val ue', :expect => ' val ue'},
247 {:name => 'trailing and internal', :result => 'val ue ', :expect => 'val ue '}
248 ].each do |scenario|
249 it "should not remove #{scenario[:name]} whitespace" do
250 @resolve.setcode {scenario[:result]}
251 @resolve.value.should == scenario[:expect]
252 end
253 end
254 end
255 end
256 end
169257 describe "and setcode has not been called" do
170258 it "should return nil" do
171259 Facter::Util::Resolution.expects(:exec).with(nil, nil).never