facter 1.7.6 → 2.0.1.rc1

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.

Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/COMMITTERS.md +194 -0
  3. data/CONTRIBUTING.md +63 -235
  4. data/Gemfile +12 -8
  5. data/README.md +1 -2
  6. data/Rakefile +1 -1
  7. data/bin/facter +0 -4
  8. data/ext/build_defaults.yaml +2 -2
  9. data/ext/project_data.yaml +18 -0
  10. data/install.rb +1 -16
  11. data/lib/facter.rb +171 -171
  12. data/lib/facter/application.rb +65 -54
  13. data/lib/facter/core/aggregate.rb +220 -0
  14. data/lib/facter/core/directed_graph.rb +46 -0
  15. data/lib/facter/core/execution.rb +100 -0
  16. data/lib/facter/core/execution/base.rb +73 -0
  17. data/lib/facter/core/execution/posix.rb +50 -0
  18. data/lib/facter/core/execution/windows.rb +57 -0
  19. data/lib/facter/core/logging.rb +169 -0
  20. data/lib/facter/core/resolvable.rb +94 -0
  21. data/lib/facter/core/suitable.rb +117 -0
  22. data/lib/facter/domain.rb +15 -9
  23. data/lib/facter/filesystems.rb +1 -1
  24. data/lib/facter/hardwaremodel.rb +1 -1
  25. data/lib/facter/hostname.rb +3 -3
  26. data/lib/facter/interfaces.rb +6 -1
  27. data/lib/facter/ipaddress.rb +2 -2
  28. data/lib/facter/kernel.rb +1 -1
  29. data/lib/facter/kernelrelease.rb +1 -1
  30. data/lib/facter/ldom.rb +1 -1
  31. data/lib/facter/lsbdistcodename.rb +1 -1
  32. data/lib/facter/lsbdistdescription.rb +1 -1
  33. data/lib/facter/lsbdistid.rb +1 -1
  34. data/lib/facter/lsbdistrelease.rb +1 -1
  35. data/lib/facter/lsbrelease.rb +1 -1
  36. data/lib/facter/macaddress.rb +1 -14
  37. data/lib/facter/macosx.rb +2 -2
  38. data/lib/facter/memory.rb +8 -19
  39. data/lib/facter/operatingsystem.rb +1 -1
  40. data/lib/facter/operatingsystemrelease.rb +34 -1
  41. data/lib/facter/physicalprocessorcount.rb +6 -6
  42. data/lib/facter/processor.rb +11 -10
  43. data/lib/facter/selinux.rb +4 -15
  44. data/lib/facter/ssh.rb +5 -2
  45. data/lib/facter/util/architecture.rb +2 -2
  46. data/lib/facter/util/collection.rb +42 -38
  47. data/lib/facter/util/config.rb +19 -9
  48. data/lib/facter/util/confine.rb +34 -4
  49. data/lib/facter/util/ec2.rb +1 -1
  50. data/lib/facter/util/fact.rb +108 -36
  51. data/lib/facter/util/file_read.rb +7 -6
  52. data/lib/facter/util/formatter.rb +38 -0
  53. data/lib/facter/util/ip.rb +3 -3
  54. data/lib/facter/util/loader.rb +62 -42
  55. data/lib/facter/util/macosx.rb +7 -8
  56. data/lib/facter/util/manufacturer.rb +3 -3
  57. data/lib/facter/util/memory.rb +13 -13
  58. data/lib/facter/util/monkey_patches.rb +0 -31
  59. data/lib/facter/util/netmask.rb +3 -3
  60. data/lib/facter/util/normalization.rb +94 -0
  61. data/lib/facter/util/nothing_loader.rb +3 -6
  62. data/lib/facter/util/parser.rb +3 -5
  63. data/lib/facter/util/plist/generator.rb +1 -1
  64. data/lib/facter/util/processor.rb +15 -15
  65. data/lib/facter/util/resolution.rb +112 -289
  66. data/lib/facter/util/solaris_zones.rb +4 -4
  67. data/lib/facter/util/uptime.rb +8 -3
  68. data/lib/facter/util/values.rb +67 -1
  69. data/lib/facter/util/virtual.rb +10 -10
  70. data/lib/facter/util/xendomains.rb +1 -1
  71. data/lib/facter/version.rb +42 -39
  72. data/lib/facter/virtual.rb +6 -7
  73. data/lib/facter/zfs_version.rb +3 -3
  74. data/lib/facter/zpool_version.rb +3 -3
  75. data/spec/fixtures/unit/netmask/darwin_10_8_5.txt +30 -0
  76. data/spec/unit/application_spec.rb +46 -1
  77. data/spec/unit/core/aggregate_spec.rb +125 -0
  78. data/spec/unit/core/directed_graph_spec.rb +79 -0
  79. data/spec/unit/core/execution/base_spec.rb +119 -0
  80. data/spec/unit/core/execution/posix_spec.rb +86 -0
  81. data/spec/unit/core/execution/windows_spec.rb +106 -0
  82. data/spec/unit/core/execution_spec.rb +37 -0
  83. data/spec/unit/core/logging_spec.rb +104 -0
  84. data/spec/unit/core/resolvable_spec.rb +81 -0
  85. data/spec/unit/core/suitable_spec.rb +96 -0
  86. data/spec/unit/domain_spec.rb +5 -5
  87. data/spec/unit/facter_spec.rb +61 -222
  88. data/spec/unit/filesystems_spec.rb +2 -2
  89. data/spec/unit/hardwareisa_spec.rb +5 -5
  90. data/spec/unit/hardwaremodel_spec.rb +1 -1
  91. data/spec/unit/hostname_spec.rb +4 -4
  92. data/spec/unit/id_spec.rb +3 -3
  93. data/spec/unit/interfaces_spec.rb +10 -0
  94. data/spec/unit/ipaddress6_spec.rb +4 -4
  95. data/spec/unit/ipaddress_spec.rb +1 -1
  96. data/spec/unit/kernel_spec.rb +2 -2
  97. data/spec/unit/kernelmajversion_spec.rb +1 -1
  98. data/spec/unit/kernelrelease_spec.rb +4 -4
  99. data/spec/unit/kernelversion_spec.rb +2 -2
  100. data/spec/unit/ldom_spec.rb +2 -2
  101. data/spec/unit/lsbdistcodename_spec.rb +2 -2
  102. data/spec/unit/lsbdistdescription_spec.rb +2 -2
  103. data/spec/unit/lsbdistid_spec.rb +2 -2
  104. data/spec/unit/lsbdistrelease_spec.rb +2 -2
  105. data/spec/unit/lsbrelease_spec.rb +2 -2
  106. data/spec/unit/manufacturer_spec.rb +1 -1
  107. data/spec/unit/memory_spec.rb +24 -31
  108. data/spec/unit/netmask_spec.rb +9 -0
  109. data/spec/unit/operatingsystem_spec.rb +1 -1
  110. data/spec/unit/operatingsystemrelease_spec.rb +62 -4
  111. data/spec/unit/physicalprocessorcount_spec.rb +10 -10
  112. data/spec/unit/processor_spec.rb +11 -11
  113. data/spec/unit/selinux_spec.rb +2 -8
  114. data/spec/unit/ssh_spec.rb +3 -2
  115. data/spec/unit/uniqueid_spec.rb +3 -3
  116. data/spec/unit/util/collection_spec.rb +37 -35
  117. data/spec/unit/util/config_spec.rb +20 -0
  118. data/spec/unit/util/confine_spec.rb +21 -0
  119. data/spec/unit/util/directory_loader_spec.rb +1 -0
  120. data/spec/unit/util/ec2_spec.rb +6 -6
  121. data/spec/unit/util/fact_spec.rb +92 -90
  122. data/spec/unit/util/ip_spec.rb +2 -2
  123. data/spec/unit/util/loader_spec.rb +127 -186
  124. data/spec/unit/util/macaddress_spec.rb +2 -2
  125. data/spec/unit/util/macosx_spec.rb +8 -8
  126. data/spec/unit/util/manufacturer_spec.rb +3 -3
  127. data/spec/unit/util/normalization_spec.rb +113 -0
  128. data/spec/unit/util/parser_spec.rb +25 -3
  129. data/spec/unit/util/processor_spec.rb +2 -2
  130. data/spec/unit/util/resolution_spec.rb +60 -631
  131. data/spec/unit/util/solaris_zones_spec.rb +5 -5
  132. data/spec/unit/util/uptime_spec.rb +1 -1
  133. data/spec/unit/util/values_spec.rb +131 -0
  134. data/spec/unit/util/virtual_spec.rb +16 -16
  135. data/spec/unit/util/xendomains_spec.rb +2 -2
  136. data/spec/unit/virtual_spec.rb +39 -39
  137. data/spec/unit/zfs_version_spec.rb +11 -11
  138. data/spec/unit/zonename_spec.rb +2 -2
  139. data/spec/unit/zones_spec.rb +1 -1
  140. data/spec/unit/zpool_version_spec.rb +11 -11
  141. metadata +466 -447
  142. data/lib/facter/util/cfpropertylist.rb +0 -6
  143. data/lib/facter/util/cfpropertylist/LICENSE +0 -19
  144. data/lib/facter/util/cfpropertylist/README +0 -44
  145. data/lib/facter/util/cfpropertylist/Rakefile +0 -44
  146. data/lib/facter/util/cfpropertylist/THANKS +0 -7
  147. data/lib/facter/util/cfpropertylist/lib/cfpropertylist.rb +0 -6
  148. data/lib/facter/util/cfpropertylist/lib/rbBinaryCFPropertyList.rb +0 -562
  149. data/lib/facter/util/cfpropertylist/lib/rbCFPlistError.rb +0 -26
  150. data/lib/facter/util/cfpropertylist/lib/rbCFPropertyList.rb +0 -407
  151. data/lib/facter/util/cfpropertylist/lib/rbCFTypes.rb +0 -244
  152. data/lib/facter/util/cfpropertylist/lib/rbLibXMLParser.rb +0 -135
  153. data/lib/facter/util/cfpropertylist/lib/rbNokogiriParser.rb +0 -140
  154. data/lib/facter/util/cfpropertylist/lib/rbREXMLParser.rb +0 -136
  155. data/spec/unit/util/monkey_patches_spec.rb +0 -42
