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