Cleanup code. Delegate to StringIO. Don't do the file mode check ourself - make StringIO do it.

This commit is contained in:
Scott Taylor 2009-10-29 04:59:52 -04:00
parent 11e80d2ec3
commit 1d410d4955
2 changed files with 11 additions and 49 deletions

View File

@ -19,6 +19,12 @@ module FakeFS
def unlink(file) def unlink(file)
links.delete(file) links.delete(file)
end end
def clone
clone = super
clone.content = content.dup
clone
end
end end
def initialize(name = nil, parent = nil) def initialize(name = nil, parent = nil)

View File

@ -13,20 +13,6 @@ module FakeFS
FILE_CREATION_MODES = MODES - [READ_ONLY, READ_WRITE] FILE_CREATION_MODES = MODES - [READ_ONLY, READ_WRITE]
READ_ONLY_MODES = [
READ_ONLY
]
WRITE_ONLY_MODES = [
WRITE_ONLY,
APPEND_WRITE_ONLY
]
TRUNCATION_MODES = [
WRITE_ONLY,
READ_WRITE_TRUNCATE
]
def self.extname(path) def self.extname(path)
RealFile.extname(path) RealFile.extname(path)
end end
@ -209,11 +195,10 @@ 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
file_creation_mode? ? create_missing_file : check_file_existence! file_creation_mode? ? create_missing_file : check_file_existence!
truncate_file if truncation_mode?
@stream = StringIO.new(@file.content, mode)
end end
def close def close
@ -221,8 +206,6 @@ module FakeFS
end end
def read(chunk = nil) def read(chunk = nil)
raise IOError, 'closed stream' unless @open
raise IOError, 'not opened for reading' if write_only?
@stream.read(chunk) @stream.read(chunk)
end end
@ -231,20 +214,15 @@ module FakeFS
end end
def exists? def exists?
@file true
end end
def puts(*content) def puts(*content)
content.flatten.each do |obj| @stream.puts(*content)
write(obj.to_s + "\n")
end
end end
def write(content) def write(content)
raise IOError, 'closed stream' unless @open @stream.write(content)
raise IOError, 'not open for writing' if read_only?
@file.content += content
end end
alias_method :print, :write alias_method :print, :write
alias_method :<<, :write alias_method :<<, :write
@ -259,28 +237,10 @@ module FakeFS
end end
end end
def check_valid_mode
if !mode_in?(MODES)
raise ArgumentError, "illegal access mode #{@mode}"
end
end
def read_only?
mode_in? READ_ONLY_MODES
end
def file_creation_mode? def file_creation_mode?
mode_in? FILE_CREATION_MODES mode_in? FILE_CREATION_MODES
end end
def write_only?
mode_in? WRITE_ONLY_MODES
end
def truncation_mode?
mode_in? TRUNCATION_MODES
end
def mode_in?(list) def mode_in?(list)
list.include?(@mode) list.include?(@mode)
end end
@ -290,9 +250,5 @@ module FakeFS
@file = FileSystem.add(path, FakeFile.new) @file = FileSystem.add(path, FakeFile.new)
end end
end end
def truncate_file
@file.content = ""
end
end end
end end