benry-config 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/config_test.rb CHANGED
@@ -1,15 +1,13 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require 'minitest/spec'
4
- require 'minitest/autorun'
5
- require 'minitest/ok'
3
+ require 'oktest'
6
4
 
7
5
  require 'benry/config'
8
6
 
9
7
 
10
8
 
11
9
 
12
- class TestCommonConfig < Benry::BaseConfig
10
+ class TestCommonConfig < Benry::Config
13
11
  add :db_name , "db1"
14
12
  add :db_user , "user1"
15
13
  add :db_pass , ABSTRACT
@@ -31,151 +29,330 @@ class TestConfig3 < TestCommonConfig
31
29
  #set :session_secret , "abc123"
32
30
  end
33
31
 
32
+ class TestConfig4 < TestCommonConfig
33
+ set :db_pass , ABSTRACT['DBPASS']
34
+ set :session_secret , ABSTRACT['SESS_SECRET']
35
+ end
36
+
37
+
34
38
 
39
+ Oktest.scope do
35
40
 
36
- describe Benry::BaseConfig do
37
41
 
42
+ topic Benry::Config::AbstractValue do
38
43
 
39
- describe '#initialize()' do
40
44
 
41
- it "[!7rdq4] traverses parent class and gathers config values." do
42
- config = TestConfig.new
43
- ok {config.db_name} == "db1"
44
- ok {config.db_user} == "user1"
45
- ok {config.db_pass} == "pass1"
46
- ok {config.session_secret} == "abc123"
45
+ topic '#initialize()' do
46
+
47
+ spec "[!6hcf9] accepts environment variable name." do
48
+ v = Benry::Config::AbstractValue.new(:FOO)
49
+ ok {v.envvar} == :FOO
50
+ end
51
+
47
52
  end
48
53
 
49
- it "[!z9mno] raises ConfigError when ABSTRACT or SECRET is not overriden." do
50
- pr = proc { TestConfig2.new }
51
- ex = ok {pr}.raise?(Benry::ConfigError)
52
- ok {ex.message} == "config ':db_pass' should be set, but not."
53
- #
54
- pr = proc { TestConfig3.new }
55
- ex = ok {pr}.raise?(Benry::ConfigError)
56
- ok {ex.message} == "config ':session_secret' should be set, but not."
54
+
55
+ topic '#[]' do
56
+
57
+ spec "[!p0acp] returns new object with environment variable name." do
58
+ foo = Benry::Config::ABSTRACT[:FOO]
59
+ ok {foo.class} == Benry::Config::AbstractValue
60
+ ok {foo.envvar} == :FOO
61
+ #
62
+ bar = Benry::Config::SECRET[:BAR]
63
+ ok {bar.class} == Benry::Config::SecretValue
64
+ ok {bar.envvar} == :BAR
65
+ end
57
66
  end
58
67
 
68
+
59
69
  end
60
70
 
61
71
 
62
- describe '.add()' do
63
72
 
64
- it "[!m7w96] raises ConfigError when already added." do
65
- pr = proc do
66
- TestCommonConfig.class_eval do
67
- add :db_name, "db9"
73
+ topic Benry::Config do
74
+
75
+
76
+ topic '#initialize()' do
77
+
78
+ spec "[!7rdq4] traverses parent class and gathers config values." do
79
+ config = TestConfig.new
80
+ ok {config.db_name} == "db1"
81
+ ok {config.db_user} == "user1"
82
+ ok {config.db_pass} == "pass1"
83
+ ok {config.session_secret} == "abc123"
84
+ end
85
+
86
+ case_when "[!v9f3k] when envvar name not specified..." do
87
+
88
+ spec "[!z9mno] raises ConfigError if ABSTRACT or SECRET is not overriden." do
89
+ pr = proc { TestConfig2.new }
90
+ ok {pr}.raise?(Benry::ConfigError,
91
+ "config ':db_pass' should be set, but not.")
92
+ #
93
+ pr = proc { TestConfig3.new }
94
+ ok {pr}.raise?(Benry::ConfigError,
95
+ "config ':session_secret' should be set, but not.")
68
96
  end
97
+
69
98
  end
70
- ex = ok {pr}.raise?(Benry::ConfigError)
71
- ok {ex.message} == "add :db_name : already defined (use set() instead)."
72
- end
73
99
 