@@ -123,5 +123,26 @@ describe Facter::Util::Confine do
123
123
  it "should return false if none of the provided ranges matches the fact's value" do
124
124
  confined(8, (5..7)).should be_false
125
125
  end
126
+
127
+ it "should accept and evaluate a block argument against the fact" do
128
+ @fact.expects(:value).returns 'foo'
129
+ confine = Facter::Util::Confine.new :yay do |f| f === 'foo' end
130
+ confine.true?.should be_true
131
+ end
132
+
133
+ it "should return false if the block raises a StandardError when checking a fact" do
134
+ @fact.stubs(:value).returns 'foo'
135
+ confine = Facter::Util::Confine.new :yay do |f| raise StandardError end
136
+ confine.true?.should be_false
137
+ end
138
+
139
+ it "should accept and evaluate only a block argument" do
140
+ Facter::Util::Confine.new { true }.true?.should be_true
141
+ Facter::Util::Confine.new { false }.true?.should be_false
142
+ end
143
+
144
+ it "should return false if the block raises a StandardError" do
145
+ Facter::Util::Confine.new { raise StandardError }.true?.should be_false
146
+ end
126
147
  end
127
148
  end
@@ -55,6 +55,7 @@ describe Facter::Util::DirectoryLoader do
55
55
 
