]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/inifile/spec/unit/puppet/provider/ini_setting/ruby_spec.rb
add puppetlabs/inifile to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / inifile / spec / unit / puppet / provider / ini_setting / ruby_spec.rb
1 require 'spec_helper'
2 require 'puppet'
3
4 provider_class = Puppet::Type.type(:ini_setting).provider(:ruby)
5 describe provider_class do
6   include PuppetlabsSpec::Files
7
8   let(:tmpfile) { tmpfilename("ini_setting_test") }
9   let(:emptyfile) { tmpfilename("ini_setting_test_empty") }
10
11   let(:common_params) { {
12       :title    => 'ini_setting_ensure_present_test',
13       :path     => tmpfile,
14       :section  => 'section2',
15   } }
16
17   def validate_file(expected_content,tmpfile = tmpfile)
18     File.read(tmpfile).should == expected_content
19   end
20
21
22   before :each do
23     File.open(tmpfile, 'w') do |fh|
24       fh.write(orig_content)
25     end
26     File.open(emptyfile, 'w') do |fh|
27       fh.write("")
28     end
29   end
30
31   context 'when calling instances' do
32
33     let :orig_content do
34       ''
35     end
36
37     it 'should fail when file path is not set' do
38       expect {
39         provider_class.instances
40       }.to raise_error(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded')
41     end
42
43     context 'when file path is set by a child class' do
44       it 'should return [] when file is empty' do
45         child_one = Class.new(provider_class) do
46           def self.file_path
47             emptyfile
48           end
49         end
50         child_one.stubs(:file_path).returns(emptyfile)
51         child_one.instances.should == []
52       end
53       it 'should override the provider instances file_path' do
54         child_two = Class.new(provider_class) do
55           def self.file_path
56             '/some/file/path'
57           end
58         end
59         resource = Puppet::Type::Ini_setting.new(common_params)
60         provider = child_two.new(resource)
61         provider.file_path.should == '/some/file/path'
62       end
63       context 'when file has contecnts' do
64         let(:orig_content) {
65           <<-EOS
66 # This is a comment
67 [section1]
68 ; This is also a comment
69 foo=foovalue
70
71 bar = barvalue
72 master = true
73 [section2]
74
75 foo= foovalue2
76 baz=bazvalue
77 url = http://192.168.1.1:8080
78 [section:sub]
79 subby=bar
80     #another comment
81  ; yet another comment
82           EOS
83         }
84
85         it 'should be able to parse the results' do
86           child_three = Class.new(provider_class) do
87             def self.file_path
88               '/some/file/path'
89             end
90           end
91           child_three.stubs(:file_path).returns(tmpfile)
92           child_three.instances.size == 7
93           expected_array = [
94             {:name => 'section1/foo', :value => 'foovalue' },
95             {:name => 'section1/bar', :value => 'barvalue' },
96             {:name => 'section1/master', :value => 'true' },
97             {:name => 'section2/foo', :value => 'foovalue2' },
98             {:name => 'section2/baz', :value => 'bazvalue' },
99             {:name => 'section2/url', :value => 'http://192.168.1.1:8080' },
100             {:name => 'section:sub/subby', :value => 'bar' }
101           ]
102           real_array = []
103           ensure_array = []
104           child_three.instances.each do |x|
105             prop_hash    = x.instance_variable_get(:@property_hash)
106             ensure_value = prop_hash.delete(:ensure)
107             ensure_array.push(ensure_value)
108             real_array.push(prop_hash)
109           end
110           ensure_array.uniq.should == [:present]
111           ((real_array - expected_array) && (expected_array - real_array)).should == []
112
113         end
114
115       end
116
117     end
118
119   end
120
121   context "when ensuring that a setting is present" do
122     let(:orig_content) {
123       <<-EOS
124 # This is a comment
125 [section1]
126 ; This is also a comment
127 foo=foovalue
128
129 bar = barvalue
130 master = true
131 [section2]
132
133 foo= foovalue2
134 baz=bazvalue
135 url = http://192.168.1.1:8080
136 [section:sub]
137 subby=bar
138     #another comment
139  ; yet another comment
140
141 -nonstandard-
142   shoes = purple
143       EOS
144     }
145
146     it "should add a missing setting to the correct section" do
147       resource = Puppet::Type::Ini_setting.new(common_params.merge(
148           :setting => 'yahoo', :value => 'yippee'))
149       provider = described_class.new(resource)
150       provider.exists?.should be false
151       provider.create
152       validate_file(<<-EOS
153 # This is a comment
154 [section1]
155 ; This is also a comment
156 foo=foovalue
157
158 bar = barvalue
159 master = true
160 [section2]
161
162 foo= foovalue2
163 baz=bazvalue
164 url = http://192.168.1.1:8080
165 yahoo = yippee
166 [section:sub]
167 subby=bar
168     #another comment
169  ; yet another comment
170
171 -nonstandard-
172   shoes = purple
173       EOS
174 )
175     end
176
177     it "should add a missing setting to the correct section with pre/suffix" do
178       resource = Puppet::Type::Ini_setting.new(common_params.merge(
179           :section => 'nonstandard',
180           :setting => 'yahoo', :value => 'yippee',
181           :section_prefix => '-', :section_suffix => '-'))
182       provider = described_class.new(resource)
183       provider.exists?.should be false
184       provider.create
185       validate_file(<<-EOS
186 # This is a comment
187 [section1]
188 ; This is also a comment
189 foo=foovalue
190
191 bar = barvalue
192 master = true
193 [section2]
194
195 foo= foovalue2
196 baz=bazvalue
197 url = http://192.168.1.1:8080
198 [section:sub]
199 subby=bar
200     #another comment
201  ; yet another comment
202
203 -nonstandard-
204   shoes = purple
205   yahoo = yippee
206       EOS
207 )
208     end
209
210     it "should add a missing setting to the correct section with colon" do
211       resource = Puppet::Type::Ini_setting.new(common_params.merge(
212           :section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
213       provider = described_class.new(resource)
214       provider.exists?.should be false
215       provider.create
216       validate_file(<<-EOS
217 # This is a comment
218 [section1]
219 ; This is also a comment
220 foo=foovalue
221
222 bar = barvalue
223 master = true
224 [section2]
225
226 foo= foovalue2
227 baz=bazvalue
228 url = http://192.168.1.1:8080
229 [section:sub]
230 subby=bar
231     #another comment
232  ; yet another comment
233
234 -nonstandard-
235   shoes = purple
236 yahoo = yippee
237       EOS
238 )
239     end
240
241     it "should modify an existing setting with a different value" do
242       resource = Puppet::Type::Ini_setting.new(common_params.merge(
243            :setting => 'baz', :value => 'bazvalue2'))
244       provider = described_class.new(resource)
245       provider.exists?.should be true
246       provider.value=('bazvalue2')
247       validate_file(<<-EOS
248 # This is a comment
249 [section1]
250 ; This is also a comment
251 foo=foovalue
252
253 bar = barvalue
254 master = true
255 [section2]
256
257 foo= foovalue2
258 baz=bazvalue2
259 url = http://192.168.1.1:8080
260 [section:sub]
261 subby=bar
262     #another comment
263  ; yet another comment
264
265 -nonstandard-
266   shoes = purple
267       EOS
268       )
269     end
270
271     it "should modify an existing setting with pre/suffix with a different value" do
272       resource = Puppet::Type::Ini_setting.new(common_params.merge(
273            :section => 'nonstandard',
274            :setting => 'shoes', :value => 'orange',
275            :section_prefix => '-', :section_suffix => '-' ))
276       provider = described_class.new(resource)
277       provider.exists?.should be true
278       provider.value=('orange')
279       validate_file(<<-EOS
280 # This is a comment
281 [section1]
282 ; This is also a comment
283 foo=foovalue
284
285 bar = barvalue
286 master = true
287 [section2]
288
289 foo= foovalue2
290 baz=bazvalue
291 url = http://192.168.1.1:8080
292 [section:sub]
293 subby=bar
294     #another comment
295  ; yet another comment
296
297 -nonstandard-
298   shoes = orange
299       EOS
300       )
301     end
302
303     it "should modify an existing setting with a different value - with colon in section" do
304       resource = Puppet::Type::Ini_setting.new(common_params.merge(
305            :section => 'section:sub', :setting => 'subby', :value => 'foo'))
306       provider = described_class.new(resource)
307       provider.exists?.should be true
308       provider.value.should == 'bar'
309       provider.value=('foo')
310       validate_file(<<-EOS
311 # This is a comment
312 [section1]
313 ; This is also a comment
314 foo=foovalue
315
316 bar = barvalue
317 master = true
318 [section2]
319
320 foo= foovalue2
321 baz=bazvalue
322 url = http://192.168.1.1:8080
323 [section:sub]
324 subby=foo
325     #another comment
326  ; yet another comment
327
328 -nonstandard-
329   shoes = purple
330       EOS
331       )
332     end
333
334     it "should be able to handle settings with non alphanumbering settings " do
335       resource = Puppet::Type::Ini_setting.new(common_params.merge(
336            :setting => 'url', :value => 'http://192.168.0.1:8080'))
337       provider = described_class.new(resource)
338       provider.exists?.should be true
339       provider.value.should == 'http://192.168.1.1:8080'
340       provider.value=('http://192.168.0.1:8080')
341
342       validate_file( <<-EOS
343 # This is a comment
344 [section1]
345 ; This is also a comment
346 foo=foovalue
347
348 bar = barvalue
349 master = true
350 [section2]
351
352 foo= foovalue2
353 baz=bazvalue
354 url = http://192.168.0.1:8080
355 [section:sub]
356 subby=bar
357     #another comment
358  ; yet another comment
359
360 -nonstandard-
361   shoes = purple
362     EOS
363       )
364     end
365
366     it "should be able to handle settings with pre/suffix with non alphanumbering settings " do
367       resource = Puppet::Type::Ini_setting.new(common_params.merge(
368            :section => 'nonstandard',
369            :setting => 'shoes', :value => 'http://192.168.0.1:8080',
370            :section_prefix => '-', :section_suffix => '-' ))
371       provider = described_class.new(resource)
372       provider.exists?.should be true
373       provider.value.should == 'purple'
374       provider.value=('http://192.168.0.1:8080')
375
376       validate_file( <<-EOS
377 # This is a comment
378 [section1]
379 ; This is also a comment
380 foo=foovalue
381
382 bar = barvalue
383 master = true
384 [section2]
385
386 foo= foovalue2
387 baz=bazvalue
388 url = http://192.168.1.1:8080
389 [section:sub]
390 subby=bar
391     #another comment
392  ; yet another comment
393
394 -nonstandard-
395   shoes = http://192.168.0.1:8080
396     EOS
397       )
398     end
399
400     it "should recognize an existing setting with the specified value" do
401       resource = Puppet::Type::Ini_setting.new(common_params.merge(
402            :setting => 'baz', :value => 'bazvalue'))
403       provider = described_class.new(resource)
404       provider.exists?.should be true
405     end
406
407     it "should recognize an existing setting with pre/suffix with the specified value" do
408       resource = Puppet::Type::Ini_setting.new(common_params.merge(
409            :section => 'nonstandard',
410            :setting => 'shoes', :value => 'purple',
411            :section_prefix => '-', :section_suffix => '-' ))
412       provider = described_class.new(resource)
413       provider.exists?.should be true
414     end
415
416     it "should add a new section if the section does not exist" do
417       resource = Puppet::Type::Ini_setting.new(common_params.merge(
418           :section => "section3", :setting => 'huzzah', :value => 'shazaam'))
419       provider = described_class.new(resource)
420       provider.exists?.should be false
421       provider.create
422       validate_file(<<-EOS
423 # This is a comment
424 [section1]
425 ; This is also a comment
426 foo=foovalue
427
428 bar = barvalue
429 master = true
430 [section2]
431
432 foo= foovalue2
433 baz=bazvalue
434 url = http://192.168.1.1:8080
435 [section:sub]
436 subby=bar
437     #another comment
438  ; yet another comment
439
440 -nonstandard-
441   shoes = purple
442
443 [section3]
444 huzzah = shazaam
445       EOS
446       )
447     end
448
449     it "should add a new section with pre/suffix if the section does not exist" do
450       resource = Puppet::Type::Ini_setting.new(common_params.merge(
451           :section => "section3", :setting => 'huzzah', :value => 'shazaam',
452           :section_prefix => '-', :section_suffix => '-' ))
453       provider = described_class.new(resource)
454       provider.exists?.should be false
455       provider.create
456       validate_file(<<-EOS
457 # This is a comment
458 [section1]
459 ; This is also a comment
460 foo=foovalue
461
462 bar = barvalue
463 master = true
464 [section2]
465
466 foo= foovalue2
467 baz=bazvalue
468 url = http://192.168.1.1:8080
469 [section:sub]
470 subby=bar
471     #another comment
472  ; yet another comment
473
474 -nonstandard-
475   shoes = purple
476
477 -section3-
478 huzzah = shazaam
479       EOS
480       )
481     end
482
483     it "should add a new section if the section does not exist - with colon" do
484       resource = Puppet::Type::Ini_setting.new(common_params.merge(
485           :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam'))
486       provider = described_class.new(resource)
487       provider.exists?.should be false
488       provider.create
489       validate_file(<<-EOS
490 # This is a comment
491 [section1]
492 ; This is also a comment
493 foo=foovalue
494
495 bar = barvalue
496 master = true
497 [section2]
498
499 foo= foovalue2
500 baz=bazvalue
501 url = http://192.168.1.1:8080
502 [section:sub]
503 subby=bar
504     #another comment
505  ; yet another comment
506
507 -nonstandard-
508   shoes = purple
509
510 [section:subsection]
511 huzzah = shazaam
512       EOS
513       )
514     end
515
516     it "should add a new section with pre/suffix if the section does not exist - with colon" do
517       resource = Puppet::Type::Ini_setting.new(common_params.merge(
518           :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam',
519           :section_prefix => '-', :section_suffix => '-' ))
520       provider = described_class.new(resource)
521       provider.exists?.should be false
522       provider.create
523       validate_file(<<-EOS
524 # This is a comment
525 [section1]
526 ; This is also a comment
527 foo=foovalue
528
529 bar = barvalue
530 master = true
531 [section2]
532
533 foo= foovalue2
534 baz=bazvalue
535 url = http://192.168.1.1:8080
536 [section:sub]
537 subby=bar
538     #another comment
539  ; yet another comment
540
541 -nonstandard-
542   shoes = purple
543
544 -section:subsection-
545 huzzah = shazaam
546       EOS
547       )
548     end
549
550     it "should add a new section if no sections exists" do
551       resource = Puppet::Type::Ini_setting.new(common_params.merge(
552           :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
553       provider = described_class.new(resource)
554       provider.exists?.should be false
555       provider.create
556       validate_file("
557 [section1]
558 setting1 = hellowworld
559 ", emptyfile)
560     end
561
562     it "should add a new section with pre/suffix if no sections exists" do
563       resource = Puppet::Type::Ini_setting.new(common_params.merge(
564           :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile,
565           :section_prefix => '-', :section_suffix => '-' ))
566       provider = described_class.new(resource)
567       provider.exists?.should be false
568       provider.create
569       validate_file("
570 -section1-
571 setting1 = hellowworld
572 ", emptyfile)
573     end
574
575     it "should add a new section with colon if no sections exists" do
576       resource = Puppet::Type::Ini_setting.new(common_params.merge(
577           :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
578       provider = described_class.new(resource)
579       provider.exists?.should be false
580       provider.create
581       validate_file("
582 [section:subsection]
583 setting1 = hellowworld
584 ", emptyfile)
585     end
586
587     it "should add a new section with pre/suffix with colon if no sections exists" do
588       resource = Puppet::Type::Ini_setting.new(common_params.merge(
589           :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile,
590           :section_prefix => '-', :section_suffix => '-' ))
591       provider = described_class.new(resource)
592       provider.exists?.should be false
593       provider.create
594       validate_file("
595 -section:subsection-
596 setting1 = hellowworld
597 ", emptyfile)
598     end
599     it "should be able to handle variables of any type" do
600       resource = Puppet::Type::Ini_setting.new(common_params.merge(
601           :section => "section1", :setting => 'master', :value => true))
602       provider = described_class.new(resource)
603       provider.exists?.should be true
604       provider.value.should == 'true'
605     end
606
607   end
608
609   context "when dealing with a global section" do
610     let(:orig_content) {
611       <<-EOS
612 # This is a comment
613 foo=blah
614 [section2]
615 foo = http://192.168.1.1:8080
616  ; yet another comment
617       EOS
618     }
619
620
621     it "should add a missing setting if it doesn't exist" do
622       resource = Puppet::Type::Ini_setting.new(common_params.merge(
623           :section => '', :setting => 'bar', :value => 'yippee'))
624       provider = described_class.new(resource)
625       provider.exists?.should be false
626       provider.create
627       validate_file(<<-EOS
628 # This is a comment
629 foo=blah
630 bar = yippee
631 [section2]
632 foo = http://192.168.1.1:8080
633  ; yet another comment
634       EOS
635       )
636     end
637
638     it "should modify an existing setting with a different value" do
639       resource = Puppet::Type::Ini_setting.new(common_params.merge(
640            :section => '', :setting => 'foo', :value => 'yippee'))
641       provider = described_class.new(resource)
642       provider.exists?.should be true
643       provider.value.should == 'blah'
644       provider.value=('yippee')
645       validate_file(<<-EOS
646 # This is a comment
647 foo=yippee
648 [section2]
649 foo = http://192.168.1.1:8080
650  ; yet another comment
651       EOS
652       )
653     end
654
655     it "should recognize an existing setting with the specified value" do
656       resource = Puppet::Type::Ini_setting.new(common_params.merge(
657            :section => '', :setting => 'foo', :value => 'blah'))
658       provider = described_class.new(resource)
659       provider.exists?.should be true
660     end
661   end
662
663   context "when the first line of the file is a section" do
664     let(:orig_content) {
665       <<-EOS
666 [section2]
667 foo = http://192.168.1.1:8080
668       EOS
669     }
670
671     it "should be able to add a global setting" do
672       resource = Puppet::Type::Ini_setting.new(common_params.merge(
673            :section => '', :setting => 'foo', :value => 'yippee'))
674       provider = described_class.new(resource)
675       provider.exists?.should be false
676       provider.create
677       validate_file(<<-EOS
678 foo = yippee
679
680 [section2]
681 foo = http://192.168.1.1:8080
682       EOS
683       )
684     end
685
686     it "should modify an existing setting" do
687       resource = Puppet::Type::Ini_setting.new(common_params.merge(
688           :section => 'section2', :setting => 'foo', :value => 'yippee'))
689       provider = described_class.new(resource)
690       provider.exists?.should be true
691       provider.value.should == 'http://192.168.1.1:8080'
692       provider.value=('yippee')
693       validate_file(<<-EOS
694 [section2]
695 foo = yippee
696       EOS
697       )
698     end
699
700     it "should add a new setting" do
701       resource = Puppet::Type::Ini_setting.new(common_params.merge(
702           :section => 'section2', :setting => 'bar', :value => 'baz'))
703       provider = described_class.new(resource)
704       provider.exists?.should be false
705       provider.create
706       validate_file(<<-EOS
707 [section2]
708 foo = http://192.168.1.1:8080
709 bar = baz
710       EOS
711       )
712     end
713   end
714
715   context "when overriding the separator" do
716     let(:orig_content) {
717       <<-EOS
718 [section2]
719 foo=bar
720       EOS
721     }
722
723     it "should modify an existing setting" do
724       resource = Puppet::Type::Ini_setting.new(common_params.merge(
725                                                    :section           => 'section2',
726                                                    :setting           => 'foo',
727                                                    :value             => 'yippee',
728                                                    :key_val_separator => '='))
729       provider = described_class.new(resource)
730       provider.exists?.should be true
731       provider.value.should == 'bar'
732       provider.value=('yippee')
733       validate_file(<<-EOS
734 [section2]
735 foo=yippee
736       EOS
737       )
738     end
739
740   end
741
742   context "when overriding the separator to something other than =" do
743     let(:orig_content) {
744       <<-EOS
745 [section2]
746 foo: bar
747       EOS
748     }
749
750     it "should modify an existing setting" do
751       resource = Puppet::Type::Ini_setting.new(common_params.merge(
752                                                    :section           => 'section2',
753                                                    :setting           => 'foo',
754                                                    :value             => 'yippee',
755                                                    :key_val_separator => ': '))
756       provider = described_class.new(resource)
757       provider.exists?.should be true
758       provider.value.should == 'bar'
759       provider.value=('yippee')
760       validate_file(<<-EOS
761 [section2]
762 foo: yippee
763       EOS
764       )
765     end
766
767     it "should add a new setting" do
768       resource = Puppet::Type::Ini_setting.new(common_params.merge(
769                                                    :section           => 'section2',
770                                                    :setting           => 'bar',
771                                                    :value             => 'baz',
772                                                    :key_val_separator => ': '))
773       provider = described_class.new(resource)
774       provider.exists?.should be false
775       provider.create
776       validate_file(<<-EOS
777 [section2]
778 foo: bar
779 bar: baz
780       EOS
781       )
782     end
783
784   end
785
786   context "when ensuring that a setting is absent" do
787     let(:orig_content) {
788       <<-EOS
789 [section1]
790 ; This is also a comment
791 foo=foovalue
792
793 bar = barvalue
794 master = true
795 [section2]
796
797 foo= foovalue2
798 baz=bazvalue
799 url = http://192.168.1.1:8080
800 [section:sub]
801 subby=bar
802     #another comment
803  ; yet another comment
804
805  -nonstandard-
806    shoes = purple
807 EOS
808     }
809
810     it "should remove a setting that exists" do
811       resource = Puppet::Type::Ini_setting.new(common_params.merge(
812       :section => 'section1', :setting => 'foo', :ensure => 'absent'))
813       provider = described_class.new(resource)
814       provider.exists?.should be true
815       provider.destroy
816       validate_file(<<-EOS
817 [section1]
818 ; This is also a comment
819
820 bar = barvalue
821 master = true
822 [section2]
823
824 foo= foovalue2
825 baz=bazvalue
826 url = http://192.168.1.1:8080
827 [section:sub]
828 subby=bar
829     #another comment
830  ; yet another comment
831
832  -nonstandard-
833    shoes = purple
834 EOS
835     )
836     end
837
838     it "should remove a setting with pre/suffix that exists" do
839       resource = Puppet::Type::Ini_setting.new(common_params.merge(
840       :section => 'nonstandard', :setting => 'shoes', :ensure => 'absent',
841       :section_prefix => '-', :section_suffix => '-' ))
842       provider = described_class.new(resource)
843       provider.exists?.should be true
844       provider.destroy
845       validate_file(<<-EOS
846 [section1]
847 ; This is also a comment
848 foo=foovalue
849
850 bar = barvalue
851 master = true
852 [section2]
853
854 foo= foovalue2
855 baz=bazvalue
856 url = http://192.168.1.1:8080
857 [section:sub]
858 subby=bar
859     #another comment
860  ; yet another comment
861
862  -nonstandard-
863 EOS
864     )
865     end
866
867     it "should do nothing for a setting that does not exist" do
868       resource = Puppet::Type::Ini_setting.new(common_params.merge(
869                                                    :section => 'section:sub', :setting => 'foo', :ensure => 'absent'))
870       provider = described_class.new(resource)
871       provider.exists?.should be false
872       provider.destroy
873       validate_file(<<-EOS
874 [section1]
875 ; This is also a comment
876 foo=foovalue
877
878 bar = barvalue
879 master = true
880 [section2]
881
882 foo= foovalue2
883 baz=bazvalue
884 url = http://192.168.1.1:8080
885 [section:sub]
886 subby=bar
887     #another comment
888  ; yet another comment
889
890  -nonstandard-
891    shoes = purple
892       EOS
893       )
894     end
895
896     it "should do nothing for a setting with pre/suffix that does not exist" do
897       resource = Puppet::Type::Ini_setting.new(common_params.merge(
898          :section => 'nonstandard', :setting => 'foo', :ensure => 'absent',
899          :section_prefix => '-', :section_suffix => '-' ))
900       provider = described_class.new(resource)
901       provider.exists?.should be false
902       provider.destroy
903       validate_file(<<-EOS
904 [section1]
905 ; This is also a comment
906 foo=foovalue
907
908 bar = barvalue
909 master = true
910 [section2]
911
912 foo= foovalue2
913 baz=bazvalue
914 url = http://192.168.1.1:8080
915 [section:sub]
916 subby=bar
917     #another comment
918  ; yet another comment
919
920  -nonstandard-
921    shoes = purple
922       EOS
923       )
924     end
925   end
926
927   context "when dealing with indentation in sections" do
928     let(:orig_content) {
929       <<-EOS
930 # This is a comment
931      [section1]
932      ; This is also a comment
933      foo=foovalue
934
935      bar = barvalue
936      master = true
937
938 [section2]
939   foo= foovalue2
940   baz=bazvalue
941   url = http://192.168.1.1:8080
942 [section:sub]
943  subby=bar
944     #another comment
945   fleezy = flam
946  ; yet another comment
947       EOS
948     }
949
950     it "should add a missing setting at the correct indentation when the header is aligned" do
951       resource = Puppet::Type::Ini_setting.new(common_params.merge(
952                     :section => 'section1', :setting => 'yahoo', :value => 'yippee'))
953       provider = described_class.new(resource)
954       provider.exists?.should be false
955       provider.create
956       validate_file(<<-EOS
957 # This is a comment
958      [section1]
959      ; This is also a comment
960      foo=foovalue
961
962      bar = barvalue
963      master = true
964      yahoo = yippee
965
966 [section2]
967   foo= foovalue2
968   baz=bazvalue
969   url = http://192.168.1.1:8080
970 [section:sub]
971  subby=bar
972     #another comment
973   fleezy = flam
974  ; yet another comment
975       EOS
976       )
977     end
978
979     it "should update an existing setting at the correct indentation when the header is aligned" do
980       resource = Puppet::Type::Ini_setting.new(
981           common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2'))
982       provider = described_class.new(resource)
983       provider.exists?.should be true
984       provider.create
985       validate_file(<<-EOS
986 # This is a comment
987      [section1]
988      ; This is also a comment
989      foo=foovalue
990
991      bar = barvalue2
992      master = true
993
994 [section2]
995   foo= foovalue2
996   baz=bazvalue
997   url = http://192.168.1.1:8080
998 [section:sub]
999  subby=bar
1000     #another comment
1001   fleezy = flam
1002  ; yet another comment
1003       EOS
1004       )
1005     end
1006
1007     it "should add a missing setting at the correct indentation when the header is not aligned" do
1008       resource = Puppet::Type::Ini_setting.new(common_params.merge(
1009                                                    :section => 'section2', :setting => 'yahoo', :value => 'yippee'))
1010       provider = described_class.new(resource)
1011       provider.exists?.should be false
1012       provider.create
1013       validate_file(<<-EOS
1014 # This is a comment
1015      [section1]
1016      ; This is also a comment
1017      foo=foovalue
1018
1019      bar = barvalue
1020      master = true
1021
1022 [section2]
1023   foo= foovalue2
1024   baz=bazvalue
1025   url = http://192.168.1.1:8080
1026   yahoo = yippee
1027 [section:sub]
1028  subby=bar
1029     #another comment
1030   fleezy = flam
1031  ; yet another comment
1032       EOS
1033       )
1034     end
1035
1036     it "should update an existing setting at the correct indentation when the header is not aligned" do
1037       resource = Puppet::Type::Ini_setting.new(
1038           common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue2'))
1039       provider = described_class.new(resource)
1040       provider.exists?.should be true
1041       provider.create
1042       validate_file(<<-EOS
1043 # This is a comment
1044      [section1]
1045      ; This is also a comment
1046      foo=foovalue
1047
1048      bar = barvalue
1049      master = true
1050
1051 [section2]
1052   foo= foovalue2
1053   baz=bazvalue2
1054   url = http://192.168.1.1:8080
1055 [section:sub]
1056  subby=bar
1057     #another comment
1058   fleezy = flam
1059  ; yet another comment
1060       EOS
1061       )
1062     end
1063
1064     it "should add a missing setting at the min indentation when the section is not aligned" do
1065       resource = Puppet::Type::Ini_setting.new(
1066           common_params.merge(:section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
1067       provider = described_class.new(resource)
1068       provider.exists?.should be false
1069       provider.create
1070       validate_file(<<-EOS
1071 # This is a comment
1072      [section1]
1073      ; This is also a comment
1074      foo=foovalue
1075
1076      bar = barvalue
1077      master = true
1078
1079 [section2]
1080   foo= foovalue2
1081   baz=bazvalue
1082   url = http://192.168.1.1:8080
1083 [section:sub]
1084  subby=bar
1085     #another comment
1086   fleezy = flam
1087  ; yet another comment
1088  yahoo = yippee
1089       EOS
1090       )
1091     end
1092
1093     it "should update an existing setting at the previous indentation when the section is not aligned" do
1094       resource = Puppet::Type::Ini_setting.new(
1095           common_params.merge(:section => 'section:sub', :setting => 'fleezy', :value => 'flam2'))
1096       provider = described_class.new(resource)
1097       provider.exists?.should be true
1098       provider.create
1099       validate_file(<<-EOS
1100 # This is a comment
1101      [section1]
1102      ; This is also a comment
1103      foo=foovalue
1104
1105      bar = barvalue
1106      master = true
1107
1108 [section2]
1109   foo= foovalue2
1110   baz=bazvalue
1111   url = http://192.168.1.1:8080
1112 [section:sub]
1113  subby=bar
1114     #another comment
1115   fleezy = flam2
1116  ; yet another comment
1117       EOS
1118       )
1119     end
1120
1121   end
1122
1123
1124   context "when dealing settings that have a commented version present" do
1125     let(:orig_content) {
1126       <<-EOS
1127      [section1]
1128      # foo=foovalue
1129      bar=barvalue
1130      foo = foovalue2
1131
1132 [section2]
1133 # foo = foovalue
1134 ;bar=barvalue
1135 blah = blah
1136 #baz=
1137       EOS
1138     }
1139
1140     it "should add a new setting below a commented version of that setting" do
1141       resource = Puppet::Type::Ini_setting.new(
1142           common_params.merge(:section => 'section2', :setting => 'foo', :value => 'foo3'))
1143       provider = described_class.new(resource)
1144       provider.exists?.should be false
1145       provider.create
1146       validate_file(<<-EOS
1147      [section1]
1148      # foo=foovalue
1149      bar=barvalue
1150      foo = foovalue2
1151
1152 [section2]
1153 # foo = foovalue
1154 foo = foo3
1155 ;bar=barvalue
1156 blah = blah
1157 #baz=
1158       EOS
1159       )
1160     end
1161
1162     it "should update an existing setting in place, even if there is a commented version of that setting" do
1163       resource = Puppet::Type::Ini_setting.new(
1164           common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foo3'))
1165       provider = described_class.new(resource)
1166       provider.exists?.should be true
1167       provider.create
1168       validate_file(<<-EOS
1169      [section1]
1170      # foo=foovalue
1171      bar=barvalue
1172      foo = foo3
1173
1174 [section2]
1175 # foo = foovalue
1176 ;bar=barvalue
1177 blah = blah
1178 #baz=
1179       EOS
1180       )
1181     end
1182
1183     it "should add a new setting below a commented version of that setting, respecting semicolons as comments" do
1184       resource = Puppet::Type::Ini_setting.new(
1185           common_params.merge(:section => 'section2', :setting => 'bar', :value => 'bar3'))
1186       provider = described_class.new(resource)
1187       provider.exists?.should be false
1188       provider.create
1189       validate_file(<<-EOS
1190      [section1]
1191      # foo=foovalue
1192      bar=barvalue
1193      foo = foovalue2
1194
1195 [section2]
1196 # foo = foovalue
1197 ;bar=barvalue
1198 bar=bar3
1199 blah = blah
1200 #baz=
1201       EOS
1202       )
1203     end
1204
1205     it "should add a new setting below an empty commented version of that setting" do
1206       resource = Puppet::Type::Ini_setting.new(
1207           common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue'))
1208       provider = described_class.new(resource)
1209       provider.exists?.should be false
1210       provider.create
1211       validate_file(<<-EOS
1212      [section1]
1213      # foo=foovalue
1214      bar=barvalue
1215      foo = foovalue2
1216
1217 [section2]
1218 # foo = foovalue
1219 ;bar=barvalue
1220 blah = blah
1221 #baz=
1222 baz=bazvalue
1223       EOS
1224       )
1225     end
1226
1227     context 'when a section only contains comments' do
1228      let(:orig_content) {
1229       <<-EOS
1230 [section1]
1231 # foo=foovalue
1232 # bar=bar2
1233 EOS
1234     }
1235       it 'should be able to add a new setting when a section contains only comments' do
1236         resource = Puppet::Type::Ini_setting.new(
1237           common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foovalue2')
1238         )
1239         provider = described_class.new(resource)
1240         provider.exists?.should be false
1241         provider.create
1242         validate_file(<<-EOS
1243 [section1]
1244 # foo=foovalue
1245 foo=foovalue2
1246 # bar=bar2
1247         EOS
1248         )
1249       end
1250       it 'should be able to add a new setting when it matches a commented out line other than the first one' do
1251         resource = Puppet::Type::Ini_setting.new(
1252           common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2')
1253         )
1254         provider = described_class.new(resource)
1255         provider.exists?.should be false
1256         provider.create
1257         validate_file(<<-EOS
1258 [section1]
1259 # foo=foovalue
1260 # bar=bar2
1261 bar=barvalue2
1262         EOS
1263         )
1264       end
1265     end
1266
1267     context "when sections have spaces and dashes" do
1268       let(:orig_content) {
1269         <<-EOS
1270 # This is a comment
1271 [section - one]
1272 ; This is also a comment
1273 foo=foovalue
1274
1275 bar = barvalue
1276 master = true
1277 [section - two]
1278
1279 foo= foovalue2
1280 baz=bazvalue
1281 url = http://192.168.1.1:8080
1282 [section:sub]
1283 subby=bar
1284     #another comment
1285  ; yet another comment
1286         EOS
1287       }
1288
1289       it "should add a missing setting to the correct section" do
1290         resource = Puppet::Type::Ini_setting.new(common_params.merge(
1291             :section => 'section - two', :setting => 'yahoo', :value => 'yippee'))
1292         provider = described_class.new(resource)
1293         provider.exists?.should be false
1294         provider.create
1295         validate_file(<<-EOS
1296 # This is a comment
1297 [section - one]
1298 ; This is also a comment
1299 foo=foovalue
1300
1301 bar = barvalue
1302 master = true
1303 [section - two]
1304
1305 foo= foovalue2
1306 baz=bazvalue
1307 url = http://192.168.1.1:8080
1308 yahoo = yippee
1309 [section:sub]
1310 subby=bar
1311     #another comment
1312  ; yet another comment
1313         EOS
1314   )
1315       end
1316
1317     end
1318
1319   end
1320
1321   context "when sections have spaces and quotations" do
1322     let(:orig_content) do
1323       <<-EOS
1324 [branch "master"]
1325         remote = origin
1326         merge = refs/heads/master
1327
1328 [alias]
1329 to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
1330 [branch "production"]
1331         remote = origin
1332         merge = refs/heads/production
1333       EOS
1334     end
1335
1336     it "should add a missing setting to the correct section" do
1337       resource = Puppet::Type::Ini_setting.new(common_params.merge(
1338         :section => 'alias',
1339         :setting => 'foo',
1340         :value => 'bar'
1341       ))
1342       provider = described_class.new(resource)
1343       provider.exists?.should be false
1344       provider.create
1345       validate_file(<<-EOS
1346 [branch "master"]
1347         remote = origin
1348         merge = refs/heads/master
1349
1350 [alias]
1351 to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
1352 foo = bar
1353 [branch "production"]
1354         remote = origin
1355         merge = refs/heads/production
1356                     EOS
1357                    )
1358     end
1359
1360   end
1361
1362 end