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

View File

@ -200,6 +200,19 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 'Yada Yada', File.read(path)
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
path = '/path/to/file.txt'
File.open(path, 'w') do |f|