Skip to content

Commit

Permalink
Revert "(CAT-1646) - Remove section if its not have any settings"
Browse files Browse the repository at this point in the history
PR #532 introduced 4ebe030 breaks
backwards compatibility and is part of a patch release, as stated here:
#532 (comment)

This reverts commit 4ebe030.

This will allow us to do a new patch release that fix the unexpected
backwards incompatible change, and leave the room to implement this as
part of a next major version of the module.
  • Loading branch information
smortex committed Mar 31, 2024
1 parent a28847c commit 54a1732
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 39 deletions.
5 changes: 1 addition & 4 deletions lib/puppet/util/ini_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,24 +222,21 @@ def read_section(name, start_line, line_iter)
end_line_num = start_line
min_indentation = nil
empty = true
empty_line_count = 0
loop do
line, line_num = line_iter.peek
if line_num.nil? || @section_regex.match(line)
# the global section always exists, even when it's empty;
# when it's empty, we must be sure it's thought of as new,
# which is signalled with a nil ending line
end_line_num = nil if name == '' && empty
return Section.new(name, start_line, end_line_num, settings, min_indentation, empty_line_count)
return Section.new(name, start_line, end_line_num, settings, min_indentation)
end
if (match = @setting_regex.match(line))
settings[match[2]] = match[4]
indentation = match[1].length
min_indentation = [indentation, min_indentation || indentation].min
end
end_line_num = line_num
empty_line_count += 1 if line == "\n"

empty = false
line_iter.next
end
Expand Down
5 changes: 2 additions & 3 deletions lib/puppet/util/ini_file/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ class Section
# `end_line` of `nil`.
# * `start_line` and `end_line` will be set to `nil` for a new non-global
# section.
def initialize(name, start_line, end_line, settings, indentation, empty_line_count = 0)
def initialize(name, start_line, end_line, settings, indentation)
@name = name
@start_line = start_line
@end_line = end_line
@existing_settings = settings.nil? ? {} : settings
@additional_settings = {}
@indentation = indentation
@empty_line_count = empty_line_count
end

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

def update_existing_setting(setting_name, value)
Expand Down
32 changes: 0 additions & 32 deletions spec/unit/puppet/provider/ini_setting/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1141,38 +1141,6 @@ def self.file_path
end
end

context 'when section has only empty line' do
let(:orig_content) do
<<~INIFILE
[section1]
foo=foovalue
[section2]
foo= foovalue2
baz=bazvalue
url = http://
INIFILE
end

expected_content = <<~INIFILE
[section2]
foo= foovalue2
baz=bazvalue
url = http://
INIFILE

it 'remove empty section' do
resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section1', setting: 'foo', ensure: 'absent'))
provider = described_class.new(resource)
expect(provider.exists?).to be true
provider.destroy
validate_file(expected_content, tmpfile)
end
end

context 'when dealing with indentation in sections' do
let(:orig_content) do
<<~INIFILE
Expand Down

0 comments on commit 54a1732

Please sign in to comment.