activeconfig 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,435 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pp'
3
+ # TEST_CONFIG_BEGIN
4
+ # enabled: true
5
+ # TEST_CONFIG_END
6
+
7
+ # Test target dependencies
8
+
9
+ # even if a gem is installed, load cnu_config and active_config locally
10
+ dir = File.dirname __FILE__
11
+ $LOAD_PATH.unshift File.join(dir, "..", "lib")
12
+
13
+ # Configure ActiveConfig to use our test config files.
14
+ RAILS_ENV = 'development'
15
+ ENV['ACTIVE_CONFIG_PATH'] = File.expand_path(File.dirname(__FILE__) + "/active_config_test/")
16
+ ENV.delete('ACTIVE_CONFIG_OVERLAY') # Avoid gb magic.
17
+
18
+ # Test environment.
19
+ require 'rubygems'
20
+
21
+ $:.unshift File.expand_path("../../lib",__FILE__)
22
+ # Test target
23
+ require 'active_config'
24
+
25
+ # Test dependencies
26
+ require 'test/unit'
27
+ require 'fileutils' # FileUtils.touch
28
+ require 'benchmark'
29
+
30
+ AC=ActiveConfig.new
31
+ class ActiveConfig::Test < Test::Unit::TestCase
32
+ def active_config
33
+ @active_config||= ActiveConfig.new :suffixes =>[
34
+ nil,
35
+ [:overlay, nil],
36
+ [:local],
37
+ [:overlay, [:local]],
38
+ :config,
39
+ [:overlay, :config],
40
+ :local_config,
41
+ [:overlay, :local_config],
42
+ :hostname,
43
+ [:overlay, :hostname],
44
+ [:hostname, :config_local],
45
+ [:overlay, [:hostname, :config_local]]
46
+ ]
47
+ end
48
+ def setup
49
+ super
50
+ begin
51
+ active_config._verbose = nil # default
52
+ active_config.reload(true)
53
+ active_config._reload_disabled = nil # default
54
+ active_config._reload_delay = nil # default
55
+ rescue => err
56
+ # NOTHING
57
+ end
58
+ end
59
+
60
+
61
+ def teardown
62
+ super
63
+ end
64
+
65
+ def test_global
66
+ assert_equal 101,active_config.using_array_index
67
+ end
68
+ def test_gbracket
69
+ assert_equal 101,active_config[:using_array_index]
70
+ end
71
+ def test_rails_env
72
+ assert_equal RAILS_ENV, active_config._suffixes.rails_env
73
+ end
74
+
75
+ def test_suffixes
76
+ end
77
+
78
+ def test_basic
79
+ assert_equal true, active_config.test.secure_login
80
+ end
81
+
82
+
83
+ def test_default
84
+ assert_equal "yo!", active_config.test.default
85
+ end
86
+
87
+
88
+ def test_indifferent
89
+ assert h = active_config.test
90
+ # STDERR.puts "h = #{h.inspect}:#{h.class}"
91
+
92
+ assert hstr = h['hash_1']
93
+ assert_kind_of Hash, hstr
94
+ # STDERR.puts "hstr = #{hstr.inspect}:#{hstr.class}"
95
+
96
+ assert hsym = h[:hash_1]
97
+ assert hsym.object_id == hstr.object_id
98
+ end
99
+
100
+
101
+ def test_dot_notation
102
+ assert h = active_config.test
103
+ assert h = h.hash_1
104
+ assert h.foo
105
+ end
106
+
107
+
108
+ def test_dot_notation_overrun
109
+ assert_raise NoMethodError do
110
+ active_config.test.hash_1.foo.a_bridge_too_far
111
+ end
112
+ end
113
+
114
+
115
+ def test_array_notation
116
+ assert h = active_config.test[:hash_1]
117
+ assert a = active_config.test[:array_1]
118
+ end
119
+
120
+
121
+ def test_function_notation
122
+ assert h = active_config.test(:hash_1, 'foo')
123
+ assert_equal nil, active_config.test(:hash_1, 'foo', :too_far)
124
+ assert_equal 'c', active_config.test(:array_1, 2)
125
+ assert_equal nil, active_config.test(:array_1, "2")
126
+ end
127
+
128
+
129
+ def test_immutable
130
+ assert active_config.test.frozen?
131
+ assert active_config.test.hash_1.frozen?
132
+ # ruby 1.8 and ruby 1.9 raise different exception classes
133
+ assert_raise TypeError, RuntimeError do
134
+ active_config.test.hash_1[:foo] = 1
135
+ end
136
+ end
137
+
138
+
139
+ def test_to_yaml
140
+ assert active_config.test.to_yaml
141
+ end
142
+
143
+
144
+ def test_disable_reload
145
+ @active_config=nil
146
+ # Clear out everything.
147
+ active_config.reload(true)
148
+
149
+ # Reload delay
150
+ active_config._reload_delay = -1
151
+ # active_config._verbose = true
152
+ active_config._flush_cache
153
+
154
+ # Get the name of a config file to touch.
155
+ assert cf1 = active_config._config_files("test")
156
+ assert cf1 = cf1[0]
157
+
158
+ v = nil
159
+ th = nil
160
+ active_config.disable_reload do
161
+ # Make sure first access works inside disable reload.
162
+ assert th = active_config.test
163
+ assert_equal "foo", v = active_config.test.hash_1.foo
164
+
165
+ # Get access again and insure that file was not reloaded.
166
+ assert_equal v, active_config.test.hash_1.foo
167
+ assert th.object_id == active_config.test.object_id
168
+
169
+ # STDERR.puts "touching #{cf1.inspect}"
170
+ FileUtils.touch(cf1)
171
+
172
+ assert_equal v, active_config.test.hash_1.foo
173
+ assert th.object_id == active_config.test.object_id
174
+ end
175
+
176
+ # STDERR.puts "reload allowed"
177
+ #assert ! active_config._config_file_loaded
178
+ #assert th.object_id != active_config.test.object_id
179
+ assert_equal v, active_config.test.hash_1.foo
180
+
181
+ #assert active_config._config_file_loaded
182
+ assert_equal v, active_config.test.hash_1.foo
183
+
184
+
185
+ # Restore reload_delay
186
+ active_config._reload_delay = false
187
+ active_config._verbose = false
188
+ end
189
+
190
+
191
+ def test_hash_merge
192
+ assert_equal "foo", active_config.test.hash_1.foo
193
+ assert_equal "baz", active_config.test.hash_1.bar
194
+ assert_equal "bok", active_config.test.hash_1.bok
195
+ assert_equal "zzz", active_config.test.hash_1.zzz
196
+ end
197
+
198
+
199
+ def test_array
200
+ assert_equal [ 'a', 'b', 'c', 'd' ], active_config.test.array_1
201
+ end
202
+
203
+
204
+ def test_index
205
+ assert_kind_of Hash, active_config.get_config_file(:test)
206
+ end
207
+
208
+
209
+ def test_config_files
210
+ return
211
+ #FIXME TODO: 1) Figure out if this functionality needs to be replicated
212
+ # 2) If so, do it.
213
+ assert_kind_of Array, cf = active_config._load_config_files("test").select{|x| x[3]}
214
+ # STDERR.puts "cf = #{cf.inspect}"
215
+
216
+ if ENV['ACTIVE_CONFIG_OVERLAY']
217
+ # assert_equal 3, cf.size
218
+ else
219
+ # assert_equal 2, cf.size
220
+ end
221
+
222
+ assert_equal 4, cf[0].size
223
+ assert_equal "test", cf[0][0]
224
+ assert_equal "test", cf[0][1]
225
+
226
+ assert_equal 4, cf[1].size
227
+ if ENV['ACTIVE_CONFIG_OVERLAY'] == 'gb'
228
+ assert_equal "test_gb", cf[1][0]
229
+ assert_equal "test_gb", cf[1][1]
230
+
231
+ assert_equal 4, cf[2].size
232
+ assert_equal "test", cf[2][0]
233
+ assert_equal "test_local", cf[2][1]
234
+ else
235
+ assert_equal "test", cf[1][0]
236
+ assert_equal "test_local", cf[1][1]
237
+ end
238
+
239
+ end
240
+
241
+
242
+ def test_config_changed
243
+ return
244
+ active_config.reload(true)
245
+
246
+ cf1 = active_config._config_files("test")
247
+ cf2 = active_config._config_files("test")
248
+ cf3 = active_config._config_files("test")
249
+
250
+ file_to_touch = cf1[1]
251
+
252
+ # Check that _config_files is cached.
253
+ # STDERR.puts "cf1 = #{cf1.object_id.inspect}"
254
+ # STDERR.puts "cf2 = #{cf2.object_id.inspect}"
255
+ assert cf1.object_id != cf2.object_id
256
+ # assert cf1.object_id == cf3.object_id
257
+ # FIXME TODO: WTF Does the above 2 asserts mean???
258
+
259
+ # STDERR.puts "cf1 = #{cf1.inspect}"
260
+ # STDERR.puts "cf2 = #{cf2.inspect}"
261
+ # Check that config_changed? is false, until touch.
262
+ assert cf1.object_id != cf2.object_id
263
+ assert_equal cf1, cf2
264
+ #assert_equal false, active_config.config_changed?("test")
265
+
266
+ # Touch a file.
267
+ # $stderr.puts "file_to_touch = #{file_to_touch.inspect}"
268
+ FileUtils.touch(file_to_touch)
269
+ cf2 = active_config._load_config_files("test")
270
+ # Ensure that files were not reloaded until reload(true) below.
271
+ assert cf1.object_id != cf2.object_id
272
+ assert ! (cf1 === cf2)
273
+ # assert_equal true, active_config.config_changed?("test")
274
+
275
+ # Pull config again.
276
+ active_config.reload(true)
277
+ cf3 = active_config._load_config_files("test")
278
+ cf2 = active_config._load_config_files("test")
279
+ # $stderr.puts "cf1.object_id = #{cf1.object_id}"
280
+ # $stderr.puts "cf2.object_id = #{cf2.object_id}"
281
+ # $stderr.puts "cf3.object_id = #{cf3.object_id}"
282
+ # STDERR.puts "cf3 = #{cf1.inspect}"
283
+ # STDERR.puts "cf2 = #{cf2.inspect}"
284
+
285
+ # Insure that the list of files actually changed:
286
+ assert cf1.object_id != cf3.object_id
287
+ assert !(cf3 === cf1)
288
+ assert (cf3 === cf2)
289
+ # assert_equal false, active_config.config_changed?("test")
290
+
291
+ # Pull config again, expect no changes.
292
+ cf4 = active_config._load_config_files("test")
293
+ # STDERR.puts "cf3 = #{cf1.inspect}"
294
+ # STDERR.puts "cf2 = #{cf2.inspect}"
295
+ assert cf3.object_id == cf4.object_id
296
+ assert active_config._load_config_files("test")
297
+ assert_equal false, active_config.config_changed?("test")
298
+
299
+ end
300
+
301
+
302
+ def test_check_reload_disabled
303
+ active_config.reload(true)
304
+
305
+ assert_kind_of Array, active_config._config_files('test')
306
+
307
+ active_config._reload_disabled = true
308
+
309
+ assert_kind_of Array, active_config._config_files('test')
310
+
311
+ active_config._reload_disabled = nil
312
+ end
313
+
314
+
315
+ def test_on_load_callback
316
+ # STDERR.puts "test_on_load_callback"
317
+
318
+ active_config.reload(true)
319
+ # active_config._verbose = 1
320
+
321
+ cf1 = active_config._config_files("test")
322
+
323
+ assert_equal "foo", active_config.test.hash_1.foo
324
+
325
+ sleep 1
326
+
327
+ called_back = 0
328
+
329
+ active_config.on_load(:test) do
330
+ called_back += 1
331
+ # STDERR.puts "on_load #{called_back}"
332
+ end
333
+
334
+ assert_equal 1, called_back
335
+
336
+ assert_equal "foo", active_config.test.hash_1.foo
337
+
338
+
339
+ # STDERR.puts "Not expecting config change."
340
+ assert_nil active_config._check_config_changed
341
+ assert_equal "foo", active_config.test.hash_1.foo
342
+ assert_equal 1, called_back
343
+
344
+ old_test_oid=active_config.test.object_id
345
+ file = cf1[0]
346
+ # STDERR.puts "Touching file #{file.inspect}"
347
+ active_config._flush_cache
348
+ File.chmod(0644, file)
349
+ FileUtils.touch(file)
350
+ File.chmod(0444, file)
351
+
352
+ # STDERR.puts "Expect config change."
353
+ assert_equal "foo", active_config.test.hash_1.foo
354
+ assert_not_equal old_test_oid, active_config.test.object_id
355
+ assert_equal 2, called_back
356
+
357
+ # STDERR.puts "Not expecting config change."
358
+ assert_nil active_config._check_config_changed
359
+ assert_equal "foo", active_config.test.hash_1.foo
360
+ assert_equal 2, called_back
361
+
362
+ # STDERR.puts "test_on_load_callback: END"
363
+ end
364
+
365
+
366
+ def test_overlay_by_name
367
+ assert_equal nil, active_config._suffixes.overlay
368
+
369
+ assert_equal "foo", active_config.test.hash_1.foo
370
+ assert_equal "foo", active_config.test_GB.hash_1.foo
371
+
372
+ assert_equal "bok", active_config.test.hash_1.bok
373
+ assert_equal "GB", active_config.test_GB.hash_1.bok
374
+
375
+ assert_equal nil, active_config.test.hash_1.gb
376
+ assert_equal "GB", active_config.test_GB.hash_1.gb
377
+ end
378
+
379
+
380
+ def test_overlay_change
381
+ begin
382
+ active_config._suffixes.overlay = 'gb'
383
+
384
+ assert_equal "foo", active_config.test.hash_1.foo
385
+ assert_equal "foo", active_config.test_GB.hash_1.foo
386
+ assert_equal "foo", active_config.test_US.hash_1.foo
387
+
388
+ assert_equal "GB", active_config.test.hash_1.bok
389
+ assert_equal "GB", active_config.test_GB.hash_1.bok
390
+ assert_equal "US", active_config.test_US.hash_1.bok
391
+
392
+ assert_equal "GB", active_config.test.hash_1.gb
393
+ assert_equal "GB", active_config.test_GB.hash_1.gb
394
+ assert_equal nil, active_config.test_US.hash_1.gb
395
+
396
+ active_config._suffixes.overlay = 'us'
397
+
398
+ assert_equal "foo", active_config.test.hash_1.foo
399
+ assert_equal "foo", active_config.test_GB.hash_1.foo
400
+ assert_equal "foo", active_config.test_US.hash_1.foo
401
+
402
+ assert_equal "US", active_config.test.hash_1.bok
403
+ assert_equal "GB", active_config.test_GB.hash_1.bok
404
+ assert_equal "US", active_config.test_US.hash_1.bok
405
+
406
+ assert_equal nil, active_config.test.hash_1.gb
407
+ assert_equal "GB", active_config.test_GB.hash_1.gb
408
+ assert_equal nil, active_config.test_US.hash_1.gb
409
+
410
+ active_config._suffixes.overlay = nil
411
+
412
+ ensure
413
+ active_config._suffixes.overlay = nil
414
+ end
415
+ end
416
+
417
+
418
+ # Expand this benchmark to
419
+ # compare with relative minimum performance, for example
420
+ # a loop from 1 to 1000000.
421
+ # Make this test fail if the minimum peformance criteria
422
+ # is not met.
423
+ # -- kurt@cashnetusa.com 2007/06/12
424
+ def test_zzz_benchmark
425
+ n = 10000
426
+ bm = Benchmark.measure do
427
+ n.times do
428
+ active_config.test.hash_1.foo
429
+ end
430
+ end
431
+ STDERR.puts "\n#{n}.times =>#{bm}\n"
432
+ end
433
+
434
+ end # class
435
+
@@ -0,0 +1,2 @@
1
+ using_array_index: 101
2
+ :display: 'bar'
@@ -0,0 +1,14 @@
1
+ #test_mode: false
2
+ secure_login: true
3
+ perform_caching: true
4
+ lazy: true
5
+
6
+ default: "yo!" # Test for #default override for .default notation
7
+
8
+ hash_1:
9
+ foo: "foo"
10
+ bar: "bar"
11
+ bok: "bok"
12
+
13
+
14
+ array_1: [ a, b, c, d ]