Commit 2fb4ba9e by Jon Yurek

Merged close-all-the-files

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