56
56
  %w{bak orig}.each do |ext|
57
57
  it "should ignore files with an extension of '#{ext}'" do
58
+ Facter.expects(:warn).with(regexp_matches(/#{ext}/))
58
59
  write_to_file("data" + ".#{ext}", "foo=bar")
59
60
 
60
61
  subject.load(collection)
@@ -17,21 +17,21 @@ describe Facter::Util::EC2 do
17
17
 
18
18
  it "should succeed if arp table contains fe:ff:ff:ff:ff:ff" do
19
19
  ec2arp = my_fixture_read("linux-arp-ec2.out")
20
- Facter::Util::Resolution.expects(:exec).with("arp -an").\
20
+ Facter::Core::Execution.expects(:exec).with("arp -an").\
21
21
  at_least_once.returns(ec2arp)
22
22
  Facter::Util::EC2.has_ec2_arp?.should == true
23
23
  end
24
24
 
25
25
  it "should succeed if arp table contains FE:FF:FF:FF:FF:FF" do
26
26
  ec2arp = my_fixture_read("centos-arp-ec2.out")
27
- Facter::Util::Resolution.expects(:exec).with("arp -an").\
27
+ Facter::Core::Execution.expects(:exec).with("arp -an").\
28
28
  at_least_once.returns(ec2arp)
29
29
  Facter::Util::EC2.has_ec2_arp?.should == true
30
30
  end
31
31
 
32
32
  it "should fail if arp table does not contain fe:ff:ff:ff:ff:ff" do
33
33
  ec2arp = my_fixture_read("linux-arp-not-ec2.out")
34
- Facter::Util::Resolution.expects(:exec).with("arp -an").
34
+ Facter::Core::Execution.expects(:exec).with("arp -an").
35
35
  at_least_once.returns(ec2arp)
36
36
  Facter::Util::EC2.has_ec2_arp?.should == false
37
37
  end
@@ -45,14 +45,14 @@ describe Facter::Util::EC2 do
45
45
 
46
46
  it "should succeed if arp table contains fe-ff-ff-ff-ff-ff" do
47
47
  ec2arp = my_fixture_read("windows-2008-arp-a.out")
48
- Facter::Util::Resolution.expects(:exec).with("arp -a").\
48
+ Facter::Core::Execution.expects(:exec).with("arp -a").\
49
49
  at_least_once.returns(ec2arp)
50
50
  Facter::Util::EC2.has_ec2_arp?.should == true
51
51
  end
52
52
 
53
53
  it "should fail if arp table does not contain fe-ff-ff-ff-ff-ff" do
54
54
  ec2arp = my_fixture_read("windows-2008-arp-a-not-ec2.out")
55
- Facter::Util::Resolution.expects(:exec).with("arp -a").
55
+ Facter::Core::Execution.expects(:exec).with("arp -a").
56
56
  at_least_once.returns(ec2arp)
57
57
  Facter::Util::EC2.has_ec2_arp?.should == false
58
58
  end
@@ -66,7 +66,7 @@ describe Facter::Util::EC2 do
66
66
  it "should fail if arp table does not contain fe:ff:ff:ff:ff:ff" do
67
67
  ec2arp = my_fixture_read("solaris8_arp_a_not_ec2.out")
68
68
 
69
- Facter::Util::Resolution.expects(:exec).with("arp -a").
69
+ Facter::Core::Execution.expects(:exec).with("arp -a").
70
70
  at_least_once.returns(ec2arp)
71
71
 
72
72
  Facter::Util::EC2.has_ec2_arp?.should == false
@@ -4,108 +4,123 @@ require 'spec_helper'
4
4
  require 'facter/util/fact'
5
5
 
6
6
  describe Facter::Util::Fact do
7
- it "should require a name" do
8
- lambda { Facter::Util::Fact.new }.should raise_error(ArgumentError)
9
- end
10
7
 
11
- it "should always downcase the name and convert it to a symbol" do
12
- Facter::Util::Fact.new("YayNess").name.should == :yayness
13
- end
8
+ subject(:fact) { Facter::Util::Fact.new("yay") }
14
9
 
15
- it "should default to its name converted to a string as its ldapname" do
16
- Facter::Util::Fact.new("YayNess").ldapname.should == "yayness"
10
+ let(:resolution) { Facter::Util::Resolution.new("yay", fact) }
11
+
12
+ it "requires a name" do
13
+ expect { Facter::Util::Fact.new }.to raise_error(ArgumentError)
17
14
  end
18
15
 
19
- it "should allow specifying the ldap name at initialization" do
20
- Facter::Util::Fact.new("YayNess", :ldapname => "fooness").ldapname.should == "fooness"
16
+ it "downcases and converts the name to a symbol" do
17
+ expect(Facter::Util::Fact.new("YayNess").name).to eq :yayness
21
18
  end
22
19
 
23
- it "should fail if an unknown option is provided" do
24
- lambda { Facter::Util::Fact.new('yay', :foo => :bar) }.should raise_error(ArgumentError)
20
+ it "issues a deprecation warning for use of ldapname" do
21
+ Facter.expects(:warnonce).with("ldapname is deprecated and will be removed in a future version")
22
+ Facter::Util::Fact.new("YayNess", :ldapname => "fooness")
25
23
  end
26
24
 
27
- it "should have a method for adding resolution mechanisms" do
28
- Facter::Util::Fact.new("yay").should respond_to(:add)
25
+ describe "when adding resolution mechanisms using #add" do
26
+ it "delegates to #define_resolution with an anonymous resolution" do
27
+ subject.expects(:define_resolution).with(nil, {})
28
+ subject.add
29
+ end
29
30
  end
30
31
 
31
- describe "when adding resolution mechanisms" do
32
- before do
33
- @fact = Facter::Util::Fact.new("yay")
32
+ describe "looking up resolutions by name" do
33
+ subject(:fact) { described_class.new('yay') }
34
34
 
35
- @resolution = Facter::Util::Resolution.new("yay")
35
+ it "returns nil if no such resolution exists" do
36
+ expect(fact.resolution('nope')).to be_nil
36
37
  end
37
38
 
38
- it "should be to create a new resolution instance with a block" do
39
- Facter::Util::Resolution.expects(:new).returns @resolution
39
+ it "never returns anonymous resolutions" do
40
+ fact.add() { setcode { 'anonymous' } }
40
41
 
41
- @fact.add { }
42
- end
43
- it "should instance_eval the passed block on the new resolution" do
44
- @fact.add {
45
- setcode { "foo" }
46
- }
47
- @fact.value.should == "foo"
42
+ expect(fact.resolution(nil)).to be_nil
48
43
  end
44
+ end
45
+
46
+ describe "adding resolution mechanisms by name" do
49
47
 
50
- it "should re-sort the resolutions by weight, so the most restricted resolutions are first" do
51
- @fact.add { self.value = "1"; self.weight = 1 }
52
- @fact.add { self.value = "2"; self.weight = 2 }
53
- @fact.add { self.value = "0"; self.weight = 0 }
54
- @fact.value.should == "2"
48
+ let(:res) do
49
+ stub 'resolution',
50
+ :name => 'named',
51
+ :set_options => nil,
52
+ :resolution_type => :simple
55
53
  end
56
- end
57
54
 
58
- it "should be able to return a value" do
59
- Facter::Util::Fact.new("yay").should respond_to(:value)
60
- end
55
+ it "creates a new resolution if no such resolution exists" do
56
+ Facter::Util::Resolution.expects(:new).once.with('named', fact).returns(res)
61
57
 
62
- describe "when returning a value" do
63
- before do
64
- @fact = Facter::Util::Fact.new("yay")
58
+ fact.define_resolution('named')
59
+
60
+ expect(fact.resolution('named')).to eq res
65
61
  end
66
62
 
67
- it "should return nil if there are no resolutions" do
68
- Facter::Util::Fact.new("yay").value.should be_nil
63
+ it "creates a simple resolution when the type is nil" do
64
+ fact.define_resolution('named')
65
+ expect(fact.resolution('named')).to be_a_kind_of Facter::Util::Resolution
66
+ end
67
+
68
+ it "creates a simple resolution when the type is :simple" do
69
+ fact.define_resolution('named', :type => :simple)
70
+ expect(fact.resolution('named')).to be_a_kind_of Facter::Util::Resolution
69
71
  end
70
72
 
71
- it "should return the first value returned by a resolution" do
72
- r1 = stub 'r1', :weight => 2, :value => nil, :suitable? => true
73
- r2 = stub 'r2', :weight => 1, :value => "yay", :suitable? => true
74
- r3 = stub 'r3', :weight => 0, :value => "foo", :suitable? => true
75
- Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3)
76
- @fact.add { }
77
- @fact.add { }
78
- @fact.add { }
73
+ it "creates an aggregate resolution when the type is :aggregate" do
74
+ fact.define_resolution('named', :type => :aggregate)
75
+ expect(fact.resolution('named')).to be_a_kind_of Facter::Core::Aggregate
76
+ end
79
77
 
80
- @fact.value.should == "yay"
78
+ it "raises an error if there is an existing resolution with a different type" do
79
+ pending "We need to stop rescuing all errors when instantiating resolutions"
80
+ fact.define_resolution('named')
81
+ expect {
82
+ fact.define_resolution('named', :type => :aggregate)
83
+ }.to raise_error(ArgumentError, /Cannot return resolution.*already defined as simple/)
81
84
  end
82
85
 
83
- it "should short-cut returning the value once one is found" do
84
- r1 = stub 'r1', :weight => 2, :value => "foo", :suitable? => true
85
- r2 = stub 'r2', :weight => 1, :suitable? => true # would fail if 'value' were asked for
86
- Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2)
87
- @fact.add { }
88
- @fact.add { }
86
+ it "returns existing resolutions by name" do
87
+ Facter::Util::Resolution.expects(:new).once.with('named', fact).returns(res)
88
+
89
+ fact.define_resolution('named')
90
+ fact.define_resolution('named')
89
91
 
