Codebase list facter / d118d81
(#13678) Add filename extension on absolute paths on windows Running Facter::Util::Resolution.exec calls Facter::Util::Resolution.which to get the absolute pathname to a binary. If passing a relative path, puppet will check different search paths and different filename extensions (like .com, .exe) to find the absolute path on windows machines For absolute paths the former behaviour was to just return true if the path is a valid path to an executable, so "C:\Windows\System32\netsh" was treated as invalid because it misses the correct extension (netsh.exe) which caused the ipaddress6 fact to fail. Change the behaviour of Facter::Util::Resolution.which to also try out the different filename extensions if an absolute path is used. Stefan Schulte 11 years ago
2 changed file(s) with 22 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
3535 def self.which(bin)
3636 if absolute_path?(bin)
3737 return bin if File.executable?(bin)
38 if Facter::Util::Config.is_windows? and File.extname(bin).empty?
39 exts = ENV['PATHEXT']
40 exts = exts ? exts.split(File::PATH_SEPARATOR) : %w[.COM .EXE .BAT .CMD]
41 exts.each do |ext|
42 destext = bin + ext
43 if File.executable?(destext)
44 Facter.warnonce("Using Facter::Util::Resolution.which with an absolute path like #{bin} but no fileextension is deprecated. Please add the correct extension (#{ext})")
45 return destext
46 end
47 end
48 end
3849 else
3950 search_paths.each do |dir|
4051 dest = File.join(dir, bin)
368368 context "when run on windows", :as_platform => :windows do
369369 before :each do
370370 Facter::Util::Resolution.stubs(:search_paths).returns ['C:\Windows\system32', 'C:\Windows', 'C:\Windows\System32\Wbem' ]
371 ENV.stubs(:[]).with('PATHEXT').returns nil
371372 end
372373
373374 context "and provided with an absolute path" do
378379 Facter::Util::Resolution.which('\\\\remote\dir\foo.exe').should == '\\\\remote\dir\foo.exe'
379380 end
380381
382 it "should return the binary with added extension if executable" do
383 ['.COM', '.BAT', '.CMD', '' ].each do |ext|
384 File.stubs(:executable?).with('C:\Windows\system32\netsh'+ext).returns false
385 end
386 File.expects(:executable?).with('C:\Windows\system32\netsh.EXE').returns true
387
388 Facter.expects(:warnonce).with('Using Facter::Util::Resolution.which with an absolute path like C:\\Windows\\system32\\netsh but no fileextension is deprecated. Please add the correct extension (.EXE)')
389 Facter::Util::Resolution.which('C:\Windows\system32\netsh').should == 'C:\Windows\system32\netsh.EXE'
390 end
391
381392 it "should return nil if the binary is not executable" do
382393 File.expects(:executable?).with('C:\Tools\foo.exe').returns false
383394 File.expects(:executable?).with('\\\\remote\dir\foo.exe').returns false
395406 end
396407
397408 it "should return the absolute path with file extension if found" do
398 ENV.stubs(:[]).with('PATHEXT').returns nil
399409 ['.COM', '.EXE', '.BAT', '.CMD', '' ].each do |ext|
400410 File.stubs(:executable?).with('C:\Windows\system32\foo'+ext).returns false
401411 File.stubs(:executable?).with('C:\Windows\System32\Wbem\foo'+ext).returns false