From 20eb3f238eb853b2f6fadf1bfabd24ce5b8bff5d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 30 Sep 2009 02:45:48 -0400 Subject: [PATCH] Add file modes. Replace magic values with constants. --- lib/fakefs/file.rb | 11 ++++++++++- lib/fakefs/file_system.rb | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 7a1f9b3..19909c6 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -2,6 +2,15 @@ module FakeFS class File PATH_SEPARATOR = '/' + MODES = [ + READ_ONLY = "r", + READ_WRITE = "r+", + WRITE_ONLY = "w", + READ_WRITE_TRUNCATE = "w+", + APPEND_WRITE_ONLY = "a", + APPEND_READ_WRITE = "a+" + ] + def self.extname(path) RealFile.extname(path) end @@ -69,7 +78,7 @@ module FakeFS FileSystem.find(symlink.target).to_s end - def self.open(path, mode='r', perm = 0644) + def self.open(path, mode=READ_ONLY, perm = 0644) if block_given? yield new(path, mode, perm) else diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb index f24a50b..93426f3 100644 --- a/lib/fakefs/file_system.rb +++ b/lib/fakefs/file_system.rb @@ -54,7 +54,7 @@ module FakeFS files.each do |f| if RealFile.file?(f) FileUtils.mkdir_p(File.dirname(f)) - File.open(f, 'w') do |g| + File.open(f, File::WRITE_ONLY) do |g| g.print RealFile.open(f){|h| h.read } end elsif RealFile.directory?(f)