90
- @fact.value
92
+ expect(fact.resolution('named')).to eq res
93
+ end
94
+ end
95
+
96
+ describe "when returning a value" do
97
+ it "returns nil if there are no resolutions" do
98
+ Facter::Util::Fact.new("yay").value.should be_nil
91
99
  end
92
100
 
93
- it "should skip unsuitable resolutions" do
94
- r1 = stub 'r1', :weight => 2, :suitable? => false # would fail if 'value' were asked for'
95
- r2 = stub 'r2', :weight => 1, :value => "yay", :suitable? => true
96
- Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2)
97
- @fact.add { }
98
- @fact.add { }
101
+ it "prefers the highest weight resolution" do
102
+ fact.add { has_weight 1; setcode { "1" } }
103
+ fact.add { has_weight 2; setcode { "2" } }
104
+ fact.add { has_weight 0; setcode { "0" } }
105
+ expect(fact.value).to eq "2"
106
+ end
99
107
 
100
- @fact.value.should == "yay"
108
+ it "returns the first value returned by a resolution" do
109
+ fact.add { has_weight 1; setcode { "1" } }
110
+ fact.add { has_weight 2; setcode { nil } }
111
+ fact.add { has_weight 0; setcode { "0" } }
112
+ expect(fact.value).to eq "1"
101
113
  end
