Skip to content

Commit ac3f688

Browse files
committed
(CAT-1646) - Remove section if its not have any settings
1 parent 94a3c57 commit ac3f688

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/puppet/util/ini_file.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def read_section(name, start_line, line_iter)
222222
end_line_num = start_line
223223
min_indentation = nil
224224
empty = true
225+
empty_line_count = 0
225226
loop do
226227
line, line_num = line_iter.peek
227228
if line_num.nil? || @section_regex.match(line)
@@ -237,6 +238,8 @@ def read_section(name, start_line, line_iter)
237238
min_indentation = [indentation, min_indentation || indentation].min
238239
end
239240
end_line_num = line_num
241+
empty_line_count += 1 if line == "\n"
242+
240243
empty = false
241244
line_iter.next
242245
end

lib/puppet/util/ini_file/section.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ class Section
1414
# `end_line` of `nil`.
1515
# * `start_line` and `end_line` will be set to `nil` for a new non-global
1616
# section.
17-
def initialize(name, start_line, end_line, settings, indentation)
17+
def initialize(name, start_line, end_line, settings, indentation, empty_line_count = 0)
1818
@name = name
1919
@start_line = start_line
2020
@end_line = end_line
2121
@existing_settings = settings.nil? ? {} : settings
2222
@additional_settings = {}
2323
@indentation = indentation
24+
@empty_line_count = empty_line_count
2425
end
2526

2627
attr_reader :name, :start_line, :end_line, :additional_settings, :indentation
@@ -50,7 +51,7 @@ def existing_setting?(setting_name)
5051
# the global section is empty whenever it's new;
5152
# other sections are empty when they have no lines
5253
def empty?
53-
global? ? new_section? : start_line == end_line
54+
global? ? new_section? : (start_line == end_line || (end_line && end_line - @empty_line_count) == start_line)
5455
end
5556

5657
def update_existing_setting(setting_name, value)

0 commit comments

Comments
 (0)