yaml2env 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -73,7 +73,52 @@ describe Yaml2env do
73
73
 
74
74
  it 'should be writable' do
75
75
  Yaml2env.root = "/tmp"
76
- Yaml2env.root.must_equal "/tmp"
76
+ Yaml2env.root.to_s.must_equal "/tmp"
77
+ end
78
+
79
+ it 'should be Pathname - respond to #join - if present' do
80
+ Yaml2env.root = nil
81
+ Yaml2env.root.must_be_kind_of NilClass
82
+ Yaml2env.root.wont_respond_to :join
83
+
84
+ Yaml2env.root = ""
85
+ Yaml2env.root.must_be_kind_of String
86
+ Yaml2env.root.wont_respond_to :join
87
+
88
+ Yaml2env.root = "/tmp"
89
+ Yaml2env.root.must_be_kind_of Pathname
90
+ Yaml2env.root.must_respond_to :join
91
+ Yaml2env.root.join('ponies').to_s.must_equal "/tmp/ponies"
92
+ end
93
+
94
+ # TODO: Need to alter other specs to get this work well - later.
95
+ # it 'should not accept non-existing path - makes no sense' do
96
+ # lambda { Yaml2env.root = "/tmp/i_dont_exist" }.must_raise Yaml2env::InvalidRootError
97
+ # end
98
+
99
+ it 'should expand path automatically' do
100
+ Yaml2env.root = "/tmp/path/../path/.."
101
+ Yaml2env.root.to_s.must_equal "/tmp"
102
+ end
103
+ end
104
+
105
+ describe ".root?" do
106
+ it 'should be defined' do
107
+ Yaml2env.must_respond_to :root?
108
+ end
109
+
110
+ it 'should return true if root is present, otherwise false' do
111
+ Yaml2env.root = nil
112
+ Yaml2env.root?.must_equal false
113
+
114
+ Yaml2env.root = ""
115
+ Yaml2env.root?.must_equal false
116
+
117
+ Yaml2env.root = "/"
118
+ Yaml2env.root?.must_equal true
119
+
120
+ Yaml2env.root = "/path/to/project"
121
+ Yaml2env.root?.must_equal true
77
122
  end
78
123
  end
79
124
 
@@ -84,7 +129,177 @@ describe Yaml2env do
84
129
 
85
130
  it 'should be writable' do
86
131
  Yaml2env.root = "/tmp"