102
114
 
103
- it "should return nil if the value is the empty string" do
104
- r1 = stub 'r1', :suitable? => true, :value => ""
105
- Facter::Util::Resolution.expects(:new).returns r1
106
- @fact.add { }
115
+ it "skips unsuitable resolutions" do
116
+ fact.add { has_weight 1; setcode { "1" } }
117
+ fact.add do
118
+ def suitable?; false; end
119
+ has_weight 2
120
+ setcode { 2 }
121
+ end
107
122
 
108
- @fact.value.should be_nil
123
+ expect(fact.value).to eq "1"
109
124
  end
110
125
  end
111
126
 
@@ -113,28 +128,15 @@ describe Facter::Util::Fact do
113
128
  subject do
114
129
  Facter::Util::Fact.new(:foo)
115
130
  end
116
- context 'basic facts using setcode' do
117
- it "flushes the cached value when invoked" do
118
- system = mock('some_system_call')
119
- system.expects(:data).twice.returns(100,200)
120
131
 
121
- subject.add { setcode { system.data } }
122
- 5.times { subject.value.should == 100 }
123
- subject.flush
124
- subject.value.should == 200
125
- end
126
- end
127
- context 'facts using setcode and on_flush' do
128
- it 'invokes the block passed to on_flush' do
129
- model = { :data => "Hello World" }
130
- subject.add do
131
- on_flush { model[:data] = "FLUSHED!" }
132
- setcode { model[:data] }
133
- end
134
- subject.value.should == "Hello World"
135
- subject.flush
136
- subject.value.should == "FLUSHED!"
137
- end
132
+ it "invokes #flush on all resolutions" do
133
+ simple = subject.add(:type => :simple)
134
+ simple.expects(:flush)
135
+
136
+ aggregate = subject.add(:type => :aggregate)
137
+ aggregate.expects(:flush)
138
+
139
+ subject.flush
138
140
  end
139
141
  end
140
142
  end
@@ -408,14 +408,14 @@ describe Facter::Util::IP do
408
408
  it "support additional arguments" do
409
409
  Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
410
410
 
411
- Facter::Util::Resolution.stubs(:exec).with("/sbin/ifconfig -a")
411
+ Facter::Core::Execution.stubs(:exec).with("/sbin/ifconfig -a")
412
412
 
413
413
  Facter::Util::IP.exec_ifconfig(["-a"])
414
414
  end
415
415
  it "joins multiple arguments correctly" do
416
416
  Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
417
417
 
418
- Facter::Util::Resolution.stubs(:exec).with("/sbin/ifconfig -a -e -i -j")
418
+ Facter::Core::Execution.stubs(:exec).with("/sbin/ifconfig -a -e -i -j")
419
419
 
420
420
  Facter::Util::IP.exec_ifconfig(["-a","-e","-i","-j"])
421
421
  end
@@ -3,24 +3,19 @@
3
3
  require 'spec_helper'
4
4
  require 'facter/util/loader'
5
5
 
6
- # loader subclass for making assertions about file/directory ordering
7
- class TestLoader < Facter::Util::Loader
8
- def loaded_files
9
- @loaded_files ||= []
10
- end
11
-
12
- def load_file(file)
13
- loaded_files << file
14
- super
15
- end
16
- end
17
-
18
-
19
6
  describe Facter::Util::Loader do
20
7
  before :each do
21
8
  Facter::Util::Loader.any_instance.unstub(:load_all)
22
9
  end
23
10
 
11
+ def loader_from(places)
12
+ env = places[:env] || {}
13
+ search_path = places[:search_path] || []
14
+ loader = Facter::Util::Loader.new(env)
15
+ loader.stubs(:search_path).returns search_path
16
+ loader
17
+ end
18
+
24
19
  it "should have a method for loading individual facts by name" do
25
20
  Facter::Util::Loader.new.should respond_to(:load)
26
21
  end
@@ -34,20 +29,7 @@ describe Facter::Util::Loader do
34
29
  end
35
30
 
36
31
  describe "#valid_seach_path?" do
37
- before :each do
38
- @loader = Facter::Util::Loader.new
39
- @settings = mock 'settings'
40
- @settings.stubs(:value).returns "/eh"
41
- end
42
-
43
- it "should cache the result of a previous check" do
44
- Pathname.any_instance.expects(:absolute?).returns(true).once
45
-
46
- # we explicitly want two calls here to check that we get
47
- # the second from the cache
48
- @loader.should be_valid_search_path "/foo"
49
- @loader.should be_valid_search_path "/foo"
50
- end
32
+ let(:loader) { Facter::Util::Loader.new }
51
33
 
52
34
  # Used to have test for " " as a directory since that should
53
35
  # be a relative directory, but on Windows in both 1.8.7 and
@@ -68,7 +50,7 @@ describe Facter::Util::Loader do
68
50
  ' \/',
69
51
  ].each do |dir|
70
52
  it "should be false for relative path #{dir}" do
71
- @loader.should_not be_valid_search_path dir
53
+ loader.should_not be_valid_search_path dir
72
54
  end
73
55
  end
74
56
  [
@@ -84,41 +66,30 @@ describe Facter::Util::Loader do
84
66
  '/ /..',
85
67
  ].each do |dir|
86
68
  it "should be true for absolute path #{dir}" do
87
- @loader.should be_valid_search_path dir
69
+ loader.should be_valid_search_path dir
88
70
  end
89
71
  end
90
72
  end
91
73
 
