jlawler-activeconfig 0.1.4 → 0.2.0

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