Ver Fonte

Better handling of whitespace lines at ends of sections

This is another bit of cosmetic functionality; prior to
this commit, when adding a new setting to a section, we'd
write it on the very last line before the next section,
even if there was a chunk of trailing whitespace lines
at the end of the existing section.  This was functional,
but the output was not always particularly pleasant for
human consumption.

This commit tweaks things so that we insert new settings
just before the final chunk of whitespace lines in an
existing section.  This keeps things a bit cleaner.
Chris Price há 11 anos atrás
pai
commit
845fa707be

+ 45 - 2
lib/puppet/util/ini_file.rb

@@ -64,21 +64,57 @@ module Util
     def save
       File.open(@path, 'w') do |fh|
 
-        @section_names.each do |name|
+        @section_names.each_index do |index|
+          name = @section_names[index]
 
           section = @sections_hash[name]
 
+          # We need a buffer to cache lines that are only whitespace
+          whitespace_buffer = []
+
           if section.start_line.nil?
             fh.puts("\n[#{section.name}]")
           elsif ! section.end_line.nil?
             (section.start_line..section.end_line).each do |line_num|
-              fh.puts(lines[line_num])
+              line = lines[line_num]
+
+              # We buffer any lines that are only whitespace so that
+              # if they are at the end of a section, we can insert
+              # any new settings *before* the final chunk of whitespace
+              # lines.
+              if (line =~ /^\s*$/)
+                whitespace_buffer << line
+              else
+                # If we get here, we've found a non-whitespace line.
+                # We'll flush any cached whitespace lines before we
+                # write it.
+                flush_buffer_to_file(whitespace_buffer, fh)
+                fh.puts(line)
+              end
             end
           end
 
           section.additional_settings.each_pair do |key, value|
             fh.puts("#{' ' * (section.indentation || 0)}#{key}#{@key_val_separator}#{value}")
           end
+
+          if (whitespace_buffer.length > 0)
+            flush_buffer_to_file(whitespace_buffer, fh)
+          else
+            # We get here if there were no blank lines at the end of the
+            # section.
+            #
+            # If we are adding a new section with a new setting,
+            # and if there are more sections that come after this one,
+            # we'll write one blank line just so that there is a little
+            # whitespace between the sections.
+            if (section.end_line.nil? &&
+                (section.additional_settings.length > 0) &&
+                (index < @section_names.length - 1))
+              fh.puts("")
+            end
+          end
+
         end
       end
     end
@@ -176,6 +212,13 @@ module Util
       end
     end
 
+    def flush_buffer_to_file(buffer, fh)
+      if buffer.length > 0
+        buffer.each { |l| fh.puts(l) }
+        buffer.clear
+      end
+    end
+
   end
 end
 end

+ 2 - 1
spec/unit/puppet/provider/ini_setting/ruby_spec.rb

@@ -361,6 +361,7 @@ foo = http://192.168.1.1:8080
       provider.create
       validate_file(<<-EOS
 foo = yippee
+
 [section2]
 foo = http://192.168.1.1:8080
       EOS
@@ -571,8 +572,8 @@ subby=bar
 
      bar = barvalue
      master = true
-
      yahoo = yippee
+
 [section2]
   foo= foovalue2
   baz=bazvalue