92
74
  describe "when determining the search path" do
93
- before do
94
- @loader = Facter::Util::Loader.new
95
- @settings = mock 'settings'
96
- @settings.stubs(:value).returns "/eh"
97
- end
75
+ let(:loader) { Facter::Util::Loader.new }
98
76
 
99
77
  it "should include the facter subdirectory of all paths in ruby LOAD_PATH" do
100
- dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") }
101
- @loader.stubs(:valid_search_path?).returns(true)
102
- paths = @loader.search_path
78
+ dirs = $LOAD_PATH.collect { |d| File.expand_path('facter', d) }
79
+ loader.stubs(:valid_search_path?).returns(true)
80
+ File.stubs(:directory?).returns true
103
81
 
104
- dirs.each do |dir|
105
- paths.should be_include(dir)
106
- end
107
- end
82
+ paths = loader.search_path
108
83
 
109
- it "should warn the user when an invalid search path has been excluded" do
110
- dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") }
111
- @loader.stubs(:valid_search_path?).returns(false)
112
84
  dirs.each do |dir|
113
- Facter.expects(:debugonce).with("Relative directory #{dir} removed from search path.").once
85
+ paths.should be_include(dir)
114
86
  end
115
- paths = @loader.search_path
116
87
  end
117
88
 
118
89
  it "should exclude invalid search paths" do
119
90
  dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") }
120
- @loader.stubs(:valid_search_path?).returns(false)
121
- paths = @loader.search_path
91
+ loader.stubs(:valid_search_path?).returns(false)
92
+ paths = loader.search_path
122
93
  dirs.each do |dir|
123
94
  paths.should_not be_include(dir)
124
95
  end
@@ -126,56 +97,93 @@ describe Facter::Util::Loader do
126
97
 
127
98
  it "should include all search paths registered with Facter" do
128
99
  Facter.expects(:search_path).returns %w{/one /two}
129
- paths = @loader.search_path
100
+ loader.stubs(:valid_search_path?).returns true
101
+
102
+ File.stubs(:directory?).returns false
103
+ File.stubs(:directory?).with('/one').returns true
104
+ File.stubs(:directory?).with('/two').returns true
105
+
106
+ paths = loader.search_path
130
107
  paths.should be_include("/one")
131
108
  paths.should be_include("/two")
132
109
  end
133
110
 
111
+ it "should warn on invalid search paths registered with Facter" do
112
+ Facter.expects(:search_path).returns %w{/one two/three}
113
+ loader.stubs(:valid_search_path?).returns false
114
+ loader.stubs(:valid_search_path?).with('/one').returns true
115
+ loader.stubs(:valid_search_path?).with('two/three').returns false
116
+ Facter.expects(:warn).with('Excluding two/three from search path. Fact file paths must be an absolute directory').once
117
+
118
+ File.stubs(:directory?).returns false
119
+ File.stubs(:directory?).with('/one').returns true
120
+
121
+ paths = loader.search_path
122
+ paths.should be_include("/one")
123
+ paths.should_not be_include("two/three")
124
+ end
125
+
126
+ it "should strip paths that are valid paths but not are not present" do
127
+ Facter.expects(:search_path).returns %w{/one /two}
128
+ loader.stubs(:valid_search_path?).returns false
129
+ loader.stubs(:valid_search_path?).with('/one').returns true
130
+ loader.stubs(:valid_search_path?).with('/two').returns true
131
+
132
+ File.stubs(:directory?).returns false
133
+ File.stubs(:directory?).with('/one').returns true
134
+ File.stubs(:directory?).with('/two').returns false
135
+
136
+ paths = loader.search_path
137
+ paths.should be_include("/one")
138
+ paths.should_not be_include('/two')
139
+ end
140
+
134
141
  describe "and the FACTERLIB environment variable is set" do
135
142
  it "should include all paths in FACTERLIB" do
136
- Facter::Util::Resolution.with_env "FACTERLIB" => "/one/path#{File::PATH_SEPARATOR}/two/path" do
137
- paths = @loader.search_path
138
- %w{/one/path /two/path}.each do |dir|
139
- paths.should be_include(dir)
140
- end
143
+ loader = Facter::Util::Loader.new("FACTERLIB" => "/one/path#{File::PATH_SEPARATOR}/two/path")
144
+
145
+ File.stubs(:directory?).returns false
146
+ File.stubs(:directory?).with('/one/path').returns true
147
+ File.stubs(:directory?).with('/two/path').returns true
148
+
149
+ loader.stubs(:valid_search_path?).returns true
150
+ paths = loader.search_path
151
+ %w{/one/path /two/path}.each do |dir|
152
+ paths.should be_include(dir)
141
153
  end
142
154
  end
143
155
  end
144
156
  end
145
157
 
146
158
  describe "when loading facts" do
147
- before do
148
- @loader = Facter::Util::Loader.new
149
- @loader.stubs(:search_path).returns []
150
- end
151
-
152
159
  it "should load values from the matching environment variable if one is present" do
160
+ loader = loader_from(:env => { "facter_testing" => "yayness" })
161
+
153
162
  Facter.expects(:add).with("testing")
154
163
 
155
- Facter::Util::Resolution.with_env "facter_testing" => "yayness" do
156
- @loader.load(:testing)
157
- end
164
+ loader.load(:testing)
158
165
  end
159
166
 
160
167
  it "should load any files in the search path with names matching the fact name" do
161
- @loader.expects(:search_path).returns %w{/one/dir /two/dir}
162
- FileTest.stubs(:exist?).returns false
163
- FileTest.expects(:exist?).with("/one/dir/testing.rb").returns true
164
- FileTest.expects(:exist?).with("/two/dir/testing.rb").returns true
168
+ loader = loader_from(:search_path => %w{/one/dir /two/dir})
169
+
170
+ loader.expects(:search_path).returns %w{/one/dir /two/dir}
171
+ File.stubs(:file?).returns false
172
+ File.expects(:file?).with("/one/dir/testing.rb").returns true
165
173
 
