facter 1.6.9 → 1.6.10

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of facter might be problematic. Click here for more details.

@@ -0,0 +1,9 @@
1
+ # Support code for running stuff with warnings disabled.
2
+ module Kernel
3
+ def with_verbose_disabled
4
+ verbose, $VERBOSE = $VERBOSE, nil
5
+ result = yield
6
+ $VERBOSE = verbose
7
+ return result
8
+ end
9
+ end
@@ -11,6 +11,7 @@ end
11
11
  require 'puppetlabs_spec/files'
12
12
  require 'puppetlabs_spec/fixtures'
13
13
  require 'puppetlabs_spec/matchers'
14
+ require 'puppetlabs_spec/verbose'
14
15
 
15
16
  RSpec.configure do |config|
16
17
  # Include PuppetlabsSpec helpers so they can be called at convenience
@@ -0,0 +1,55 @@
1
+ # Contexts for stubbing platforms
2
+ # In a describe or context block, adding :as_platform => :windows or
3
+ # :as_platform => :posix will stub the relevant facter config, as well as
4
+ # the behavior of Ruby's filesystem methods by changing File::ALT_SEPARATOR.
5
+ #
6
+ #
7
+ #
8
+ shared_context "windows", :as_platform => :windows do
9
+ before :each do
10
+ Facter.fact(:operatingsystem).stubs(:value).returns('Windows')
11
+ Facter::Util::Config.stubs(:is_windows?).returns true
12
+ end
13
+
14
+ around do |example|
15
+ file_alt_separator = File::ALT_SEPARATOR
16
+ file_path_separator = File::PATH_SEPARATOR
17
+ # prevent Ruby from warning about changing a constant
18
+ with_verbose_disabled do
19
+ File::ALT_SEPARATOR = '\\'
20
+ File::PATH_SEPARATOR = ';'
21
+ end
22
+ begin
23
+ example.run
24
+ ensure
25
+ with_verbose_disabled do
26
+ File::ALT_SEPARATOR = file_alt_separator
27
+ File::PATH_SEPARATOR = file_path_separator
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ shared_context "posix", :as_platform => :posix do
34
+ before :each do
35
+ Facter::Util::Config.stubs(:is_windows?).returns false
36
+ end
37
+
38
+ around do |example|
39
+ file_alt_separator = File::ALT_SEPARATOR
40
+ file_path_separator = File::PATH_SEPARATOR
41
+ # prevent Ruby from warning about changing a constant
42
+ with_verbose_disabled do
43
+ File::ALT_SEPARATOR = nil
44
+ File::PATH_SEPARATOR = ':'
45
+ end
46
+ begin
47
+ example.run
48
+ ensure
49
+ with_verbose_disabled do
50
+ File::ALT_SEPARATOR = file_alt_separator
51
+ File::PATH_SEPARATOR = file_path_separator
52
+ end
53
+ end
54
+ end
55
+ end
data/spec/spec_helper.rb CHANGED
@@ -11,6 +11,11 @@ require 'rspec'
11
11
  require 'facter'
12
12
  require 'fileutils'
13
13
  require 'puppetlabs_spec_helper'
14
+ require 'pathname'
15
+
16
+ Pathname.glob("#{dir}/shared_contexts/*.rb") do |file|
17
+ require file.relative_path_from(Pathname.new(dir))
18
+ end
14
19
 
15
20
  RSpec.configure do |config|
16
21
  config.mock_with :mocha
@@ -21,6 +21,8 @@ describe "Architecture fact" do
21
21
  ["Gentoo","i586"] => "x86",
22
22
  ["Gentoo","i686"] => "x86",
23
23
  ["Gentoo","pentium"] => "x86",
24
+ ["windows","i386"] => "x86",
25
+ ["windows","x64"] => "x64",
24
26
  }
25
27
  generic_archs = Hash.new
