Skip to content

Commit df37025

Browse files
authored
Merge pull request #532 from puppetlabs/CAT-1646-remove-section-when-its-empty
(CAT-1646) - Remove section if it has empty line but does not have any settings
2 parents 94a3c57 + 4ebe030 commit df37025

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

lib/puppet/util/ini_file.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,24 @@ 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)
228229
# the global section always exists, even when it's empty;
229230
# when it's empty, we must be sure it's thought of as new,
230231
# which is signalled with a nil ending line
231232
end_line_num = nil if name == '' && empty
232-
return Section.new(name, start_line, end_line_num, settings, min_indentation)
233+
return Section.new(name, start_line, end_line_num, settings, min_indentation, empty_line_count)
233234
end
234235
if (match = @setting_regex.match(line))
235236
settings[match[2]] = match[4]
236237
indentation = match[1].length
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)

spec/unit/puppet/provider/ini_setting/ruby_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,38 @@ def self.file_path
11411141
end
11421142
end
11431143

1144+
context 'when section has only empty line' do
1145+
let(:orig_content) do
1146+
<<~INIFILE
1147+
[section1]
1148+
foo=foovalue
1149+
1150+
1151+
[section2]
1152+
1153+
foo= foovalue2
1154+
baz=bazvalue
1155+
url = http://
1156+
INIFILE
1157+
end
1158+
1159+
expected_content = <<~INIFILE
1160+
[section2]
1161+
1162+
foo= foovalue2
1163+
baz=bazvalue
1164+
url = http://
1165+
INIFILE
1166+
1167+
it 'remove empty section' do
1168+
resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'foo', ensure: 'absent'))
1169+
provider = described_class.new(resource)
1170+
expect(provider.exists?).to be true
1171+
provider.destroy
1172+
validate_file(expected_content, tmpfile)
1173+
end
1174+
end
1175+
11441176
context 'when dealing with indentation in sections' do
11451177
let(:orig_content) do
11461178
<<~INIFILE

0 commit comments

Comments
 (0)