mixlib-config 2.2.13 → 3.0.9

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.
data/Rakefile DELETED
@@ -1,32 +0,0 @@
1
- require "bundler"
2
- require "rubygems"
3
- require "rubygems/package_task"
4
- require "rdoc/task"
5
- require "rspec/core/rake_task"
6
- require "mixlib/config/version"
7
-
8
- Bundler::GemHelper.install_tasks
9
-
10
- task default: [:style, :spec]
11
-
12
- desc "Run specs"
13
- RSpec::Core::RakeTask.new(:spec) do |spec|
14
- spec.pattern = "spec/**/*_spec.rb"
15
- end
16
-
17
- begin
18
- require "chefstyle"
19
- require "rubocop/rake_task"
20
- RuboCop::RakeTask.new(:style) do |task|
21
- task.options += ["--display-cop-names", "--no-color"]
22
- end
23
- rescue LoadError
24
- puts "chefstyle/rubocop is not available. gem install chefstyle to do style checking."
25
- end
26
-
27
- RDoc::Task.new do |rdoc|
28
- rdoc.rdoc_dir = "rdoc"
29
- rdoc.title = "mixlib-config #{Mixlib::Config::VERSION}"
30
- rdoc.rdoc_files.include("README*")
31
- rdoc.rdoc_files.include("lib/**/*.rb")
32
- end
@@ -1,32 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- $:.unshift(File.dirname(__FILE__) + "/lib")
4
- require "mixlib/config/version"
5
-
6
- Gem::Specification.new do |s|
7
- s.name = "mixlib-config"
8
- s.version = Mixlib::Config::VERSION
9
-
10
- s.authors = ["Chef Software, Inc."]
11
- s.email = "legal@chef.io"
12
- s.extra_rdoc_files = [
13
- "LICENSE",
14
- "README.md",
15
- ]
16
- s.files = ["LICENSE", "NOTICE", "README.md", "Gemfile", "Rakefile"] + Dir.glob("*.gemspec") +
17
- Dir.glob("{lib,spec}/**/*", File::FNM_DOTMATCH).reject { |f| File.directory?(f) }
18
- s.homepage = "https://www.chef.io"
19
- s.require_paths = ["lib"]
20
- s.rubygems_version = "1.8.23"
21
- s.required_ruby_version = ">= 2.2"
22
- s.summary = "A class based configuration library"
23
- s.description = s.summary
24
- s.license = "Apache-2.0"
25
-
26
- s.add_dependency "tomlrb"
27
-
28
- s.add_development_dependency "rake"
29
- s.add_development_dependency "rspec", "~> 3.0"
30
- s.add_development_dependency "chefstyle"
31
- s.add_development_dependency "rdoc"
32
- end
@@ -1,1301 +0,0 @@
1
- #
2
- # Author:: Adam Jacob (<adam@chef.io>)
3
- # Copyright:: Copyright (c) 2008-2018, Chef Software Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
20
-
21
- describe Mixlib::Config do
22
- before(:each) do
23
- ConfigIt.configure do |c|
24
- c[:alpha] = "omega"
25
- c[:foo] = nil
26
- end
27
- end
28
-
29
- it "loads a config file" do
30
- allow(File).to receive(:exists?).and_return(true)
31
- allow(File).to receive(:readable?).and_return(true)
32
- allow(IO).to receive(:read).with("config.rb").and_return("alpha = 'omega'\nfoo = 'bar'")
33
- expect(lambda do
34
- ConfigIt.from_file("config.rb")
35
- end).to_not raise_error
36
- end
37
-
38
- it "doesn't raise an ArgumentError with an explanation if you try and set a non-existent variable" do
39
- expect(lambda do
40
- ConfigIt[:foobar] = "blah"
41
- end).to_not raise_error
42
- end
43
-
44
- it "raises an Errno::ENOENT if it can't find the file" do
45
- expect(lambda do
46
- ConfigIt.from_file("/tmp/timmytimmytimmy")
47
- end).to raise_error(Errno::ENOENT)
48
- end
49
-
50
- it "allows the error to bubble up when it's anything other than IOError" do
51
- allow(IO).to receive(:read).with("config.rb").and_return("@#asdf")
52
- expect(lambda do
53
- ConfigIt.from_file("config.rb")
54
- end).to raise_error(SyntaxError)
55
- end
56
-
57
- it "allows you to reference a value by index" do
58
- expect(ConfigIt[:alpha]).to eql("omega")
59
- end
60
-
61
- it "allows you to reference a value by string index" do
62
- expect(ConfigIt["alpha"]).to eql("omega")
63
- end
64
-
65
- it "allows you to set a value by index" do
66
- ConfigIt[:alpha] = "one"
67
- expect(ConfigIt[:alpha]).to eql("one")
68
- end
69
-
70
- it "allows you to set a value by string index" do
71
- ConfigIt["alpha"] = "one"
72
- expect(ConfigIt[:alpha]).to eql("one")
73
- end
74
-
75
- it "allows setting a value with attribute form" do
76
- ConfigIt.arbitrary_value = 50
77
- expect(ConfigIt.arbitrary_value).to eql(50)
78
- expect(ConfigIt[:arbitrary_value]).to eql(50)
79
- end
80
-
81
- it "allows setting a value with method form" do
82
- ConfigIt.arbitrary_value 50
83
- expect(ConfigIt.arbitrary_value).to eql(50)
84
- expect(ConfigIt[:arbitrary_value]).to eql(50)
85
- end
86
-
87
- describe "when strict mode is on" do
88
- class StrictClass
89
- extend ::Mixlib::Config
90
- config_strict_mode true
91
- default :x, 1
92
- end
93
-
94
- it "allows you to get and set configured values" do
95
- StrictClass.x = StrictClass.x * 2
96
- StrictClass[:x] = StrictClass[:x] * 2
97
- end
98
-
99
- it "raises an error when you get an arbitrary config option with .y" do
100
- expect(lambda { StrictClass.y }).to raise_error(Mixlib::Config::UnknownConfigOptionError, "Reading unsupported config value y.")
101
- end
102
-
103
- it "raises an error when you get an arbitrary config option with [:y]" do
104
- expect(lambda { StrictClass[:y] }).to raise_error(Mixlib::Config::UnknownConfigOptionError, "Reading unsupported config value y.")
105
- end
106
-
107
- it "raises an error when you set an arbitrary config option with .y = 10" do
108
- expect(lambda { StrictClass.y = 10 }).to raise_error(Mixlib::Config::UnknownConfigOptionError, "Cannot set unsupported config value y.")
109
- end
110
-
111
- it "raises an error when you set an arbitrary config option with .y 10" do
112
- expect(lambda { StrictClass.y 10 }).to raise_error(Mixlib::Config::UnknownConfigOptionError, "Cannot set unsupported config value y.")
113
- end
114
-
115
- it "raises an error when you set an arbitrary config option with [:y] = 10" do
116
- expect(lambda { StrictClass[:y] = 10 }).to raise_error(Mixlib::Config::UnknownConfigOptionError, "Cannot set unsupported config value y.")
117
- end
118
-
119
- it "does not break config_context_list" do
120
- expect(lambda do
121
- StrictClass.class_eval do
122
- config_context_list(:lists, :list) do
123
- default :y, 20
124
- end
125
- end
126
- end).not_to raise_error
127
- end
128
-
129
- it "does not break config_context_hash" do
130
- expect(lambda do
131
- StrictClass.class_eval do
132
- config_context_hash(:hashes, :hash) do
133
- default :z, 20
134
- end
135
- end
136
- end).not_to raise_error
137
- end
138
- end
139
-
140
- describe "when a block has been used to set config values" do
141
- before do
142
- ConfigIt.configure { |c| c[:cookbook_path] = "monkey_rabbit"; c[:otherthing] = "boo" }
143
- end
144
-
145
- { cookbook_path: "monkey_rabbit", otherthing: "boo" }.each do |k, v|
146
- it "allows you to retrieve the config value for #{k} via []" do
147
- expect(ConfigIt[k]).to eql(v)
148
- end
149
- it "allows you to retrieve the config value for #{k} via method_missing" do
150
- expect(ConfigIt.send(k)).to eql(v)
151
- end
152
- end
153
- end
154
-
155
- it "doesn't raise an ArgumentError if you access a config option that does not exist" do
156
- expect(lambda { ConfigIt[:snob_hobbery] }).to_not raise_error
157
- end
158
-
159
- it "returns true or false with has_key?" do
160
- expect(ConfigIt.has_key?(:monkey)).to be false
161
- ConfigIt[:monkey] = "gotcha"
162
- expect(ConfigIt.has_key?(:monkey)).to be true
163
- end
164
-
165
- it "returns true or false with key?" do
166
- expect(ConfigIt.key?(:monkey2)).to be false
167
- ConfigIt[:monkey2] = "gotcha"
168
- expect(ConfigIt.key?(:monkey2)).to be true
169
- end
170
-
171
- describe "when a class method override writer exists" do
172
- before do
173
- @klass = Class.new
174
- @klass.extend(::Mixlib::Config)
175
- @klass.class_eval do
176
- config_attr_writer :test_method do |blah|
177
- blah.is_a?(Integer) ? blah * 1000 : blah
178
- end
179
- end
180
- end
181
-
182
- it "multiplies an integer by 1000" do
183
- @klass[:test_method] = 53
184
- expect(@klass[:test_method]).to eql(53000)
185
- end
186
-
187
- it "multiplies an integer by 1000 with the method_missing form" do
188
- @klass.test_method = 63
189
- expect(@klass.test_method).to eql(63000)
190
- end
191
-
192
- it "multiplies an integer by 1000 with the instance_eval DSL form" do
193
- @klass.instance_eval("test_method 73")
194
- expect(@klass.test_method).to eql(73000)
195
- end
196
-
197
- it "multiplies an integer by 1000 via from-file, too" do
198
- allow(IO).to receive(:read).with("config.rb").and_return("test_method 99")
199
- @klass.from_file("config.rb")
200
- expect(@klass.test_method).to eql(99000)
201
- end
202
-
203
- it "receives internal_set with the method name and config value" do
204
- expect(@klass).to receive(:internal_set).with(:test_method, 53).and_return(true)
205
- @klass[:test_method] = 53
206
- end
207
-
208
- end
209
-
210
- describe "When a configurable exists" do
211
- before :each do
212
- @klass = Class.new
213
- @klass.extend(::Mixlib::Config)
214
- @klass.class_eval do
215
- configurable :daemonizeme
216
- default :a, 1
217
- config_attr_writer(:b) { |v| v }
218
- config_context(:c)
219
- end
220
- end
221
-
222
- it "Getter methods are created for the configurable" do
223
- expect(@klass.respond_to?(:daemonizeme)).to be true
224
- expect(@klass.respond_to?(:a)).to be true
225
- expect(@klass.respond_to?(:b)).to be true
226
- expect(@klass.respond_to?(:c)).to be true
227
- expect(@klass.respond_to?(:z)).to be false
228
- end
229
-
230
- it "Setter methods are created for the configurable" do
231
- expect(@klass.respond_to?("daemonizeme=".to_sym)).to be true
232
- expect(@klass.respond_to?("a=".to_sym)).to be true
233
- expect(@klass.respond_to?("b=".to_sym)).to be true
234
- expect(@klass.respond_to?("c=".to_sym)).to be true
235
- expect(@klass.respond_to?("z=".to_sym)).to be false
236
- end
237
-
238
- it "returns true for is_default? for a default value" do
239
- expect(@klass[:a]).to eql(1)
240
- expect(@klass.is_default?(:a)).to be true
241
- end
242
-
243
- it "returns true for is_default? for an overwritten default value" do
244
- @klass[:a] = 1
245
- expect(@klass[:a]).to eql(1)
246
- expect(@klass.is_default?(:a)).to be true
247
- end
248
-
249
- it "returns false for is_default? for a value that is not the default" do
250
- @klass[:a] = 2
251
- expect(@klass[:a]).to eql(2)
252
- expect(@klass.is_default?(:a)).to be false
253
- end
254
-
255
- describe "and extra methods have been dumped into Object" do
256
- class NopeError < StandardError
257
- end
258
- before :each do
259
- Object.send :define_method, :daemonizeme do
260
- raise NopeError, "NOPE"
261
- end
262
- Object.send :define_method, "daemonizeme=".to_sym do
263
- raise NopeError, "NOPE"
264
- end
265
- end
266
-
267
- after do
268
- Object.send :remove_method, :daemonizeme
269
- Object.send :remove_method, :'daemonizeme='
270
- end
271
-
272
- it "Normal classes call the extra method" do
273
- normal_class = Class.new
274
- normal_class.extend(::Mixlib::Config)
275
- expect(lambda { normal_class.daemonizeme }).to raise_error(NopeError)
276
- end
277
-
278
- it "Configurables with the same name as the extra method can be set" do
279
- @klass.daemonizeme = 10
280
- expect(@klass[:daemonizeme]).to eql(10)
281
- end
282
-
283
- it "Configurables with the same name as the extra method can be retrieved" do
284
- @klass[:daemonizeme] = 10
285
- expect(@klass.daemonizeme).to eql(10)
286
- end
287
- end
288
- end
289
-
290
- describe "When config has a default value" do
291
- before :each do
292
- @klass = Class.new
293
- @klass.extend(::Mixlib::Config)
294
- @klass.class_eval { default :attr, 4 }
295
- end
296
-
297
- it "defaults to that value" do
298
- expect(@klass.attr).to eql(4)
299
- end
300
-
301
- it "defaults to that value when retrieved as a hash" do
302
- expect(@klass[:attr]).to eql(4)
303
- end
304
-
305
- it "is settable to another value" do
306
- @klass.attr 5
307
- expect(@klass.attr).to eql(5)
308
- end
309
-
310
- it "still defaults to that value after delete" do
311
- @klass.attr 5
312
- @klass.delete(:attr)
313
- expect(@klass.attr).to eql(4)
314
- end
315
-
316
- it "still defaults to that value after reset" do
317
- @klass.attr 5
318
- @klass.reset
319
- expect(@klass.attr).to eql(4)
320
- end
321
-
322
- it "save should not save anything for it" do
323
- expect(@klass.save).to eql({})
324
- end
325
-
326
- it "save with include_defaults should save all defaults" do
327
- expect(@klass.save(true)).to eql({ attr: 4 })
328
- end
329
-
330
- it "saves the new value if it gets set" do
331
- @klass.attr 5
332
- expect((saved = @klass.save)).to eql({ attr: 5 })
333
- @klass.reset
334
- expect(@klass.attr).to eql(4)
335
- @klass.restore(saved)
336
- expect(@klass.attr).to eql(5)
337
- end
338
-
339
- it "saves the new value even if it is set to its default value" do
340
- @klass.attr 4
341
- expect((saved = @klass.save)).to eql({ attr: 4 })
342
- @klass.reset
343
- expect(@klass.save).to eql({})
344
- @klass.restore(saved)
345
- expect(@klass.save).to eql({ attr: 4 })
346
- end
347
- end
348
-
349
- describe "When config has a default value block" do
350
- before :each do
351
- @klass = Class.new
352
- @klass.extend(::Mixlib::Config)
353
- @klass.class_eval do
354
- default :x, 4
355
- default(:attr) { x * 2 }
356
- end
357
- end
358
-
359
- it "defaults to that value" do
360
- expect(@klass.attr).to eql(8)
361
- end
362
-
363
- it "is recalculated each time it is retrieved" do
364
- expect(@klass.attr).to eql(8)
365
- @klass.x = 2
366
- expect(@klass.attr).to eql(4)
367
- end
368
-
369
- it "defaults to that value when retrieved as a hash" do
370
- expect(@klass[:attr]).to eql(8)
371
- end
372
-
373
- it "is settable to another value" do
374
- @klass.attr 5
375
- expect(@klass.attr).to eql(5)
376
- end
377
-
378
- it "still defaults to that value after delete" do
379
- @klass.attr 5
380
- @klass.delete(:attr)
381
- expect(@klass.attr).to eql(8)
382
- end
383
-
384
- it "still defaults to that value after reset" do
385
- @klass.attr 5
386
- @klass.reset
387
- expect(@klass.attr).to eql(8)
388
- end
389
-
390
- it "save should not save anything for it" do
391
- expect(@klass.save).to eql({})
392
- end
393
-
394
- it "save with include_defaults should save all defaults" do
395
- expect(@klass.save(true)).to eql({ attr: 8, x: 4 })
396
- end
397
-
398
- it "saves the new value if it gets set" do
399
- @klass.attr 5
400
- expect((saved = @klass.save)).to eql({ attr: 5 })
401
- @klass.reset
402
- expect(@klass.attr).to eql(8)
403
- @klass.restore(saved)
404
- expect(@klass.attr).to eql(5)
405
- end
406
-
407
- it "saves the new value even if it is set to its default value" do
408
- @klass.attr 8
409
- expect((saved = @klass.save)).to eql({ attr: 8 })
410
- @klass.reset
411
- expect(@klass.save).to eql({})
412
- @klass.restore(saved)
413
- expect(@klass.save).to eql({ attr: 8 })
414
- end
415
- end
416
-
417
- describe "When config has an array default value" do
418
- before :each do
419
- @klass = Class.new
420
- @klass.extend(::Mixlib::Config)
421
- @klass.class_eval { default :attr, [] }
422
- end
423
-
424
- it "reset clears it to its default" do
425
- @klass.attr << "x"
426
- expect(@klass.attr).to eql([ "x" ])
427
- @klass.reset
428
- expect(@klass.attr).to eql([])
429
- end
430
-
431
- it "save should not save anything for it" do
432
- expect(@klass.save).to eql({})
433
- end
434
-
435
- it "save with include_defaults should save all defaults" do
436
- expect(@klass.save(true)).to eql({ attr: [] })
437
- end
438
-
439
- it "saves the new value if it gets set" do
440
- @klass.attr << "x"
441
- expect((saved = @klass.save)).to eql({ attr: [ "x" ] })
442
- @klass.reset
443
- expect(@klass.attr).to eql([])
444
- @klass.restore(saved)
445
- expect(@klass.attr).to eql([ "x" ])
446
- end
447
-
448
- it "saves the new value even if it is set to its default value" do
449
- @klass.attr = []
450
- expect((saved = @klass.save)).to eql({ attr: [] })
451
- @klass.reset
452
- expect(@klass.save).to eql({})
453
- @klass.restore(saved)
454
- expect(@klass.save).to eql({ attr: [] })
455
- end
456
- end
457
-
458
- describe "When config has a hash default value" do
459
- before :each do
460
- @klass = Class.new
461
- @klass.extend(::Mixlib::Config)
462
- @klass.class_eval { default :attr, {} }
463
- end
464
-
465
- it "reset clears it to its default" do
466
- @klass.attr[:x] = 10
467
- expect(@klass.attr[:x]).to eql(10)
468
- @klass.reset
469
- expect(@klass.attr[:x]).to be_nil
470
- end
471
-
472
- it "save should not save anything for it" do
473
- expect(@klass.save).to eql({})
474
- end
475
-
476
- it "save with include_defaults should save all defaults" do
477
- expect(@klass.save(true)).to eql({ attr: {} })
478
- end
479
-
480
- it "saves the new value if it gets set" do
481
- @klass.attr[:hi] = "lo"
482
- expect((saved = @klass.save)).to eql({ attr: { hi: "lo" } })
483
- @klass.reset
484
- expect(@klass.attr).to eql({})
485
- @klass.restore(saved)
486
- expect(@klass.save).to eql({ attr: { hi: "lo" } })
487
- end
488
-
489
- it "saves the new value even if it is set to its default value" do
490
- @klass.attr = {}
491
- expect((saved = @klass.save)).to eql({ attr: {} })
492
- @klass.reset
493
- expect(@klass.save).to eql({})
494
- @klass.restore(saved)
495
- expect(@klass.save).to eql({ attr: {} })
496
- end
497
- end
498
-
499
- describe "When config has a string default value" do
500
- before :each do
501
- @klass = Class.new
502
- @klass.extend(::Mixlib::Config)
503
- @klass.class_eval { default :attr, "hello" }
504
- end
505
-
506
- it "reset clears it to its default" do
507
- @klass.attr << " world"
508
- expect(@klass.attr).to eql("hello world")
509
- @klass.reset
510
- expect(@klass.attr).to eql("hello")
511
- end
512
-
513
- it "save should not save anything for it" do
514
- expect(@klass.save).to eql({})
515
- end
516
-
517
- it "save with include_defaults should save all defaults" do
518
- expect(@klass.save(true)).to eql({ attr: "hello" })
519
- end
520
-
521
- it "saves the new value if it gets set" do
522
- @klass.attr << " world"
523
- expect((saved = @klass.save)).to eql({ attr: "hello world" })
524
- @klass.reset
525
- expect(@klass.attr).to eql("hello")
526
- @klass.restore(saved)
527
- expect(@klass.attr).to eql("hello world")
528
- end
529
-
530
- it "saves the new value even if it is set to its default value" do
531
- @klass.attr "hello world"
532
- expect((saved = @klass.save)).to eql({ attr: "hello world" })
533
- @klass.reset
534
- expect(@klass.save).to eql({})
535
- @klass.restore(saved)
536
- expect(@klass.save).to eql({ attr: "hello world" })
537
- end
538
- end
539
-
540
- describe "When config has a a default value block" do
541
- before :each do
542
- @klass = Class.new
543
- @klass.extend(::Mixlib::Config)
544
- @klass.class_eval do
545
- default(:attr) { 4 }
546
- end
547
- end
548
-
549
- it "defaults to that value" do
550
- expect(@klass.attr).to eql(4)
551
- end
552
-
553
- it "defaults to that value when retrieved as a hash" do
554
- expect(@klass[:attr]).to eql(4)
555
- end
556
-
557
- it "is settable to another value" do
558
- @klass.attr 5
559
- expect(@klass.attr).to eql(5)
560
- expect(@klass[:attr]).to eql(5)
561
- end
562
-
563
- it "still defaults to that value after delete" do
564
- @klass.attr 5
565
- @klass.delete(:attr)
566
- expect(@klass.attr).to eql(4)
567
- end
568
-
569
- it "still defaults to that value after reset" do
570
- @klass.attr 5
571
- @klass.reset
572
- expect(@klass.attr).to eql(4)
573
- end
574
-
575
- it "save should not save anything for it" do
576
- expect(@klass.save).to eql({})
577
- end
578
-
579
- it "save with include_defaults should save all defaults" do
580
- expect(@klass.save(true)).to eql({ attr: 4 })
581
- end
582
-
583
- it "saves the new value if it gets set" do
584
- @klass.attr 5
585
- expect((saved = @klass.save)).to eql({ attr: 5 })
586
- @klass.reset
587
- expect(@klass.attr).to eql(4)
588
- @klass.restore(saved)
589
- expect(@klass.attr).to eql(5)
590
- end
591
-
592
- it "saves the new value even if it is set to its default value" do
593
- @klass.attr 4
594
- expect((saved = @klass.save)).to eql({ attr: 4 })
595
- @klass.reset
596
- expect(@klass.save).to eql({})
597
- @klass.restore(saved)
598
- expect(@klass.save).to eql({ attr: 4 })
599
- end
600
- end
601
-
602
- describe "When a configurable exists with writer and default value" do
603
- before :each do
604
- @klass = Class.new
605
- @klass.extend(::Mixlib::Config)
606
- @klass.class_eval do
607
- configurable(:attr) do |c|
608
- c.defaults_to(4)
609
- c.writes_value { |value| value * 2 }
610
- end
611
- end
612
- end
613
-
614
- it "defaults to that value" do
615
- expect(@klass.attr).to eql(4)
616
- end
617
-
618
- it "defaults to that value when retrieved as a hash" do
619
- expect(@klass[:attr]).to eql(4)
620
- end
621
-
622
- it "is settable to another value" do
623
- @klass.attr 5
624
- expect(@klass.attr).to eql(10)
625
- expect(@klass[:attr]).to eql(10)
626
- end
627
-
628
- it "is settable to another value with attr=" do
629
- @klass.attr = 5
630
- expect(@klass.attr).to eql(10)
631
- expect(@klass[:attr]).to eql(10)
632
- end
633
-
634
- it "is settable to another value with [:attr]=" do
635
- @klass[:attr] = 5
636
- expect(@klass.attr).to eql(10)
637
- expect(@klass[:attr]).to eql(10)
638
- end
639
-
640
- it "still defaults to that value after delete" do
641
- @klass.attr 5
642
- @klass.delete(:attr)
643
- expect(@klass.attr).to eql(4)
644
- end
645
-
646
- it "still defaults to that value after reset" do
647
- @klass.attr 5
648
- @klass.reset
649
- expect(@klass.attr).to eql(4)
650
- end
651
-
652
- it "save should not save anything for it" do
653
- expect(@klass.save).to eql({})
654
- end
655
-
656
- it "save with include_defaults should save all defaults" do
657
- expect(@klass.save(true)).to eql({ attr: 4 })
658
- end
659
-
660
- it "saves the new value if it gets set" do
661
- @klass.attr 5
662
- expect((saved = @klass.save)).to eql({ attr: 10 })
663
- @klass.reset
664
- expect(@klass.attr).to eql(4)
665
- @klass.restore(saved)
666
- expect(@klass.attr).to eql(10)
667
- end
668
-
669
- it "saves the new value even if it is set to its default value" do
670
- @klass.attr 4
671
- expect((saved = @klass.save)).to eql({ attr: 8 })
672
- @klass.reset
673
- expect(@klass.save).to eql({})
674
- @klass.restore(saved)
675
- expect(@klass.save).to eql({ attr: 8 })
676
- end
677
- end
678
-
679
- describe "When a configurable exists with writer and default value set in chained form" do
680
- before :each do
681
- @klass = Class.new
682
- @klass.extend(::Mixlib::Config)
683
- @klass.class_eval do
684
- configurable(:attr).defaults_to(4).writes_value { |value| value * 2 }
685
- end
686
- end
687
-
688
- it "defaults to that value" do
689
- expect(@klass.attr).to eql(4)
690
- end
691
-
692
- it "defaults to that value when retrieved as a hash" do
693
- expect(@klass[:attr]).to eql(4)
694
- end
695
-
696
- it "is settable to another value" do
697
- @klass.attr 5
698
- expect(@klass.attr).to eql(10)
699
- expect(@klass[:attr]).to eql(10)
700
- end
701
-
702
- it "is settable to another value with attr=" do
703
- @klass.attr = 5
704
- expect(@klass.attr).to eql(10)
705
- expect(@klass[:attr]).to eql(10)
706
- end
707
-
708
- it "is settable to another value with [:attr]=" do
709
- @klass[:attr] = 5
710
- expect(@klass.attr).to eql(10)
711
- expect(@klass[:attr]).to eql(10)
712
- end
713
-
714
- it "still defaults to that value after delete" do
715
- @klass.attr 5
716
- @klass.delete(:attr)
717
- expect(@klass.attr).to eql(4)
718
- end
719
-
720
- it "still defaults to that value after reset" do
721
- @klass.attr 5
722
- @klass.reset
723
- expect(@klass.attr).to eql(4)
724
- end
725
-
726
- it "save should not save anything for it" do
727
- expect(@klass.save).to eql({})
728
- end
729
-
730
- it "save with include_defaults should save all defaults" do
731
- expect(@klass.save(true)).to eql({ attr: 4 })
732
- end
733
-
734
- it "saves the new value if it gets set" do
735
- @klass.attr 5
736
- expect((saved = @klass.save)).to eql({ attr: 10 })
737
- @klass.reset
738
- expect(@klass.attr).to eql(4)
739
- @klass.restore(saved)
740
- expect(@klass.attr).to eql(10)
741
- end
742
-
743
- it "saves the new value even if it is set to its default value" do
744
- @klass.attr 2
745
- expect((saved = @klass.save)).to eql({ attr: 4 })
746
- @klass.reset
747
- expect(@klass.save).to eql({})
748
- @klass.restore(saved)
749
- expect(@klass.save).to eql({ attr: 4 })
750
- end
751
- end
752
-
753
- describe "When a configurable exists with a context" do
754
- before :each do
755
- @klass = Class.new
756
- @klass.extend(::Mixlib::Config)
757
- @klass.class_eval do
758
- configurable :x
759
- config_context(:blah) do
760
- default :x, 5
761
- end
762
- end
763
- end
764
-
765
- it "configurable defaults in that context work" do
766
- expect(@klass.blah.x).to eql(5)
767
- end
768
-
769
- it "after setting values in the context, the values remain set" do
770
- @klass.blah.x = 10
771
- expect(@klass.blah.x).to eql(10)
772
- end
773
-
774
- it "setting values with the same name in the parent context do not affect the child context" do
775
- @klass.x = 10
776
- expect(@klass.x).to eql(10)
777
- expect(@klass.blah.x).to eql(5)
778
- end
779
-
780
- it "setting the entire context to a hash with default value overridden sets the value" do
781
- @klass.blah = { x: 10 }
782
- expect(@klass.blah.x).to eql(10)
783
- end
784
-
785
- it "setting the entire context to a hash sets non-default values" do
786
- @klass.blah = { y: 10 }
787
- expect(@klass.blah.x).to eql(5)
788
- expect(@klass.blah.y).to eql(10)
789
- end
790
-
791
- it "setting the entire context to a hash deletes any non-default values and resets default values" do
792
- @klass.blah.x = 10
793
- @klass.blah.y = 10
794
- @klass.blah = { z: 10 }
795
- expect(@klass.blah.x).to eql(5)
796
- expect(@klass.blah.y).to be_nil
797
- expect(@klass.blah.z).to eql(10)
798
- end
799
-
800
- it "setting the context values in a block overrides the default values" do
801
- @klass.blah do
802
- x 10
803
- y 20
804
- end
805
- expect(@klass.blah.x).to eq 10
806
- expect(@klass.blah.y).to eq 20
807
- end
808
-
809
- it "setting the context values in a yielded block overrides the default values" do
810
- @klass.blah do |b|
811
- b.x = 10
812
- b.y = 20
813
- end
814
- expect(@klass.blah.x).to eq 10
815
- expect(@klass.blah.y).to eq 20
816
- end
817
-
818
- it "after reset of the parent class, children are reset" do
819
- @klass.blah.x = 10
820
- expect(@klass.blah.x).to eql(10)
821
- @klass.reset
822
- expect(@klass.blah.x).to eql(5)
823
- end
824
-
825
- it "save should not save anything for it by default" do
826
- expect(@klass.save).to eql({})
827
- end
828
-
829
- it "save with include_defaults should save all defaults" do
830
- expect(@klass.save(true)).to eql({ blah: { x: 5 } })
831
- end
832
-
833
- it "saves any new values that are set in the context" do
834
- @klass.blah.x = 10
835
- expect((saved = @klass.save)).to eql({ blah: { x: 10 } })
836
- @klass.reset
837
- expect(@klass.blah.x).to eql(5)
838
- @klass.restore(saved)
839
- expect(@klass.blah.x).to eql(10)
840
- expect(@klass.save).to eql({ blah: { x: 10 } })
841
- end
842
-
843
- # this tests existing (somewhat bizzare) behavior of mixlib-config where testing to
844
- # see if a key exists is equivalent to testing if the key has been set -- we can still
845
- # retrieve the default value if it was set. the code in chef/chef which merges
846
- # knife config values into cli values will be sensitive to this behavior.
847
- it "defaults values do not show up when querying with #has_key?" do
848
- expect(@klass.blah.has_key?(:x)).to be false
849
- expect(@klass.blah.x).to be 5
850
- end
851
-
852
- it "if we assign the values, they show up when querying with #has_key?" do
853
- @klass.blah.x = 5
854
- expect(@klass.blah.has_key?(:x)).to be true
855
- end
856
- end
857
-
858
- describe "When a configurable exists with a nested context" do
859
- before :each do
860
- @klass = Class.new
861
- @klass.extend(::Mixlib::Config)
862
- @klass.class_eval do
863
- config_context(:blah) do
864
- config_context(:yarr) do
865
- default :x, 5
866
- default :y, 6
867
- end
868
- end
869
- configurable :x
870
- end
871
- end
872
-
873
- it "configurable defaults in that context work" do
874
- expect(@klass.blah.yarr.x).to eql(5)
875
- expect(@klass.blah.yarr.y).to eql(6)
876
- end
877
-
878
- it "after setting values in the context, the values remain set" do
879
- @klass.blah.yarr.x = 10
880
- @klass.blah.yarr.y = 11
881
- expect(@klass.blah.yarr.x).to eql(10)
882
- expect(@klass.blah.yarr.y).to eql(11)
883
- end
884
-
885
- it "setting values with the same name in the parent context do not affect the child context" do
886
- @klass.x = 10
887
- expect(@klass.x).to eql(10)
888
- expect(@klass.blah.yarr.x).to eql(5)
889
- end
890
-
891
- it "after reset of the parent class, children are reset" do
892
- @klass.blah.yarr.x = 10
893
- @klass.blah.yarr.y = 11
894
- expect(@klass.blah.yarr.x).to eql(10)
895
- expect(@klass.blah.yarr.y).to eql(11)
896
- @klass.reset
897
- expect(@klass.blah.yarr.x).to eql(5)
898
- expect(@klass.blah.yarr.y).to eql(6)
899
- end
900
-
901
- it "save should not save anything for it by default" do
902
- expect(@klass.save).to eql({})
903
- end
904
-
905
- it "save with include_defaults should save all defaults" do
906
- expect(@klass.save(true)).to eql({ blah: { yarr: { x: 5, y: 6 } } })
907
- end
908
-
909
- it "saves any new values that are set in the context" do
910
- @klass.blah.yarr.x = 10
911
- @klass.blah.yarr.y = 11
912
- expect((saved = @klass.save)).to eql({ blah: { yarr: { x: 10, y: 11 } } })
913
- @klass.reset
914
- expect(@klass.blah.yarr.x).to eql(5)
915
- expect(@klass.blah.yarr.y).to eql(6)
916
- @klass.restore(saved)
917
- expect(@klass.blah.yarr.x).to eql(10)
918
- expect(@klass.blah.yarr.y).to eql(11)
919
- expect(@klass.save).to eql({ blah: { yarr: { x: 10, y: 11 } } })
920
- end
921
-
922
- it "restores defaults not included in saved data" do
923
- @klass.restore( blah: { yarr: { x: 10 } } )
924
- expect(@klass.blah.yarr.x).to eql(10)
925
- expect(@klass.blah.yarr.y).to eql(6)
926
- end
927
-
928
- it "removes added properties not included in saved state" do
929
- @klass.blah.yarr.z = 12
930
- @klass.restore( blah: { yarr: { x: 10 } } )
931
- expect(@klass.blah.yarr.x).to eql(10)
932
- expect(@klass.blah.yarr.z).to eql(nil)
933
- end
934
-
935
- it "can set a config context from another context" do
936
- @klass.blah.blyme = { x: 7 }
937
- blyme = @klass.blah.blyme
938
- @klass.blah.yarr.x = 12
939
- @klass.blah.yarr = blyme
940
- expect(@klass.blah.yarr.x).to eql(7)
941
- end
942
- end
943
-
944
- describe "When a config_context with no defaulted values exists" do
945
- before :each do
946
- @klass = Class.new
947
- @klass.extend(::Mixlib::Config)
948
- @klass.class_eval do
949
- config_context(:blah) do
950
- configurable(:x)
951
- end
952
- end
953
- end
954
-
955
- it "has_key? finds the subcontext" do
956
- expect(@klass.has_key?(:blah)).to be true
957
- end
958
-
959
- it "key? finds the subcontext" do
960
- expect(@klass.key?(:blah)).to be true
961
- end
962
-
963
- it "save does not save the hash for the config_context" do
964
- expect(@klass.save).to eql({})
965
- end
966
-
967
- it "save with defaults saves the hash for the config_context" do
968
- expect(@klass.save(true)).to eql({ blah: {} })
969
- end
970
- end
971
-
972
- describe "When a config_context with no configurables exists" do
973
- before :each do
974
- @klass = Class.new
975
- @klass.extend(::Mixlib::Config)
976
- @klass.class_eval do
977
- config_context(:blah)
978
- end
979
- end
980
-
981
- it "save does not save the hash for the config_context" do
982
- expect(@klass.save).to eql({})
983
- end
984
-
985
- it "save with defaults saves the hash for the config_context" do
986
- expect(@klass.save(true)).to eql({ blah: {} })
987
- end
988
- end
989
-
990
- describe "When a nested context has strict mode on" do
991
- class StrictClass2
992
- extend ::Mixlib::Config
993
- config_context :c do
994
- config_strict_mode true
995
- default :x, 1
996
- end
997
- end
998
-
999
- it "The parent class allows you to set arbitrary config options" do
1000
- StrictClass2.y = 10
1001
- end
1002
-
1003
- it "The nested class does not allow you to set arbitrary config options" do
1004
- expect(lambda { StrictClass2.c.y = 10 }).to raise_error(Mixlib::Config::UnknownConfigOptionError, "Cannot set unsupported config value y.")
1005
- end
1006
- end
1007
-
1008
- describe "When strict mode is on but a nested context has strict mode unspecified" do
1009
- class StrictClass3
1010
- extend ::Mixlib::Config
1011
- config_strict_mode true
1012
- default :x, 1
1013
- config_context :c
1014
- end
1015
-
1016
- it "The parent class does not allow you to set arbitrary config options" do
1017
- expect(lambda { StrictClass3.y = 10 }).to raise_error(Mixlib::Config::UnknownConfigOptionError, "Cannot set unsupported config value y.")
1018
- end
1019
-
1020
- it "The nested class does not allow you to set arbitrary config options" do
1021
- expect(lambda { StrictClass3.y = 10 }).to raise_error(Mixlib::Config::UnknownConfigOptionError, "Cannot set unsupported config value y.")
1022
- end
1023
- end
1024
-
1025
- describe "When a config_context is opened twice" do
1026
- before :each do
1027
- @klass = Class.new
1028
- @klass.extend(::Mixlib::Config)
1029
- @klass.class_eval do
1030
- config_context(:blah) do
1031
- default :x, 10
1032
- end
1033
- config_context(:blah) do
1034
- default :y, 20
1035
- end
1036
- end
1037
- end
1038
-
1039
- it "Both config_context blocks are honored" do
1040
- @klass.blah.x == 10
1041
- @klass.blah.y == 20
1042
- end
1043
- end
1044
-
1045
- it "When a config_context is opened in place of a regular configurable, an error is raised" do
1046
- klass = Class.new
1047
- klass.extend(::Mixlib::Config)
1048
- expect(lambda do
1049
- klass.class_eval do
1050
- default :blah, 10
1051
- config_context(:blah) do
1052
- default :y, 20
1053
- end
1054
- end
1055
- end).to raise_error(Mixlib::Config::ReopenedConfigurableWithConfigContextError)
1056
- end
1057
-
1058
- it "When a config_context is opened in place of a regular configurable, an error is raised" do
1059
- klass = Class.new
1060
- klass.extend(::Mixlib::Config)
1061
- expect(lambda do
1062
- klass.class_eval do
1063
- config_context(:blah) do
1064
- default :y, 20
1065
- end
1066
- default :blah, 10
1067
- end
1068
- end).to raise_error(Mixlib::Config::ReopenedConfigContextWithConfigurableError)
1069
- end
1070
-
1071
- describe "config context lists" do
1072
- let(:klass) do
1073
- klass = Class.new
1074
- klass.extend ::Mixlib::Config
1075
- klass.instance_eval do
1076
- config_context_list(:tests, :test) do
1077
- default :y, 20
1078
- end
1079
- end
1080
- klass
1081
- end
1082
- it "defines list methods when declaring a config_context_list" do
1083
- expect(klass.methods).to include :test
1084
- expect(klass.methods).to include :tests
1085
- end
1086
-
1087
- it "creates a new item each time the singular list is called" do
1088
- klass.test do
1089
- y 40
1090
- end
1091
- klass.test do
1092
- y 50
1093
- end
1094
- expect(klass.tests.length).to be 2
1095
- expect(klass.tests.first.y).to be 40
1096
- expect(klass.tests.last.y).to be 50
1097
- end
1098
-
1099
- it "can save the config list" do
1100
- klass.test do
1101
- y 40
1102
- end
1103
- klass.test do
1104
- y 50
1105
- end
1106
- expect(klass.save).to eq({
1107
- tests: [
1108
- { y: 40 },
1109
- { y: 50 },
1110
- ],
1111
- })
1112
- end
1113
-
1114
- it "can restore the config list from a hash" do
1115
- hash = {
1116
- tests: [
1117
- { y: 40 },
1118
- { y: 50 },
1119
- ],
1120
- }
1121
- klass.restore(hash)
1122
- expect(klass.tests.length).to be 2
1123
- expect(klass.tests.first.y).to be 40
1124
- expect(klass.tests.last.y).to be 50
1125
- end
1126
- end
1127
-
1128
- describe "config context hashes" do
1129
- let(:klass) do
1130
- klass = Class.new
1131
- klass.extend ::Mixlib::Config
1132
- klass.instance_eval do
1133
- config_context_hash(:tests, :test) do
1134
- default :y, 20
1135
- end
1136
- end
1137
- klass
1138
- end
1139
-
1140
- it "defines list methods when declaring a config_context_hash" do
1141
- expect(klass.methods).to include :test
1142
- expect(klass.methods).to include :tests
1143
- end
1144
-
1145
- context "when called with a new key each time" do
1146
- it "creates a new item each time" do
1147
- klass.test :one do
1148
- y 40
1149
- end
1150
- klass.test :two do
1151
- y 50
1152
- end
1153
- expect(klass.tests.length).to be 2
1154
- expect(klass.tests[:one].y).to be 40
1155
- expect(klass.tests[:two].y).to be 50
1156
- end
1157
- end
1158
- context "when called with the same key" do
1159
- it "modifies the existing value" do
1160
- klass.test :only do
1161
- y 40
1162
- end
1163
- klass.test :only do
1164
- y 50
1165
- end
1166
- expect(klass.tests.length).to be 1
1167
- expect(klass.tests[:only].y).to be 50
1168
- end
1169
- end
1170
-
1171
- it "can save the config hash" do
1172
- klass.test :one do
1173
- y 40
1174
- end
1175
- klass.test :two do
1176
- y 50
1177
- end
1178
- expect(klass.save).to eq({
1179
- tests: {
1180
- one: { y: 40 },
1181
- two: { y: 50 },
1182
- },
1183
- })
1184
- end
1185
-
1186
- it "can restore the config hash from a hash" do
1187
- hash = {
1188
- tests: {
1189
- one: { y: 40 },
1190
- two: { y: 50 },
1191
- },
1192
- }
1193
- klass.restore(hash)
1194
- expect(klass.tests.length).to be 2
1195
- expect(klass.tests[:one].y).to be 40
1196
- expect(klass.tests[:two].y).to be 50
1197
- end
1198
- end
1199
-
1200
- describe ".from_yaml" do
1201
- let(:yaml) do
1202
- <<-EOH
1203
- ---
1204
- foo:
1205
- - bar
1206
- - baz
1207
- - matazz
1208
- alpha: beta
1209
- EOH
1210
- end
1211
-
1212
- it "turns YAML into method-style setting" do
1213
- allow(File).to receive(:exists?).and_return(true)
1214
- allow(File).to receive(:readable?).and_return(true)
1215
- allow(IO).to receive(:read).with("config.yml").and_return(yaml)
1216
-
1217
- expect(lambda do
1218
- ConfigIt.from_file("config.yml")
1219
- end).to_not raise_error
1220
-
1221
- expect(ConfigIt.foo).to eql(%w{ bar baz matazz })
1222
- expect(ConfigIt.alpha).to eql("beta")
1223
- end
1224
- end
1225
-
1226
- describe ".from_json" do
1227
- let(:json) do
1228
- <<-EOH
1229
- {
1230
- "foo": [
1231
- "bar",
1232
- "baz",
1233
- "matazz"
1234
- ],
1235
- "alpha": "beta"
1236
- }
1237
- EOH
1238
- end
1239
-
1240
- it "turns JSON into method-style setting" do
1241
- allow(File).to receive(:exists?).and_return(true)
1242
- allow(File).to receive(:readable?).and_return(true)
1243
- allow(IO).to receive(:read).with("config.json").and_return(json)
1244
-
1245
- expect(lambda do
1246
- ConfigIt.from_file("config.json")
1247
- end).to_not raise_error
1248
-
1249
- expect(ConfigIt.foo).to eql(%w{ bar baz matazz })
1250
- expect(ConfigIt.alpha).to eql("beta")
1251
- end
1252
- end
1253
-
1254
- describe ".from_toml" do
1255
- let(:toml) do
1256
- <<-EOH
1257
- foo = ["bar", "baz", "matazz"]
1258
- alpha = "beta"
1259
- EOH
1260
- end
1261
-
1262
- it "turns TOML into method-style setting" do
1263
- allow(File).to receive(:exists?).and_return(true)
1264
- allow(File).to receive(:readable?).and_return(true)
1265
- allow(IO).to receive(:read).with("config.toml").and_return(toml)
1266
-
1267
- expect(lambda do
1268
- ConfigIt.from_file("config.toml")
1269
- end).to_not raise_error
1270
-
1271
- expect(ConfigIt.foo).to eql(%w{ bar baz matazz })
1272
- expect(ConfigIt.alpha).to eql("beta")
1273
- end
1274
- end
1275
-
1276
- describe ".from_hash" do
1277
- let(:hash) do
1278
- {
1279
- "alpha" => "beta",
1280
- gamma: "delta",
1281
- "foo" => %w{ bar baz matazz},
1282
- "bar" => { "baz" => { "fizz" => "buzz" } },
1283
- "windows_path" => 'C:\Windows Has Awful\Paths',
1284
- }
1285
- end
1286
-
1287
- it "configures the config object from a hash" do
1288
- ConfigIt.config_context :bar do
1289
- config_context :baz do
1290
- default :fizz, "quux"
1291
- end
1292
- end
1293
- ConfigIt.from_hash(hash)
1294
- expect(ConfigIt.foo).to eql(%w{ bar baz matazz })
1295
- expect(ConfigIt.alpha).to eql("beta")
1296
- expect(ConfigIt.gamma).to eql("delta")
1297
- expect(ConfigIt[:bar][:baz][:fizz]).to eql("buzz")
1298
- expect(ConfigIt.windows_path).to eql('C:\Windows Has Awful\Paths')
1299
- end
1300
- end
1301
- end