74
- it "[!s620t] adds new key and value." do
75
- ok {TestCommonConfig.instance_variable_get('@__dict')} == {
76
- :db_name => "db1",
77
- :db_user => "user1",
78
- :db_pass => Benry::BaseConfig::ABSTRACT,
79
- :session_secret => Benry::BaseConfig::SECRET,
80
- }
81
- end
100
+ case_when "[!ida3r] when envvar name specified..." do
101
+
102
+ spec "[!txl88] raises ConfigError when envvar not set." do
103
+ ENV['DBPASS'] = nil
104
+ ENV['SESS_SECRET'] = nil
105
+ pr = proc { TestConfig4.new }
106
+ ok {pr}.raise?(Benry::ConfigError,
107
+ "environment variable '$DBPASS' should be set for config item ':db_pass'.")
108
+ #
109
+ ENV['DBPASS'] = "pass1"
110
+ pr = proc { TestConfig4.new }
111
+ ok {pr}.raise?(Benry::ConfigError,
112
+ "environment variable '$SESS_SECRET' should be set for config item ':session_secret'.")
113
+ end
114
+
115
+ spec "[!y47ul] sets envvar value as config value if envvar provided." do
116
+ ENV['DBPASS'] = "<PASS>"
117
+ ENV['SESS_SECRET'] = "<SECRETVALUE>"
118
+ conf = TestConfig4.new
119
+ ok {conf.db_pass} == "<PASS>"
120
+ ok {conf.session_secret} == "<SECRETVALUE>"
121
+ end
82
122
 
83
- it "[!o0ts4] defines getter method." do
84
- cls = Class.new(Benry::BaseConfig) do
85
- add :foo , "FOO"
86
123
  end
87
- obj = cls.new
88
- ok {obj}.respond_to?(:foo)
89
- ok {obj.foo} == "FOO"
124
+
90
125
  end
91
126
 
92
- end
93
127
 
128
+ topic '.add()' do
94
129
 
95
- describe '.set()' do
130
+ spec "[!m7w96] raises ConfigError when already added." do
131
+ pr = proc do
132
+ TestCommonConfig.class_eval do
133
+ add :db_name, "db9"
134
+ end
135
+ end
136
+ ok {pr}.raise?(Benry::ConfigError,
137
+ "add :db_name : already defined (use set() instead).")
138
+ end
96
139
 
97
- it "[!fxc4h] raises ConfigError when not defined yet." do
98
- pr = proc do
99
- TestCommonConfig.class_eval do
100
- set :db_port, 5432
140
+ spec "[!s620t] adds new key and value." do
141
+ ok {TestCommonConfig.instance_variable_get('@__dict')} == {
142
+ :db_name => "db1",
143
+ :db_user => "user1",
144
+ :db_pass => Benry::Config::ABSTRACT,
145
+ :session_secret => Benry::Config::SECRET,
146
+ }
147
+ end
148
+
149
+ spec "[!o0ts4] defines getter method." do
150
+ cls = Class.new(Benry::Config) do
151
+ add :foo , "FOO"
101
152
  end
153
+ obj = cls.new
154
+ ok {obj}.respond_to?(:foo)
155
+ ok {obj.foo} == "FOO"
102
156
  end
103
- ex = ok {pr}.raise?(Benry::ConfigError)
104
- ok {ex.message} == "set :db_port : not defined (use add() instead)."
157
+
105
158
  end
106
159
 
107
- it "[!cv8iz] overrides existing value." do
108
- ok {TestConfig.instance_variable_get('@__dict')} == {
109
- :db_pass => "pass1",
110
- :session_secret => "abc123",
111
- }
112
- #
113
- config = TestConfig.new
114
- ok {config.db_pass} == "pass1"
115
- ok {config.session_secret} == "abc123"
160
+
161
+ topic '.set()' do
162
+
163
+ spec "[!fxc4h] raises ConfigError when not defined yet." do
164
+ pr = proc do
165
+ TestCommonConfig.class_eval do
166
+ set :db_port, 5432
167
+ end
168
+ end
169
+ ok {pr}.raise?(Benry::ConfigError,
170
+ "set :db_port : not defined (use add() instead).")
171
+ end
172
+
173
+ spec "[!cv8iz] overrides existing value." do
174
+ ok {TestConfig.instance_variable_get('@__dict')} == {
175
+ :db_pass => "pass1",
176
+ :session_secret => "abc123",
177
+ }
178
+ #
179
+ config = TestConfig.new
180
+ ok {config.db_pass} == "pass1"
181
+ ok {config.session_secret} == "abc123"
182
+ end
183
+
116
184
  end
117
185
 
118
- end
119
186
 
187
+ topic '.put()' do
120
188
 
