Bitmask file creation modes allow for Tempfile compatability.

This commit is contained in:
Jon Yurek 2009-10-29 21:15:31 -04:00 committed by Chris Wanstrath
parent 1c6825fb9b
commit bfffa910e9
2 changed files with 54 additions and 3 deletions

View File

@ -13,6 +13,19 @@ module FakeFS
FILE_CREATION_MODES = MODES - [READ_ONLY, READ_WRITE]
MODE_BITMASK = RealFile::RDONLY |
RealFile::WRONLY |
RealFile::RDWR |
RealFile::APPEND |
RealFile::CREAT |
RealFile::EXCL |
RealFile::NONBLOCK |
RealFile::TRUNC |
RealFile::NOCTTY |
RealFile::SYNC
FILE_CREATION_BITMASK = RealFile::CREAT
def self.extname(path)
RealFile.extname(path)
end
@ -28,8 +41,9 @@ module FakeFS
class << self
alias_method :exists?, :exist?
# Assuming that everyone can read files
# Assuming that everyone can read and write files
alias_method :readable?, :exist?
alias_method :writable?, :exist?
end
def self.mtime(path)
@ -248,11 +262,15 @@ module FakeFS
end
def file_creation_mode?
mode_in? FILE_CREATION_MODES
mode_in?(FILE_CREATION_MODES) || mode_in_bitmask?(FILE_CREATION_BITMASK)
end
def mode_in?(list)
list.any? { |element| @mode.include?(element) }
list.any? { |element| @mode.include?(element) } if @mode.respond_to?(:include?)
end
def mode_in_bitmask?(mask)
(@mode & mask) != 0 if @mode.is_a?(Integer)
end
def create_missing_file

View File

@ -109,6 +109,18 @@ class FakeFSTest < Test::Unit::TestCase
assert File.exists?(path)
assert File.readable?(path)
assert File.writable?(path)
end
def test_can_create_files_with_bitmasks
path = '/path/to/file.txt'
File.open(path, File::RDWR | File::CREAT) do |f|
f.write "Yatta!"
end
assert File.exists?(path)
assert File.readable?(path)
assert File.writable?(path)
end
def test_file_opens_in_read_only_mode
@ -121,6 +133,16 @@ class FakeFSTest < Test::Unit::TestCase
end
end
def test_file_opens_in_read_only_mode_with_bitmasks
File.open("foo", "w") { |f| f << "foo" }
f = File.open("foo", File::RDONLY)
assert_raises(IOError) do
f << "bar"
end
end
def test_file_opens_in_invalid_mode
FileUtils.touch("foo")
@ -146,6 +168,17 @@ class FakeFSTest < Test::Unit::TestCase
assert File.exists?("foo")
end
def test_creates_files_in_write_only_mode_with_bitmasks
File.open("foo", File::WRONLY | File::CREAT)
assert File.exists?("foo")
end
def test_raises_in_write_only_mode_without_create_bitmask
assert_raises(Errno::ENOENT) do
File.open("foo", File::WRONLY)
end
end
def test_creates_files_in_read_write_truncate_mode
File.open("foo", "w+")
assert File.exists?("foo")