87
- Yaml2env.root.must_equal "/tmp"
132
+ Yaml2env.root.to_s.must_equal "/tmp"
133
+ end
134
+
135
+ # FIXME: See TODO.
136
+ it "should respond true/false for matching environment via method missing (a.k.a. string inquiry)" do
137
+ skip
138
+
139
+ Yaml2env.env.must_respond_to :development?
140
+ Yaml2env.env.must_respond_to :staging?
141
+ Yaml2env.env.must_respond_to :production?
142
+ Yaml2env.env.must_respond_to :bogus?
143
+
144
+ Yaml2env.env = nil
145
+ Yaml2env.env.development?.must_equal false
146
+
147
+ Yaml2env.env = ""
148
+ Yaml2env.env.development?.must_equal false
149
+
150
+ Yaml2env.env = "development"
151
+ Yaml2env.env.development?.must_equal true
152
+ Yaml2env.env.bogus?.must_equal false
153
+
154
+ Yaml2env.env = "staging"
155
+ Yaml2env.env.development?.must_equal false
156
+
157
+ Yaml2env.env = "production"
158
+ Yaml2env.env.production?.must_equal true
159
+ end
160
+ end
161
+
162
+ describe ".env?" do
163
+ it 'should be defined' do
164
+ Yaml2env.must_respond_to :env?
165
+ end
166
+
167
+ describe "with no arguments" do
168
+ it 'should return true if env is present, otherwise false' do
169
+ Yaml2env.env = nil
170
+ Yaml2env.env?.must_equal false
171
+
172
+ Yaml2env.env = ""
173
+ Yaml2env.env?.must_equal false
174
+
175
+ Yaml2env.env = "development"
176
+ Yaml2env.env?.must_equal true
177
+ end
178
+ end
179
+
180
+ describe "with one argument" do
181
+ # Stupid spec, but for the record...
182
+ it 'nil/blank: should return true if env equals, otherwise false' do
183
+ Yaml2env.env = nil
184
+ Yaml2env.env?(nil).must_equal true
185
+ Yaml2env.env?("").must_equal true
186
+
187
+ Yaml2env.env = ""
188
+ Yaml2env.env?(nil).must_equal true
189
+ Yaml2env.env?("").must_equal true
190
+ end
191
+
192
+ it 'string: should return true if env equals, otherwise false' do
193
+ Yaml2env.env = nil
194
+ Yaml2env.env?("development").must_equal false
195
+
196
+ Yaml2env.env = ""
197
+ Yaml2env.env?("development").must_equal false
198
+
199
+ Yaml2env.env = "development"
200
+ Yaml2env.env?("development").must_equal true
201
+
202
+ Yaml2env.env = "staging"
203
+ Yaml2env.env?("development").must_equal false
204
+ end
205
+
206
+ it 'regexp: should return true if env matches, otherwise false' do
207
+ Yaml2env.env = nil
208
+ Yaml2env.env?(/development|staging/).must_equal false
209
+
210
+ Yaml2env.env = ""
211
+ Yaml2env.env?(/development|staging/).must_equal false
212
+
213
+ Yaml2env.env = "development"
214
+ Yaml2env.env?(/development|staging/).must_equal true
215
+
216
+ Yaml2env.env = "staging"
217
+ Yaml2env.env?(/development|staging/).must_equal true
218
+ end
219
+ end
220
+
221
+ describe "with one argument" do
222
+ # *<:)
223
+ it 'nils/blanks: should return true if env equals, otherwise false' do
224
+ Yaml2env.env = nil
225
+ lambda { Yaml2env.env?(nil, nil) }.must_raise Yaml2env::HumanError
226
+ lambda { Yaml2env.env?(nil, "") }.must_raise Yaml2env::HumanError
227
+ lambda { Yaml2env.env?("", "") }.must_raise Yaml2env::HumanError
228
+
229
+ Yaml2env.env = ""
230
+ lambda { Yaml2env.env?(nil, nil) }.must_raise Yaml2env::HumanError
231
+ lambda { Yaml2env.env?(nil, "") }.must_raise Yaml2env::HumanError
232
+ lambda { Yaml2env.env?("", "") }.must_raise Yaml2env::HumanError
233
+ end
234
+
235
+ it 'strings: should return true if env equals, otherwise false' do
236
+ Yaml2env.env = nil
237
+ Yaml2env.env?("development", "staging").must_equal false
238
+
239
+ Yaml2env.env = ""
240
+ Yaml2env.env?("development", "staging").must_equal false
241
+
242
+ Yaml2env.env = "development"
243
+ Yaml2env.env?("development", "staging").must_equal true
244
+
245
+ Yaml2env.env = "staging"
246
+ Yaml2env.env?("development", "staging").must_equal true
247
+
248
+ Yaml2env.env = "production"
249
+ Yaml2env.env?("development", "staging").must_equal false
250
+ end
251
+
252
+ it 'regexpes: should return true if env matches, otherwise false' do
253
+ Yaml2env.env = nil
254
+ Yaml2env.env?(/development|staging/).must_equal false
255
+
256
+ Yaml2env.env = ""
257
+ Yaml2env.env?(/development|staging/).must_equal false
258
+
259
+ Yaml2env.env = "development"
260
+ Yaml2env.env?(/development|staging/).must_equal true
261
+
262
+ Yaml2env.env = "staging"
263
+ Yaml2env.env?(/development|staging/).must_equal true
264
+ end
265
+ end
266
+ end
267
+
268
+ describe ".default_env" do
269
+ it 'should be defined' do
270
+ Yaml2env.must_respond_to :default_env
271
+ end
272
+
273
+ it 'should be default value for Yaml2env.env if no value is set' do
274
+ with_constants :ENV => {'RACK_ENV' => nil} do
275
+ Yaml2env.default_env = 'development'
276
+ Yaml2env.env = nil
277
+ Yaml2env.detect_env!
278
+ Yaml2env.env.must_equal 'development'
279
+
280
+ Yaml2env.default_env = 'development'
281
+ Yaml2env.env = 'staging'
282
+ Yaml2env.detect_env!
283
+ Yaml2env.env.must_equal 'staging'
284
+
285
+ Yaml2env.default_env = nil
286
+ end
287
+ end
288
+ end
289
+
290
+ describe ".default_env?" do
291
+ it 'should be defined' do
292
+ Yaml2env.must_respond_to :default_env?
293
+ end
294
+
295
+ it 'should be true only if specified env equals default_env' do
296
+ Yaml2env.default_env = 'development'
297
+
298
+ Yaml2env.env = 'development'
299
+ Yaml2env.default_env?.must_equal true
300
+
301
+ Yaml2env.env = 'staging'
302
+ Yaml2env.default_env?.must_equal false
88
303
  end
