Codebase list facter / bc109e7
Merge pull request #202 from hkenney/ticket/master/3226_strip_whitespace_from_fact (#3226) Strip whitespace from fact Daniel Pittman 12 years ago
2 changed file(s) with 106 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
154154 @timeout
155155 end
156156
157 def preserve_whitespace
158 @preserve_whitespace = true
159 end
160
157161 # Set our code for returning a value.
158162 def setcode(string = nil, interp = nil, &block)
159163 Facter.warnonce "The interpreter parameter to 'setcode' is deprecated and will be removed in a future version." if interp
224228 ms = (finishtime - starttime) * 1000
225229 Facter.show_time "#{self.name}: #{"%.2f" % ms}ms"
226230
231 unless @preserve_whitespace
232 result.strip! if result && result.respond_to?(:strip!)
233 end
234
227235 return nil if result == ""
228236 return result
229237 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
175 describe "when given a string" do
176 [true, false
177 ].each do |windows|
178 describe "#{ (windows) ? '' : 'not' } on Windows" do
179 before do
180 Facter::Util::Config.stubs(:is_windows?).returns(windows)
181 end
182
183 describe "stripping whitespace" do
184 [{:name => 'leading', :result => ' value', :expect => 'value'},
185 {:name => 'trailing', :result => 'value ', :expect => 'value'},
186 {:name => 'internal', :result => 'val ue', :expect => 'val ue'},
187 {:name => 'leading and trailing', :result => ' value ', :expect => 'value'},
188 {:name => 'leading and internal', :result => ' val ue', :expect => 'val ue'},
189 {:name => 'trailing and internal', :result => 'val ue ', :expect => 'val ue'}
190 ].each do |scenario|
191
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
198 end
199 end
200
201 describe "not stripping whitespace" do
202 before do
203 @resolve.preserve_whitespace
204 end
205
206 [{:name => 'leading', :result => ' value', :expect => ' value'},
207 {:name => 'trailing', :result => 'value ', :expect => 'value '},
208 {:name => 'internal', :result => 'val ue', :expect => 'val ue'},
209 {:name => 'leading and trailing', :result => ' value ', :expect => ' value '},
210 {:name => 'leading and internal', :result => ' val ue', :expect => ' val ue'},
211 {:name => 'trailing and internal', :result => 'val ue ', :expect => 'val ue '}
212 ].each do |scenario|
213
214 it "should not remove #{scenario[:name]} whitespace" do
215 @resolve.setcode "/bin/foo"
216 Facter::Util::Resolution.expects(:exec).once.with("/bin/foo").returns scenario[:result]
217 @resolve.value.should == scenario[:expect]
218 end
219
220 end
221 end
222 end
223 end
224 end
225
226 describe "when given a block" do
227 describe "stripping whitespace" do
228 [{:name => 'leading', :result => ' value', :expect => 'value'},
229 {:name => 'trailing', :result => 'value ', :expect => 'value'},
230 {:name => 'internal', :result => 'val ue', :expect => 'val ue'},
231 {:name => 'leading and trailing', :result => ' value ', :expect => 'value'},
232 {:name => 'leading and internal', :result => ' val ue', :expect => 'val ue'},
233 {:name => 'trailing and internal', :result => 'val ue ', :expect => 'val ue'}
234 ].each do |scenario|
235
236 it "should remove outer whitespace when whitespace is #{scenario[:name]}" do
237 @resolve.setcode {scenario[:result]}
238 @resolve.value.should == scenario[:expect]
239 end
240
241 end
242 end
243
244 describe "not stripping whitespace" do
245 before do
246 @resolve.preserve_whitespace
247 end
248
249 [{:name => 'leading', :result => ' value', :expect => ' value'},
250 {:name => 'trailing', :result => 'value ', :expect => 'value '},
251 {:name => 'internal', :result => 'val ue', :expect => 'val ue'},
252 {:name => 'leading and trailing', :result => ' value ', :expect => ' value '},
253 {:name => 'leading and internal', :result => ' val ue', :expect => ' val ue'},
254 {:name => 'trailing and internal', :result => 'val ue ', :expect => 'val ue '}
255 ].each do |scenario|
256
257 it "should not remove #{scenario[:name]} whitespace" do
258 @resolve.setcode {scenario[:result]}
259 @resolve.value.should == scenario[:expect]
260 end
261
262 end
263 end
264 end
265 end
266
169267 describe "and setcode has not been called" do
170268 it "should return nil" do
171269 Facter::Util::Resolution.expects(:exec).with(nil, nil).never