Merge pull request #139 from mhaskel/flexible_key_val

Flexible key val
This commit is contained in:
Colleen Murphy 2014-12-18 13:46:54 -08:00
commit 2c9defda30
5 changed files with 42 additions and 65 deletions

View file

@ -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

View file

@ -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

View file

@ -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 = []

View file

@ -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

View file

@ -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