Browse Source

Fixes #5012 - Package: Missing backup files for files with the same content.

Co-authored-by: Dominik Klein <dk@zammad.com>
Rolf Schmidt 1 year ago
parent
commit
b6d5f7b43f
2 changed files with 32 additions and 3 deletions
  1. 4 3
      app/models/package.rb
  2. 28 0
      spec/models/package_spec.rb

+ 4 - 3
app/models/package.rb

@@ -436,12 +436,13 @@ execute all pending package migrations at once
 
 
     # rename existing file if not already the same file
     # rename existing file if not already the same file
     if File.exist?(location)
     if File.exist?(location)
-      content_fs = _read_file(file)
-      if content_fs == data
+      backup_location = "#{location}.save"
+      content_fs      = _read_file(file)
+      if content_fs == data && File.exist?(backup_location)
         logger.debug { "NOTICE: file '#{location}' already exists, skip install" }
         logger.debug { "NOTICE: file '#{location}' already exists, skip install" }
         return true
         return true
       end
       end
-      backup_location = "#{location}.save"
+
       logger.info "NOTICE: backup old file '#{location}' to #{backup_location}"
       logger.info "NOTICE: backup old file '#{location}' to #{backup_location}"
       File.rename(location, backup_location)
       File.rename(location, backup_location)
     end
     end

+ 28 - 0
spec/models/package_spec.rb

@@ -286,4 +286,32 @@ RSpec.describe Package, type: :model do
       expect(described_class.last.url).to eq('https://zammad.org/')
       expect(described_class.last.url).to eq('https://zammad.org/')
     end
     end
   end
   end
+
+  describe 'Package: Missing backup files for files with the same content #5012' do
+    let(:package_v1_files) do
+      <<-JSON
+        [
+          {
+            "permission": "644",
+            "location": "lib/version.rb",
+            "content": "#{Base64.strict_encode64(File.read('lib/version.rb')).strip}"
+          }
+        ]
+      JSON
+    end
+    let(:package_v2_files) do
+      <<-JSON
+        []
+      JSON
+    end
+
+    let(:package_v1) { get_package_structure(package_name, package_v1_files, '1.0.0') }
+    let(:package_v2) { get_package_structure(package_name, package_v2_files, '1.0.1') }
+
+    it 'does not lose core files when patched by package and released in future updates of zammad' do
+      described_class.install(string: package_v1)
+      described_class.install(string: package_v2)
+      expect(File.exist?('lib/version.rb')).to be(true)
+    end
+  end
 end
 end