Add binary mode. Resolves Github #13

This commit is contained in:
Scott Taylor 2009-10-29 05:18:12 -04:00
parent 1d410d4955
commit 6f5cff81ca
2 changed files with 18 additions and 1 deletions

View File

@ -196,6 +196,8 @@ module FakeFS
@file = FileSystem.find(path)
@open = true
check_modes!
file_creation_mode? ? create_missing_file : check_file_existence!
@stream = StringIO.new(@file.content, mode)
@ -231,6 +233,10 @@ module FakeFS
private
def check_modes!
StringIO.new("", @mode)
end
def check_file_existence!
unless @file
raise Errno::ENOENT, "No such file or directory - #{@file}"
@ -242,7 +248,7 @@ module FakeFS
end
def mode_in?(list)
list.include?(@mode)
list.any? { |element| @mode.include?(element) }
end
def create_missing_file

View File

@ -214,6 +214,17 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 'Yada Yada', File.read(path)
end
def test_raises_error_when_opening_with_binary_mode_only
assert_raise ArgumentError do
File.open("/foo", "b")
end
end
def test_can_open_file_in_binary_mode
File.open("/foo", "wb") { |x| x << "a" }
assert_equal "a", File.read("/foo")
end
def test_can_chunk_io_when_reading
path = '/path/to/file.txt'
File.open(path, 'w') do |f|