Bitmask file creation modes allow for Tempfile compatability.
This commit is contained in:
parent
1c6825fb9b
commit
bfffa910e9
@ -13,6 +13,19 @@ module FakeFS
|
|||||||
|
|
||||||
FILE_CREATION_MODES = MODES - [READ_ONLY, READ_WRITE]
|
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)
|
def self.extname(path)
|
||||||
RealFile.extname(path)
|
RealFile.extname(path)
|
||||||
end
|
end
|
||||||
@ -28,8 +41,9 @@ module FakeFS
|
|||||||
class << self
|
class << self
|
||||||
alias_method :exists?, :exist?
|
alias_method :exists?, :exist?
|
||||||
|
|
||||||
# Assuming that everyone can read files
|
# Assuming that everyone can read and write files
|
||||||
alias_method :readable?, :exist?
|
alias_method :readable?, :exist?
|
||||||
|
alias_method :writable?, :exist?
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.mtime(path)
|
def self.mtime(path)
|
||||||
@ -248,11 +262,15 @@ module FakeFS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def file_creation_mode?
|
def file_creation_mode?
|
||||||
mode_in? FILE_CREATION_MODES
|
mode_in?(FILE_CREATION_MODES) || mode_in_bitmask?(FILE_CREATION_BITMASK)
|
||||||
end
|
end
|
||||||
|
|
||||||
def mode_in?(list)
|
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
|
end
|
||||||
|
|
||||||
def create_missing_file
|
def create_missing_file
|
||||||
|
@ -109,6 +109,18 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
assert File.exists?(path)
|
assert File.exists?(path)
|
||||||
assert File.readable?(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
|
end
|
||||||
|
|
||||||
def test_file_opens_in_read_only_mode
|
def test_file_opens_in_read_only_mode
|
||||||
@ -121,6 +133,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
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
|
def test_file_opens_in_invalid_mode
|
||||||
FileUtils.touch("foo")
|
FileUtils.touch("foo")
|
||||||
|
|
||||||
@ -146,6 +168,17 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
assert File.exists?("foo")
|
assert File.exists?("foo")
|
||||||
end
|
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
|
def test_creates_files_in_read_write_truncate_mode
|
||||||
File.open("foo", "w+")
|
File.open("foo", "w+")
|
||||||
assert File.exists?("foo")
|
assert File.exists?("foo")
|
||||||
|
Loading…
Reference in New Issue
Block a user