Skip to content

Commit 725d59d

Browse files
committed
Keep sections when they become empty
When the last setting of a section was removed, the whole section was removed unless it contained white space of comments. In #532, this was changed to also remove sections that only contained space (blank lines), but it caused regressions and was reverted in #535. For consistency, we completely suppress the auto-removal of "empty" sections: removing the last setting of a section will not remove this section anymore, just like what happens for sections with only blank lines and comments.
1 parent 32b5f3c commit 725d59d

File tree

6 files changed

+11
-16
lines changed

6 files changed

+11
-16
lines changed

lib/puppet/util/ini_file.rb

-8
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,6 @@ def remove_setting(section_name, setting)
122122
# was modified.
123123
section_index = @section_names.index(section_name)
124124
decrement_section_line_numbers(section_index + 1)
125-
126-
return unless section.empty?
127-
128-
# By convention, it's time to remove this newly emptied out section
129-
lines.delete_at(section.start_line)
130-
decrement_section_line_numbers(section_index + 1)
131-
@section_names.delete_at(section_index)
132-
@sections_hash.delete(section.name)
133125
end
134126

135127
def save

lib/puppet/util/ini_file/section.rb

-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def existing_setting?(setting_name)
4747
@existing_settings.key?(setting_name)
4848
end
4949

50-
# the global section is empty whenever it's new;
51-
# other sections are empty when they have no lines
5250
def empty?
5351
global? ? new_section? : start_line == end_line
5452
end

spec/acceptance/ini_setting_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
subject { super().content }
9898

9999
it { is_expected.to match %r{four = five} }
100-
it { is_expected.not_to match %r{\[one\]} }
100+
it { is_expected.to match %r{^\[one\]$} }
101101
it { is_expected.not_to match %r{two = three} }
102102
end
103103
end
@@ -296,7 +296,8 @@
296296
describe '#content' do
297297
subject { super().content }
298298

299-
it { is_expected.to be_empty }
299+
it { is_expected.to match %r{^\[section1\]$} }
300+
it { is_expected.not_to match %r{valueinsection1 = newValue} }
300301
end
301302
end
302303
end

spec/acceptance/ini_subsetting_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
describe '#content' do
131131
subject { super().content }
132132

133-
it { is_expected.not_to match %r{\[one\]} }
133+
it { is_expected.to match %r{^\[one\]$} }
134134
it { is_expected.not_to match %r{key =} }
135135
it { is_expected.not_to match %r{alphabet} }
136136
it { is_expected.not_to match %r{betatrons} }

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ def self.file_path
10021002
#another comment
10031003
; yet another comment
10041004
1005+
-nonstandard-
10051006
INIFILE
10061007
it 'removes a setting with pre/suffix that exists' do
10071008
resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'nonstandard', setting: 'shoes', ensure: 'absent', section_prefix: '-', section_suffix: '-'))
@@ -1124,6 +1125,7 @@ def self.file_path
11241125
[section3]
11251126
# com = ment
11261127
uncom = ment
1128+
[section4]
11271129
[section:sub]
11281130
subby=bar
11291131
#another comment
@@ -1132,7 +1134,7 @@ def self.file_path
11321134
-nonstandard-
11331135
shoes = purple
11341136
INIFILE
1135-
it 'removes the section when removing the last line in the section' do
1137+
it 'does not remove the section when removing the last line in the section' do
11361138
resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section4', setting: 'uncom', ensure: 'absent'))
11371139
provider = described_class.new(resource)
11381140
expect(provider.exists?).to be true

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,11 @@ def validate_file(expected_content, tmpfile)
345345
something = else
346346
INIFILE
347347

348-
expected_content_two = ''
348+
expected_content_two = <<-INIFILE
349+
[main]
350+
INIFILE
349351

350-
it 'removes the subsetting when the it is empty' do
352+
it 'removes the subsetting and not the section when the list is empty' do
351353
resource = Puppet::Type::Ini_subsetting.new(common_params.merge(setting: 'reports', subsetting: 'http', subsetting_separator: ','))
352354
provider = described_class.new(resource)
353355
provider.destroy

0 commit comments

Comments
 (0)