Allowing File#read to chunk IO

This commit is contained in:
Nick Quaranto 2009-09-29 11:46:30 -04:00 committed by Chris Wanstrath
parent 5deda9b456
commit 72e8ee62c7
2 changed files with 22 additions and 5 deletions

View File

@ -125,7 +125,7 @@ module FakeFS
def self.readlines(path) def self.readlines(path)
read(path).split("\n") read(path).split("\n")
end end
def self.link(source, dest) def self.link(source, dest)
if directory?(source) if directory?(source)
raise Errno::EPERM, "Operation not permitted - #{source} or #{dest}" raise Errno::EPERM, "Operation not permitted - #{source} or #{dest}"
@ -134,11 +134,11 @@ module FakeFS
if !exists?(source) if !exists?(source)
raise Errno::ENOENT, "No such file or directory - #{source} or #{dest}" raise Errno::ENOENT, "No such file or directory - #{source} or #{dest}"
end end
if exists?(dest) if exists?(dest)
raise Errno::EEXIST, "File exists - #{source} or #{dest}" raise Errno::EEXIST, "File exists - #{source} or #{dest}"
end end
source = FileSystem.find(source) source = FileSystem.find(source)
dest = FileSystem.add(dest, source.entry.clone) dest = FileSystem.add(dest, source.entry.clone)
source.link(dest) source.link(dest)
@ -201,6 +201,7 @@ module FakeFS
@mode = mode @mode = mode
@file = FileSystem.find(path) @file = FileSystem.find(path)
@open = true @open = true
@stream = StringIO.new(@file.content) if @file
check_valid_mode check_valid_mode
file_creation_mode? ? create_missing_file : check_file_existence! file_creation_mode? ? create_missing_file : check_file_existence!
@ -211,11 +212,14 @@ module FakeFS
@open = false @open = false
end end
def read def read(chunk = nil)
raise IOError, 'closed stream' unless @open raise IOError, 'closed stream' unless @open
raise IOError, 'not opened for reading' if write_only? raise IOError, 'not opened for reading' if write_only?
@stream.read(chunk)
end
@file.content def rewind
@stream.rewind
end end
def exists? def exists?

View File

@ -200,6 +200,19 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 'Yada Yada', File.read(path) assert_equal 'Yada Yada', File.read(path)
end end
def test_can_chunk_io_when_reading
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
file = File.new(path, 'r')
assert_equal 'Yada', file.read(4)
assert_equal ' Yada', file.read(5)
assert_equal '', file.read
file.rewind
assert_equal 'Yada Yada', file.read
end
def test_can_get_size_of_files def test_can_get_size_of_files
path = '/path/to/file.txt' path = '/path/to/file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|