From 5904c6f7b4d24ae1985faf61c11b7bf4adb78f27 Mon Sep 17 00:00:00 2001 From: Terry Wilson Date: Wed, 10 Jul 2013 10:26:37 -0500 Subject: [PATCH] Allow empty values It is a legitimate use case to set empty values; to override a default when an empty value is acceptable for instance. This patch changes the regex in three ways: it 1) removes the requirement for a non-whitespace terminator on a setting value, 2) makes the value match non-greedy so that the \s*$ at the end can catch the newline and 3) changes the \s*=\s* to [ \t]*=[ \t]* because we don't want that to capture *any* whitespace (like a newline). --- lib/puppet/util/ini_file.rb | 4 +-- .../puppet/provider/ini_setting/ruby_spec.rb | 26 ++++++++++++++++ spec/unit/puppet/util/ini_file_spec.rb | 31 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb index 340fe18..f7ed5eb 100644 --- a/lib/puppet/util/ini_file.rb +++ b/lib/puppet/util/ini_file.rb @@ -6,8 +6,8 @@ module Util class IniFile @@SECTION_REGEX = /^\s*\[([\w\d\.\\\/\-\:]+)\]\s*$/ - @@SETTING_REGEX = /^(\s*)([\w\d\.\\\/\-]+)(\s*=\s*)([\S\s]*\S)\s*$/ - @@COMMENTED_SETTING_REGEX = /^(\s*)[#;]+(\s*)([\w\d\.\\\/\-]+)(\s*=\s*)([\S\s]*\S)\s*$/ + @@SETTING_REGEX = /^(\s*)([\w\d\.\\\/\-]+)([ \t]*=[ \t]*)([\S\s]*?)\s*$/ + @@COMMENTED_SETTING_REGEX = /^(\s*)[#;]+(\s*)([\w\d\.\\\/\-]+)([ \t]*=[ \t]*)([\S\s]*?)\s*$/ def initialize(path, key_val_separator = ' = ') @path = path diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index ea24968..550dd72 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -834,6 +834,7 @@ subby=bar # foo = foovalue ;bar=barvalue blah = blah +#baz= EOS } @@ -854,6 +855,7 @@ blah = blah foo = foo3 ;bar=barvalue blah = blah +#baz= EOS ) end @@ -874,6 +876,7 @@ blah = blah # foo = foovalue ;bar=barvalue blah = blah +#baz= EOS ) end @@ -895,6 +898,29 @@ blah = blah ;bar=barvalue bar=bar3 blah = blah +#baz= + EOS + ) + end + + it "should add a new setting below an empty commented version of that setting" do + resource = Puppet::Type::Ini_setting.new( + common_params.merge(:section => 'section2', :setting => 'baz', :value => 'bazvalue')) + provider = described_class.new(resource) + provider.exists?.should be_false + provider.create + validate_file(<<-EOS + [section1] + # foo=foovalue + bar=barvalue + foo = foovalue2 + +[section2] +# foo = foovalue +;bar=barvalue +blah = blah +#baz= +baz=bazvalue EOS ) end diff --git a/spec/unit/puppet/util/ini_file_spec.rb b/spec/unit/puppet/util/ini_file_spec.rb index 479085e..8ab18cf 100644 --- a/spec/unit/puppet/util/ini_file_spec.rb +++ b/spec/unit/puppet/util/ini_file_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'stringio' require 'puppet/util/ini_file' describe Puppet::Util::IniFile do @@ -20,6 +21,7 @@ describe Puppet::Util::IniFile do foo=foovalue bar = barvalue +baz = [section2] foo= foovalue2 @@ -44,6 +46,7 @@ baz=bazvalue it "should expose settings for sections" do subject.get_value("section1", "foo").should == "foovalue" subject.get_value("section1", "bar").should == "barvalue" + subject.get_value("section1", "baz").should == "" subject.get_value("section2", "foo").should == "foovalue2" subject.get_value("section2", "baz").should == "bazvalue" subject.get_value("section2", "zot").should == "multi word value" @@ -102,6 +105,34 @@ foo=foovalue subject.get_value("", "foo").should == "bar" subject.get_value("section1", "foo").should == "foovalue" end + end + context "when updating a file with existing empty values" do + let(:sample_content) { + template = <<-EOS +[section1] +foo= +#bar= + EOS + template.split("\n") + } + + it "should properly update uncommented values" do + subject.get_value("section1", "far").should == nil + subject.set_value("section1", "foo", "foovalue") + subject.get_value("section1", "foo").should == "foovalue" + end + + it "should properly update commented values" do + subject.get_value("section1", "bar").should == nil + subject.set_value("section1", "bar", "barvalue") + subject.get_value("section1", "bar").should == "barvalue" + end + + it "should properly add new empty values" do + subject.get_value("section1", "baz").should == nil + subject.set_value("section1", "baz", "bazvalue") + subject.get_value("section1", "baz").should == "bazvalue" + end end end