26
28
  generic_archs = {
@@ -98,19 +98,39 @@ describe "Domain name facts" do
98
98
  end
99
99
 
100
100
  describe "on Windows" do
101
- it "should use the DNSDomain for the first nic where ip is enabled" do
101
+ before(:each) do
102
102
  Facter.fact(:kernel).stubs(:value).returns("windows")
103
+ require 'facter/util/registry'
104
+ end
105
+ describe "with primary dns suffix" do
106
+ before(:each) do
107
+ Facter::Util::Registry.stubs(:hklm_read).returns('baz.com')
108
+ end
109
+ it "should get the primary dns suffix" do
110
+ Facter.fact(:domain).value.should == 'baz.com'
111
+ end
112
+ it "should not execute the wmi query" do
113
+ require 'facter/util/wmi'
114
+ Facter::Util::WMI.expects(:execquery).never
115
+ Facter.fact(:domain).value
116
+ end
117
+ end
118
+ describe "without primary dns suffix" do
119
+ before(:each) do
120
+ Facter::Util::Registry.stubs(:hklm_read).returns('')
121
+ end
122
+ it "should use the DNSDomain for the first nic where ip is enabled" do
123
+ nic = stubs 'nic'
124
+ nic.stubs(:DNSDomain).returns("foo.com")
103
125
 
104
- nic = stubs 'nic'
105
- nic.stubs(:DNSDomain).returns("foo.com")
106
-
107
- nic2 = stubs 'nic'
108
- nic2.stubs(:DNSDomain).returns("bar.com")
126
+ nic2 = stubs 'nic'
127
+ nic2.stubs(:DNSDomain).returns("bar.com")
109
128
 
110
- require 'facter/util/wmi'
111
- Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
129
+ require 'facter/util/wmi'
130
+ Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
112
131
 
113
- Facter.fact(:domain).value.should == 'foo.com'
132
+ Facter.fact(:domain).value.should == 'foo.com'
133
+ end
114
134
  end
115
135
  end
116
136
  end
@@ -74,8 +74,9 @@ describe Facter do
74
74
 
75
75
  describe "when provided code as a string" do
76
76
  it "should execute the code in the shell" do
77
+ test_command = Facter::Util::Config.is_windows? ? 'cmd.exe /c echo yup' : 'echo yup'
77
78
  Facter.add("shell_testing") do
78
- setcode "echo yup"
79
+ setcode test_command
79
80
  end
80
81
 
81
82
  Facter["shell_testing"].value.should == "yup"
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'spec_helper'
4
+ require 'facter'
5
+
6
+ describe "Hardwaremodel fact" do
7
+ it "should match uname -m by default" do
8
+ Facter.fact(:kernel).stubs(:value).returns("Darwin")
9
+ Facter::Util::Resolution.stubs(:exec).with("uname -m").returns("Inky")
10
+
11
+ Facter.fact(:hardwaremodel).value.should == "Inky"
12
+ end
13
+
14
+ describe "on Windows" do
15
+ require 'facter/util/wmi'
16
+ before :each do
17
+ Facter.fact(:kernel).stubs(:value).returns("windows")
18
+ end
19
+
20
+ it "should detect i686" do
21
+ cpu = mock('cpu', :Architecture => 0, :Level => 6)
22
+ Facter::Util::WMI.expects(:execquery).returns([cpu])
23
+
24
+ Facter.fact(:hardwaremodel).value.should == "i686"
25
+ end
26
+
27
+ it "should detect x64" do
28
+ cpu = mock('cpu', :Architecture => 9)
29
+ Facter::Util::WMI.expects(:execquery).returns([cpu])
30
+
31
+ Facter.fact(:hardwaremodel).value.should == "x64"
32
+ end
33
+ end
34
+ end
@@ -44,7 +44,7 @@ describe "IPv6 address fact" do
44
44
  Facter::Util::Config.stubs(:is_windows?).returns(true)
45
45
 
46
46
  fixture = netsh_fixture('windows_netsh_addresses_with_multiple_interfaces')
47
- Facter::Util::Resolution.stubs(:exec).with('d:/windows/system32/netsh interface ipv6 show address level=verbose').
47
+ Facter::Util::Resolution.stubs(:exec).with('d:/windows/system32/netsh.exe interface ipv6 show address level=verbose').
48
48
  returns(fixture)
49
49
 
50
50
  Facter.value(:ipaddress6).should == "2001:0:4137:9e76:2087:77a:53ef:7527"
@@ -10,13 +10,13 @@ describe "lsbdistcodename fact" do
10
10
  Facter.fact(:kernel).stubs(:value).returns kernel
11
11
  end
12
12
 
13
- it "should return the codename through lsb_release -c -s" do
14
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -c -s').returns 'n/a'
13
+ it "should return the codename through lsb_release -c -s 2>/dev/null" do
14
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -c -s 2>/dev/null').returns 'n/a'
15
15
  Facter.fact(:lsbdistcodename).value.should == 'n/a'
16
16
  end
17
17
 
18
18
  it "should return nil if lsb_release is not installed" do
19
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -c -s').returns nil
19
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -c -s 2>/dev/null').returns nil
20
20
  Facter.fact(:lsbdistcodename).value.should be_nil
21
21
  end
22
22
  end
@@ -10,13 +10,13 @@ describe "lsbdistdescription fact" do
10
10
  Facter.fact(:kernel).stubs(:value).returns kernel
11
11
  end
12
12
 
13
- it "should return the description through lsb_release -d -s" do
14
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -d -s').returns '"Gentoo Base System release 2.1"'
13
+ it "should return the description through lsb_release -d -s 2>/dev/null" do
14
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -d -s 2>/dev/null').returns '"Gentoo Base System release 2.1"'
15
15
  Facter.fact(:lsbdistdescription).value.should == 'Gentoo Base System release 2.1'
16
16
  end
17
17
 
18
18
  it "should return nil if lsb_release is not installed" do
19
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -d -s').returns nil
19
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -d -s 2>/dev/null').returns nil
20
20
  Facter.fact(:lsbdistdescription).value.should be_nil
21
21
  end
22
22
  end
@@ -10,13 +10,13 @@ describe "lsbdistid fact" do
10
10
  Facter.fact(:kernel).stubs(:value).returns kernel
11
11
  end
12
12
 
13
- it "should return the id through lsb_release -i -s" do
14
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -i -s').returns 'Gentoo'
13
+ it "should return the id through lsb_release -i -s 2>/dev/null" do
14
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -i -s 2>/dev/null').returns 'Gentoo'
15
15
  Facter.fact(:lsbdistid).value.should == 'Gentoo'
16
16
  end
17
17
 
18
- it "should return nil if lsb_release is not installed" do
19
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -i -s').returns nil
18
+ it "should return nil if lsb_release is not installed 2>/dev/null" do
19
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -i -s 2>/dev/null').returns nil
20
20
  Facter.fact(:lsbdistid).value.should be_nil
21
21
  end
22
22
  end
@@ -10,13 +10,13 @@ describe "lsbdistrelease fact" do
10
10
  Facter.fact(:kernel).stubs(:value).returns kernel
11
11
  end
12
12
 
13
- it "should return the release through lsb_release -r -s" do
14
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -r -s').returns '2.1'
13
+ it "should return the release through lsb_release -r -s 2>/dev/null" do
14
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -r -s 2>/dev/null').returns '2.1'
15
15
  Facter.fact(:lsbdistrelease).value.should == '2.1'
16
16
  end
17
17
 
18
18
  it "should return nil if lsb_release is not installed" do
19
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -r -s').returns nil
19
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -r -s 2>/dev/null').returns nil
20
20
  Facter.fact(:lsbdistrelease).value.should be_nil
21
21
  end
22
22
  end
@@ -10,13 +10,13 @@ describe "lsbrelease fact" do
10
10
  Facter.fact(:kernel).stubs(:value).returns kernel
11
11
  end
12
12
 
13
- it "should return the release through lsb_release -v -s" do
14
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -v -s').returns 'n/a'
13
+ it "should return the release through lsb_release -v -s 2>/dev/null" do
14
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -v -s 2>/dev/null').returns 'n/a'
15
15
  Facter.fact(:lsbrelease).value.should == 'n/a'
16
16
  end
17
17
 
18
18
  it "should return nil if lsb_release is not installed" do
19
- Facter::Util::Resolution.stubs(:exec).with('lsb_release -v -s').returns nil
19
+ Facter::Util::Resolution.stubs(:exec).with('lsb_release -v -s 2>/dev/null').returns nil
20
20
  Facter.fact(:lsbrelease).value.should be_nil
21
21
  end
22
22
  end
@@ -16,6 +16,8 @@ describe "Processor facts" do
16
16
  def load(procs)
17
17
  require 'facter/util/wmi'
18
18
  Facter::Util::WMI.stubs(:execquery).with("select * from Win32_Processor").returns(procs)
19
+ # This is to workaround #14674
20
+ Facter.fact(:architecture).stubs(:value).returns("x64")
19
21
 
20
22
  # processor facts belong to a file with a different name,
21
23
  # so load the file explicitly (after stubbing kernel),
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env rspec
2
+ require 'spec_helper'
3
+ require 'facter/operatingsystem'
4
+ require 'facter/util/registry'
5
+
6
+ describe Facter::Util::Registry do
7
+ describe "hklm_read", :if => Facter::Util::Config.is_windows? do
8
+ before(:all) do
9
+ require 'win32/registry'
10
+ end
11
+ describe "valid params" do
12
+ [ {:key => "valid_key", :value => "valid_value", :expected => "valid"},
13
+ {:key => "valid_key", :value => "", :expected => "valid"},
14
+ {:key => "valid_key", :value => nil, :expected => "invalid"},
15
+ {:key => "", :value => "valid_value", :expected => "valid"},
16
+ {:key => "", :value => "", :expected => "valid"},
17
+ {:key => "", :value => nil, :expected => "invalid"},
18
+ {:key => nil, :value => "valid_value", :expected => "invalid"},
19
+ {:key => nil, :value => "", :expected => "invalid"},
20
+ {:key => nil, :value => nil, :expected => "invalid"}
21
+ ].each do |scenario|
22
+ describe "with key #{scenario[:key] || "nil"} and value #{scenario[:value] || "nil"}" do
23
+ let :fake_registry_key do
24
+ fake = {}
25
+ fake[scenario[:value]] = scenario[:expected]
26
+ fake
27
+ end
28
+ it "should return #{scenario[:expected]} value" do
29
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).returns(fake_registry_key)
30
+ fake_registry_key.stubs(:close)
31
+
32
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value]).should == scenario[:expected]
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "invalid params" do
39
+ [ {:key => "valid_key", :value => "invalid_value"},
40
+ {:key => "valid_key", :value => ""},
41
+ {:key => "valid_key", :value => nil},
42
+ ].each do |scenario|
43
+ describe "with valid key and value #{scenario[:value] || "nil"}" do
44
+ let :fake_registry_key do
45
+ {}
46
+ end
47
+ it "should raise an error" do
48
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).returns(fake_registry_key)
49
+ fake_registry_key.stubs(:close)
50
+
51
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value]).should raise_error
52
+ end
53
+ end
54
+ end
55
+ [ {:key => "invalid_key", :value => "valid_value"},
56
+ {:key => "invalid_key", :value => ""},
57
+ {:key => "invalid_key", :value => nil},
58
+ {:key => "", :value => "valid_value"},
59
+ {:key => "", :value => ""},
60
+ {:key => "", :value => nil},
61
+ {:key => nil, :value => "valid_value"},
62
+ {:key => nil, :value => ""},
63
+ {:key => nil, :value => nil}
64
+ ].each do |scenario|
65
+ describe "with invalid key #{scenario[:key] || "nil"} and value #{scenario[:value] || "nil"}" do
66
+ it "should raise an error" do
67
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).raises(Win32::Registry::Error, 2)
68
+ expect do
69
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value])
70
+ end.to raise_error Win32::Registry::Error
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -284,6 +284,210 @@ describe Facter::Util::Resolution do
284
284
  Facter::Util::Resolution.should respond_to(:exec)
