Skip to content

Commit

Permalink
Merge pull request #45 from cprice-puppet/bug/master/better-handling-…
Browse files Browse the repository at this point in the history
…of-quotes-for-subsettings

Bug/master/better handling of quotes for subsettings
  • Loading branch information
cprice404 committed May 22, 2013
2 parents f46fe29 + d4ccf14 commit 6c6f9a4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG
@@ -1,13 +1,16 @@
2013-05-21 - Chris Price <chris@puppetlabs.com>
2013-05-22 - Chris Price <chris@puppetlabs.com> - 0.10.2
* Better handling of quotes for subsettings (1aa7e60)

2013-05-21 - Chris Price <chris@puppetlabs.com> - 0.10.1
* Change constants to class variables to avoid ruby warnings (6b19864)

2013-04-10 - Erik Dalén <dalen@spotify.com>
2013-04-10 - Erik Dalén <dalen@spotify.com> - 0.10.1
* Style fixes (c4af8c3)

2013-04-02 - Dan Bode <dan@puppetlabs.com>
2013-04-02 - Dan Bode <dan@puppetlabs.com> - 0.10.1
* Add travisfile and Gemfile (c2052b3)

2013-04-02 - Chris Price <chris@puppetlabs.com>
2013-04-02 - Chris Price <chris@puppetlabs.com> - 0.10.1
* Update README.markdown (ad38a08)

2013-02-15 - Karel Brezina <karel.brezina@gmail.com> - 0.10.0
Expand Down
2 changes: 1 addition & 1 deletion Modulefile
@@ -1,5 +1,5 @@
name 'cprice404-inifile'
version '0.10.1'
version '0.10.2'
source 'git://github.com/cprice-puppet/puppetlabs-inifile.git'
author 'Chris Price'
description 'Resource types for managing settings in INI files'
Expand Down
23 changes: 20 additions & 3 deletions lib/puppet/util/setting_value.rb
Expand Up @@ -8,14 +8,31 @@ def initialize(setting_value, subsetting_separator = ' ')
@subsetting_separator = subsetting_separator

if @setting_value
unquoted = setting_value[1, setting_value.length - 2]
unquoted, @quote_char = unquote_setting_value(setting_value)
@subsetting_items = unquoted.scan(Regexp.new("(?:(?:[^\\#{@subsetting_separator}]|\\.)+)")) # an item can contain escaped separator
@subsetting_items.map! { |item| item.strip }
else
@subsetting_items = []
end
end

def unquote_setting_value(setting_value)
quote_char = ""
if (setting_value.start_with?('"') and setting_value.end_with?('"'))
quote_char = '"'
elsif (setting_value.start_with?("'") and setting_value.end_with?("'"))
quote_char = "'"
end

unquoted = setting_value

if (quote_char != "")
unquoted = setting_value[1, setting_value.length - 2]
end

[unquoted, quote_char]
end

def get_value

result = ""
Expand All @@ -27,7 +44,7 @@ def get_value
first = false
}

"\"" + result + "\""
@quote_char + result + @quote_char
end

def get_subsetting_value(subsetting)
Expand Down Expand Up @@ -71,4 +88,4 @@ def remove_subsetting(subsetting)

end
end
end
end
64 changes: 55 additions & 9 deletions spec/unit/puppet/provider/ini_subsetting/ruby_spec.rb
Expand Up @@ -7,14 +7,6 @@

let(:tmpfile) { tmpfilename("ini_setting_test") }

let(:common_params) { {
:title => 'ini_setting_ensure_present_test',
:path => tmpfile,
:section => '',
:key_val_separator => '=',
:setting => 'JAVA_ARGS',
} }

def validate_file(expected_content,tmpfile = tmpfile)
File.read(tmpfile).should == expected_content
end
Expand All @@ -27,6 +19,14 @@ def validate_file(expected_content,tmpfile = tmpfile)
end

context "when ensuring that a subsetting is present" do
let(:common_params) { {
:title => 'ini_setting_ensure_present_test',
:path => tmpfile,
:section => '',
:key_val_separator => '=',
:setting => 'JAVA_ARGS',
} }

let(:orig_content) {
<<-EOS
JAVA_ARGS="-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof"
Expand Down Expand Up @@ -68,6 +68,52 @@ def validate_file(expected_content,tmpfile = tmpfile)
EOS
)
end

end

context "when working with subsettings in files with unquoted settings values" do
let(:common_params) { {
:title => 'ini_setting_ensure_present_test',
:path => tmpfile,
:section => 'master',
:key_val_separator => '=',
:setting => 'reports',
} }

let(:orig_content) {
<<-EOS
[master]
reports = http,foo
EOS
}

it "should remove an existing subsetting" do
resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
:subsetting => 'http', :subsetting_separator => ','))
provider = described_class.new(resource)
provider.exists?.should == ""
provider.destroy
validate_file(<<-EOS
[master]
reports = foo
EOS
)
end

it "should add a new subsetting" do
resource = Puppet::Type::Ini_subsetting.new(common_params.merge(
:subsetting => 'puppetdb', :subsetting_separator => ','))
provider = described_class.new(resource)
provider.exists?.should be_nil
provider.value=('')
validate_file(<<-EOS
[master]
reports = http,foo,puppetdb
EOS
)
end

end
end

0 comments on commit 6c6f9a4

Please sign in to comment.