From 6f5cff81ca4f144a1bb6f41f1062eec4cc6d91ac Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 29 Oct 2009 05:18:12 -0400 Subject: [PATCH] Add binary mode. Resolves Github #13 --- lib/fakefs/file.rb | 8 +++++++- test/fakefs_test.rb | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 72caf69..ea00aeb 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -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 diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index a30c2b9..160f9fd 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -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|