diff --git a/lib/puppet/type/ini_setting.rb b/lib/puppet/type/ini_setting.rb index 4506231..662c74c 100644 --- a/lib/puppet/type/ini_setting.rb +++ b/lib/puppet/type/ini_setting.rb @@ -28,15 +28,9 @@ Puppet::Type.newtype(:ini_setting) do newparam(:key_val_separator) do desc 'The separator string to use between each setting name and value. ' + - 'Defaults to " = ", but you could use this to override e.g. whether ' + - 'or not the separator should include whitespace.' + 'Defaults to " = ", but you could use this to override e.g. ": ", or' + + 'whether or not the separator should include whitespace.' defaultto(" = ") - - validate do |value| - unless value.scan('=').size == 1 - raise Puppet::Error, ":key_val_separator must contain exactly one = character." - end - end end newproperty(:value) do diff --git a/lib/puppet/type/ini_subsetting.rb b/lib/puppet/type/ini_subsetting.rb index 847e317..0db0738 100644 --- a/lib/puppet/type/ini_subsetting.rb +++ b/lib/puppet/type/ini_subsetting.rb @@ -37,15 +37,9 @@ Puppet::Type.newtype(:ini_subsetting) do newparam(:key_val_separator) do desc 'The separator string to use between each setting name and value. ' + - 'Defaults to " = ", but you could use this to override e.g. whether ' + - 'or not the separator should include whitespace.' + 'Defaults to " = ", but you could use this to override e.g. ": ", or' + + 'whether or not the separator should include whitespace.' defaultto(" = ") - - validate do |value| - unless value.scan('=').size == 1 - raise Puppet::Error, ":key_val_separator must contain exactly one = character." - end - end end newparam(:quote_char) do diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb index a5cefa7..c1884ce 100644 --- a/lib/puppet/util/ini_file.rb +++ b/lib/puppet/util/ini_file.rb @@ -5,11 +5,15 @@ module Puppet module Util class IniFile - @@SECTION_REGEX = /^\s*\[([^\]]*)\]\s*$/ - @@SETTING_REGEX = /^(\s*)([^\s=]*)(\s*=\s*)(.*)\s*$/ - @@COMMENTED_SETTING_REGEX = /^(\s*)[#;]+(\s*)([^\s=]*)(\s*=[ \t]*)(.*)\s*$/ - def initialize(path, key_val_separator = ' = ') + + k_v_s = key_val_separator.strip + + @@SECTION_REGEX = /^\s*\[([^\]]*)\]\s*$/ + @@SETTING_REGEX = /^(\s*)([^\s#{k_v_s}]*)(\s*#{k_v_s}\s*)(.*)\s*$/ + @@COMMENTED_SETTING_REGEX = /^(\s*)[#;]+(\s*)([^\s#{k_v_s}]*)(\s*#{k_v_s}[ \t]*)(.*)\s*$/ + + @path = path @key_val_separator = key_val_separator @section_names = [] diff --git a/spec/acceptance/ini_setting_spec.rb b/spec/acceptance/ini_setting_spec.rb index 9aad0cc..b47fdfc 100644 --- a/spec/acceptance/ini_setting_spec.rb +++ b/spec/acceptance/ini_setting_spec.rb @@ -282,27 +282,5 @@ describe 'ini_setting resource' do it_behaves_like 'has_content', "#{tmpdir}/key_val_separator.ini", pp, content end end - - { - "key_val_separator => ''," => /must contain exactly one/, - "key_val_separator => ','," => /must contain exactly one/, - "key_val_separator => ' '," => /must contain exactly one/, - "key_val_separator => ' == '," => /must contain exactly one/, - }.each do |parameter, error| - context "with \"#{parameter}\" raises \"#{error}\"" do - pp = <<-EOS - ini_setting { "with #{parameter} raises #{error}": - ensure => present, - section => 'one', - setting => 'two', - value => 'three', - path => "#{tmpdir}/key_val_separator.ini", - #{parameter} - } - EOS - - it_behaves_like 'has_error', "#{tmpdir}/key_val_separator.ini", pp, error - end - end end end diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index 1ab84b9..0810bc2 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -495,26 +495,6 @@ foo=bar EOS } - it "should fail if the separator doesn't include an equals sign" do - expect { - Puppet::Type::Ini_setting.new(common_params.merge( - :section => 'section2', - :setting => 'foo', - :value => 'yippee', - :key_val_separator => '+')) - }.to raise_error Puppet::Error, /must contain exactly one/ - end - - it "should fail if the separator includes more than one equals sign" do - expect { - Puppet::Type::Ini_setting.new(common_params.merge( - :section => 'section2', - :setting => 'foo', - :value => 'yippee', - :key_val_separator => ' = foo = ')) - }.to raise_error Puppet::Error, /must contain exactly one/ - end - it "should modify an existing setting" do resource = Puppet::Type::Ini_setting.new(common_params.merge( :section => 'section2', @@ -532,19 +512,46 @@ foo=yippee ) end + end + + context "when overriding the separator to something other than =" do + let(:orig_content) { + <<-EOS +[section2] +foo: bar + EOS + } + + it "should modify an existing setting" do + resource = Puppet::Type::Ini_setting.new(common_params.merge( + :section => 'section2', + :setting => 'foo', + :value => 'yippee', + :key_val_separator => ': ')) + provider = described_class.new(resource) + provider.exists?.should be true + provider.value.should == 'bar' + provider.value=('yippee') + validate_file(<<-EOS +[section2] +foo: yippee + EOS + ) + end + it "should add a new setting" do resource = Puppet::Type::Ini_setting.new(common_params.merge( :section => 'section2', :setting => 'bar', :value => 'baz', - :key_val_separator => '=')) + :key_val_separator => ': ')) provider = described_class.new(resource) provider.exists?.should be false provider.create validate_file(<<-EOS [section2] -foo=bar -bar=baz +foo: bar +bar: baz EOS ) end