Permalink
Browse files

Close ALL the files

Make sure that we close opened files after we're done with them.
  • Loading branch information...
1 parent 02eb725 commit 4f6d482d9823bdf14be5ef70248531fe1e3dc363 @sikachu sikachu committed Apr 17, 2012
@@ -12,6 +12,10 @@ def setup
@subject = Paperclip.io_adapters.for(@attachment)
end
+ def teardown
+ @file.close
+ end
+
should "get the right filename" do
assert_equal "5k.png", @subject.original_filename
end
View
@@ -1120,6 +1120,8 @@ def do_after_all; end
@path = @attachment.path
end
+ teardown { @file.close }
+
should "not delete the files from storage when attachment is destroyed" do
@attachment.destroy
assert File.exists?(@path)
@@ -1142,6 +1144,8 @@ def do_after_all; end
@path = @attachment.path
end
+ teardown { @file.close }
+
should "not be deleted when the model fails to destroy" do
@dummy.stubs(:destroy).raises(Exception)
View
@@ -2,49 +2,58 @@
class FileAdapterTest < Test::Unit::TestCase
context "a new instance" do
- setup do
- @file = File.new(fixture_file("5k.png"))
- @file.binmode
- @subject = Paperclip.io_adapters.for(@file)
- end
+ context "with normal file" do
+ setup do
+ @file = File.new(fixture_file("5k.png"))
+ @file.binmode
+ @subject = Paperclip.io_adapters.for(@file)
+ end
- should "get the right filename" do
- assert_equal "5k.png", @subject.original_filename
- end
+ teardown { @file.close }
- should "force binmode on tempfile" do
- assert @subject.instance_variable_get("@tempfile").binmode?
- end
+ should "get the right filename" do
+ assert_equal "5k.png", @subject.original_filename
+ end
- should "get the content type" do
- assert_equal "image/png", @subject.content_type
- end
+ should "force binmode on tempfile" do
+ assert @subject.instance_variable_get("@tempfile").binmode?
+ end
- should "provide correct mime-type for empty file" do
- @subject = Paperclip.io_adapters.for(Tempfile.new("file_adapter_test"))
+ should "get the content type" do
+ assert_equal "image/png", @subject.content_type
+ end
- # Content type contained '\n' at the end, for an empty file, on my Mac
- assert_equal "application/x-empty", @subject.content_type
- end
+ should "get the file's size" do
+ assert_equal 4456, @subject.size
+ end
- should "get the file's size" do
- assert_equal 4456, @subject.size
- end
+ should "return false for a call to nil?" do
+ assert ! @subject.nil?
+ end
- should "return false for a call to nil?" do
- assert ! @subject.nil?
- end
+ should "generate a MD5 hash of the contents" do
+ expected = Digest::MD5.file(@file.path).to_s
+ assert_equal expected, @subject.fingerprint
+ end
- should "generate a MD5 hash of the contents" do
- expected = Digest::MD5.file(@file.path).to_s
- assert_equal expected, @subject.fingerprint
+ should "read the contents of the file" do
+ expected = @file.read
+ assert expected.length > 0
+ assert_equal expected, @subject.read
+ end
end
- should "read the contents of the file" do
- expected = @file.read
- assert expected.length > 0
- assert_equal expected, @subject.read
- end
+ context "empty file" do
+ setup do
+ @file = Tempfile.new("file_adapter_test")
+ @subject = Paperclip.io_adapters.for(@file)
+ end
+ teardown { @file.close }
+
+ should "provide correct mime-type" do
+ assert_equal "application/x-empty", @subject.content_type
+ end
+ end
end
end
View
@@ -10,6 +10,8 @@ class IntegrationTest < Test::Unit::TestCase
end
end
+ teardown { @file.close }
+
should "not exceed the open file limit" do
assert_nothing_raised do
dummies = Dummy.find(:all)
@@ -287,6 +289,7 @@ class IntegrationTest < Test::Unit::TestCase
teardown do
File.umask @umask
+ @file.close
end
should "respect the current umask" do
@@ -314,6 +317,8 @@ class IntegrationTest < Test::Unit::TestCase
assert @dummy.save
end
+ teardown { [@file, @bad_file].each(&:close) }
+
should "write and delete its files" do
[["434x66", :original],
["300x46", :large],
@@ -407,6 +412,8 @@ class IntegrationTest < Test::Unit::TestCase
@dummy2.save
end
+ teardown { @file2.close }
+
should "work when assigned a file" do
assert_not_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`,
`identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"`
@@ -427,16 +434,13 @@ class IntegrationTest < Test::Unit::TestCase
has_many :attachments, :class_name => 'Dummy'
end
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@dummy = Dummy.new
- @dummy.avatar = File.new(File.join(File.dirname(__FILE__),
- "fixtures",
- "5k.png"), 'rb')
+ @dummy.avatar = @file
end
should "should not error when saving" do
- assert_nothing_raised do
- @dummy.save!
- end
+ @dummy.save!
end
end
@@ -482,6 +486,12 @@ def s3_headers_for attachment, style
@files_on_s3 = s3_files_for @dummy.avatar
end
+ teardown do
+ @file.close
+ @bad_file.close
+ @files_on_s3.values.each(&:close)
+ end
+
context 'assigning itself to a new model' do
setup do
@d2 = Dummy.new
View
@@ -67,6 +67,8 @@ class PaperclipTest < Test::Unit::TestCase
@expected = [d1, d3]
end
+ teardown { @file.close }
+
should "yield every instance of a model that has an attachment" do
actual = []
Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
@@ -2,36 +2,44 @@
class FileSystemTest < Test::Unit::TestCase
context "Filesystem" do
- setup do
- rebuild_model :styles => { :thumbnail => "25x25#" }
- @dummy = Dummy.create!
+ context "normal file" do
+ setup do
+ rebuild_model :styles => { :thumbnail => "25x25#" }
+ @dummy = Dummy.create!
- @dummy.avatar = File.open(fixture_file('5k.png'))
- end
+ @file = File.open(fixture_file('5k.png'))
+ @dummy.avatar = @file
+ end
- should "allow file assignment" do
- assert @dummy.save
- end
+ teardown { @file.close }
- should "store the original" do
- @dummy.save
- assert File.exists?(@dummy.avatar.path)
- end
+ should "allow file assignment" do
+ assert @dummy.save
+ end
- should "store the thumbnail" do
- @dummy.save
- assert File.exists?(@dummy.avatar.path(:thumbnail))
+ should "store the original" do
+ @dummy.save
+ assert File.exists?(@dummy.avatar.path)
+ end
+
+ should "store the thumbnail" do
+ @dummy.save
+ assert File.exists?(@dummy.avatar.path(:thumbnail))
+ end
end
context "with file that has space in file name" do
setup do
rebuild_model :styles => { :thumbnail => "25x25#" }
@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
end
+ teardown { @file.close }
+
should "store the file" do
assert File.exists?(@dummy.avatar.path)
end
View
@@ -12,10 +12,13 @@ class FogTest < Test::Unit::TestCase
:url => '/:attachment/:filename',
:fog_directory => "paperclip",
:fog_credentials => fixture_file('fog.yml')
+ @file = File.new(fixture_file('5k.png'), 'rb')
@dummy = Dummy.new
- @dummy.avatar = File.new(fixture_file('5k.png'), 'rb')
+ @dummy.avatar = @file
end
+ teardown { @file.close }
+
should "have the proper information loading credentials from a file" do
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
end
@@ -28,10 +31,13 @@ class FogTest < Test::Unit::TestCase
:url => '/:attachment/:filename',
:fog_directory => "paperclip",
:fog_credentials => File.open(fixture_file('fog.yml'))
+ @file = File.new(fixture_file('5k.png'), 'rb')
@dummy = Dummy.new
- @dummy.avatar = File.new(fixture_file('5k.png'), 'rb')
+ @dummy.avatar = @file
end
+ teardown { @file.close }
+
should "have the proper information loading credentials from a file" do
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
end
@@ -48,9 +54,13 @@ class FogTest < Test::Unit::TestCase
:aws_access_key_id => 'AWS_ID',
:aws_secret_access_key => 'AWS_SECRET'
}
+ @file = File.new(fixture_file('5k.png'), 'rb')
@dummy = Dummy.new
- @dummy.avatar = File.new(fixture_file('5k.png'), 'rb')
+ @dummy.avatar = @file
end
+
+ teardown { @file.close }
+
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")),
@dummy.avatar.path
Oops, something went wrong.

0 comments on commit 4f6d482

Please sign in to comment.