166
174
  Kernel.expects(:load).with("/one/dir/testing.rb")
167
- Kernel.expects(:load).with("/two/dir/testing.rb")
168
175
 
169
- @loader.load(:testing)
176
+ loader.load(:testing)
170
177
  end
171
178
 
172
- it 'should load any ruby files in directories matching the fact name in the search path in sorted order regardless of the order returned by Dir.entries' do
173
- @loader = TestLoader.new
179
+ it 'should not load any ruby files from subdirectories matching the fact name in the search path' do
180
+ loader = Facter::Util::Loader.new
181
+ File.stubs(:file?).returns false
182
+ File.expects(:file?).with("/one/dir/testing.rb").returns true
183
+ Kernel.expects(:load).with("/one/dir/testing.rb")
174
184
 
175
- @loader.stubs(:search_path).returns %w{/one/dir}
176
- FileTest.stubs(:exist?).returns false
177
- FileTest.stubs(:directory?).with("/one/dir/testing").returns true
178
- @loader.stubs(:search_path).returns %w{/one/dir}
185
+ File.stubs(:directory?).with("/one/dir/testing").returns true
186
+ loader.stubs(:search_path).returns %w{/one/dir}
179
187
 
180
188
  Dir.stubs(:entries).with("/one/dir/testing").returns %w{foo.rb bar.rb}
181
189
  %w{/one/dir/testing/foo.rb /one/dir/testing/bar.rb}.each do |f|
@@ -183,163 +191,96 @@ describe Facter::Util::Loader do
183
191
  Kernel.stubs(:load).with(f)
184
192
  end
185
193
 
186
- @loader.load(:testing)
187
- @loader.loaded_files.should == %w{/one/dir/testing/bar.rb /one/dir/testing/foo.rb}
188
- end
189
-
190
- it "should load any ruby files in directories matching the fact name in the search path" do
191
- @loader.expects(:search_path).returns %w{/one/dir}
192
- FileTest.stubs(:exist?).returns false
193
- FileTest.expects(:directory?).with("/one/dir/testing").returns true
194
-
195
- Dir.expects(:entries).with("/one/dir/testing").returns %w{two.rb}
196
-
197
- Kernel.expects(:load).with("/one/dir/testing/two.rb")
198
-
199
- @loader.load(:testing)
194
+ loader.load(:testing)
200
195
  end
201
196
 
202
197
  it "should not load files that don't end in '.rb'" do
203
- @loader.expects(:search_path).returns %w{/one/dir}
204
- FileTest.stubs(:exist?).returns false
205
- FileTest.expects(:directory?).with("/one/dir/testing").returns true
206
-
207
- Dir.expects(:entries).with("/one/dir/testing").returns %w{one}
208
-
198
+ loader = Facter::Util::Loader.new
199
+ loader.expects(:search_path).returns %w{/one/dir}
200
+ File.stubs(:file?).returns false
201
+ File.expects(:file?).with("/one/dir/testing.rb").returns false
202
+ File.expects(:exist?).with("/one/dir/testing").never
209
203
  Kernel.expects(:load).never
210
204
 
211
- @loader.load(:testing)
205
+ loader.load(:testing)
212
206
  end
213
207
  end
214
208
 
215
209
  describe "when loading all facts" do
216
- before :each do
217
- @loader = Facter::Util::Loader.new
218
- @loader.stubs(:search_path).returns []
210
+ let(:loader) { Facter::Util::Loader.new }
219
211
 
220
- FileTest.stubs(:directory?).returns true
221
- end
222
-
223
- it "should skip directories that do not exist" do
224
- @loader.expects(:search_path).returns %w{/one/dir}
225
-
226
- FileTest.expects(:directory?).with("/one/dir").returns false
227
-
228
- Dir.expects(:entries).with("/one/dir").never
212
+ before :each do
213
+ loader.stubs(:search_path).returns []
229
214
 
230
- @loader.load_all
215
+ File.stubs(:directory?).returns true
231
216
  end
232
217
 
233
218
  it "should load all files in all search paths" do
234
- @loader.expects(:search_path).returns %w{/one/dir /two/dir}
235
-
236
- Dir.expects(:entries).with("/one/dir").returns %w{a.rb b.rb}
237
- Dir.expects(:entries).with("/two/dir").returns %w{c.rb d.rb}
238
-
239
- %w{/one/dir/a.rb /one/dir/b.rb /two/dir/c.rb /two/dir/d.rb}.each { |f| Kernel.expects(:load).with(f) }
240
-
241
- @loader.load_all
242
- end
243
-
244
- it "should load all files in all subdirectories in all search paths" do
245
- @loader.expects(:search_path).returns %w{/one/dir /two/dir}
246
-
247
- Dir.expects(:entries).with("/one/dir").returns %w{a}
248
- Dir.expects(:entries).with("/two/dir").returns %w{b}
249
-
250
- %w{/one/dir/a /two/dir/b}.each { |f| File.expects(:directory?).with(f).returns true }
251
-
252
- Dir.expects(:entries).with("/one/dir/a").returns %w{c.rb}
253
- Dir.expects(:entries).with("/two/dir/b").returns %w{d.rb}
254
-
255
- %w{/one/dir/a/c.rb /two/dir/b/d.rb}.each { |f| Kernel.expects(:load).with(f) }
256
-
257
- @loader.load_all
258
- end
219
+ loader = loader_from(:search_path => %w{/one/dir /two/dir})
259
220
 
