summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Price <chris@puppetlabs.com>2012-09-20 12:53:59 -0700
committerChris Price <chris@puppetlabs.com>2012-09-20 12:53:59 -0700
commit4ff4995d9efa15504eaf33e27385b5e2c4092087 (patch)
tree363d5bd285f3f03e113b08b1deae5eac45eb98da
parent310a4b1575f7af903ac97108c6f1352d05f5f3fc (diff)
parente527908b7c6ac5d91677229a96d5a97194dc68b7 (diff)
downloadpuppet-inifile-4ff4995d9efa15504eaf33e27385b5e2c4092087.tar.gz
puppet-inifile-4ff4995d9efa15504eaf33e27385b5e2c4092087.tar.bz2
Merge pull request #9 from cprice-puppet/feature/master/allow-override-of-separator-str
Allow overriding separator string between key/val pairs
-rw-r--r--lib/puppet/provider/ini_setting/ruby.rb3
-rw-r--r--lib/puppet/type/ini_setting.rb15
-rw-r--r--lib/puppet/util/ini_file.rb7
-rw-r--r--spec/unit/puppet/provider/ini_setting/ruby_spec.rb64
-rw-r--r--tests/ini_setting.pp13
5 files changed, 91 insertions, 11 deletions
diff --git a/lib/puppet/provider/ini_setting/ruby.rb b/lib/puppet/provider/ini_setting/ruby.rb
index 9f13dff..ba8165c 100644
--- a/lib/puppet/provider/ini_setting/ruby.rb
+++ b/lib/puppet/provider/ini_setting/ruby.rb
@@ -14,6 +14,7 @@ Puppet::Type.type(:ini_setting).provide(:ruby) do
private
def ini_file
- @ini_file ||= Puppet::Util::IniFile.new(resource[:path])
+ @ini_file ||= Puppet::Util::IniFile.new(resource[:path],
+ resource[:key_val_separator])
end
end
diff --git a/lib/puppet/type/ini_setting.rb b/lib/puppet/type/ini_setting.rb
index 9af47c1..50b6b38 100644
--- a/lib/puppet/type/ini_setting.rb
+++ b/lib/puppet/type/ini_setting.rb
@@ -30,4 +30,17 @@ Puppet::Type.newtype(:ini_setting) do
end
end
-end \ No newline at end of file
+ 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.'
+ 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/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb
index 4fe4169..36bc8a6 100644
--- a/lib/puppet/util/ini_file.rb
+++ b/lib/puppet/util/ini_file.rb
@@ -8,8 +8,9 @@ module Util
SECTION_REGEX = /^\s*\[([\w\d\.\\\/\-\:]+)\]\s*$/
SETTING_REGEX = /^\s*([\w\d\.\\\/\-]+)\s*=\s*([\S]+)\s*$/
- def initialize(path)
+ def initialize(path, key_val_separator = ' = ')
@path = path
+ @key_val_separator = key_val_separator
@section_names = []
@sections_hash = {}
if File.file?(@path)
@@ -56,7 +57,7 @@ module Util
end
section.additional_settings.each_pair do |key, value|
- fh.puts("#{key} = #{value}")
+ fh.puts("#{key}#{@key_val_separator}#{value}")
end
end
end
@@ -106,7 +107,7 @@ module Util
(section.start_line..section.end_line).each do |line_num|
if (match = SETTING_REGEX.match(lines[line_num]))
if (match[1] == setting)
- lines[line_num] = "#{setting} = #{value}"
+ lines[line_num] = "#{setting}#{@key_val_separator}#{value}"
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 2dbbf55..20882fd 100644
--- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb
+++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb
@@ -392,4 +392,68 @@ bar = baz
end
end
+ context "when overriding the separator" do
+ let(:orig_content) {
+ <<-EOS
+[section2]
+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',
+ :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
diff --git a/tests/ini_setting.pp b/tests/ini_setting.pp
index 598bedf..6fd7686 100644
--- a/tests/ini_setting.pp
+++ b/tests/ini_setting.pp
@@ -7,10 +7,11 @@ ini_setting { "sample setting":
}
ini_setting { "sample setting2":
- path => '/tmp/foo.ini',
- section => 'bar',
- setting => 'barsetting',
- value => 'BAR!',
- ensure => present,
- require => Ini_setting["sample setting"],
+ path => '/tmp/foo.ini',
+ section => 'bar',
+ setting => 'barsetting',
+ value => 'BAR!',
+ key_value_separator => '=',
+ ensure => present,
+ require => Ini_setting["sample setting"],
}