From 8d1fdc5c29d70027b0f1859145ced5ebb266cf9c Mon Sep 17 00:00:00 2001 From: Chris Price Date: Wed, 19 Sep 2012 15:42:16 -0700 Subject: Allow overriding separator string between key/val pairs This introduces a new parameter, 'key_val_separator', which can be set in order to override the string that is used as a separator between the key/value pair of a setting line. The default is ' = ', but you could set the param to '=' if you don't want to include whitespace in your settings file. --- spec/unit/puppet/provider/ini_setting/ruby_spec.rb | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'spec') diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index 2dbbf55..ac9bb91 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -392,4 +392,48 @@ bar = baz end end + context "when overriding the separator" 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 == false + provider.create + 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 => '=')) + provider = described_class.new(resource) + provider.exists?.should == false + provider.create + validate_file(<<-EOS +[section2] +foo=bar +bar=baz + EOS + ) + end + + + end + end -- cgit v1.2.3 From e527908b7c6ac5d91677229a96d5a97194dc68b7 Mon Sep 17 00:00:00 2001 From: Chris Price Date: Thu, 20 Sep 2012 11:35:45 -0700 Subject: Add validation for key_val_separator For now, the key_val_separator is required to contain exactly one '=' character. This commit simply validates that that is the case. --- lib/puppet/type/ini_setting.rb | 6 ++++++ spec/unit/puppet/provider/ini_setting/ruby_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) (limited to 'spec') diff --git a/lib/puppet/type/ini_setting.rb b/lib/puppet/type/ini_setting.rb index f1ab490..50b6b38 100644 --- a/lib/puppet/type/ini_setting.rb +++ b/lib/puppet/type/ini_setting.rb @@ -35,6 +35,12 @@ Puppet::Type.newtype(:ini_setting) do 'Defaults to " = ", but you could use this to override e.g. 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 end diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index ac9bb91..20882fd 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -400,6 +400,26 @@ 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', -- cgit v1.2.3