89
304
  end
90
305
 
@@ -100,7 +315,7 @@ describe Yaml2env do
100
315
  c.root = '/home/grimen/projects/hello_world'
101
316
  c.env = 'production'
102
317
  end
103
- Yaml2env.root.must_equal '/home/grimen/projects/hello_world'
318
+ Yaml2env.root.to_s.must_equal '/home/grimen/projects/hello_world'
104
319
  Yaml2env.env.must_equal 'production'
105
320
  end
106
321
  end
@@ -110,34 +325,34 @@ describe Yaml2env do
110
325
  Yaml2env.must_respond_to :detect_root!
111
326
  end
112
327
 
113
- it "should detect environment for Rack-apps - 1st" do
328
+ it "should detect environment for Rack_apps - 1st" do
114
329
  rack!(true)
115
330
  rails!(true)
116
331
  sinatra!(true)
117
332
 
118
333
  Yaml2env.root = nil
119
334
  Yaml2env.detect_root!
120
- Yaml2env.root.must_equal '/home/grimen/development/rack-app'
335
+ Yaml2env.root.to_s.must_equal '/home/grimen/development/rack_app'
121
336
  end
122
337
 
123
- it "should detect environment for Rails-apps - 2nd" do
338
+ it "should detect environment for Rails_apps - 2nd" do
124
339
  rack!(false)
125
340
  rails!(true)
126
341
  sinatra!(true)
127
342
 
128
343
  Yaml2env.root = nil
129
344
  Yaml2env.detect_root!
130
- Yaml2env.root.must_equal '/home/grimen/development/rails-app'
345
+ Yaml2env.root.to_s.must_equal '/home/grimen/development/rails_app'
131
346
  end
132
347
 
133
- it "should detect environment for Sinatra-apps - 3rd" do
348
+ it "should detect environment for Sinatra_apps - 3rd" do
134
349
  rack!(false)
135
350
  rails!(false)
136
351
  sinatra!(true)
137
352
 
138
353
  Yaml2env.root = nil
139
354
  Yaml2env.detect_root!
140
- Yaml2env.root.must_equal '/home/grimen/development/sinatra-app'
355
+ Yaml2env.root.to_s.must_equal '/home/grimen/development/sinatra_app'
141
356
  end
142
357
 
143
358
  it 'should complain if no environment could be detected' do
@@ -153,38 +368,42 @@ describe Yaml2env do
153
368
  end
154
369
 
155
370
  describe ".detect_env!" do
371
+ before do
372
+ Yaml2env.default_env = nil
373
+ end
374
+
156
375
  it 'should be defined' do
157
376
  Yaml2env.must_respond_to :detect_env!
158
377
  end
159
378
 
160
- it "should detect environment for Rack-apps - 1st" do
379
+ it "should detect environment for Rack_apps - 1st" do
161
380
  rack!(true)
162
381
  rails!(true)
163
382
  sinatra!(true)
164
383
 
165
384
  Yaml2env.env = nil
166
385
  Yaml2env.detect_env!
167
- Yaml2env.env.must_equal 'rack-env'
386
+ Yaml2env.env.must_equal 'rack_env'
168
387
  end
169
388
 
170
- it "should detect environment for Rails-apps - 2nd" do
389
+ it "should detect environment for Rails_apps - 2nd" do
171
390
  rack!(false)
172
391
  rails!(true)
173
392
  sinatra!(true)
174
393
 
175
394
  Yaml2env.env = nil
176
395
  Yaml2env.detect_env!
177
- Yaml2env.env.must_equal 'rails-env'
396
+ Yaml2env.env.must_equal 'rails_env'
178
397
  end
179
398
 
180
- it "should detect environment for Sinatra-apps - 3rd" do
399
+ it "should detect environment for Sinatra_apps - 3rd" do
181
400
  rack!(false)
182
401
  rails!(false)
183
402
  sinatra!(true)
184
403
 
185
404
  Yaml2env.env = nil
186
405
  Yaml2env.detect_env!
187
- Yaml2env.env.must_equal 'sinatra-env'
406
+ Yaml2env.env.must_equal 'sinatra_env'
188
407
  end
189
408
 
190
409
  it 'should complain if no environment could be detected' do
