From 676c124f8440adff22f6fcfe7641b21669137bdb Mon Sep 17 00:00:00 2001 From: alpaca-tc Date: Wed, 16 Aug 2023 12:03:28 +0900 Subject: [PATCH 1/2] fix wrong condition follow-up #435 `File.extname(f)` returns '.gz' but `config.compression` is 'gz' --- lib/asset_sync/storage.rb | 2 +- spec/unit/storage_spec.rb | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/asset_sync/storage.rb b/lib/asset_sync/storage.rb index 28232e9..879d793 100644 --- a/lib/asset_sync/storage.rb +++ b/lib/asset_sync/storage.rb @@ -264,7 +264,7 @@ def upload_file(f) if config.compression compressed_name = "#{path}/#{f}.#{config.compression}" - if File.extname(f) == config.compression + if File.extname(f)[1..-1] == config.compression # Don't bother uploading compressed assets if we are in compression mode # as we will overwrite file.css with file.css.gz if it exists. log "Ignoring: #{f}" diff --git a/spec/unit/storage_spec.rb b/spec/unit/storage_spec.rb index cc7d1d3..1d68768 100644 --- a/spec/unit/storage_spec.rb +++ b/spec/unit/storage_spec.rb @@ -393,6 +393,46 @@ def check_file(file) end storage.upload_file('assets/some_longer_path/local_image2.jpg') end + + context 'config.gzip_compression is enabled' do + context 'when the file is a css file' do + it 'should upload the file' do + @config.gzip_compression = true + + storage = AssetSync::Storage.new(@config) + allow(storage).to receive(:get_local_files).and_return(@local_files) + allow(storage).to receive(:get_remote_files).and_return(@remote_files) + # Pretend they all exist + allow(File).to receive(:open).and_return(file_like_object) + bucket = double + files = double + allow(storage).to receive(:bucket).and_return(bucket) + allow(bucket).to receive(:files).and_return(files) + + expect(files).to receive(:create).with({ body: file_like_object, content_type: "text/css", key: "assets/local.css", public: true }).once + storage.upload_file('assets/local.css') + end + end + + context 'when the file is a gz file' do + it 'should not upload the file' do + @config.gzip_compression = true + + storage = AssetSync::Storage.new(@config) + allow(storage).to receive(:get_local_files).and_return(@local_files) + allow(storage).to receive(:get_remote_files).and_return(@remote_files) + # Pretend they all exist + allow(File).to receive(:open).and_return(file_like_object) + bucket = double + files = double + allow(storage).to receive(:bucket).and_return(bucket) + allow(bucket).to receive(:files).and_return(files) + + expect(files).to_not receive(:create) + storage.upload_file('assets/local.css.gz') + end + end + end end describe '#delete_extra_remote_files' do From f2ecf240ea6501e90fd997b704cfea768229a3ee Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Wed, 16 Aug 2023 11:14:11 +0800 Subject: [PATCH 2/2] Update lib/asset_sync/storage.rb --- lib/asset_sync/storage.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/asset_sync/storage.rb b/lib/asset_sync/storage.rb index 879d793..fdac4ab 100644 --- a/lib/asset_sync/storage.rb +++ b/lib/asset_sync/storage.rb @@ -264,6 +264,7 @@ def upload_file(f) if config.compression compressed_name = "#{path}/#{f}.#{config.compression}" + # `File.extname` returns value with `.` prefix, `config.compression` contains value without `.` if File.extname(f)[1..-1] == config.compression # Don't bother uploading compressed assets if we are in compression mode # as we will overwrite file.css with file.css.gz if it exists.