Skip to content

Commit 09b574e

Browse files
committed
Merge pull request puppetlabs#115 from johnsyweb/handle-quotation-marks-in-section-names
Handle quotation marks in section names
2 parents 07c4827 + b3fed85 commit 09b574e

4 files changed

Lines changed: 155 additions & 36 deletions

File tree

lib/puppet/provider/ini_setting/ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def self.namevar(section_name, setting)
3838
end
3939

4040
def exists?
41-
ini_file.get_value(section, setting)
41+
!ini_file.get_value(section, setting).nil?
4242
end
4343

4444
def create

lib/puppet/util/ini_file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Puppet
55
module Util
66
class IniFile
77

8-
@@SECTION_REGEX = /^\s*\[([\w\d\.\\\/\-\:\s]*[\w\d\.\\\/\-])\]\s*$/
8+
@@SECTION_REGEX = /^\s*\[([^\]]*)\]\s*$/
99
@@SETTING_REGEX = /^(\s*)([^\[#;][\w\d\.\\\/\-\s\[\]\']*[\w\d\.\\\/\-\]])([ \t]*=[ \t]*)([\S\s]*?)\s*$/
1010
@@COMMENTED_SETTING_REGEX = /^(\s*)[#;]+(\s*)([^\[]*[\w\d\.\\\/\-]+[\w\d\.\\\/\-\[\]\']+)([ \t]*=[ \t]*)([\S\s]*?)\s*$/
1111

spec/unit/puppet/provider/ini_setting/ruby_spec.rb

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def self.file_path
144144
resource = Puppet::Type::Ini_setting.new(common_params.merge(
145145
:setting => 'yahoo', :value => 'yippee'))
146146
provider = described_class.new(resource)
147-
provider.exists?.should be_nil
147+
provider.exists?.should be false
148148
provider.create
149149
validate_file(<<-EOS
150150
# This is a comment
@@ -172,7 +172,7 @@ def self.file_path
172172
resource = Puppet::Type::Ini_setting.new(common_params.merge(
173173
:section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
174174
provider = described_class.new(resource)
175-
provider.exists?.should be_nil
175+
provider.exists?.should be false
176176
provider.create
177177
validate_file(<<-EOS
178178
# This is a comment
@@ -200,7 +200,7 @@ def self.file_path
200200
resource = Puppet::Type::Ini_setting.new(common_params.merge(
201201
:setting => 'baz', :value => 'bazvalue2'))
202202
provider = described_class.new(resource)
203-
provider.exists?.should == 'bazvalue'
203+
provider.exists?.should be true
204204
provider.value=('bazvalue2')
205205
validate_file(<<-EOS
206206
# This is a comment
@@ -227,7 +227,7 @@ def self.file_path
227227
resource = Puppet::Type::Ini_setting.new(common_params.merge(
228228
:section => 'section:sub', :setting => 'subby', :value => 'foo'))
229229
provider = described_class.new(resource)
230-
provider.exists?.should == 'bar'
230+
provider.exists?.should be true
231231
provider.value.should == 'bar'
232232
provider.value=('foo')
233233
validate_file(<<-EOS
@@ -255,7 +255,7 @@ def self.file_path
255255
resource = Puppet::Type::Ini_setting.new(common_params.merge(
256256
:setting => 'url', :value => 'http://192.168.0.1:8080'))
257257
provider = described_class.new(resource)
258-
provider.exists?.should == 'http://192.168.1.1:8080'
258+
provider.exists?.should be true
259259
provider.value.should == 'http://192.168.1.1:8080'
260260
provider.value=('http://192.168.0.1:8080')
261261

@@ -284,14 +284,14 @@ def self.file_path
284284
resource = Puppet::Type::Ini_setting.new(common_params.merge(
285285
:setting => 'baz', :value => 'bazvalue'))
286286
provider = described_class.new(resource)
287-
provider.exists?.should == 'bazvalue'
287+
provider.exists?.should be true
288288
end
289289

290290
it "should add a new section if the section does not exist" do
291291
resource = Puppet::Type::Ini_setting.new(common_params.merge(
292292
:section => "section3", :setting => 'huzzah', :value => 'shazaam'))
293293
provider = described_class.new(resource)
294-
provider.exists?.should be_nil
294+
provider.exists?.should be false
295295
provider.create
296296
validate_file(<<-EOS
297297
# This is a comment
@@ -321,7 +321,7 @@ def self.file_path
321321
resource = Puppet::Type::Ini_setting.new(common_params.merge(
322322
:section => "section:subsection", :setting => 'huzzah', :value => 'shazaam'))
323323
provider = described_class.new(resource)
324-
provider.exists?.should be_nil
324+
provider.exists?.should be false
325325
provider.create
326326
validate_file(<<-EOS
327327
# This is a comment
@@ -351,7 +351,7 @@ def self.file_path
351351
resource = Puppet::Type::Ini_setting.new(common_params.merge(
352352
:section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
353353
provider = described_class.new(resource)
354-
provider.exists?.should be_nil
354+
provider.exists?.should be false
355355
provider.create
356356
validate_file("
357357
[section1]
@@ -363,7 +363,7 @@ def self.file_path
363363
resource = Puppet::Type::Ini_setting.new(common_params.merge(
364364
:section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
365365
provider = described_class.new(resource)
366-
provider.exists?.should be_nil
366+
provider.exists?.should be false
367367
provider.create
368368
validate_file("
369369
[section:subsection]
@@ -375,7 +375,7 @@ def self.file_path
375375
resource = Puppet::Type::Ini_setting.new(common_params.merge(
376376
:section => "section1", :setting => 'master', :value => true))
377377
provider = described_class.new(resource)
378-
provider.exists?.should == 'true'
378+
provider.exists?.should be true
379379
provider.value.should == 'true'
380380
end
381381

@@ -397,7 +397,7 @@ def self.file_path
397397
resource = Puppet::Type::Ini_setting.new(common_params.merge(
398398
:section => '', :setting => 'bar', :value => 'yippee'))
399399
provider = described_class.new(resource)
400-
provider.exists?.should be_nil
400+
provider.exists?.should be false
401401
provider.create
402402
validate_file(<<-EOS
403403
# This is a comment
@@ -414,7 +414,7 @@ def self.file_path
414414
resource = Puppet::Type::Ini_setting.new(common_params.merge(
415415
:section => '', :setting => 'foo', :value => 'yippee'))
416416
provider = described_class.new(resource)
417-
provider.exists?.should == 'blah'
417+
provider.exists?.should be true
418418
provider.value.should == 'blah'
419419
provider.value=('yippee')
420420
validate_file(<<-EOS
@@ -431,7 +431,7 @@ def self.file_path
431431
resource = Puppet::Type::Ini_setting.new(common_params.merge(
432432
:section => '', :setting => 'foo', :value => 'blah'))
433433
provider = described_class.new(resource)
434-
provider.exists?.should == 'blah'
434+
provider.exists?.should be true
435435
end
436436
end
437437

@@ -447,7 +447,7 @@ def self.file_path
447447
resource = Puppet::Type::Ini_setting.new(common_params.merge(
448448
:section => '', :setting => 'foo', :value => 'yippee'))
449449
provider = described_class.new(resource)
450-
provider.exists?.should be_nil
450+
provider.exists?.should be false
451451
provider.create
452452
validate_file(<<-EOS
453453
foo = yippee
@@ -462,7 +462,7 @@ def self.file_path
462462
resource = Puppet::Type::Ini_setting.new(common_params.merge(
463463
:section => 'section2', :setting => 'foo', :value => 'yippee'))
464464
provider = described_class.new(resource)
465-
provider.exists?.should == 'http://192.168.1.1:8080'
465+
provider.exists?.should be true
466466
provider.value.should == 'http://192.168.1.1:8080'
467467
provider.value=('yippee')
468468
validate_file(<<-EOS
@@ -476,7 +476,7 @@ def self.file_path
476476
resource = Puppet::Type::Ini_setting.new(common_params.merge(
477477
:section => 'section2', :setting => 'bar', :value => 'baz'))
478478
provider = described_class.new(resource)
479-
provider.exists?.should be_nil
479+
provider.exists?.should be false
480480
provider.create
481481
validate_file(<<-EOS
482482
[section2]
@@ -522,7 +522,7 @@ def self.file_path
522522
:value => 'yippee',
523523
:key_val_separator => '='))
524524
provider = described_class.new(resource)
525-
provider.exists?.should == 'bar'
525+
provider.exists?.should be true
526526
provider.value.should == 'bar'
527527
provider.value=('yippee')
528528
validate_file(<<-EOS
@@ -539,7 +539,7 @@ def self.file_path
539539
:value => 'baz',
540540
:key_val_separator => '='))
541541
provider = described_class.new(resource)
542-
provider.exists?.should be_nil
542+
provider.exists?.should be false
543543
provider.create
544544
validate_file(<<-EOS
545545
[section2]
@@ -576,7 +576,7 @@ def self.file_path
576576
resource = Puppet::Type::Ini_setting.new(common_params.merge(
577577
:section => 'section1', :setting => 'foo', :ensure => 'absent'))
578578
provider = described_class.new(resource)
579-
provider.exists?.should be_true
579+
provider.exists?.should be true
580580
provider.destroy
581581
validate_file(<<-EOS
582582
[section1]
@@ -601,7 +601,7 @@ def self.file_path
601601
resource = Puppet::Type::Ini_setting.new(common_params.merge(
602602
:section => 'section:sub', :setting => 'foo', :ensure => 'absent'))
603603
provider = described_class.new(resource)
604-
provider.exists?.should be_nil
604+
provider.exists?.should be false
605605
provider.destroy
606606
validate_file(<<-EOS
607607
[section1]
@@ -652,7 +652,7 @@ def self.file_path
652652
resource = Puppet::Type::Ini_setting.new(common_params.merge(
653653
:section => 'section1', :setting => 'yahoo', :value => 'yippee'))
654654
provider = described_class.new(resource)
655-
provider.exists?.should be_nil
655+
provider.exists?.should be false
656656
provider.create
657657
validate_file(<<-EOS
658658
# This is a comment
@@ -681,7 +681,7 @@ def self.file_path
681681
resource = Puppet::Type::Ini_setting.new(
682682
common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2'))
683683
provider = described_class.new(resource)
684-
provider.exists?.should be_true
684+
provider.exists?.should be true
685685
provider.create
686686
validate_file(<<-EOS
687687
# This is a comment
@@ -709,7 +709,7 @@ def self.file_path
709709
resource = Puppet::Type::Ini_setting.new(common_params.merge(
710710
:section => 'section2', :setting => 'yahoo', :value => 'yippee'))
711711
provider = described_class.new(resource)
712-
provider.exists?.should be_nil
712+
provider.exists?.should be false
713713
provider.create
714714
validate_file(<<-EOS
715715
# This is a comment
@@ -738,7 +738,7 @@ def self.file_path
738738
resource = Puppet::Type::Ini_setting.new(
739739
common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue2'))
740740
provider = described_class.new(resource)
741-
provider.exists?.should be_true
741+
provider.exists?.should be true
742742
provider.create
743743
validate_file(<<-EOS
744744
# This is a comment
@@ -766,7 +766,7 @@ def self.file_path
766766
resource = Puppet::Type::Ini_setting.new(
767767
common_params.merge(:section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
768768
provider = described_class.new(resource)
769-
provider.exists?.should be_nil
769+
provider.exists?.should be false
770770
provider.create
771771
validate_file(<<-EOS
772772
# This is a comment
@@ -795,7 +795,7 @@ def self.file_path
795795
resource = Puppet::Type::Ini_setting.new(
796796
common_params.merge(:section => 'section:sub', :setting => 'fleezy', :value => 'flam2'))
797797
provider = described_class.new(resource)
798-
provider.exists?.should be_true
798+
provider.exists?.should be true
799799
provider.create
800800
validate_file(<<-EOS
801801
# This is a comment
@@ -842,7 +842,7 @@ def self.file_path
842842
resource = Puppet::Type::Ini_setting.new(
843843
common_params.merge(:section => 'section2', :setting => 'foo', :value => 'foo3'))
844844
provider = described_class.new(resource)
845-
provider.exists?.should be_false
845+
provider.exists?.should be false
846846
provider.create
847847
validate_file(<<-EOS
848848
[section1]
@@ -864,7 +864,7 @@ def self.file_path
864864
resource = Puppet::Type::Ini_setting.new(
865865
common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foo3'))
866866
provider = described_class.new(resource)
867-
provider.exists?.should be_true
867+
provider.exists?.should be true
868868
provider.create
869869
validate_file(<<-EOS
870870
[section1]
@@ -885,7 +885,7 @@ def self.file_path
885885
resource = Puppet::Type::Ini_setting.new(
886886
common_params.merge(:section => 'section2', :setting => 'bar', :value => 'bar3'))
887887
provider = described_class.new(resource)
888-
provider.exists?.should be_false
888+
provider.exists?.should be false
889889
provider.create
890890
validate_file(<<-EOS
891891
[section1]
@@ -907,7 +907,7 @@ def self.file_path
907907
resource = Puppet::Type::Ini_setting.new(
908908
common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue'))
909909
provider = described_class.new(resource)
910-
provider.exists?.should be_false
910+
provider.exists?.should be false
911911
provider.create
912912
validate_file(<<-EOS
913913
[section1]
@@ -938,7 +938,7 @@ def self.file_path
938938
common_params.merge(:section => 'section1', :setting => 'foo', :value => 'foovalue2')
939939
)
940940
provider = described_class.new(resource)
941-
provider.exists?.should be_false
941+
provider.exists?.should be false
942942
provider.create
943943
validate_file(<<-EOS
944944
[section1]
@@ -953,7 +953,7 @@ def self.file_path
953953
common_params.merge(:section => 'section1', :setting => 'bar', :value => 'barvalue2')
954954
)
955955
provider = described_class.new(resource)
956-
provider.exists?.should be_false
956+
provider.exists?.should be false
957957
provider.create
958958
validate_file(<<-EOS
959959
[section1]
@@ -991,7 +991,7 @@ def self.file_path
991991
resource = Puppet::Type::Ini_setting.new(common_params.merge(
992992
:section => 'section - two', :setting => 'yahoo', :value => 'yippee'))
993993
provider = described_class.new(resource)
994-
provider.exists?.should be_nil
994+
provider.exists?.should be false
995995
provider.create
996996
validate_file(<<-EOS
997997
# This is a comment
@@ -1019,4 +1019,45 @@ def self.file_path
10191019

10201020
end
10211021

1022+
context "when sections have spaces and quotations" do
1023+
let(:orig_content) do
1024+
<<-EOS
1025+
[branch "master"]
1026+
remote = origin
1027+
merge = refs/heads/master
1028+
1029+
[alias]
1030+
to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
1031+
[branch "production"]
1032+
remote = origin
1033+
merge = refs/heads/production
1034+
EOS
1035+
end
1036+
1037+
it "should add a missing setting to the correct section" do
1038+
resource = Puppet::Type::Ini_setting.new(common_params.merge(
1039+
:section => 'alias',
1040+
:setting => 'foo',
1041+
:value => 'bar'
1042+
))
1043+
provider = described_class.new(resource)
1044+
provider.exists?.should be false
1045+
provider.create
1046+
validate_file(<<-EOS
1047+
[branch "master"]
1048+
remote = origin
1049+
merge = refs/heads/master
1050+
1051+
[alias]
1052+
to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
1053+
foo = bar
1054+
[branch "production"]
1055+
remote = origin
1056+
merge = refs/heads/production
1057+
EOS
1058+
)
1059+
end
1060+
1061+
end
1062+
10221063
end

0 commit comments

Comments
 (0)