class FakeFS::File

Constants

FILE_CREATION_BITMASK
FILE_CREATION_MODES
MODES
MODE_BITMASK

Attributes

path[R]

Public Class Methods

atime(path) click to toggle source
# File lib/fakefs/file.rb, line 70
def self.atime(path)
  if exists?(path)
    FileSystem.find(path).atime
  else
    raise Errno::ENOENT
  end
end
basename(*args) click to toggle source
# File lib/fakefs/file.rb, line 137
def self.basename(*args)
  RealFile.basename(*args)
end
chmod(mode_int, filename) click to toggle source
# File lib/fakefs/file.rb, line 235
def self.chmod(mode_int, filename)
  FileSystem.find(filename).mode = 0100000 + mode_int
end
chown(owner_int, group_int, filename) click to toggle source
# File lib/fakefs/file.rb, line 239
def self.chown(owner_int, group_int, filename)
  file = FileSystem.find(filename)
  if owner_int && owner_int != -1
    owner_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
    file.uid = owner_int
  end
  if group_int && group_int != -1
    group_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
    file.gid = group_int
  end
end
const_missing(name) click to toggle source
# File lib/fakefs/file.rb, line 103
def self.const_missing(name)
  RealFile.const_get(name)
end
ctime(path) click to toggle source
# File lib/fakefs/file.rb, line 62
def self.ctime(path)
  if exists?(path)
    FileSystem.find(path).ctime
  else
    raise Errno::ENOENT
  end
end
delete(file_name, *additional_file_names) click to toggle source
# File lib/fakefs/file.rb, line 201
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 107
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 141
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)
  if(File.symlink?(path)) then
    referent = File.expand_path(File.readlink(path), File.dirname(path))
    exist?(referent)
  else
    !!FileSystem.find(path)
  end
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 133
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 124
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)
  RealFile.join(parts)
end
lstat(file) click to toggle source
# File lib/fakefs/file.rb, line 227
def self.lstat(file)
  File::Stat.new(file, true)
end
mtime(path) click to toggle source
# File lib/fakefs/file.rb, line 54
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
Calls superclass method
# File lib/fakefs/file.rb, line 315
def initialize(path, mode = READ_ONLY, perm = nil)
  @path = path
  @mode = mode.is_a?(Hash) ? (mode[:mode] || READ_ONLY) : mode
  @file = FileSystem.find(path)
  @autoclose = true

  check_modes!

  file_creation_mode? ? create_missing_file : check_file_existence!

  super(@file.content, @mode)
end
read(path, *args) click to toggle source
# File lib/fakefs/file.rb, line 150
def self.read(path, *args)
  file = new(path)
  if file.exists?
    FileSystem.find(path).atime = Time.now
    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 160
def self.readlines(path)
  read(path).split("\n")
end
rename(source, dest) click to toggle source
# File lib/fakefs/file.rb, line 164
def self.rename(source, dest)
  if directory?(source) && file?(dest)
    raise Errno::ENOTDIR, "Not a directory - #{source} or #{dest}"
  elsif file?(source) && directory?(dest)
    raise Errno::EISDIR, "Is a directory - #{source} or #{dest}"
  end

  if target = FileSystem.find(source)
    FileSystem.add(dest, target.entry.clone)
    FileSystem.delete(source)
  else
    raise Errno::ENOENT,  "No such file or directory - #{source} or #{dest}"
  end

  0
end
size(path) click to toggle source
# File lib/fakefs/file.rb, line 91
def self.size(path)
  read(path).length
end
size?(path) click to toggle source
# File lib/fakefs/file.rb, line 95
def self.size?(path)
  if exists?(path) && !size(path).zero?
    true
  else
    nil
  end
end
split(path) click to toggle source
# File lib/fakefs/file.rb, line 231
def self.split(path)
  return RealFile.split(path)
end
stat(file) click to toggle source
# File lib/fakefs/file.rb, line 223
def self.stat(file)
  File::Stat.new(file)
end
umask() click to toggle source
# File lib/fakefs/file.rb, line 251
def self.umask
  RealFile.umask
end
utime(atime, mtime, *paths) click to toggle source
# File lib/fakefs/file.rb, line 78
def self.utime(atime, mtime, *paths)
  paths.each do |path|
    if exists?(path)
      FileSystem.find(path).atime = atime
      FileSystem.find(path).mtime = mtime
    else
      raise Errno::ENOENT
    end
  end

  paths.size
end
writable?(path)
Alias for: exist?

Public Instance Methods

advise(advice, offset=0, len=0) click to toggle source
# File lib/fakefs/file.rb, line 461
def advise(advice, offset=0, len=0)
end
atime() click to toggle source
# File lib/fakefs/file.rb, line 389
def atime
  self.class.atime(@path)