285
285
  end
286
286
 
287
+ # taken from puppet: spec/unit/util_spec.rb
288
+ describe "#absolute_path?" do
289
+ context "when run on unix", :as_platform => :posix do
290
+ %w[/ /foo /foo/../bar //foo //Server/Foo/Bar //?/C:/foo/bar /\Server/Foo /foo//bar/baz].each do |path|
291
+ it "should return true for #{path}" do
292
+ Facter::Util::Resolution.should be_absolute_path(path)
293
+ end
294
+ end
295
+
296
+ %w[. ./foo \foo C:/foo \\Server\Foo\Bar \\?\C:\foo\bar \/?/foo\bar \/Server/foo foo//bar/baz].each do |path|
297
+ it "should return false for #{path}" do
298
+ Facter::Util::Resolution.should_not be_absolute_path(path)
299
+ end
300
+ end
301
+ end
302
+
303
+ context "when run on windows", :as_platform => :windows do
304
+ %w[C:/foo C:\foo \\\\Server\Foo\Bar \\\\?\C:\foo\bar //Server/Foo/Bar //?/C:/foo/bar /\?\C:/foo\bar \/Server\Foo/Bar c:/foo//bar//baz].each do |path|
305
+ it "should return true for #{path}" do
306
+ Facter::Util::Resolution.should be_absolute_path(path)
307
+ end
308
+ end
309
+
310
+ %w[/ . ./foo \foo /foo /foo/../bar //foo C:foo/bar foo//bar/baz].each do |path|
311
+ it "should return false for #{path}" do
312
+ Facter::Util::Resolution.should_not be_absolute_path(path)
313
+ end
314
+ end
315
+ end
316
+ end
317
+
318
+ describe "#search_paths" do
319
+ context "on windows", :as_platform => :windows do
320
+ it "should use the PATH environment variable to determine locations" do
321
+ ENV.expects(:[]).with('PATH').returns 'C:\Windows;C:\Windows\System32'
322
+ Facter::Util::Resolution.search_paths.should == %w{C:\Windows C:\Windows\System32}
323
+ end
324
+ end
325
+
326
+ context "on posix", :as_platform => :posix do
327
+ it "should use the PATH environment variable plus /sbin and /usr/sbin on unix" do
328
+ ENV.expects(:[]).with('PATH').returns "/bin:/usr/bin"
329
+ Facter::Util::Resolution.search_paths.should == %w{/bin /usr/bin /sbin /usr/sbin}
330
+ end
331
+ end
332
+ end
333
+
334
+ describe "#which" do
335
+ context "when run on posix", :as_platform => :posix do
336
+ before :each do
337
+ Facter::Util::Resolution.stubs(:search_paths).returns [ '/bin', '/sbin', '/usr/sbin']
338
+ end
339
+
340
+ context "and provided with an absolute path" do
341
+ it "should return the binary if executable" do
342
+ File.expects(:executable?).with('/opt/foo').returns true
343
+ Facter::Util::Resolution.which('/opt/foo').should == '/opt/foo'
344
+ end
345
+
346
+ it "should return nil if the binary is not executable" do
347
+ File.expects(:executable?).with('/opt/foo').returns false
348
+ Facter::Util::Resolution.which('/opt/foo').should be_nil
349
+ end
350
+ end
351
+
352
+ context "and not provided with an absolute path" do
353
+ it "should return the absolute path if found" do
354
+ File.expects(:executable?).with('/bin/foo').returns false
355
+ File.expects(:executable?).with('/sbin/foo').returns true
356
+ File.expects(:executable?).with('/usr/sbin/foo').never
357
+ Facter::Util::Resolution.which('foo').should == '/sbin/foo'
358
+ end
359
+
360
+ it "should return nil if not found" do
361
+ File.expects(:executable?).with('/bin/foo').returns false
362
+ File.expects(:executable?).with('/sbin/foo').returns false
363
+ File.expects(:executable?).with('/usr/sbin/foo').returns false
364
+ Facter::Util::Resolution.which('foo').should be_nil
365
+ end
366
+ end
367
+ end
368
+
369
+ context "when run on windows", :as_platform => :windows do
370
+ before :each do
371
+ Facter::Util::Resolution.stubs(:search_paths).returns ['C:\Windows\system32', 'C:\Windows', 'C:\Windows\System32\Wbem' ]
372
+ ENV.stubs(:[]).with('PATHEXT').returns nil
373
+ end
374
+
375
+ context "and provided with an absolute path" do
376
+ it "should return the binary if executable" do
377
+ File.expects(:executable?).with('C:\Tools\foo.exe').returns true
378
+ File.expects(:executable?).with('\\\\remote\dir\foo.exe').returns true
379
+ Facter::Util::Resolution.which('C:\Tools\foo.exe').should == 'C:\Tools\foo.exe'
380
+ Facter::Util::Resolution.which('\\\\remote\dir\foo.exe').should == '\\\\remote\dir\foo.exe'
381
+ end
382
+
383
+ it "should return the binary with added extension if executable" do
384
+ ['.COM', '.BAT', '.CMD', '' ].each do |ext|
385
+ File.stubs(:executable?).with('C:\Windows\system32\netsh'+ext).returns false
386
+ end
387
+ File.expects(:executable?).with('C:\Windows\system32\netsh.EXE').returns true
388
+
389
+ 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)')
390
+ Facter::Util::Resolution.which('C:\Windows\system32\netsh').should == 'C:\Windows\system32\netsh.EXE'
391
+ end
392
+
393
+ it "should return nil if the binary is not executable" do
394
+ File.expects(:executable?).with('C:\Tools\foo.exe').returns false
395
+ File.expects(:executable?).with('\\\\remote\dir\foo.exe').returns false
396
+ Facter::Util::Resolution.which('C:\Tools\foo.exe').should be_nil
397
+ Facter::Util::Resolution.which('\\\\remote\dir\foo.exe').should be_nil
398
+ end
399
+ end
400
+
401
+ context "and not provided with an absolute path" do
402
+ it "should return the absolute path if found" do
403
+ File.expects(:executable?).with('C:\Windows\system32\foo.exe').returns false
404
+ File.expects(:executable?).with('C:\Windows\foo.exe').returns true
405
+ File.expects(:executable?).with('C:\Windows\System32\Wbem\foo.exe').never
406
+ Facter::Util::Resolution.which('foo.exe').should == 'C:\Windows\foo.exe'
407
+ end
408
+
409
+ it "should return the absolute path with file extension if found" do
410
+ ['.COM', '.EXE', '.BAT', '.CMD', '' ].each do |ext|
411
+ File.stubs(:executable?).with('C:\Windows\system32\foo'+ext).returns false
412
+ File.stubs(:executable?).with('C:\Windows\System32\Wbem\foo'+ext).returns false
413
+ end
414
+ ['.COM', '.BAT', '.CMD', '' ].each do |ext|
415
+ File.stubs(:executable?).with('C:\Windows\foo'+ext).returns false
416
+ end
417
+ File.stubs(:executable?).with('C:\Windows\foo.EXE').returns true
418
+
419
+ Facter::Util::Resolution.which('foo').should == 'C:\Windows\foo.EXE'
420
+ end
421
+
422
+ it "should return nil if not found" do
423
+ File.expects(:executable?).with('C:\Windows\system32\foo.exe').returns false
424
+ File.expects(:executable?).with('C:\Windows\foo.exe').returns false
425
+ File.expects(:executable?).with('C:\Windows\System32\Wbem\foo.exe').returns false
426
+ Facter::Util::Resolution.which('foo.exe').should be_nil
427
+ end
428
+ end
429
+ end
430
+
431
+ describe "#expand_command" do
432
+ context "on windows", :as_platform => :windows do
433
+ it "should expand binary" do
434
+ Facter::Util::Resolution.expects(:which).with('cmd').returns 'C:\Windows\System32\cmd'
435
+ Facter::Util::Resolution.expand_command(
436
+ 'cmd /c echo foo > C:\bar'
437
+ ).should == 'C:\Windows\System32\cmd /c echo foo > C:\bar'
438
+ end
439
+
440
+ it "should expand double quoted binary" do
441
+ Facter::Util::Resolution.expects(:which).with('my foo').returns 'C:\My Tools\my foo.exe'
442
+ Facter::Util::Resolution.expand_command('"my foo" /a /b').should == '"C:\My Tools\my foo.exe" /a /b'
443
+ end
444
+
445
+ it "should not expand single quoted binary" do
446
+ Facter::Util::Resolution.expects(:which).with('\'C:\My').returns nil
447
+ Facter::Util::Resolution.expand_command('\'C:\My Tools\foo.exe\' /a /b').should be_nil
448
+ end
449
+
450
+ it "should quote expanded binary if found in path with spaces" do
451
+ Facter::Util::Resolution.expects(:which).with('foo').returns 'C:\My Tools\foo.exe'
452
+ Facter::Util::Resolution.expand_command('foo /a /b').should == '"C:\My Tools\foo.exe" /a /b'
453
+ end
454
+
455
+ it "should return nil if not found" do
456
+ Facter::Util::Resolution.expects(:which).with('foo').returns nil
457
+ Facter::Util::Resolution.expand_command('foo /a | stuff >> /dev/null').should be_nil
458
+ end
459
+ end
460
+
461
+ context "on unix", :as_platform => :posix do
462
+ it "should expand binary" do
463
+ Facter::Util::Resolution.expects(:which).with('foo').returns '/bin/foo'
464
+ Facter::Util::Resolution.expand_command('foo -a | stuff >> /dev/null').should == '/bin/foo -a | stuff >> /dev/null'
465
+ end
466
+
467
+ it "should expand double quoted binary" do
468
+ Facter::Util::Resolution.expects(:which).with('/tmp/my foo').returns '/tmp/my foo'
469
+ Facter::Util::Resolution.expand_command(%q{"/tmp/my foo" bar}).should == %q{"/tmp/my foo" bar}
470
+ end
471
+
472
+ it "should expand single quoted binary" do
473
+ Facter::Util::Resolution.expects(:which).with('my foo').returns '/home/bob/my path/my foo'
474
+ Facter::Util::Resolution.expand_command(%q{'my foo' -a}).should == %q{'/home/bob/my path/my foo' -a}
475
+ end
476
+
477
+ it "should quote expanded binary if found in path with spaces" do
478
+ Facter::Util::Resolution.expects(:which).with('foo.sh').returns '/home/bob/my tools/foo.sh'
479
+ Facter::Util::Resolution.expand_command('foo.sh /a /b').should == %q{'/home/bob/my tools/foo.sh' /a /b}
480
+ end
481
+
482
+ it "should return nil if not found" do
483
+ Facter::Util::Resolution.expects(:which).with('foo').returns nil
484
+ Facter::Util::Resolution.expand_command('foo -a | stuff >> /dev/null').should be_nil
485
+ end
486
+ end
487
+ end
488
+
489
+ end
490
+
287
491
  # It's not possible, AFAICT, to mock %x{}, so I can't really test this bit.
288
492
  describe "when executing code" do
289
493
  it "should deprecate the interpreter parameter" do
@@ -292,41 +496,73 @@ describe Facter::Util::Resolution do
292
496
  end
293
497
 
294
498
  it "should execute the binary" do
295
- Facter::Util::Resolution.exec("echo foo").should == "foo"
499
+ test_command = Facter::Util::Config.is_windows? ? 'cmd.exe /c echo foo' : 'echo foo'
500
+ Facter::Util::Resolution.exec(test_command).should == "foo"
296
501
  end
297
- end
298
502
 
299
- describe "have_which" do
300
- before :each do
301
- Facter::Util::Resolution.instance_variable_set(:@have_which, nil)
503
+ context "when run on unix", :as_platform => :posix do
504
+ context "binary is present" do
505
+ it "should run the command if path to binary is absolute" do
506
+ Facter::Util::Resolution.expects(:expand_command).with('/usr/bin/uname -m').returns('/usr/bin/uname -m')
507
+ Facter::Util::Resolution.expects(:`).with('/usr/bin/uname -m').returns 'x86_64'
508
+ Facter::Util::Resolution.exec('/usr/bin/uname -m').should == 'x86_64'
509
+ end
302
510
 
303
- # we do not execute anything in the following test cases itself
304
- # but we rely on $? to be an instance of Process::Status. So
305
- # just execute anything here to make sure that $? is not nil
306
- %x{echo foo}
307
- end
511
+ it "should run the expanded command if path to binary not absolute" do
512
+ Facter::Util::Resolution.expects(:expand_command).with('uname -m').returns('/usr/bin/uname -m')
513
+ Facter::Util::Resolution.expects(:`).with('/usr/bin/uname -m').returns 'x86_64'
514
+ Facter::Util::Resolution.exec('uname -m').should == 'x86_64'
515
+ end
516
+ end
308
517
 
309
- it "on windows should always return false" do
310
- Facter::Util::Config.stubs(:is_windows?).returns(true)
311
- Facter::Util::Resolution.expects(:`).
312
- with('which which >/dev/null 2>&1').never
313
- Facter::Util::Resolution.have_which.should == false
518
+ context "binary is not present" do
519
+ it "should not run the command if path to binary is absolute" do
520
+ Facter::Util::Resolution.expects(:expand_command).with('/usr/bin/uname -m').returns nil
521
+ Facter::Util::Resolution.expects(:`).with('/usr/bin/uname -m').never
522
+ Facter::Util::Resolution.exec('/usr/bin/uname -m').should be_nil
523
+ end
524
+ it "should not run the command if path to binary is not absolute" do
525
+ Facter::Util::Resolution.expects(:expand_command).with('uname -m').returns nil
526
+ Facter::Util::Resolution.expects(:`).with('uname -m').never
527
+ Facter::Util::Resolution.exec('uname -m').should be_nil
528
+ end
529
+ end
314
530
  end
315
531
 
316
- it "on other platforms than windows should return true if which exists" do
317
- Facter::Util::Config.stubs(:is_windows?).returns(false)
318
- Facter::Util::Resolution.expects(:`).
319
- with('which which >/dev/null 2>&1').returns('')
320
- Process::Status.any_instance.stubs(:success?).returns true
321
- Facter::Util::Resolution.have_which.should == true
322
- end
532
+ context "when run on windows", :as_platform => :windows do
533
+ context "binary is present" do
534
+ it "should run the command if path to binary is absolute" do
535
+ Facter::Util::Resolution.expects(:expand_command).with(%q{C:\Windows\foo.exe /a /b}).returns(%q{C:\Windows\foo.exe /a /b})
536
+ Facter::Util::Resolution.expects(:`).with(%q{C:\Windows\foo.exe /a /b}).returns 'bar'
537
+ Facter::Util::Resolution.exec(%q{C:\Windows\foo.exe /a /b}).should == 'bar'
538
+ end
323
539
 
324
- it "on other platforms than windows should return false if which returns non-zero exit code" do
325
- Facter::Util::Config.stubs(:is_windows?).returns(false)
326
- Facter::Util::Resolution.expects(:`).
327
- with('which which >/dev/null 2>&1').returns('')
328
- Process::Status.any_instance.stubs(:success?).returns false
329
- Facter::Util::Resolution.have_which.should == false
540
+ it "should run the expanded command if path to binary not absolute" do
541
+ Facter::Util::Resolution.expects(:expand_command).with(%q{foo.exe /a /b}).returns(%q{C:\Windows\foo.exe /a /b})
542
+ Facter::Util::Resolution.expects(:`).with(%q{C:\Windows\foo.exe /a /b}).returns 'bar'
543
+ Facter::Util::Resolution.exec(%q{foo.exe /a /b}).should == 'bar'
544
+ end
545
+ end
546
+
547
+ context "binary is not present" do
548
+ it "should not run the command if path to binary is absolute" do
549
+ Facter::Util::Resolution.expects(:expand_command).with(%q{C:\Windows\foo.exe /a /b}).returns nil
550
+ Facter::Util::Resolution.expects(:`).with(%q{C:\Windows\foo.exe /a /b}).never
551
+ Facter::Util::Resolution.exec(%q{C:\Windows\foo.exe /a /b}).should be_nil
552
+ end
553
+ it "should try to run the command and return output of a shell-builtin" do
554
+ Facter::Util::Resolution.expects(:expand_command).with(%q{echo foo}).returns nil
555
+ Facter::Util::Resolution.expects(:`).with(%q{echo foo}).returns 'foo'
556
+ Facter.expects(:warnonce).with('Using Facter::Util::Resolution.exec with a shell built-in is deprecated. Most built-ins can be replaced with native ruby commands. If you really have to run a built-in, pass "cmd /c your_builtin" as a command')
557
+ Facter::Util::Resolution.exec(%q{echo foo}).should == 'foo'
558
+ end
559
+ it "should try to run the command and return nil if not shell-builtin" do
560
+ Facter::Util::Resolution.expects(:expand_command).with(%q{echo foo}).returns nil
561
+ Facter::Util::Resolution.stubs(:`).with(%q{echo foo}).raises Errno::ENOENT, 'some_error_message'
562
+ Facter.expects(:warnonce).never
563
+ Facter::Util::Resolution.exec(%q{echo foo}).should be_nil
564
+ end
565
+ end
330
566
  end
331
567
  end
332
568
  end