Skip to content

Commit b7ed9dd

Browse files
authored
Merge pull request #536 from puppetlabs/524-ini_setting-keep-sections
Keep sections when they become empty
2 parents 2f77497 + 810a3a7 commit b7ed9dd

6 files changed

Lines changed: 11 additions & 82 deletions

File tree

lib/puppet/util/ini_file.rb

Lines changed: 1 addition & 12 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
@@ -222,24 +214,21 @@ def read_section(name, start_line, line_iter)
222214
end_line_num = start_line
223215
min_indentation = nil
224216
empty = true
225-
empty_line_count = 0
226217
loop do
227218
line, line_num = line_iter.peek
228219
if line_num.nil? || @section_regex.match(line)
229220
# the global section always exists, even when it's empty;
230221
# when it's empty, we must be sure it's thought of as new,
231222
# which is signalled with a nil ending line
232223
end_line_num = nil if name == '' && empty
233-
return Section.new(name, start_line, end_line_num, settings, min_indentation, empty_line_count)
224+
return Section.new(name, start_line, end_line_num, settings, min_indentation)
234225
end
235226
if (match = @setting_regex.match(line))
236227
settings[match[2]] = match[4]
237228
indentation = match[1].length
238229
min_indentation = [indentation, min_indentation || indentation].min
239230
end
240231
end_line_num = line_num
241-
empty_line_count += 1 if line == "\n"
242-
243232
empty = false
244233
line_iter.next
245234
end

lib/puppet/util/ini_file/section.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ 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, empty_line_count = 0)
17+
def initialize(name, start_line, end_line, settings, indentation)
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
2524
end
2625

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

5756
def update_existing_setting(setting_name, value)

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 & 63 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,69 +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
1142-
end
1143-
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
11741112
end
11751113

11761114
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)