end
autoclose=(autoclose) click to toggle source
# File lib/fakefs/file.rb, line 449
def autoclose=(autoclose)
  @autoclose = autoclose
end
autoclose?() click to toggle source
# File lib/fakefs/file.rb, line 441
def autoclose?
  @autoclose
end
binmode?() click to toggle source
# File lib/fakefs/file.rb, line 421
def binmode?
  raise NotImplementedError
end
chmod(mode_int) click to toggle source
# File lib/fakefs/file.rb, line 405
def chmod(mode_int)
  @file.mode = 0100000 + mode_int
end
chown(owner_int, group_int) click to toggle source
# File lib/fakefs/file.rb, line 409
def chown(owner_int, group_int)
  if owner_int && owner_int != -1
    owner_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
    @file.uid = owner_int
  end
  if group_int && group_int != -1
    group_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
    @file.gid = group_int
  end
end
close_on_exec=(bool) click to toggle source
# File lib/fakefs/file.rb, line 425
def close_on_exec=(bool)
  raise NotImplementedError
end
close_on_exec?() click to toggle source
# File lib/fakefs/file.rb, line 429
def close_on_exec?
  raise NotImplementedError
end
ctime() click to toggle source
# File lib/fakefs/file.rb, line 393
def ctime
  self.class.ctime(@path)
end
exists?() click to toggle source
# File lib/fakefs/file.rb, line 328
def exists?
  true
end
flock(locking_constant) click to toggle source
# File lib/fakefs/file.rb, line 397
def flock(locking_constant)
  raise NotImplementedError
end
ioctl(integer_cmd, arg) click to toggle source
# File lib/fakefs/file.rb, line 354
def ioctl(integer_cmd, arg)
  raise NotImplementedError
end
lstat() click to toggle source
# File lib/fakefs/file.rb, line 366
def lstat
  self.class.lstat(@path)
end
mtime() click to toggle source
# File lib/fakefs/file.rb, line 401
def mtime
  self.class.mtime(@path)
end
read_nonblock(maxlen, outbuf = nil) click to toggle source
# File lib/fakefs/file.rb, line 358
def read_nonblock(maxlen, outbuf = nil)
  raise NotImplementedError
end
readpartial(maxlen, outbuf = nil) click to toggle source
# File lib/fakefs/file.rb, line 385
def readpartial(maxlen, outbuf = nil)
  raise NotImplementedError
end
size() click to toggle source
# File lib/fakefs/file.rb, line 455
def size
  File.size(@path)
end
stat() click to toggle source
# File lib/fakefs/file.rb, line 362
def stat
  self.class.stat(@path)
end
sysseek(position, whence = SEEK_SET) click to toggle source
# File lib/fakefs/file.rb, line 370
def sysseek(position, whence = SEEK_SET)
  seek(position, whence)
  pos
end
syswrite(str)
Alias for: write
to_io() click to toggle source
# File lib/fakefs/file.rb, line 377
def to_io
  self
end
to_path() click to toggle source
# File lib/fakefs/file.rb, line 433
def to_path
  @path
end
write(str) click to toggle source
Calls superclass method
# File lib/fakefs/file.rb, line 332
def write(str)
  val = super(str)
  @file.mtime = Time.now
  val
end
Also aliased as: syswrite
write_nonblock(string) click to toggle source
# File lib/fakefs/file.rb, line 381
def write_nonblock(string)
  raise NotImplementedError
end

Private Instance Methods

check_file_existence!() click to toggle source
# File lib/fakefs/file.rb, line 471
def check_file_existence!
  raise Errno::ENOENT, @path unless @file
end
check_modes!() click to toggle source
# File lib/fakefs/file.rb, line 467
def check_modes!
  StringIO.new("", @mode)
end
create_missing_file() click to toggle source

Create a missing file if the path is valid.

# File lib/fakefs/file.rb, line 489
def create_missing_file
  raise Errno::EISDIR, "Is a directory - #{path}" if File.directory?(@path)

  if !File.exists?(@path) # Unnecessary check, probably.
    dirname = RealFile.dirname @path

    unless dirname == "."
      dir = FileSystem.find dirname

      unless dir.kind_of? FakeDir
        raise Errno::ENOENT, "No such file or directory - #{path}"
      end
    end

    @file = FileSystem.add(path, FakeFile.new)
  end
end
file_creation_mode?() click to toggle source
# File lib/fakefs/file.rb, line 475
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 479
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 483
def mode_in_bitmask?(mask)
  (@mode & mask) != 0 if @mode.is_a?(Integer)
end