From ac3f688f0b0b53f0767f71643ba449513b8c3900 Mon Sep 17 00:00:00 2001 From: Ramesh Sencha Date: Thu, 7 Mar 2024 14:39:27 +0530 Subject: [PATCH] (CAT-1646) - Remove section if its not have any settings --- lib/puppet/util/ini_file.rb | 3 +++ lib/puppet/util/ini_file/section.rb | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb index b7529812..3e74ea52 100644 --- a/lib/puppet/util/ini_file.rb +++ b/lib/puppet/util/ini_file.rb @@ -222,6 +222,7 @@ def read_section(name, start_line, line_iter) end_line_num = start_line min_indentation = nil empty = true + empty_line_count = 0 loop do line, line_num = line_iter.peek if line_num.nil? || @section_regex.match(line) @@ -237,6 +238,8 @@ def read_section(name, start_line, line_iter) min_indentation = [indentation, min_indentation || indentation].min end end_line_num = line_num + empty_line_count += 1 if line == "\n" + empty = false line_iter.next end diff --git a/lib/puppet/util/ini_file/section.rb b/lib/puppet/util/ini_file/section.rb index 46e01477..c8ae886a 100644 --- a/lib/puppet/util/ini_file/section.rb +++ b/lib/puppet/util/ini_file/section.rb @@ -14,13 +14,14 @@ class Section # `end_line` of `nil`. # * `start_line` and `end_line` will be set to `nil` for a new non-global # section. - def initialize(name, start_line, end_line, settings, indentation) + def initialize(name, start_line, end_line, settings, indentation, empty_line_count = 0) @name = name @start_line = start_line @end_line = end_line @existing_settings = settings.nil? ? {} : settings @additional_settings = {} @indentation = indentation + @empty_line_count = empty_line_count end attr_reader :name, :start_line, :end_line, :additional_settings, :indentation @@ -50,7 +51,7 @@ def existing_setting?(setting_name) # the global section is empty whenever it's new; # other sections are empty when they have no lines def empty? - global? ? new_section? : start_line == end_line + global? ? new_section? : (start_line == end_line || (end_line && end_line - @empty_line_count) == start_line) end def update_existing_setting(setting_name, value)