blob: 0a280208b1efca669d52d5e2d59d858bae6604e4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
require 'facter/util/loader'
describe Facter::Util::Loader do
def with_env(values)
old = {}
values.each do |var, value|
if old_val = ENV[var]
old[var] = old_val
end
ENV[var] = value
end
yield
values.each do |var, value|
if old.include?(var)
ENV[var] = old[var]
else
ENV.delete(var)
end
end
end
it "should have a method for loading individual facts by name" do
Facter::Util::Loader.new.should respond_to(:load)
end
it "should have a method for loading all facts" do
Facter::Util::Loader.new.should respond_to(:load_all)
end
it "should have a method for returning directories containing facts" do
Facter::Util::Loader.new.should respond_to(:search_path)
end
describe "when determining the search path" do
before do
@loader = Facter::Util::Loader.new
@settings = mock 'settings'
@settings.stubs(:value).returns "/eh"
end
it "should include the facter subdirectory of all paths in ruby LOAD_PATH" do
dirs = $LOAD_PATH.collect { |d| File.join(d, "facter") }
paths = @loader.search_path
dirs.each do |dir|
paths.should be_include(dir)
end
end
it "should include all search paths registered with Facter" do
Facter.expects(:search_path).returns %w{/one /two}
paths = @loader.search_path
paths.should be_include("/one")
paths.should be_include("/two")
end
describe "and the FACTERLIB environment variable is set" do
it "should include all paths in FACTERLIB" do
with_env "FACTERLIB" => "/one/path:/two/path" do
paths = @loader.search_path
%w{/one/path /two/path}.each do |dir|
paths.should be_include(dir)
end
end
end
end
end
describe "when loading facts" do
before do
@loader = Facter::Util::Loader.new
@loader.stubs(:search_path).returns []
end
it "should load values from the matching environment variable if one is present" do
Facter.expects(:add).with("testing")
with_env "facter_testing" => "yayness" do
@loader.load(:testing)
end
end
it "should load any files in the search path with names matching the fact name" do
@loader.expects(:search_path).returns %w{/one/dir /two/dir}
FileTest.stubs(:exist?).returns false
FileTest.expects(:exist?).with("/one/dir/testing.rb").returns true
FileTest.expects(:exist?).with("/two/dir/testing.rb").returns true
Kernel.expects(:load).with("/one/dir/testing.rb")
Kernel.expects(:load).with("/two/dir/testing.rb")
@loader.load(:testing)
end
it "should load any ruby files in directories matching the fact name in the search path" do
@loader.expects(:search_path).returns %w{/one/dir}
FileTest.stubs(:exist?).returns false
FileTest.expects(:directory?).with("/one/dir/testing").returns true
Dir.expects(:entries).with("/one/dir/testing").returns %w{two.rb}
Kernel.expects(:load).with("/one/dir/testing/two.rb")
@loader.load(:testing)
end
it "should not load files that don't end in '.rb'" do
@loader.expects(:search_path).returns %w{/one/dir}
FileTest.stubs(:exist?).returns false
FileTest.expects(:directory?).with("/one/dir/testing").returns true
Dir.expects(:entries).with("/one/dir/testing").returns %w{one}
Kernel.expects(:load).never
@loader.load(:testing)
end
end
describe "when loading all facts" do
before do
@loader = Facter::Util::Loader.new
@loader.stubs(:search_path).returns []
FileTest.stubs(:directory?).returns true
end
it "should skip directories that do not exist" do
@loader.expects(:search_path).returns %w{/one/dir}
FileTest.expects(:directory?).with("/one/dir").returns false
Dir.expects(:entries).with("/one/dir").never
@loader.load_all
end
it "should load all files in all search paths" do
@loader.expects(:search_path).returns %w{/one/dir /two/dir}
Dir.expects(:entries).with("/one/dir").returns %w{a.rb b.rb}
Dir.expects(:entries).with("/two/dir").returns %w{c.rb d.rb}
%w{/one/dir/a.rb /one/dir/b.rb /two/dir/c.rb /two/dir/d.rb}.each { |f| Kernel.expects(:load).with(f) }
@loader.load_all
end
it "should load all files in all subdirectories in all search paths" do
@loader.expects(:search_path).returns %w{/one/dir /two/dir}
Dir.expects(:entries).with("/one/dir").returns %w{a}
Dir.expects(:entries).with("/two/dir").returns %w{b}
%w{/one/dir/a /two/dir/b}.each { |f| File.expects(:directory?).with(f).returns true }
Dir.expects(:entries).with("/one/dir/a").returns %w{c.rb}
Dir.expects(:entries).with("/two/dir/b").returns %w{d.rb}
%w{/one/dir/a/c.rb /two/dir/b/d.rb}.each { |f| Kernel.expects(:load).with(f) }
@loader.load_all
end
it "should not load files in the util subdirectory" do
@loader.expects(:search_path).returns %w{/one/dir}
Dir.expects(:entries).with("/one/dir").returns %w{util}
File.expects(:directory?).with("/one/dir/util").returns true
Dir.expects(:entries).with("/one/dir/util").never
@loader.load_all
end
it "should not load files in a lib subdirectory" do
@loader.expects(:search_path).returns %w{/one/dir}
Dir.expects(:entries).with("/one/dir").returns %w{lib}
File.expects(:directory?).with("/one/dir/lib").returns true
Dir.expects(:entries).with("/one/dir/lib").never
@loader.load_all
end
it "should not load files in '.' or '..'" do
@loader.expects(:search_path).returns %w{/one/dir}
Dir.expects(:entries).with("/one/dir").returns %w{. ..}
File.expects(:entries).with("/one/dir/.").never
File.expects(:entries).with("/one/dir/..").never
@loader.load_all
end
it "should not raise an exception when a file is unloadable" do
@loader.expects(:search_path).returns %w{/one/dir}
Dir.expects(:entries).with("/one/dir").returns %w{a.rb}
Kernel.expects(:load).with("/one/dir/a.rb").raises(LoadError)
@loader.expects(:warn)
lambda { @loader.load_all }.should_not raise_error
end
it "should load all facts from the environment" do
Facter.expects(:add).with('one')
Facter.expects(:add).with('two')
with_env "facter_one" => "yayness", "facter_two" => "boo" do
@loader.load_all
end
end
it "should only load all facts one time" do
@loader.expects(:load_env).once
@loader.load_all
@loader.load_all
end
end
end
|