Commit 7696b88d by Daniel Evans

Adding tests and logic to ensure that tempfiles are closed and unlinked after…

Adding tests and logic to ensure that tempfiles are closed and unlinked after queued_for_writes finishes uploading/saving
parent 74784558
...@@ -420,5 +420,13 @@ module Paperclip ...@@ -420,5 +420,13 @@ module Paperclip
end end
end end
# called by storage after the writes are flushed and before @queued_for_writes is cleared
def after_flush_writes
@queued_for_write.each do |style, file|
file.close unless file.closed?
file.unlink if file.respond_to?(:unlink) && File.exist?(file.path)
end
end
end end
end end
...@@ -46,6 +46,9 @@ module Paperclip ...@@ -46,6 +46,9 @@ module Paperclip
end end
FileUtils.chmod(0666&~File.umask, path(style_name)) FileUtils.chmod(0666&~File.umask, path(style_name))
end end
after_flush_writes # allows attachment to clean up temp files
@queued_for_write = {} @queued_for_write = {}
end end
......
...@@ -82,6 +82,9 @@ module Paperclip ...@@ -82,6 +82,9 @@ module Paperclip
retry retry
end end
end end
after_flush_writes # allows attachment to clean up temp files
@queued_for_write = {} @queued_for_write = {}
end end
......
...@@ -196,6 +196,9 @@ module Paperclip ...@@ -196,6 +196,9 @@ module Paperclip
raise raise
end end
end end
after_flush_writes # allows attachment to clean up temp files
@queued_for_write = {} @queued_for_write = {}
end end
......
...@@ -24,6 +24,14 @@ class FogTest < Test::Unit::TestCase ...@@ -24,6 +24,14 @@ class FogTest < Test::Unit::TestCase
assert_equal File.expand_path(File.join(File.dirname(__FILE__), "../public/avatars/5k.png")), assert_equal File.expand_path(File.join(File.dirname(__FILE__), "../public/avatars/5k.png")),
@dummy.avatar.path @dummy.avatar.path
end end
should "clean up file objects" do
File.stubs(:exist?).returns(true)
Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
@dummy.save!
end
end end
setup do setup do
......
...@@ -29,6 +29,14 @@ class StorageTest < Test::Unit::TestCase ...@@ -29,6 +29,14 @@ class StorageTest < Test::Unit::TestCase
@dummy.save @dummy.save
assert File.exists?(@dummy.avatar.path(:thumbnail)) assert File.exists?(@dummy.avatar.path(:thumbnail))
end end
should "clean up file objects" do
File.stubs(:exist?).returns(true)
Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
@dummy.save!
end
end end
context "Parsing S3 credentials" do context "Parsing S3 credentials" do
...@@ -349,6 +357,15 @@ class StorageTest < Test::Unit::TestCase ...@@ -349,6 +357,15 @@ class StorageTest < Test::Unit::TestCase
end end
end end
should "delete tempfiles" do
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png', :access => :public_read)
File.stubs(:exist?).returns(true)
Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
@dummy.save!
end
context "and saved without a bucket" do context "and saved without a bucket" do
setup do setup do
class AWS::S3::NoSuchBucket < AWS::S3::ResponseError class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment