Commit 2fb4ba9e by Jon Yurek

Merged close-all-the-files

parents 8e900b2a 8c2904cc
......@@ -429,10 +429,16 @@ module Paperclip
def post_process_style(name, style) #:nodoc:
begin
raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
original_file = @queued_for_write[:original]
@queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
Paperclip.processor(processor).make(file, style.processor_options, self)
new_file = Paperclip.processor(processor).make(file, style.processor_options, self)
file.close unless file == original_file
new_file
end
unadapted_file = @queued_for_write[name]
@queued_for_write[name] = Paperclip.io_adapters.for(@queued_for_write[name])
unadapted_file.close if unadapted_file.respond_to?(:close)
@queued_for_write[name]
rescue Paperclip::Error => e
log("An error was received while processing: #{e.inspect}")
(@errors[:processing] ||= []) << e.message if @options[:whiny]
......
......@@ -643,7 +643,7 @@ class AttachmentTest < Test::Unit::TestCase
rebuild_model :processors => [:thumbnail, :test], :styles => @style_params
@dummy = Dummy.new
@file = StringIO.new("...")
@file.stubs(:to_tempfile).returns(@file)
@file.stubs(:close)
Paperclip::Test.stubs(:make).returns(@file)
Paperclip::Thumbnail.stubs(:make).returns(@file)
end
......
......@@ -8,6 +8,7 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
should 'return the empty content type when the file is empty' do
tempfile = Tempfile.new("empty")
assert_equal "inode/x-empty", Paperclip::ContentTypeDetector.new(tempfile.path).detect
tempfile.close
end
should 'return content type of file if it is an acceptable type' do
......
......@@ -7,6 +7,8 @@ class FileCommandContentTypeDetectorTest < Test::Unit::TestCase
tempfile.rewind
assert_equal "text/plain", Paperclip::FileCommandContentTypeDetector.new(tempfile.path).detect
tempfile.close
end
should 'return a sensible default when the file command is missing' do
......
......@@ -32,6 +32,7 @@ end
ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
$previous_count = 0
class Test::Unit::TestCase
def setup
silence_warnings do
......@@ -41,6 +42,22 @@ class Test::Unit::TestCase
Rails.stubs(:const_defined?).with(:Railtie).returns(false)
end
end
def teardown
end
def report_files
files = []
ObjectSpace.each_object(IO){|io| files << io unless io.closed? }
if files.count > $previous_count
puts __name__
puts "#{files.count} files"
files.each do |file|
puts "Open IO: #{file.inspect}"
end
end
$previous_count = files.count
end
end
$LOAD_PATH << File.join(ROOT, 'lib')
......
require './test/helper'
require 'pp'
class AttachmentAdapterTest < Test::Unit::TestCase
......@@ -19,6 +20,7 @@ class AttachmentAdapterTest < Test::Unit::TestCase
teardown do
@file.close
@subject.close
end
should "get the right filename" do
......@@ -58,8 +60,8 @@ class AttachmentAdapterTest < Test::Unit::TestCase
context "for a file with restricted characters in the name" do
setup do
file_contents = File.new(fixture_file("animated.gif"))
@file = StringIO.new(file_contents.read)
file_contents = IO.read(fixture_file("animated.gif"))
@file = StringIO.new(file_contents)
@file.stubs(:original_filename).returns('image:restricted.gif')
@file.binmode
......@@ -69,7 +71,7 @@ class AttachmentAdapterTest < Test::Unit::TestCase
end
teardown do
@file.close
@subject.close
end
should "not generate paths that include restricted characters" do
......@@ -98,6 +100,7 @@ class AttachmentAdapterTest < Test::Unit::TestCase
teardown do
@file.close
@thumb.close
@subject.close
end
should "get the original filename" do
......
require './test/helper'
class DataUriAdapterTest < Test::Unit::TestCase
def teardown
if @subject
@subject.close
end
end
context "a new instance" do
setup do
@contents = "data:image/png;base64,#{original_base64_content}"
......
require './test/helper'
class EmptyStringAdapterTest < Test::Unit::TestCase
context 'a new instance' do
setup do
@subject = Paperclip.io_adapters.for('')
......
......@@ -6,49 +6,58 @@ class FileAdapterTest < Test::Unit::TestCase
setup do
@file = File.new(fixture_file("5k.png"))
@file.binmode
@subject = Paperclip.io_adapters.for(@file)
end
teardown { @file.close }
should "get the right filename" do
assert_equal "5k.png", @subject.original_filename
teardown do
@file.close
@subject.close if @subject
end
should "force binmode on tempfile" do
assert @subject.instance_variable_get("@tempfile").binmode?
end
context 'doing normal things' do
setup do
@subject = Paperclip.io_adapters.for(@file)
end
should "get the content type" do
assert_equal "image/png", @subject.content_type
end
should "get the right filename" do
assert_equal "5k.png", @subject.original_filename
end
should "return content type as a string" do
assert_kind_of String, @subject.content_type
end
should "force binmode on tempfile" do
assert @subject.instance_variable_get("@tempfile").binmode?
end
should "get the file's size" do
assert_equal 4456, @subject.size
end
should "get the content type" do
assert_equal "image/png", @subject.content_type
end
should "return false for a call to nil?" do
assert ! @subject.nil?
end
should "return content type as a string" do
assert_kind_of String, @subject.content_type
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 "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 "read the contents of the file" do
expected = @file.read
assert expected.length > 0
assert_equal expected, @subject.read
should "generate a MD5 hash of the contents" do
expected = Digest::MD5.file(@file.path).to_s
assert_equal expected, @subject.fingerprint
end
should "read the contents of the file" do
expected = @file.read
assert expected.length > 0
assert_equal expected, @subject.read
end
end
context "file with multiple possible content type" do
setup do
MIME::Types.stubs(:type_for).returns([MIME::Type.new('image/x-png'), MIME::Type.new('image/png')])
@subject = Paperclip.io_adapters.for(@file)
end
should "prefer officially registered mime type" do
......@@ -86,13 +95,17 @@ class FileAdapterTest < Test::Unit::TestCase
context "filename with restricted characters" do
setup do
file_contents = File.new(fixture_file("animated.gif"))
@file = StringIO.new(file_contents.read)
@file = File.open(fixture_file("animated.gif")) do |file|
StringIO.new(file.read)
end
@file.stubs(:original_filename).returns('image:restricted.gif')
@subject = Paperclip.io_adapters.for(@file)
end
teardown { @file.close }
teardown do
@file.close
@subject.close
end
should "not generate filenames that include restricted characters" do
assert_equal 'image_restricted.gif', @subject.original_filename
......@@ -109,7 +122,10 @@ class FileAdapterTest < Test::Unit::TestCase
@subject = Paperclip.io_adapters.for(@file)
end
teardown { @file.close }
teardown do
@file.close
@subject.close
end
should "provide correct mime-type" do
assert_match %r{.*/x-empty}, @subject.content_type
......
......@@ -4,6 +4,7 @@ class AttachmentContentTypeValidatorTest < Test::Unit::TestCase
def setup
rebuild_model
@dummy = Dummy.new
super
end
def build_validator(options)
......
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