121
- describe '.put()' do
189
+ spec "[!abd3f] raises nothing whener defined or not." do
190
+ pr = proc do
191
+ Class.new(TestCommonConfig) do
192
+ put :db_name , "db9" # existing
193
+ put :db_port , "5432" # not existing
194
+ end
195
+ end
196
+ ok {pr}.NOT.raise?(Exception)
197
+ end
198
+
199
+ spec "[!gu2f0] sets key and value." do
200
+ cls = Class.new(TestCommonConfig) do
201
+ put :db_name , "db9" # existing
202
+ put :db_port , 5432 # not existing
203
+ end
204
+ ok {cls.instance_variable_get('@__dict')} == {
205
+ :db_name => "db9",
206
+ :db_port => 5432,
207
+ }
208
+ end
122
209
 
123
- it "[!abd3f] raises nothing whener defined or not." do
124
- pr = proc do
125
- Class.new(TestCommonConfig) do
126
- put :db_name , "db9" # existing
127
- put :db_port , "5432" # not existing
210
+ spec "[!84kbr] defines getter method." do
211
+ cls = Class.new(TestCommonConfig) do
212
+ put :db_name , "db9" # existing
213
+ put :db_port , 5432 # not existing
128
214
  end
215
+ ok {cls}.method_defined?(:db_name)
216
+ ok {cls}.method_defined?(:db_port)
129
217
  end
130
- ok {pr}.NOT.raise?(Exception)
218
+
131
219
  end
132
220
 
133
- it "[!gu2f0] sets key and value." do
134
- cls = Class.new(TestCommonConfig) do
135
- put :db_name , "db9" # existing
136
- put :db_port , 5432 # not existing
221
+
222
+ topic '#[]' do
223
+
224
+ spec "[!z9r30] returns config value." do
225
+ config = TestConfig.new()
226
+ ok {config[:db_user]} == "user1"
227
+ ok {config[:db_pass]} == "pass1"
137
228
  end
138
- ok {cls.instance_variable_get('@__dict')} == {
139
- :db_name => "db9",
140
- :db_port => 5432,
141
- }
229
+
142
230
  end
143
231
 
144
- it "[!84kbr] defines getter method." do
145
- cls = Class.new(TestCommonConfig) do
146
- put :db_name , "db9" # existing
147
- put :db_port , 5432 # not existing
232
+
233
+ topic '#get_all()' do
234
+
235
+ spec "[!85z23] gathers configs which name starts with specified prefix." do
236
+ config = TestConfig.new()
237
+ ok {config.get_all(:db_)} == {:name=>"db1", :user=>"user1", :pass=>"pass1"}
148
238
  end
149
- ok {cls}.method_defined?(:db_name)
150
- ok {cls}.method_defined?(:db_port)
239
+
240
+ spec "[!b72fr] if prefix is a string, then keys returend will also be string." do
241
+ config = TestConfig.new()
242
+ ok {config.get_all("db_")} == {"name"=>"db1", "user"=>"user1", "pass"=>"pass1"}
243
+ end
244
+
151
245
  end
152
246
 
153
- end
154
247
 
248
+ topic '#defined?()' do
155
249
 
156
- describe '#[]' do
250
+ spec "[!y1fsh] returns true if config key defined." do
251
+ conf = TestConfig.new()
252
+ ok {conf.defined?(:db_pass)} == true
253
+ end
254
+
255
+ spec "[!k1b5q] returns false if config key not defined." do
256
+ conf = TestConfig.new()
257
+ ok {conf.defined?(:db_password)} == false
258
+ end
157
259
 
158
- it "[!z9r30] returns config value." do
159
- config = TestConfig.new()
160
- ok {config[:db_user]} == "user1"
161
- ok {config[:db_pass]} == "pass1"
162
260
  end
163
261
 
164
- end
165
262
 
263
+ topic '#each()' do
264
+
265
+ spec "[!f4ljv] returns Enumerator object if block not given." do
266
+ conf = TestConfig.new
267
+ ok {conf.each}.is_a?(Enumerator)
268
+ end
269
+
270
+ spec "[!4wqpu] yields each key and val with hiding secret values." do
271
+ conf = TestConfig.new
272
+ d = {}
273
+ conf.each {|k, v| d[k] = v }
274
+ ok {d} == {:db_name=>"db1", :db_user=>"user1", :db_pass=>"pass1",
275
+ :session_secret=>"(secret)"} # !!!
276
+ end
277
+
278
+ spec "[!a9glw] sorts keys if 'true' specified as the first argument." do
279
+ conf = TestConfig.new
280
+ keys1 = []
281
+ conf.each {|k, v| keys1 << k }
282
+ keys2 = []
283
+ conf.each(true) {|k, v| keys2 << k }
284
+ ok {keys1} == [:db_name, :db_user, :db_pass, :session_secret]
285
+ ok {keys2} == [:db_name, :db_pass, :db_user, :session_secret]
286
+ end
287
+
288
+ spec "[!wggik] returns self if block given." do
289
+ conf = TestConfig.new
290
+ ok {conf.each {|k, v| nil }}.same?(conf)
291
+ end
292
+
293
+ end
166
294
 