260
- it 'should load all files in sorted order for any given directory regardless of the order returned by Dir.entries' do
261
- @loader = TestLoader.new
221
+ Dir.expects(:glob).with('/one/dir/*.rb').returns %w{/one/dir/a.rb /one/dir/b.rb}
222
+ Dir.expects(:glob).with('/two/dir/*.rb').returns %w{/two/dir/c.rb /two/dir/d.rb}
262
223
 
263
- @loader.stubs(:search_path).returns %w{/one/dir}
264
- Dir.stubs(:entries).with("/one/dir").returns %w{foo.rb bar.rb}
265
-
266
- %w{/one/dir}.each { |f| File.stubs(:directory?).with(f).returns true }
267
-
268
- %w{/one/dir/foo.rb /one/dir/bar.rb}.each do |f|
269
- File.stubs(:directory?).with(f).returns false
224
+ %w{/one/dir/a.rb /one/dir/b.rb /two/dir/c.rb /two/dir/d.rb}.each do |f|
225
+ File.expects(:file?).with(f).returns true
270
226
  Kernel.expects(:load).with(f)
271
227
  end
272
228
 
273
- @loader.load_all
274
-
275
- @loader.loaded_files.should == %w{/one/dir/bar.rb /one/dir/foo.rb}
229
+ loader.load_all
276
230
  end
277
231
 
278
- it "should not load files in the util subdirectory" do
279
- @loader.expects(:search_path).returns %w{/one/dir}
280
-
281
- Dir.expects(:entries).with("/one/dir").returns %w{util}
282
-
283
- File.expects(:directory?).with("/one/dir/util").returns true
284
-
285
- Dir.expects(:entries).with("/one/dir/util").never
286
-
287
- @loader.load_all
288
- end
289
-
290
- it "should not load files in a lib subdirectory" do
291
- @loader.expects(:search_path).returns %w{/one/dir}
292
-
293
- Dir.expects(:entries).with("/one/dir").returns %w{lib}
232
+ it "should not try to load subdirectories of search paths" do
233
+ loader.expects(:search_path).returns %w{/one/dir /two/dir}
294
234
 
295
- File.expects(:directory?).with("/one/dir/lib").returns true
235
+ # a.rb is a directory
236
+ Dir.expects(:glob).with('/one/dir/*.rb').returns %w{/one/dir/a.rb /one/dir/b.rb}
237
+ File.expects(:file?).with('/one/dir/a.rb').returns false
238
+ File.expects(:file?).with('/one/dir/b.rb').returns true
239
+ Kernel.expects(:load).with('/one/dir/b.rb')
296
240
 
297
- Dir.expects(:entries).with("/one/dir/lib").never
241
+ # c.rb is a directory
242
+ Dir.expects(:glob).with('/two/dir/*.rb').returns %w{/two/dir/c.rb /two/dir/d.rb}
243
+ File.expects(:file?).with('/two/dir/c.rb').returns false
244
+ File.expects(:file?).with('/two/dir/d.rb').returns true
245
+ Kernel.expects(:load).with('/two/dir/d.rb')
298
246
 
299
- @loader.load_all
300
- end
301
-
302
- it "should not load files in '.' or '..'" do
303
- @loader.expects(:search_path).returns %w{/one/dir}
304
-
305
- Dir.expects(:entries).with("/one/dir").returns %w{. ..}
306
-
307
- File.expects(:entries).with("/one/dir/.").never
308
- File.expects(:entries).with("/one/dir/..").never
309
-
310
- @loader.load_all
247
+ loader.load_all
311
248
  end
312
249
 
313
250
  it "should not raise an exception when a file is unloadable" do
314
- @loader.expects(:search_path).returns %w{/one/dir}
315
- Dir.expects(:entries).with("/one/dir").returns %w{a.rb}
251
+ loader.expects(:search_path).returns %w{/one/dir}
252
+
253
+ Dir.expects(:glob).with('/one/dir/*.rb').returns %w{/one/dir/a.rb}
254
+ File.expects(:file?).with('/one/dir/a.rb').returns true
316
255
 
317
256
  Kernel.expects(:load).with("/one/dir/a.rb").raises(LoadError)
318
257
  Facter.expects(:warn)
319
258
 
320
- lambda { @loader.load_all }.should_not raise_error
259
+ expect { loader.load_all }.to_not raise_error
321
260
  end
322
261
 
323
262
  it "should load all facts from the environment" do
324
263
  Facter::Util::Resolution.with_env "facter_one" => "yayness", "facter_two" => "boo" do
325
- @loader.load_all
264
+ loader.load_all
326
265
  end
327
266
  Facter.value(:one).should == 'yayness'
328
267
  Facter.value(:two).should == 'boo'
329
268
  end
330
269
 
331
270
  it "should only load all facts one time" do
332
- @loader.expects(:load_env).once
333
- @loader.load_all
334
- @loader.load_all
271
+ loader = loader_from(:env => {})
272
+ loader.expects(:load_env).once
273
+
274
+ loader.load_all
275
+ loader.load_all
335
276
  end
336
277
  end
337
278
 
338
279
  it "should load facts on the facter search path only once" do
339
280
  facterlibdir = File.expand_path(File.dirname(__FILE__) + '../../../fixtures/unit/util/loader')
340
- Facter::Util::Resolution.with_env 'FACTERLIB' => facterlibdir do
341
- Facter::Util::Loader.new.load_all
342
- Facter.value(:nosuchfact).should be_nil
343
- end
281
+
282
+ Facter::Util::Loader.new('FACTERLIB' => facterlibdir).load_all
283
+
284
+ Facter.value(:nosuchfact).should be_nil
344
285
  end
345
286
  end