Fix and test recursive_thumbnail

This commit is contained in:
Vasily Fedoseyev
2024-04-10 19:00:55 +03:00
parent c4009e1ca1
commit 6c99a6f052
2 changed files with 51 additions and 7 deletions

View File

@@ -7,14 +7,24 @@ module Paperclip
# если по каким-то причинам не сформировался файл прыдущего размера - генерим из оригинального
source_file = begin
attachment.to_file(source_style)
rescue
rescue StandardError
nil
end
unless source_file
Paperclip.log "Using original for #{options}"
file
source_file = file
end
@original_file = file
super(source_file, options, attachment)
end
def make
super
ensure
if source_file != file && source_file.respond_to?(:close!) && !attachment&.queued_for_write&.value?(source_file)
source_file.close!
if @file != @original_file && @file.respond_to?(:close!) && !attachment&.queued_for_write&.value?(@file)
@file.close!
end
end
end

View File

@@ -0,0 +1,34 @@
# frozen_string_literal: true
require 'test_helper'
class RecursiveThumbnailTest < Test::Unit::TestCase
setup do
Paperclip::Geometry.stubs from_file: Paperclip::Geometry.parse('1x1')
@original_file = stub("originalfile", path: 'originalfile.txt')
@attachment = attachment({})
end
should "use original when style not present" do
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1'}, @attachment)
assert_equal @original_file, processor.file
end
should "use original when style failed to download" do
@attachment.expects(:to_file).with(:missing).raises("cannot haz filez")
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1'}, @attachment)
assert_equal @original_file, processor.file
end
should "use style when present" do
style_file = stub("stylefile", path: 'style.txt')
style_file.expects(:close!).once
@original_file.expects(:close!).never
@attachment.expects(:to_file).with(:existent).returns(style_file)
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :existent, geometry: '1x1'}, @attachment)
Paperclip.stubs run: ""
assert_equal style_file, processor.file
res = processor.make
assert_equal Tempfile, res.class
end
end