file copy fix

This commit is contained in:
John Bender 2010-03-11 00:02:05 -08:00
parent a71815df4e
commit 751a8bfa1b
2 changed files with 6 additions and 31 deletions

View File

@ -3,19 +3,9 @@ module Vagrant
# "Downloads" a file to a temporary file. Basically, this downloader
# simply does a file copy.
class File < Base
BUFFERSIZE = 1048576 # 1 MB
def download!(source_url, destination_file)
# For now we read the contents of one into a buffer
# and copy it into the other. In the future, we should do
# a system-level file copy (FileUtils.cp).
open(source_url) do |f|
loop do
break if f.eof?
destination_file.write(f.read(BUFFERSIZE))
end
end
FileUtils.cp(source_url, destination_file.path)
end
end
end
end
end

View File

@ -7,25 +7,10 @@ class FileDownloaderTest < Test::Unit::TestCase
end
context "downloading" do
setup do
@file = mock("file")
@file.stubs(:read)
@file.stubs(:eof?).returns(false)
@downloader.stubs(:open).yields(@file)
end
should "open with the given uri" do
@downloader.expects(:open).with(@uri).once
@downloader.download!(@uri, @tempfile)
end
should "buffer the read from the file and write to the tempfile" do
data = mock("data")
write_seq = sequence("write_seq")
@file.stubs(:eof?).returns(false).in_sequence(write_seq)
@file.expects(:read).returns(data).in_sequence(write_seq)
@tempfile.expects(:write).with(data).in_sequence(write_seq)
@file.stubs(:eof?).returns(true).in_sequence(write_seq)
should "cp the file" do
path = '/path'
@tempfile.expects(:path).returns(path)
FileUtils.expects(:cp).with(@uri, path)
@downloader.download!(@uri, @tempfile)
end
end