class FakeFS::File

Constants

FILE_CREATION_BITMASK
FILE_CREATION_MODES
MODES
MODE_BITMASK
PATH_SEPARATOR

Attributes

path[R]

Public Class Methods

basename(*args) click to toggle source
# File lib/fakefs/file.rb, line 103
def self.basename(*args)
  RealFile.basename(*args)
end
const_missing(name) click to toggle source
# File lib/fakefs/file.rb, line 69
def self.const_missing(name)
  RealFile.const_get(name)
end
delete(file_name, *additional_file_names) click to toggle source
# File lib/fakefs/file.rb, line 157
def self.delete(file_name, *additional_file_names)
  if !exists?(file_name)
    raise Errno::ENOENT, "No such file or directory - #{file_name}"
  end

  FileUtils.rm(file_name)

  additional_file_names.each do |file_name|
    FileUtils.rm(file_name)
  end

  additional_file_names.size + 1
end
Also aliased as: unlink
directory?(path) click to toggle source
# File lib/fakefs/file.rb, line 73
def self.directory?(path)
  if path.respond_to? :entry
    path.entry.is_a? FakeDir
  else
    result = FileSystem.find(path)
    result ? result.entry.is_a?(FakeDir) : false
  end
end
dirname(path) click to toggle source
# File lib/fakefs/file.rb, line 107
def self.dirname(path)
  RealFile.dirname(path)
end
exist?(path) click to toggle source
# File lib/fakefs/file.rb, line 37
def self.exist?(path)
  !!FileSystem.find(path)
end
Also aliased as: exists?, readable?, writable?
exists?(path)
Alias for: exist?
expand_path(*args) click to toggle source
# File lib/fakefs/file.rb, line 99
def self.expand_path(*args)
  RealFile.expand_path(*args)
end
extname(path) click to toggle source
# File lib/fakefs/file.rb, line 29
def self.extname(path)
  RealFile.extname(path)
end
file?(path) click to toggle source
# File lib/fakefs/file.rb, line 90
def self.file?(path)
  if path.respond_to? :entry
    path.entry.is_a? FakeFile
  else
    result = FileSystem.find(path)
    result ? result.entry.is_a?(FakeFile) : false
  end
end
join(*parts) click to toggle source
# File lib/fakefs/file.rb, line 33
def self.join(*parts)
  parts * PATH_SEPARATOR
end
mtime(path) click to toggle source
# File lib/fakefs/file.rb, line 49
def self.mtime(path)
  if exists?(path)
    FileSystem.find(path).mtime
  else
    raise Errno::ENOENT
  end
end
new(path, mode = READ_ONLY, perm = nil) click to toggle source
# File lib/fakefs/file.rb, line 207
def initialize(path, mode = READ_ONLY, perm = nil)
  @path = path
  @mode = mode
  @file = FileSystem.find(path)
  @open = true

  check_modes!

  file_creation_mode? ? create_missing_file : check_file_existence!

  @stream = StringIO.new(@file.content, mode)
end
open(path, mode=READ_ONLY, perm = 0644) { |new(path, mode, perm)| ... } click to toggle source
# File lib/fakefs/file.rb, line 116
def self.open(path, mode=READ_ONLY, perm = 0644)
  if block_given?
    yield new(path, mode, perm)
  else
    new(path, mode, perm)
  end
end
read(path) click to toggle source
# File lib/fakefs/file.rb, line 124
def self.read(path)
  file = new(path)
  if file.exists?
    file.read
  else
    raise Errno::ENOENT
  end
end
readable?(path)

Assuming that everyone can read and write files

Alias for: exist?
readlines(path) click to toggle source
# File lib/fakefs/file.rb, line 133
def self.readlines(path)
  read(path).split("\n")
end
size(path) click to toggle source
# File lib/fakefs/file.rb, line 57
def self.size(path)
  read(path).length
end
size?(path) click to toggle source
# File lib/fakefs/file.rb, line 61
def self.size?(path)
  if exists?(path) && !size(path).zero?
    true
  else
    nil
  end
end
stat(file) click to toggle source
# File lib/fakefs/file.rb, line 179
def self.stat(file)
  File::Stat.new(file)
end
writable?(path)
Alias for: exist?

Public Instance Methods

<<(content)
Alias for: write
close() click to toggle source
# File lib/fakefs/file.rb, line 220
def close
  @open = false
end
exists?() click to toggle source
# File lib/fakefs/file.rb, line 232
def exists?
  true
end
flush() click to toggle source
# File lib/fakefs/file.rb, line 246
def flush; self; end
print(content)
Alias for: write
puts(*content) click to toggle source
# File lib/fakefs/file.rb, line 236
def puts(*content)
  @stream.puts(*content)
end
read(chunk = nil) click to toggle source
# File lib/fakefs/file.rb, line 224
def read(chunk = nil)
  @stream.read(chunk)
end
rewind() click to toggle source
# File lib/fakefs/file.rb, line 228
def rewind
  @stream.rewind
end
seek(amount, whence = SEEK_SET) click to toggle source
# File lib/fakefs/file.rb, line 248
def seek(amount, whence = SEEK_SET)
  @stream.seek(amount, whence)
end
write(content) click to toggle source
# File lib/fakefs/file.rb, line 240
def write(content)
  @stream.write(content)
end
Also aliased as: print, <<

Private Instance Methods

check_file_existence!() click to toggle source
# File lib/fakefs/file.rb, line 258
def check_file_existence!
  unless @file
    raise Errno::ENOENT, "No such file or directory - #{@file}"
  end
end
check_modes!() click to toggle source
# File lib/fakefs/file.rb, line 254
def check_modes!
  StringIO.new("", @mode)
end
create_missing_file() click to toggle source
# File lib/fakefs/file.rb, line 276
def create_missing_file
  if !File.exists?(@path)
    @file = FileSystem.add(path, FakeFile.new)
  end
end
file_creation_mode?() click to toggle source
# File lib/fakefs/file.rb, line 264
def file_creation_mode?
  mode_in?(FILE_CREATION_MODES) || mode_in_bitmask?(FILE_CREATION_BITMASK)
end
mode_in?(list) click to toggle source
# File lib/fakefs/file.rb, line 268
def mode_in?(list)
  list.any? { |element| @mode.include?(element) } if @mode.respond_to?(:include?)
end
mode_in_bitmask?(mask) click to toggle source
# File lib/fakefs/file.rb, line 272
def mode_in_bitmask?(mask)
  (@mode & mask) != 0 if @mode.is_a?(Integer)
end