Skip to content

Commit

Permalink
Updated iostream with upstream to fix 1.9 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
krobertson committed Apr 16, 2010
1 parent f4b6e5f commit 0295594
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
7 changes: 4 additions & 3 deletions lib/dm-paperclip/iostream.rb
Expand Up @@ -4,7 +4,8 @@ module IOStream

# Returns a Tempfile containing the contents of the readable object.
def to_tempfile
tempfile = Tempfile.new("stream")
name = respond_to?(:original_filename) ? original_filename : (respond_to?(:path) ? path : "stream")
tempfile = Paperclip::Tempfile.new(File.basename(name))
tempfile.binmode
self.stream_to(tempfile)
end
Expand All @@ -25,7 +26,7 @@ def stream_to path_or_file, in_blocks_of = 8192
while self.read(in_blocks_of, buffer) do
dstio.write(buffer)
end
dstio.rewind
dstio.rewind
dstio
end
end
Expand Down Expand Up @@ -55,4 +56,4 @@ def size
end
end
end
end
end
66 changes: 42 additions & 24 deletions test/iostream_test.rb
@@ -1,10 +1,4 @@
require 'rubygems'
require 'test/unit'
require 'stringio'
require 'tempfile'
require 'shoulda'

require File.join(File.dirname(__FILE__), '..', 'lib', 'dm-paperclip', 'iostream.rb')
require 'test/helper'

class IOStreamTest < Test::Unit::TestCase
context "IOStream" do
Expand All @@ -17,38 +11,62 @@ class IOStreamTest < Test::Unit::TestCase

context "A file" do
setup do
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
end

teardown { @file.close }

context "that is sent #stream_to" do

[["/tmp/iostream.string.test", File],
[Tempfile.new('iostream.test'), Tempfile]].each do |args|
context "and given a String" do
setup do
FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test'))
end

context "and given a #{args[0].class.to_s}" do
setup do
assert @result = @file.stream_to(args[0])
end
should "return a File" do
assert @result.is_a?(File)
end

should "return a #{args[1].to_s}" do
assert @result.is_a?(args[1])
end
should "contain the same data as the original file" do
@file.rewind; @result.rewind
assert_equal @file.read, @result.read
end
end

should "contain the same data as the original file" do
@file.rewind; @result.rewind
assert_equal @file.read, @result.read
end
context "and given a Tempfile" do
setup do
tempfile = Tempfile.new('iostream.test')
tempfile.binmode
assert @result = @file.stream_to(tempfile)
end

should "return a Tempfile" do
assert @result.is_a?(Tempfile)
end

should "contain the same data as the original file" do
@file.rewind; @result.rewind
assert_equal @file.read, @result.read
end
end

end

context "that is sent #to_tempfile" do
setup do
assert @tempfile = @file.to_tempfile
end

should "convert it to a Tempfile" do
assert @tempfile.is_a?(Tempfile)
should "convert it to a Paperclip Tempfile" do
assert @tempfile.is_a?(Paperclip::Tempfile)
end

should "have the name be based on the original_filename" do
name = File.basename(@file.path)
extension = File.extname(name)
basename = File.basename(name, extension)
assert_match %r[^#{Regexp.quote(basename)}.*?#{Regexp.quote(extension)}], File.basename(@tempfile.path)
end

should "have the Tempfile contain the same data as the file" do
Expand All @@ -57,4 +75,4 @@ class IOStreamTest < Test::Unit::TestCase
end
end
end
end
end

0 comments on commit 0295594

Please sign in to comment.