Skip to content

Commit 085e4a9

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 085e4a9

File tree

5 files changed

+8
-43
lines changed

5 files changed

+8
-43
lines changed

lib/puppet/util/ini_file.rb

Lines changed: 0 additions & 8 deletions
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

spec/acceptance/ini_setting_spec.rb

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 31 deletions
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: '-'))
@@ -1108,37 +1109,6 @@ def self.file_path
11081109
provider.destroy
11091110
validate_file(expected_content_five, tmpfile)
11101111
end
1111-
1112-
expected_content_six = <<~INIFILE
1113-
[section1]
1114-
; This is also a comment
1115-
foo=foovalue
1116-
1117-
bar = barvalue
1118-
main = true
1119-
[section2]
1120-
1121-
foo= foovalue2
1122-
baz=bazvalue
1123-
url = http://192.168.1.1:8080
1124-
[section3]
1125-
# com = ment
1126-
uncom = ment
1127-
[section:sub]
1128-
subby=bar
1129-
#another comment
1130-
; yet another comment
1131-
1132-
-nonstandard-
1133-
shoes = purple
1134-
INIFILE
1135-
it 'removes the section when removing the last line in the section' do
1136-
resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section4', setting: 'uncom', ensure: 'absent'))
1137-
provider = described_class.new(resource)
1138-
expect(provider.exists?).to be true
1139-
provider.destroy
1140-
validate_file(expected_content_six, tmpfile)
1141-
end
11421112
end
11431113

11441114
context 'when dealing with indentation in sections' do

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ 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

350352
it 'removes the subsetting when the it is empty' do
351353
resource = Puppet::Type::Ini_subsetting.new(common_params.merge(setting: 'reports', subsetting: 'http', subsetting_separator: ','))

0 commit comments

Comments
 (0)