whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [whimsy] branch master updated: Skip update if block returns nil
Date Sat, 17 Oct 2020 11:06:29 GMT
This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new 28b8819  Skip update if block returns nil
28b8819 is described below

commit 28b88193f7057258ae4c43aaf7c884ea606b60db
Author: Sebb <sebb@apache.org>
AuthorDate: Sat Oct 17 12:06:21 2020 +0100

    Skip update if block returns nil
---
 lib/spec/lib/yaml_spec.rb | 16 ++++++++++++++--
 lib/whimsy/asf/yaml.rb    | 23 +++++++++++++++++------
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/lib/spec/lib/yaml_spec.rb b/lib/spec/lib/yaml_spec.rb
index 93f1599..d4a5ed4 100644
--- a/lib/spec/lib/yaml_spec.rb
+++ b/lib/spec/lib/yaml_spec.rb
@@ -29,16 +29,20 @@ describe YamlFile do
       yaml = YamlFile.read(workfile)
       expect(yaml.size).to equal(3)
     end
-    it "should fail with missing section" do
+    it "should fail with missing section and not update file" do
+      mtime = File.mtime workfile
       expect do
         YamlFile.update_section(workfile, 'none') {|yaml| yaml}
       end.to raise_error(ArgumentError)
+      expect(File.mtime(workfile)).to eql(mtime)
     end
-    it "should find 2 entries" do
+    it "should find 2 entries and touch file" do
+      mtime = File.mtime workfile
       YamlFile.update_section(workfile, :key1) do |yaml|
         expect(yaml.size).to eql(2)
         yaml # return it unchanged
       end
+      expect(File.mtime(workfile)).to be > mtime
     end
     # check it is still OK after dummy update
     it "read should return 3 entries" do
@@ -48,6 +52,14 @@ describe YamlFile do
     it "should be unchanged" do
       expect(File.read(testfile)).to eql(File.read(workfile))
     end
+    it "should not touch file if nil returned" do
+      mtime = File.mtime workfile
+      YamlFile.update_section(workfile, :key1) do |yaml|
+        expect(yaml.size).to eql(2)
+        nil # return it unchanged
+      end
+      expect(File.mtime(workfile)).to eql(mtime)
+    end
   end
   describe "YamlFile.update" do
     it "should create empty file" do
diff --git a/lib/whimsy/asf/yaml.rb b/lib/whimsy/asf/yaml.rb
index a118c83..b287b17 100644
--- a/lib/whimsy/asf/yaml.rb
+++ b/lib/whimsy/asf/yaml.rb
@@ -28,6 +28,11 @@ module YamlFile
 
   # replace a section of YAML text whilst preserving surrounding data including comments.
   # The args are passed to YAML.safe_load, and default to [Symbol]
+  # The caller must provide a block, which is passed two JSON parameters:
+  # - the section related to the key
+  # - the entire file (this is for validation purposes)
+  # Returns the updated text. If the block returns nil, returns nil so the
+  # caller can skip the file update
   def self.replace_section(content, key, *args)
     raise ArgumentError, 'block is required' unless block_given?
 
@@ -39,11 +44,15 @@ module YamlFile
       raise ArgumentError, "Could not find section #{key.inspect}"
     end
 
-    output = content.dup
+    res = yield(section, yaml) # get the updated JSON
+
+    return nil if res.nil? # i.e. don't update text
+  
+    output = content.dup # don't mutate caller data
 
     # Create the updated section with the correct indentation
     # Use YAML dump to ensure correct syntax; drop the YAML header
-    new_section = YAML.dump({key => yield(section, yaml)}).sub(/\A---\n/, '')
+    new_section = YAML.dump({key => res}).sub(/\A---\n/, '')
 
     # replace the old section with the new one
     # assume it is delimited by the key and '...' or another key.
@@ -69,10 +78,12 @@ module YamlFile
 
       content = replace_section(file.read, key, *args, &block)
 
-      # rewrite the file
-      file.rewind
-      file.write content
-      file.truncate(file.pos)
+      unless content.nil?
+        # rewrite the file
+        file.rewind
+        file.write content
+        file.truncate(file.pos)
+      end
     end
   end
 


Mime
View raw message