From 1106d70e881028ee2dfa476307444780c9c4cbaa Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Wed, 10 Oct 2012 17:40:38 -0700 Subject: Add support for removing lines This commit adds the ability to ensure that lines are absent. --- spec/unit/puppet/provider/ini_setting/ruby_spec.rb | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'spec/unit/puppet/provider/ini_setting') diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb index 4062205..c8fcec4 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -458,7 +458,53 @@ bar=baz ) end + end + + context "when ensuring that a setting is absent" do + let(:orig_content) { + <<-EOS +[section1] +; This is also a comment +foo=foovalue + +bar = barvalue +master = true +[section2] + +foo= foovalue2 +baz=bazvalue +url = http://192.168.1.1:8080 +[section:sub] +subby=bar + #another comment + ; yet another comment +EOS + } + + it "should remove a setting that exists" do + resource = Puppet::Type::Ini_setting.new(common_params.merge( + :section => 'section1', :setting => 'foo', :ensure => 'absent')) + provider = described_class.new(resource) + provider.exists?.should be_true + provider.destroy + validate_file(<<-EOS +[section1] +; This is also a comment +bar = barvalue +master = true +[section2] + +foo= foovalue2 +baz=bazvalue +url = http://192.168.1.1:8080 +[section:sub] +subby=bar + #another comment + ; yet another comment +EOS + ) + end end end -- cgit v1.2.3 From cda30a6ab9b6fa673ead091ab0b130cbc6e75dbc Mon Sep 17 00:00:00 2001 From: Chris Price Date: Wed, 17 Oct 2012 13:27:28 -0700 Subject: Minor tweaks to handling of removing settings This commit makes some minor changes to how we handle removing settings. In particular, it updates all of the line numbers of the various 'section' objects to correspond to the new state of the world based on the removal of a line that appeared before them. Also adds one more test related to setting removal. --- lib/puppet/util/ini_file.rb | 26 +++++++++++++++++++--- lib/puppet/util/ini_file/section.rb | 18 ++++++++++++++- spec/unit/puppet/provider/ini_setting/ruby_spec.rb | 26 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) (limited to 'spec/unit/puppet/provider/ini_setting') diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb index 107924d..52ad32c 100644 --- a/lib/puppet/util/ini_file.rb +++ b/lib/puppet/util/ini_file.rb @@ -45,8 +45,19 @@ module Util def remove_setting(section_name, setting) section = @sections_hash[section_name] if (section.has_existing_setting?(setting)) + # If the setting is found, we have some work to do. + # First, we remove the line from our array of lines: remove_line(section, setting) + + # Then, we need to tell the setting object to remove + # the setting from its state: section.remove_existing_setting(setting) + + # Finally, we need to update all of the start/end line + # numbers for all of the sections *after* the one that + # was modified. + section_index = @section_names.index(section_name) + decrement_section_line_numbers(section_index + 1) end end @@ -61,9 +72,7 @@ module Util fh.puts("\n[#{section.name}]") elsif ! section.end_line.nil? (section.start_line..section.end_line).each do |line_num| - if lines[line_num] - fh.puts(lines[line_num]) - end + fh.puts(lines[line_num]) end end @@ -153,6 +162,17 @@ module Util File.readlines(path) end + + # Utility method; given a section index (index into the @section_names + # array), decrement the start/end line numbers for that section and all + # all of the other sections that appear *after* the specified section. + def decrement_section_line_numbers(section_index) + @section_names[section_index..(@section_names.length - 1)].each do |name| + section = @sections_hash[name] + section.decrement_line_nums + end + end + end end end diff --git a/lib/puppet/util/ini_file/section.rb b/lib/puppet/util/ini_file/section.rb index e192d5b..16f19d3 100644 --- a/lib/puppet/util/ini_file/section.rb +++ b/lib/puppet/util/ini_file/section.rb @@ -25,13 +25,29 @@ class IniFile end def remove_existing_setting(setting_name) - @existing_settings.delete(setting_name) + if (@existing_settings.delete(setting_name)) + if @end_line + @end_line = @end_line - 1 + end + end end def set_additional_setting(setting_name, value) @additional_settings[setting_name] = value end + # Decrement the start and end line numbers for the section (if they are + # defined); this is intended to be called when a setting is removed + # from a section that comes before this section in the ini file. + def decrement_line_nums() + if @start_line + @start_line = @start_line - 1 + end + if @end_line + @end_line = @end_line - 1 + end + 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 c8fcec4..8b2f8e5 100644 --- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb @@ -505,6 +505,32 @@ subby=bar EOS ) end + + it "should do nothing for a setting that does not exist" do + resource = Puppet::Type::Ini_setting.new(common_params.merge( + :section => 'section:sub', :setting => 'foo', :ensure => 'absent')) + provider = described_class.new(resource) + provider.exists?.should be_nil + provider.destroy + validate_file(<<-EOS +[section1] +; This is also a comment +foo=foovalue + +bar = barvalue +master = true +[section2] + +foo= foovalue2 +baz=bazvalue +url = http://192.168.1.1:8080 +[section:sub] +subby=bar + #another comment + ; yet another comment + EOS + ) + end end end -- cgit v1.2.3