@@ -332,6 +551,146 @@ describe Yaml2env do
332
551
  end
333
552
  end
334
553
 
554
+ describe ".require!" do
555
+ before do
556
+ Yaml2env.env = 'production'
557
+ Yaml2env.root = File.dirname(__FILE__)
558
+ Yaml2env.logger = nil # ::Logger.new(::STDOUT)
559
+ Yaml2env.stubs(:loaded).returns({})
560
+ end
561
+
562
+ it 'should be defined' do
563
+ Yaml2env.must_respond_to :require!
564
+ end
565
+
566
+ it 'should throw error if specified config file that do not exist' do
567
+ proc { Yaml2env.require! 'null.yml' }.must_raise Yaml2env::ConfigLoadingError
568
+ end
569
+
570
+ it 'should not throw error if specified config file do exist' do
571
+ proc { Yaml2env.require!('fixtures/example.yml') }.must_be_silent
572
+ end
573
+
574
+ it 'should throw error if a specified constant-key do not exist in the config file' do
575
+ proc {
576
+ Yaml2env.require! 'fixtures/example.yml', {:API_KEY => 'bla'}
577
+ }.must_raise Yaml2env::MissingConfigKeyError
578
+ end
579
+
580
+ it 'should not throw error if a specified constant-key do in fact exist in the config file' do
581
+ assert Yaml2env.require! 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
582
+ end
583
+
584
+ it 'should set - with Yaml2env - required ENV-values' do
585
+ Yaml2env::LOADED_ENV.clear unless Yaml2env::LOADED_ENV.empty?
586
+ Yaml2env.require! 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
587
+ Yaml2env::LOADED_ENV.must_equal({"API_SECRET" => "PRODUCTION_SECRET", "API_KEY" => "PRODUCTION_KEY"})
588
+ end
589
+
590
+ it "should only load specified file if it has not been loaded already - like Ruby require vs. load" do
591
+ assert Yaml2env.require! 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
592
+ lambda {
593
+ Yaml2env.require! 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
594
+ }.must_raise Yaml2env::AlreadyLoadedError
595
+ assert Yaml2env.require! 'fixtures/example2.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
596
+ end
597
+ end
598
+
599
+ describe ".require" do
600
+ before do
601
+ Yaml2env.env = 'production'
602
+ Yaml2env.root = File.dirname(__FILE__)
603
+ Yaml2env.logger = nil # ::Logger.new(::STDOUT)
604
+ Yaml2env.stubs(:loaded).returns({})
605
+ end
606
+
607
+ it 'should be defined' do
608
+ Yaml2env.must_respond_to :require
609
+ end
610
+
611
+ it 'should at maximum log warning if specified config file that do not exist' do
612
+ proc { Yaml2env.require('null.yml') }.must_be_silent
613
+ end
614
+
615
+ it 'should not log warning or raise error if specified config file do exist' do
616
+ proc { Yaml2env.require('fixtures/example.yml') }.must_be_silent
617
+ end
618
+
619
+ it 'should at maximum log warning if a specified constant-key do not exist in the config file' do
620
+ proc { Yaml2env.require 'fixtures/example.yml', {:API_KEY => 'bla'} }.must_be_silent
621
+ end
622
+
623
+ it 'should not log warning or raise error if a specified constant-key do in fact exist in the config file' do
624
+ proc {
625
+ assert Yaml2env.require 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
626
+ }.must_be_silent
627
+ end
628
+
629
+ it 'should set - with Yaml2env - requireed ENV-values' do
630
+ Yaml2env::LOADED_ENV.clear unless Yaml2env::LOADED_ENV.empty?
631
+ Yaml2env.require 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
632
+ Yaml2env::LOADED_ENV.must_equal({"API_SECRET" => "PRODUCTION_SECRET", "API_KEY" => "PRODUCTION_KEY"})
633
+ end
634
+
635
+ it "should only load specified file if it has not been loaded already - like Ruby require vs. load" do
636
+ Yaml2env.require('fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}).must_equal true
637
+ Yaml2env.require('fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}).must_equal false
638
+ Yaml2env.require('fixtures/example2.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}).must_equal true
639
+
640
+ # FIXME: How test output using Logger - only works with "puts", and also need to silence spec output. :P
641
+ # args = ['fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}]
642
+ # lambda {
643
+ # Yaml2env.require *args
644
+ # }.must_output "[Yaml2env] Already loaded: -- arguments: [\"fixtures/example.yml\", {:API_KEY=>\"api_key\", :API_SECRET=>\"api_secret\"}])\n"
645
+ end
646
+ end
647
+
648
+ describe ".loaded" do
649
+ before do
650
+ Yaml2env.env = 'production'
651
+ Yaml2env.root = File.dirname(__FILE__)
652
+ Yaml2env.logger = nil
653
+ end
654
+
655
+ it 'should be defined' do
656
+ Yaml2env.must_respond_to :loaded
657
+ end
658
+
659
+ it 'should hold any loaded values - based on loaded filename as key' do
660
+ key_1 = File.join(File.dirname(__FILE__), 'fixtures/example.yml')
661
+ key_2 = File.join(File.dirname(__FILE__), 'fixtures/example2.yml')
662
+
663
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
664
+ Yaml2env.loaded.keys.must_include key_1
665
+ Yaml2env.loaded[key_1].must_equal({'api_key' => 'PRODUCTION_KEY', 'api_secret' => 'PRODUCTION_SECRET'})
666
+
667
+ Yaml2env.load 'fixtures/example2.yml', {:API_KEY => 'api_key', :API_SECRET => 'api_secret'}
668
+ Yaml2env.loaded.keys.must_include key_1
669
+ Yaml2env.loaded.keys.must_include key_2
670
+ Yaml2env.loaded[key_1].must_equal({'api_key' => 'PRODUCTION_KEY', 'api_secret' => 'PRODUCTION_SECRET'})
671
+ Yaml2env.loaded[key_2].must_equal({'api_key' => 'PRODUCTION_KEY_2', 'api_secret' => 'PRODUCTION_SECRET_2'})
672
+ end
673
+ end
674
+
675
+ describe ".loaded_files" do
676
+ it 'should be defined' do
677
+ Yaml2env.must_respond_to :loaded_files
678
+ end
679
+
680
+ it 'should return loaded files' do
681
+ file_1 = File.join(File.dirname(__FILE__), 'fixtures/example.yml')
682
+ file_2 = File.join(File.dirname(__FILE__), 'fixtures/example2.yml')
683
+
684
+ Yaml2env.loaded_files.must_be_kind_of Array
685
+
686
+ Yaml2env.load 'fixtures/example.yml'
687
+ Yaml2env.loaded_files.must_include file_1
688
+
689
+ Yaml2env.load 'fixtures/example2.yml'
690
+ Yaml2env.loaded_files.must_include file_1, file_2
691
+ end
692
+ end
693
+
335
694
  describe ".loaded?" do
336
695
  before do
337
696
  Yaml2env::LOADED_ENV.clear unless Yaml2env::LOADED_ENV.empty?
@@ -369,54 +728,308 @@ describe Yaml2env do
369
728
  end
370
729
  end
371
730
 
372
- protected
731
+ describe ".log_env" do
732
+ before do
733
+ Yaml2env.default_env = nil
734
+ end
735
+
736
+ it 'should be defined' do
737
+ Yaml2env.must_respond_to :log_env
738
+ end
373
739
 
374
- def rack!(loaded)
375
- if loaded
376
- ::ENV['RACK_ROOT'] = '/home/grimen/development/rack-app'
377
- ::ENV['RACK_ENV'] = 'rack-env'
378
- else
379
- ::ENV['RACK_ROOT'] = nil
380
- ::ENV['RACK_ENV'] = nil
740
+ it 'should log env value' do
741
+ Yaml2env.env = nil
742
+ lambda {
743
+ Yaml2env.log_env
744
+ }.must_output %{:: Yaml2env.env = nil\n}
745
+
746
+ Yaml2env.env = 'development'
747
+ lambda {
748
+ Yaml2env.log_env
749
+ }.must_output %{:: Yaml2env.env = "development"\n}
750
+ end
751
+
752
+ it 'should show default env is used if this is the case' do
753
+ Yaml2env.default_env = 'development'
754
+ Yaml2env.env = 'development'
755
+ lambda {
756
+ Yaml2env.log_env
757
+ }.must_output %{:: Yaml2env.env = "development" (default)\n}
758
+
759
+ Yaml2env.default_env = 'development'
760
+ Yaml2env.env = 'staging'
761
+ lambda {
762
+ Yaml2env.log_env
763
+ }.must_output %{:: Yaml2env.env = "staging"\n}
764
+ end
765
+ end
766
+
767
+ describe ".log_root" do
768
+ it 'should be defined' do
769
+ Yaml2env.must_respond_to :log_root
770
+ end
771
+
772
+ it 'should log root value' do
773
+ Yaml2env.root = nil
774
+ lambda {
775
+ Yaml2env.log_root
776
+ }.must_output %{:: Yaml2env.root = nil\n}
777
+
778
+ Yaml2env.root = '/tmp/path'
779
+ lambda {
780
+ Yaml2env.log_root
781
+ }.must_output %{:: Yaml2env.root = "/tmp/path"\n}
782
+ end
783
+ end
784
+
785
+ describe ".log_values" do
786
+ it 'should be defined' do
787
+ Yaml2env.must_respond_to :log_values
788
+ end
789
+
790
+ it 'should log ENV values' do
791
+ with_constants :ENV => {"API_SECRET" => "PRODUCTION_SECRET", "API_KEY" => "PRODUCTION_KEY"} do
792
+ lambda {
793
+ Yaml2env.log_values
794
+ }.must_output %{:: ENV = \"API_KEY\" => \"PRODUCTION_KEY\", \"API_SECRET\" => \"PRODUCTION_SECRET\"\n}
381
795
  end
382
796
  end
383
797
 
384
- def rails!(loaded)
385
- if loaded
386
- eval <<-EVAL
387
- unless defined?(::Rails)
388
- module ::Rails
389
- class << self
390
- attr_accessor :root, :env
391
- end
392
- end
393
- end
394
- EVAL
395
- Rails.root = '/home/grimen/development/rails-app'
396
- Rails.env = 'rails-env'
397
- else
398
- Object.send(:remove_const, :Rails) if defined?(::Rails)
798
+ it 'should log ENV values for specified key(s)' do
799
+ with_constants :ENV => {"API_SECRET" => "PRODUCTION_SECRET", "API_KEY" => "PRODUCTION_KEY"} do
800
+ lambda {
801
+ Yaml2env.log_values 'API_KEY'
802
+ }.must_output %{:: ENV = \"API_KEY\" => \"PRODUCTION_KEY\"\n}
803
+
804
+ lambda {
805
+ Yaml2env.log_values 'API_KEY', 'API_SECRET', 'BOGUS'
806
+ }.must_output %{:: ENV = \"API_KEY\" => \"PRODUCTION_KEY\", \"API_SECRET\" => \"PRODUCTION_SECRET\"\n}
399
807
  end
400
808
  end
401
809
 
402
- def sinatra!(loaded)
403
- if loaded
404
- eval <<-EVAL
405
- unless defined?(::Sinatra::Application)
406
- module ::Sinatra
407
- class Application
408
- class << self
409
- attr_accessor :root, :environment
410
- end
411
- end
412
- end
413
- end
414
- EVAL
415
- Sinatra::Application.root = '/home/grimen/development/sinatra-app'
416
- Sinatra::Application.environment = 'sinatra-env'
417
- else
418
- Object.send(:remove_const, :Sinatra) if defined?(::Sinatra::Application)
810
+ it 'should log ENV values for specified key expression' do
811
+ with_constants :ENV => {"API_SECRET" => "PRODUCTION_SECRET", "API_KEY" => "PRODUCTION_KEY"} do
812
+ lambda {
813
+ Yaml2env.log_values /API_KEY/
814
+ }.must_output %{:: ENV = \"API_KEY\" => \"PRODUCTION_KEY\"\n}
815
+
816
+ lambda {
817
+ Yaml2env.log_values /API_KEY|API_SECRET|BOGUS/
818
+ }.must_output %{:: ENV = \"API_KEY\" => \"PRODUCTION_KEY\", \"API_SECRET\" => \"PRODUCTION_SECRET\"\n}
419
819
  end
420
820
  end
821
+ end
822
+
823
+ describe ".assert_keys!" do
824
+ before do
825
+ Yaml2env.root = File.dirname(__FILE__)
826
+ Yaml2env.env = 'development'
827
+ Yaml2env.logger = nil # ::Logger.new(::STDOUT)
828
+ end
829
+
830
+ it 'should be defined' do
831
+ Yaml2env.must_respond_to :assert_keys!
832
+ end
833
+
834
+ it 'should raise error if no arguments are specified' do
835
+ lambda {
836
+ Yaml2env.assert_keys!
837
+ }.must_raise Yaml2env::ArgumentError
838
+ end
839
+
840
+ it 'should raise error if specified keys are not loaded into ENV' do
841
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY_6 => 'api_key', :API_SECRET_6 => 'api_secret'}
842
+
843
+ lambda {
844
+ Yaml2env.assert_keys! :BOGUS
845
+ }.must_raise Yaml2env::MissingConfigKeyError
846
+
847
+ lambda {
848
+ Yaml2env.assert_keys! :BOGUS, :BOGUS_2
849
+ }.must_raise Yaml2env::MissingConfigKeyError
850
+ end
851
+
852
+ it 'should not raise error if specified keys are loaded into ENV' do
853
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY_6 => 'api_key', :API_SECRET_6 => 'api_secret'}
854
+
855
+ lambda {
856
+ Yaml2env.assert_keys! :API_KEY_6
857
+ }.must_be_silent
858
+
859
+ lambda {
860
+ Yaml2env.assert_keys! :API_KEY_6, :API_SECRET_6
861
+ }.must_be_silent
862
+ end
863
+ end
864
+
865
+ describe ".assert_keys" do
866
+ before do
867
+ Yaml2env.root = File.dirname(__FILE__)
868
+ Yaml2env.env = 'development'
869
+ Yaml2env.logger = nil # ::Logger.new(::STDOUT)
870
+ end
871
+
872
+ it 'should be defined' do
873
+ Yaml2env.must_respond_to :assert_keys
874
+ end
875
+
876
+ it 'should raise error if no arguments are specified' do
877
+ lambda {
878
+ Yaml2env.assert_keys
879
+ }.must_raise Yaml2env::ArgumentError
880
+ end
881
+
882
+ it 'should log warning if specified keys are not loaded into ENV' do
883
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY_5 => 'api_key', :API_SECRET_5 => 'api_secret'}
884
+
885
+ lambda {
886
+ Yaml2env.assert_keys :BOGUS
887
+ }.must_output %{[Yaml2env] WARN: Assertion failed, no such ENV-keys loaded: \"BOGUS\"\n}
888
+
889
+ lambda {
890
+ Yaml2env.assert_keys :BOGUS, :BOGUS_2
891
+ }.must_output %{[Yaml2env] WARN: Assertion failed, no such ENV-keys loaded: \"BOGUS\", \"BOGUS_2\"\n}
892
+ end
893
+
894
+ it 'should not log warning if specified keys are loaded into ENV' do
895
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY_5 => 'api_key', :API_SECRET_5 => 'api_secret'}
896
+
897
+ lambda {
898
+ Yaml2env.assert_keys :API_KEY_5
899
+ }.must_be_silent
900
+
901
+ lambda {
902
+ Yaml2env.assert_keys :API_KEY_5, :API_SECRET_5
903
+ }.must_be_silent
904
+ end
905
+ end
906
+
907
+ describe ".assert_values!" do
908
+ before do
909
+ Yaml2env.root = File.dirname(__FILE__)
910
+ Yaml2env.env = 'development'
911
+ Yaml2env.logger = nil # ::Logger.new(::STDOUT)
912
+ end
913
+
914
+ it 'should be defined' do
915
+ Yaml2env.must_respond_to :assert_values!
916
+ end
917
+
918
+ it 'should raise error if no hash is specified' do
919
+ lambda {
920
+ Yaml2env.assert_values!
921
+ }.must_raise ArgumentError
922
+
923
+ lambda {
924
+ Yaml2env.assert_values!({})
925
+ }.must_raise Yaml2env::ArgumentError
926
+
927
+ lambda {
928
+ Yaml2env.assert_values! :API_KEY_4
929
+ }.must_raise Yaml2env::ArgumentError
930
+ end
931
+
932
+ it 'should raise error if specified hash values are not regular expression values' do
933
+ lambda {
934
+ Yaml2env.assert_values! :API_KEY_4 => 'DEVELOPMENT_KEY'
935
+ }.must_raise Yaml2env::ArgumentError
936
+
937
+ lambda {
938
+ Yaml2env.assert_values! :API_KEY_4 => 'DEVELOPMENT_KEY', :API_SECRET_4 => /[A-Z]+/
939
+ }.must_raise Yaml2env::ArgumentError
940
+ end
941
+
942
+ it 'should raise error if specified keys with - based on the expression - valid values are not loaded into ENV' do
943
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY_4 => 'api_key', :API_SECRET_4 => 'api_secret'}
944
+
945
+ lambda {
946
+ Yaml2env.assert_values! :API_KEY_4 => /[a-z]+/
947
+ }.must_raise Yaml2env::InvalidConfigValueError
948
+
949
+ lambda {
950
+ Yaml2env.assert_values! :API_KEY_4 => /[a-z]+/, :API_SECRET_4 => /[A-Z]+/
951
+ }.must_raise Yaml2env::InvalidConfigValueError
952
+
953
+ lambda {
954
+ Yaml2env.assert_values! :API_KEY_4 => /[a-z]+/, :API_SECRET_4 => /[a-z]+/
955
+ }.must_raise Yaml2env::InvalidConfigValueError
956
+ end
957
+
958
+ it 'should not raise error if specified keys with - based on the expression - valid values are loaded into ENV' do
959
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY_4 => 'api_key', :API_SECRET_4 => 'api_secret'}
960
+
961
+ lambda {
962
+ Yaml2env.assert_values! :API_KEY_4 => /[A-Z]+/
963
+ }.must_be_silent
964
+
965
+ lambda {
966
+ Yaml2env.assert_values! :API_KEY_4 => /[A-Z]+/, :API_SECRET_4 => /[A-Z]+/
967
+ }.must_be_silent
968
+ end
969
+ end
970
+
971
+ describe ".assert_values" do
972
+ before do
973
+ Yaml2env.root = File.dirname(__FILE__)
974
+ Yaml2env.env = 'development'
975
+ Yaml2env.logger = nil # ::Logger.new(::STDOUT)
976
+ end
977
+
978
+ it 'should be defined' do
979
+ Yaml2env.must_respond_to :assert_values
980
+ end
981
+
982
+ it 'should raise error if no hash is specified' do
983
+ lambda {
984
+ Yaml2env.assert_values
985
+ }.must_raise ArgumentError
986
+
987
+ lambda {
988
+ Yaml2env.assert_values({})
989
+ }.must_raise Yaml2env::ArgumentError
990
+
991
+ lambda {
992
+ Yaml2env.assert_values :API_KEY_2
993
+ }.must_raise Yaml2env::ArgumentError
994
+ end
995
+
996
+ it 'should raise error if specified hash values are not regular expression values' do
997
+ lambda {
998
+ Yaml2env.assert_values :API_KEY_2 => 'DEVELOPMENT_KEY'
999
+ }.must_raise Yaml2env::ArgumentError
1000
+
1001
+ lambda {
1002
+ Yaml2env.assert_values :API_KEY_2 => 'DEVELOPMENT_KEY', :API_SECRET_2 => /[A-Z]+/
1003
+ }.must_raise Yaml2env::ArgumentError
1004
+ end
1005
+
1006
+ it 'should log warning if specified keys with - based on the expression - valid values are not loaded into ENV' do
1007
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY_2 => 'api_key', :API_SECRET_2 => 'api_secret'}
1008
+
1009
+ lambda {
1010
+ Yaml2env.assert_values :API_KEY_2 => /[a-z]+/
1011
+ }.must_output %{[Yaml2env] WARN: Assertion failed, invalid values: \"API_KEY_2\" => /[a-z]+/\n}
1012
+
1013
+ lambda {
1014
+ Yaml2env.assert_values :API_KEY_2 => /[a-z]+/, :API_SECRET_2 => /[A-Z]+/
1015
+ }.must_output %{[Yaml2env] WARN: Assertion failed, invalid values: \"API_KEY_2\" => /[a-z]+/, \"API_SECRET_2\" => /[A-Z]+/\n}
1016
+
1017
+ lambda {
1018
+ Yaml2env.assert_values :API_KEY_2 => /[a-z]+/, :API_SECRET_2 => /[a-z]+/
1019
+ }.must_output %{[Yaml2env] WARN: Assertion failed, invalid values: \"API_KEY_2\" => /[a-z]+/, \"API_SECRET_2\" => /[a-z]+/\n}
1020
+ end
1021
+
1022
+ it 'should not log warning if specified keys with - based on the expression - valid values are loaded into ENV' do
1023
+ Yaml2env.load 'fixtures/example.yml', {:API_KEY_3 => 'api_key', :API_SECRET_3 => 'api_secret'}
1024
+
1025
+ lambda {
1026
+ Yaml2env.assert_values :API_KEY_3 => /[A-Z]+/
1027
+ }.must_be_silent
1028
+
1029
+ lambda {
1030
+ Yaml2env.assert_values :API_KEY_3 => /[A-Z]+/, :API_SECRET_3 => /[A-Z]+/
1031
+ }.must_be_silent
1032
+ end
1033
+ end
421
1034
 
422
1035
  end