Commit 4f6d482d by Prem Sichanugrist

Close ALL the files

Make sure that we close opened files after we're done with them.
parent 02eb7259
...@@ -12,6 +12,10 @@ class AttachmentAdapterTest < Test::Unit::TestCase ...@@ -12,6 +12,10 @@ class AttachmentAdapterTest < Test::Unit::TestCase
@subject = Paperclip.io_adapters.for(@attachment) @subject = Paperclip.io_adapters.for(@attachment)
end end
def teardown
@file.close
end
should "get the right filename" do should "get the right filename" do
assert_equal "5k.png", @subject.original_filename assert_equal "5k.png", @subject.original_filename
end end
......
...@@ -1120,6 +1120,8 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -1120,6 +1120,8 @@ class AttachmentTest < Test::Unit::TestCase
@path = @attachment.path @path = @attachment.path
end end
teardown { @file.close }
should "not delete the files from storage when attachment is destroyed" do should "not delete the files from storage when attachment is destroyed" do
@attachment.destroy @attachment.destroy
assert File.exists?(@path) assert File.exists?(@path)
...@@ -1142,6 +1144,8 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -1142,6 +1144,8 @@ class AttachmentTest < Test::Unit::TestCase
@path = @attachment.path @path = @attachment.path
end end
teardown { @file.close }
should "not be deleted when the model fails to destroy" do should "not be deleted when the model fails to destroy" do
@dummy.stubs(:destroy).raises(Exception) @dummy.stubs(:destroy).raises(Exception)
......
...@@ -2,49 +2,58 @@ require './test/helper' ...@@ -2,49 +2,58 @@ require './test/helper'
class FileAdapterTest < Test::Unit::TestCase class FileAdapterTest < Test::Unit::TestCase
context "a new instance" do context "a new instance" do
setup do context "with normal file" do
@file = File.new(fixture_file("5k.png")) setup do
@file.binmode @file = File.new(fixture_file("5k.png"))
@subject = Paperclip.io_adapters.for(@file) @file.binmode
end @subject = Paperclip.io_adapters.for(@file)
end
should "get the right filename" do teardown { @file.close }
assert_equal "5k.png", @subject.original_filename
end
should "force binmode on tempfile" do should "get the right filename" do
assert @subject.instance_variable_get("@tempfile").binmode? assert_equal "5k.png", @subject.original_filename
end end
should "get the content type" do should "force binmode on tempfile" do
assert_equal "image/png", @subject.content_type assert @subject.instance_variable_get("@tempfile").binmode?
end end
should "provide correct mime-type for empty file" do should "get the content type" do
@subject = Paperclip.io_adapters.for(Tempfile.new("file_adapter_test")) assert_equal "image/png", @subject.content_type
end
# Content type contained '\n' at the end, for an empty file, on my Mac should "get the file's size" do
assert_equal "application/x-empty", @subject.content_type assert_equal 4456, @subject.size
end end
should "get the file's size" do should "return false for a call to nil?" do
assert_equal 4456, @subject.size assert ! @subject.nil?
end end
should "return false for a call to nil?" do should "generate a MD5 hash of the contents" do
assert ! @subject.nil? expected = Digest::MD5.file(@file.path).to_s
end assert_equal expected, @subject.fingerprint
end
should "generate a MD5 hash of the contents" do should "read the contents of the file" do
expected = Digest::MD5.file(@file.path).to_s expected = @file.read
assert_equal expected, @subject.fingerprint assert expected.length > 0
assert_equal expected, @subject.read
end
end end
should "read the contents of the file" do context "empty file" do
expected = @file.read setup do
assert expected.length > 0 @file = Tempfile.new("file_adapter_test")
assert_equal expected, @subject.read @subject = Paperclip.io_adapters.for(@file)
end end
teardown { @file.close }
should "provide correct mime-type" do
assert_equal "application/x-empty", @subject.content_type
end
end
end end
end end
...@@ -10,6 +10,8 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -10,6 +10,8 @@ class IntegrationTest < Test::Unit::TestCase
end end
end end
teardown { @file.close }
should "not exceed the open file limit" do should "not exceed the open file limit" do
assert_nothing_raised do assert_nothing_raised do
dummies = Dummy.find(:all) dummies = Dummy.find(:all)
...@@ -287,6 +289,7 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -287,6 +289,7 @@ class IntegrationTest < Test::Unit::TestCase
teardown do teardown do
File.umask @umask File.umask @umask
@file.close
end end
should "respect the current umask" do should "respect the current umask" do
...@@ -314,6 +317,8 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -314,6 +317,8 @@ class IntegrationTest < Test::Unit::TestCase
assert @dummy.save assert @dummy.save
end end
teardown { [@file, @bad_file].each(&:close) }
should "write and delete its files" do should "write and delete its files" do
[["434x66", :original], [["434x66", :original],
["300x46", :large], ["300x46", :large],
...@@ -407,6 +412,8 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -407,6 +412,8 @@ class IntegrationTest < Test::Unit::TestCase
@dummy2.save @dummy2.save
end end
teardown { @file2.close }
should "work when assigned a file" do should "work when assigned a file" do
assert_not_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`, assert_not_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`,
`identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"` `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"`
...@@ -427,16 +434,13 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -427,16 +434,13 @@ class IntegrationTest < Test::Unit::TestCase
has_many :attachments, :class_name => 'Dummy' has_many :attachments, :class_name => 'Dummy'
end end
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@dummy = Dummy.new @dummy = Dummy.new
@dummy.avatar = File.new(File.join(File.dirname(__FILE__), @dummy.avatar = @file
"fixtures",
"5k.png"), 'rb')
end end
should "should not error when saving" do should "should not error when saving" do
assert_nothing_raised do @dummy.save!
@dummy.save!
end
end end
end end
...@@ -482,6 +486,12 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -482,6 +486,12 @@ class IntegrationTest < Test::Unit::TestCase
@files_on_s3 = s3_files_for @dummy.avatar @files_on_s3 = s3_files_for @dummy.avatar
end end
teardown do
@file.close
@bad_file.close
@files_on_s3.values.each(&:close)
end
context 'assigning itself to a new model' do context 'assigning itself to a new model' do
setup do setup do
@d2 = Dummy.new @d2 = Dummy.new
......
...@@ -67,6 +67,8 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -67,6 +67,8 @@ class PaperclipTest < Test::Unit::TestCase
@expected = [d1, d3] @expected = [d1, d3]
end end
teardown { @file.close }
should "yield every instance of a model that has an attachment" do should "yield every instance of a model that has an attachment" do
actual = [] actual = []
Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance| Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
......
...@@ -2,25 +2,30 @@ require './test/helper' ...@@ -2,25 +2,30 @@ require './test/helper'
class FileSystemTest < Test::Unit::TestCase class FileSystemTest < Test::Unit::TestCase
context "Filesystem" do context "Filesystem" do
setup do context "normal file" do
rebuild_model :styles => { :thumbnail => "25x25#" } setup do
@dummy = Dummy.create! rebuild_model :styles => { :thumbnail => "25x25#" }
@dummy = Dummy.create!
@dummy.avatar = File.open(fixture_file('5k.png')) @file = File.open(fixture_file('5k.png'))
end @dummy.avatar = @file
end
should "allow file assignment" do teardown { @file.close }
assert @dummy.save
end
should "store the original" do should "allow file assignment" do
@dummy.save assert @dummy.save
assert File.exists?(@dummy.avatar.path) end
end
should "store the thumbnail" do should "store the original" do
@dummy.save @dummy.save
assert File.exists?(@dummy.avatar.path(:thumbnail)) assert File.exists?(@dummy.avatar.path)
end
should "store the thumbnail" do
@dummy.save
assert File.exists?(@dummy.avatar.path(:thumbnail))
end
end end
context "with file that has space in file name" do context "with file that has space in file name" do
...@@ -28,10 +33,13 @@ class FileSystemTest < Test::Unit::TestCase ...@@ -28,10 +33,13 @@ class FileSystemTest < Test::Unit::TestCase
rebuild_model :styles => { :thumbnail => "25x25#" } rebuild_model :styles => { :thumbnail => "25x25#" }
@dummy = Dummy.create! @dummy = Dummy.create!
@dummy.avatar = File.open(fixture_file('spaced file.png')) @file = File.open(fixture_file('spaced file.png'))
@dummy.avatar = @file
@dummy.save @dummy.save
end end
teardown { @file.close }
should "store the file" do should "store the file" do
assert File.exists?(@dummy.avatar.path) assert File.exists?(@dummy.avatar.path)
end end
......
...@@ -12,10 +12,13 @@ class FogTest < Test::Unit::TestCase ...@@ -12,10 +12,13 @@ class FogTest < Test::Unit::TestCase
:url => '/:attachment/:filename', :url => '/:attachment/:filename',
:fog_directory => "paperclip", :fog_directory => "paperclip",
:fog_credentials => fixture_file('fog.yml') :fog_credentials => fixture_file('fog.yml')
@file = File.new(fixture_file('5k.png'), 'rb')
@dummy = Dummy.new @dummy = Dummy.new
@dummy.avatar = File.new(fixture_file('5k.png'), 'rb') @dummy.avatar = @file
end end
teardown { @file.close }
should "have the proper information loading credentials from a file" do should "have the proper information loading credentials from a file" do
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS' assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
end end
...@@ -28,10 +31,13 @@ class FogTest < Test::Unit::TestCase ...@@ -28,10 +31,13 @@ class FogTest < Test::Unit::TestCase
:url => '/:attachment/:filename', :url => '/:attachment/:filename',
:fog_directory => "paperclip", :fog_directory => "paperclip",
:fog_credentials => File.open(fixture_file('fog.yml')) :fog_credentials => File.open(fixture_file('fog.yml'))
@file = File.new(fixture_file('5k.png'), 'rb')
@dummy = Dummy.new @dummy = Dummy.new
@dummy.avatar = File.new(fixture_file('5k.png'), 'rb') @dummy.avatar = @file
end end
teardown { @file.close }
should "have the proper information loading credentials from a file" do should "have the proper information loading credentials from a file" do
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS' assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
end end
...@@ -48,9 +54,13 @@ class FogTest < Test::Unit::TestCase ...@@ -48,9 +54,13 @@ class FogTest < Test::Unit::TestCase
:aws_access_key_id => 'AWS_ID', :aws_access_key_id => 'AWS_ID',
:aws_secret_access_key => 'AWS_SECRET' :aws_secret_access_key => 'AWS_SECRET'
} }
@file = File.new(fixture_file('5k.png'), 'rb')
@dummy = Dummy.new @dummy = Dummy.new
@dummy.avatar = File.new(fixture_file('5k.png'), 'rb') @dummy.avatar = @file
end end
teardown { @file.close }
should "be able to interpolate the path without blowing up" do should "be able to interpolate the path without blowing up" do
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
......
...@@ -5,11 +5,12 @@ unless ENV["S3_BUCKET"].blank? ...@@ -5,11 +5,12 @@ unless ENV["S3_BUCKET"].blank?
class S3LiveTest < Test::Unit::TestCase class S3LiveTest < Test::Unit::TestCase
context "when assigning an S3 attachment directly to another model" do context "when assigning an S3 attachment directly to another model" do
setup do setup do
@s3_credentials = File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml"))
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" }, rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
:storage => :s3, :storage => :s3,
:bucket => ENV["S3_BUCKET"], :bucket => ENV["S3_BUCKET"],
:path => ":class/:attachment/:id/:style.:extension", :path => ":class/:attachment/:id/:style.:extension",
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml")) :s3_credentials => @s3_credentials
@dummy = Dummy.new @dummy = Dummy.new
@attachment = Dummy.new.avatar @attachment = Dummy.new.avatar
...@@ -21,16 +22,18 @@ unless ENV["S3_BUCKET"].blank? ...@@ -21,16 +22,18 @@ unless ENV["S3_BUCKET"].blank?
@attachment2.assign(@file) @attachment2.assign(@file)
@attachment2.save @attachment2.save
end end
end
teardown { [@s3_credentials, @file].each(&:close) }
end
context "Generating an expiring url on a nonexistant attachment" do context "Generating an expiring url on a nonexistant attachment" do
setup do setup do
@s3_credentials = File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml"))
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" }, rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
:storage => :s3, :storage => :s3,
:bucket => ENV["S3_BUCKET"], :bucket => ENV["S3_BUCKET"],
:path => ":class/:attachment/:id/:style.:extension", :path => ":class/:attachment/:id/:style.:extension",
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml")) :s3_credentials => @s3_credentials
@dummy = Dummy.new @dummy = Dummy.new
end end
...@@ -41,16 +44,19 @@ unless ENV["S3_BUCKET"].blank? ...@@ -41,16 +44,19 @@ unless ENV["S3_BUCKET"].blank?
context "Using S3 for real, an attachment with S3 storage" do context "Using S3 for real, an attachment with S3 storage" do
setup do setup do
@s3_credentials = File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml"))
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" }, rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
:storage => :s3, :storage => :s3,
:bucket => ENV["S3_BUCKET"], :bucket => ENV["S3_BUCKET"],
:path => ":class/:attachment/:id/:style.:extension", :path => ":class/:attachment/:id/:style.:extension",
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml")) :s3_credentials => @s3_credentials
Dummy.delete_all Dummy.delete_all
@dummy = Dummy.new @dummy = Dummy.new
end end
teardown { @s3_credentials.close }
should "be extended by the S3 module" do should "be extended by the S3 module" do
assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3) assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
end end
...@@ -80,17 +86,21 @@ unless ENV["S3_BUCKET"].blank? ...@@ -80,17 +86,21 @@ unless ENV["S3_BUCKET"].blank?
context "An attachment that uses S3 for storage and has spaces in file name" do context "An attachment that uses S3 for storage and has spaces in file name" do
setup do setup do
@s3_credentials = File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml"))
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" }, rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
:storage => :s3, :storage => :s3,
:bucket => ENV["S3_BUCKET"], :bucket => ENV["S3_BUCKET"],
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml")) :s3_credentials => @s3_credentials
Dummy.delete_all Dummy.delete_all
@file = File.new(fixture_file('spaced file.png'), 'rb')
@dummy = Dummy.new @dummy = Dummy.new
@dummy.avatar = File.new(fixture_file('spaced file.png'), 'rb') @dummy.avatar = @file
@dummy.save @dummy.save
end end
teardown { @s3_credentials.close }
should "return a replaced version for path" do should "return a replaced version for path" do
assert_match /.+\/spaced_file\.png/, @dummy.avatar.path assert_match /.+\/spaced_file\.png/, @dummy.avatar.path
end end
...@@ -116,17 +126,20 @@ unless ENV["S3_BUCKET"].blank? ...@@ -116,17 +126,20 @@ unless ENV["S3_BUCKET"].blank?
context "An attachment that uses S3 for storage and uses AES256 encryption" do context "An attachment that uses S3 for storage and uses AES256 encryption" do
setup do setup do
@s3_credentials = File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml"))
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" }, rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
:storage => :s3, :storage => :s3,
:bucket => ENV["S3_BUCKET"], :bucket => ENV["S3_BUCKET"],
:path => ":class/:attachment/:id/:style.:extension", :path => ":class/:attachment/:id/:style.:extension",
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "s3.yml")), :s3_credentials => @s3_credentials,
:s3_server_side_encryption => :aes256 :s3_server_side_encryption => :aes256
Dummy.delete_all Dummy.delete_all
@dummy = Dummy.new @dummy = Dummy.new
end end
teardown { @s3_credentials.close }
context "when assigned" do context "when assigned" do
setup do setup do
@file = File.new(fixture_file('5k.png'), 'rb') @file = File.new(fixture_file('5k.png'), 'rb')
......
...@@ -186,8 +186,10 @@ class S3Test < Test::Unit::TestCase ...@@ -186,8 +186,10 @@ class S3Test < Test::Unit::TestCase
'secret_access_key' => "54321" 'secret_access_key' => "54321"
} }
@dummy = Dummy.new File.open(fixture_file('5k.png'), 'rb') do |file|
@dummy.avatar = File.new(fixture_file('5k.png'), 'rb') @dummy = Dummy.new
@dummy.avatar = file
end
end end
should "return a url containing the correct original file mime type" do should "return a url containing the correct original file mime type" do
...@@ -217,8 +219,10 @@ class S3Test < Test::Unit::TestCase ...@@ -217,8 +219,10 @@ class S3Test < Test::Unit::TestCase
'secret_access_key' => "54321" 'secret_access_key' => "54321"
} }
@dummy = Dummy.new File.open(fixture_file('spaced file.png'), 'rb') do |file|
@dummy.avatar = File.new(fixture_file('spaced file.png'), 'rb') @dummy = Dummy.new
@dummy.avatar = file
end
end end
should "return a replaced version for path" do should "return a replaced version for path" do
......
...@@ -7,6 +7,8 @@ class ThumbnailTest < Test::Unit::TestCase ...@@ -7,6 +7,8 @@ class ThumbnailTest < Test::Unit::TestCase
@tempfile = Paperclip::Tempfile.new(["file", ".jpg"]) @tempfile = Paperclip::Tempfile.new(["file", ".jpg"])
end end
teardown { @tempfile.close }
should "have its path contain a real extension" do should "have its path contain a real extension" do
assert_equal ".jpg", File.extname(@tempfile.path) assert_equal ".jpg", File.extname(@tempfile.path)
end end
...@@ -21,6 +23,8 @@ class ThumbnailTest < Test::Unit::TestCase ...@@ -21,6 +23,8 @@ class ThumbnailTest < Test::Unit::TestCase
@tempfile = Paperclip::Tempfile.new("file") @tempfile = Paperclip::Tempfile.new("file")
end end
teardown { @tempfile.close }
should "not have an extension if not given one" do should "not have an extension if not given one" do
assert_equal "", File.extname(@tempfile.path) assert_equal "", File.extname(@tempfile.path)
end end
......
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