167
- describe '#get_all()' do
168
295
 
169
- it "[!85z23] gathers configs which name starts with specified prefix." do
170
- config = TestConfig.new()
171
- ok {config.get_all(:db_)} == {:name=>"db1", :user=>"user1", :pass=>"pass1"}
296
+ topic '#each!()' do
297
+
298
+ spec "[!zd9lk] returns Enumerator object if block not given." do
299
+ conf = TestConfig.new
300
+ ok {conf.each!}.is_a?(Enumerator)
301
+ end
302
+
303
+ spec "[!7i5p2] yields each key and val without hiding secret values." do
304
+ conf = TestConfig.new
305
+ d = {}
306
+ conf.each! {|k, v| d[k] = v }
307
+ ok {d} == {:db_name=>"db1", :db_user=>"user1", :db_pass=>"pass1",
308
+ :session_secret=>"abc123"} # !!!
309
+ end
310
+
311
+ spec "[!aib7c] sorts keys if 'true' specified as the first argument." do
312
+ conf = TestConfig.new
313
+ keys1 = []
314
+ conf.each! {|k, v| keys1 << k }
315
+ keys2 = []
316
+ conf.each(true) {|k, v| keys2 << k }
317
+ ok {keys1} == [:db_name, :db_user, :db_pass, :session_secret]
318
+ ok {keys2} == [:db_name, :db_pass, :db_user, :session_secret]
319
+ end
320
+
321
+ spec "[!2abgb] returns self if block given." do
322
+ conf = TestConfig.new
323
+ ok {conf.each! {|k, v| nil }}.same?(conf)
324
+ end
325
+
172
326
  end
173
327
 
174
- it "[!b72fr] if prefix is a string, then keys returend will also be string." do
175
- config = TestConfig.new()
176
- ok {config.get_all("db_")} == {"name"=>"db1", "user"=>"user1", "pass"=>"pass1"}
328
+
329
+ topic '#_each()' do
330
+
331
+ spec "[!6yvgd] sorts keys if 'sort' is true." do
332
+ keys1 = []
333
+ keys2 = []
334
+ TestConfig.new.instance_eval do
335
+ _each(false, false) {|k, v| keys1 << k }
336
+ _each(true, false) {|k, v| keys2 << k }
337
+ end
338
+ ok {keys1} == [:db_name, :db_user, :db_pass, :session_secret]
339
+ ok {keys2} == [:db_name, :db_pass, :db_user, :session_secret]
340
+ end
341
+
342
+ spec "[!5ledb] hides value if 'hide_secret' is true and value is Secretvalue object." do
343
+ d1 = {}
344
+ d2 = {}
345
+ TestConfig.new.instance_eval do
346
+ _each(false, true ) {|k, v| d1[k] = v }
347
+ _each(false, false) {|k, v| d2[k] = v }
348
+ end
349
+ ok {d1[:session_secret]} == "(secret)"
350
+ ok {d2[:session_secret]} == "abc123"
351
+ end
352
+
177
353
  end
178
354
 
355
+
179
356
  end
180
357
 
181
358
 
metadata CHANGED
@@ -1,62 +1,61 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benry-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - makoto kuwata
8
- autorequire:
7
+ - kwatch
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-04 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: minitest
14
+ name: oktest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: minitest-ok
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- description: 'See https://github.com/kwatch/benry/tree/ruby/benry-config for details.
26
+ version: '1'
27
+ description: |
28
+ Small library for configuration of application.
42
29
 
43
- '
44
- email:
45
- - kwa(at)kuwata-lab.com
30
+ See https://kwatch.github.io/benry-ruby/benry-config.html for details.
31
+ email: kwatch@gmail.com
46
32
  executables: []
47
33
  extensions: []
48
34
  extra_rdoc_files: []
49
35
  files:
50
- - MIT-LICENSE.txt
36
+ - CHANGES.md
37
+ - MIT-LICENSE
51
38
  - README.md
52
- - Rakefile
39
+ - Rakefile.rb
40
+ - benry-config.gemspec
41
+ - doc/benry-config.html
42
+ - doc/css/style.css
43
+ - examples/config/app.rb
44
+ - examples/config/app.secret
45
+ - examples/config/app_dev.rb
46
+ - examples/config/app_prod.rb
47
+ - examples/main.rb
53
48
  - lib/benry/config.rb
49
+ - task/common-task.rb
50
+ - task/package-task.rb
51
+ - task/readme-task.rb
52
+ - task/test-task.rb
54
53
  - test/config_test.rb
55
- homepage: https://github.com/kwatch/benry/tree/ruby/benry-config
54
+ homepage: https://kwatch.github.io/benry-ruby/benry-config.html
56
55
  licenses:
57
56
  - MIT
58
57
  metadata: {}
59
- post_install_message:
58
+ post_install_message:
60
59
  rdoc_options: []
61
60
  require_paths:
62
61
  - lib
@@ -64,16 +63,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
63
  requirements:
65
64
  - - ">="
66
65
  - !ruby/object:Gem::Version
67
- version: '0'
66
+ version: '2.3'
68
67
  required_rubygems_version: !ruby/object:Gem::Requirement
69
68
  requirements:
70
69
  - - ">="
71
70
  - !ruby/object:Gem::Version
72
71
  version: '0'
73
72
  requirements: []
74
- rubyforge_project:
75
- rubygems_version: 2.5.1
76
- signing_key:
73
+ rubygems_version: 3.4.10
74
+ signing_key:
77
75
  specification_version: 4
78
- summary: useful configuration class
79
- test_files: []
76
+ summary: Application configuration library
77
+ test_files:
78
+ - test/config_test.rb
data/Rakefile DELETED
@@ -1,83 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
- project = "benry-config"
5
- release = ENV['RELEASE'] || "0.0.0"
6
- copyright = "copyright(c) 2016 kuwata-lab.com all rights reserved"
7
- license = "MIT License"
8
-
9
- target_files = Dir[*%W[
10
- README.md MIT-LICENSE.txt Rakefile
11
- lib/**/*.rb
12
- test/**/*_test.rb
13
- #{project}.gemspec
14
- ]]
15
-
16
-
17
- task :default => :help
18
-
19
-
20
- desc "show help"
21
- task :help do
22
- puts "rake help # help"
23
- puts "rake test # run test"
24
- puts "rake package RELEASE=X.X.X # create gem file"
25
- puts "rake publish RELEASE=X.X.X # upload gem file"
26
- end
27
-
28
-
29
- desc "do test"
30
- task :test do
31
- sh "ruby", *Dir.glob("test/*.rb")
32
- end
33
-
34
-
35
- desc "create package"
36
- task :package do
37
- release != "0.0.0" or
38
- raise "specify $RELEASE"
39
- ## copy
40
- dir = "build"
41
- rm_rf dir if File.exist?(dir)
42
- mkdir dir
43
- target_files.each do |file|
44
- dest = File.join(dir, File.dirname(file))
45
- mkdir_p dest, :verbose=>false unless File.exist?(dest)
46
- cp file, "#{dir}/#{file}"
47
- end
48
- ## edit
49
- Dir.glob("#{dir}/**/*").each do |file|
50
- next unless File.file?(file)
51
- File.open(file, 'rb+') do |f|
52
- s = f.read()
53
- s = s.gsub(/\$Release: 0.1.0 $/, "$"+"Release: #{release} $")
54
- s = s.gsub(/\$Copyright: copyright(c) 2016 kuwata-lab.com all rights reserved $/, "$"+"Copyright: #{copyright} $")
55
- s = s.gsub(/\$License: MIT License $/, "$"+"License: #{license} $")
56
- #
57
- f.rewind()
58
- f.truncate(0)
59
- f.write(s)
60
- end
61
- end
62
- ## build
63
- chdir dir do
64
- sh "gem build #{project}.gemspec"
65
- end
66
- mv "#{dir}/#{project}-#{release}.gem", "."
67
- end
68
-
69
-
70
- desc "upload gem file to rubygems.org"
71
- task :publish do
72
- release != "0.0.0" or
73
- raise "specify $RELEASE"
74
- #
75
- gemfile = "#{project}-#{release}.gem"
76
- print "** Are you sure to publish #{gemfile}? [y/N]: "
77
- ans = $stdin.gets().strip()
78
- if ans.downcase.start_with?("y")
79
- sh "gem push #{gemfile}"
80
- sh "git tag #{project}-#{release}"
81
- sh "git push --tags"
82
- end
83
- end