class FakeFS::File

FakeFS File class inherit StringIO

Constants

FILE_CREATION_BITMASK
FILE_CREATION_MODES
MODES
MODE_BITMASK

Attributes

path[R]

Public Class Methods

absolute_path(file_name, dir_name = Dir.getwd) click to toggle source
# File lib/fakefs/file.rb, line 545
def self.absolute_path(file_name, dir_name = Dir.getwd)
  RealFile.absolute_path(file_name, dir_name)
end
atime(path) click to toggle source
# File lib/fakefs/file.rb, line 80
def self.atime(path)
  if exists?(path)
    FileSystem.find(path).atime
  else
    fail Errno::ENOENT
  end
end
basename(*args) click to toggle source
# File lib/fakefs/file.rb, line 157
def self.basename(*args)
  RealFile.basename(*args)
end
binread(file, length = nil, offset = 0) click to toggle source
# File lib/fakefs/file.rb, line 305
def self.binread(file, length = nil, offset = 0)
  File.read(file, length, offset, mode: 'rb:ASCII-8BIT')
end
birthtime(path) click to toggle source
# File lib/fakefs/file.rb, line 601
def self.birthtime(path)
  if exists?(path)
    FileSystem.find(path).birthtime
  else
    fail Errno::ENOENT
  end
end
chmod(mode_int, filename) click to toggle source
# File lib/fakefs/file.rb, line 274
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 286
def self.chown(owner_int, group_int, filename)
  file = FileSystem.find(filename)

  if owner_int && owner_int != -1
    owner_int.is_a?(Integer) || fail(TypeError,
                                     "can't convert String into Integer")
    file.uid = owner_int
  end
  if group_int && group_int != -1
    group_int.is_a?(Integer) || fail(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 119
def self.const_missing(name)
  RealFile.const_get(name)
end
ctime(path) click to toggle source
# File lib/fakefs/file.rb, line 72
def self.ctime(path)
  if exists?(path)
    FileSystem.find(path).ctime
  else
    fail Errno::ENOENT
  end
end
delete(*file_names) click to toggle source
# File lib/fakefs/file.rb, line 244
def self.delete(*file_names)
  file_names.each do |file_name|
    fail Errno::ENOENT, file_name unless exists?(file_name)

    FileUtils.rm(file_name)
  end

  file_names.size
end
Also aliased as: unlink
directory?(path) click to toggle source
# File lib/fakefs/file.rb, line 123
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 161
def self.dirname(path)
  RealFile.dirname(path)
end
empty?(path)
Alias for: zero?
executable?(filename) click to toggle source

Not exactly right, returns true if the file is chmod +x for owner. In the context of when you would use fakefs, this is usually what you want.

# File lib/fakefs/file.rb, line 280
def self.executable?(filename)
  file = FileSystem.find(filename)
  return false unless file
  (file.mode - 0100000) & 0100 != 0
end
exist?(path) click to toggle source
# File lib/fakefs/file.rb, line 42
def self.exist?(path)
  if File.symlink?(path)
    referent = File.expand_path(File.readlink(path), File.dirname(path))
    exist?(referent)
  else
    !FileSystem.find(path).nil?
  end
end
Also aliased as: exists?, readable?, writable?
exists?(path)
Alias for: exist?
expand_path(file_name, dir_string = FileSystem.current_dir.to_s) click to toggle source
# File lib/fakefs/file.rb, line 153
def self.expand_path(file_name, dir_string = FileSystem.current_dir.to_s)
  RealFile.expand_path(file_name, RealFile.expand_path(dir_string, Dir.pwd))
end
extname(path) click to toggle source
# File lib/fakefs/file.rb, line 30
def self.extname(path)
  RealFile.extname(path)
end
file?(path) click to toggle source
# File lib/fakefs/file.rb, line 140
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
fnmatch(pattern, path, flags = 0)
Alias for: fnmatch?
fnmatch?(pattern, path, flags = 0) click to toggle source
# File lib/fakefs/file.rb, line 309
def self.fnmatch?(pattern, path, flags = 0)
  RealFile.fnmatch?(pattern, path, flags)
end
Also aliased as: fnmatch
foreach(path, *args, &block) click to toggle source
# File lib/fakefs/file.rb, line 194
def self.foreach(path, *args, &block)
  file = new(path)
  if file.exists?
    FileSystem.find(path).atime = Time.now
    if block_given?
      file.each_line(*args, &block)
    else
      file.each_line(*args)
    end
  else
    fail Errno::ENOENT
  end
end
ftype(filename) click to toggle source
# File lib/fakefs/file.rb, line 149
def self.ftype(filename)
  File.lstat(filename).ftype
end
join(*parts) click to toggle source
# File lib/fakefs/file.rb, line 34
def self.join(*parts)
  RealFile.join(parts)
end
lstat(file) click to toggle source
# File lib/fakefs/file.rb, line 266
def self.lstat(file)
  File::Stat.new(file, true)
end
mtime(path) click to toggle source
# File lib/fakefs/file.rb, line 64
def self.mtime(path)
  if exists?(path)
    FileSystem.find(path).mtime
  else
    fail Errno::ENOENT
  end
end
new(path, mode = READ_ONLY, _perm = nil) click to toggle source
Calls superclass method
# File lib/fakefs/file.rb, line 409
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
path(file) click to toggle source
# File lib/fakefs/file.rb, line 38
def self.path(file)
  RealFile.path(file)
end
read(path, *args) click to toggle source
# File lib/fakefs/file.rb, line 170
def self.read(path, *args)
  options = args[-1].is_a?(Hash) ? args.pop : {}
  length = args.size > 0 ? args.shift : nil
  offset = args.size > 0 ? args.shift : 0
  file = new(path, options)

  fail Errno::ENOENT unless file.exists?
  fail Errno::EISDIR, path if directory?(path)

  FileSystem.find(path).atime = Time.now
  file.seek(offset)
  file.read(length)
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 184
def self.readlines(path)
  file = new(path)
  if file.exists?
    FileSystem.find(path).atime = Time.now
    file.readlines
  else
    fail Errno::ENOENT
  end
end
realdirpath(*args) click to toggle source
# File lib/fakefs/file.rb, line 563
def self.realdirpath(*args)
  RealFile.realdirpath(*args)
end
realpath(*args) click to toggle source
# File lib/fakefs/file.rb, line 523
def self.realpath(*args)
  RealFile.realpath(*args)
end
rename(source, dest) click to toggle source
# File lib/fakefs/file.rb, line 208
def self.rename(source, dest)
  if directory?(source) && file?(dest)
    fail Errno::ENOTDIR, "#{source} or #{dest}"
  elsif file?(source) && directory?(dest)
    fail Errno::EISDIR, "#{source} or #{dest}"
  elsif !exist?(dirname(dest))
    fail Errno::ENOENT, "#{source} or #{dest}"
  end

  if (target = FileSystem.find(source))
    if target.is_a?(FakeFS::FakeSymlink)
      File.symlink(target.target, dest)
    else
      FileSystem.add(dest, target.entry.clone)
    end

    FileSystem.delete(source)
  else
    fail Errno::ENOENT, "#{source} or #{dest}"
  end

  0
end
size(path) click to toggle source
# File lib/fakefs/file.rb, line 101
def self.size(path)
  read(path).bytesize
end
size?(path) click to toggle source
# File lib/fakefs/file.rb, line 105
def self.size?(path)
  size(path) if exists?(path) && !size(path).zero?
end
split(path) click to toggle source
# File lib/fakefs/file.rb, line 270
def self.split(path)
  RealFile.split(path)
end
stat(file) click to toggle source
# File lib/fakefs/file.rb, line 262
def self.stat(file)
  File::Stat.new(file)
end
sticky?(_path) click to toggle source

Assume nothing is sticky.

# File lib/fakefs/file.rb, line 59
def sticky?(_path)
  false
end
umask(*args) click to toggle source
# File lib/fakefs/file.rb, line 301
def self.umask(*args)
  RealFile.umask(*args)
end
utime(atime, mtime, *paths) click to toggle source
# File lib/fakefs/file.rb, line 88
def self.utime(atime, mtime, *paths)
  paths.each do |path|
    if exists?(path)
      FileSystem.find(path).atime = atime
      FileSystem.find(path).mtime = mtime
    else
      fail Errno::ENOENT
    end
  end

  paths.size
end
writable?(path)
Alias for: exist?
write(filename, contents, offset = nil, open_args = {}) click to toggle source
# File lib/fakefs/file.rb, line 572
def self.write(filename, contents, offset = nil, open_args = {})
  offset, open_args = nil, offset if offset.is_a?(Hash)
  mode = offset ? 'a' : 'w'
  if open_args.size > 0
    if open_args[:open_args]
      args = [filename, *open_args[:open_args]]
    else
      mode = open_args[:mode] || mode
      args = [filename, mode, open_args]
    end
  else
    args = [filename, mode]
  end
  if offset
    open(*args) do |f|
      f.seek(offset)
      f.write(contents)
    end
  else
    open(*args) do |f|
      f << contents
    end
  end

  contents.length
end
zero?(path) click to toggle source
# File lib/fakefs/file.rb, line 109
def self.zero?(path)
  exists?(path) && size(path) == 0
end
Also aliased as: empty?

Public Instance Methods

advise(_advice, _offset = 0, _len = 0) click to toggle source
# File lib/fakefs/file.rb, line 569
def advise(_advice, _offset = 0, _len = 0)
end
atime() click to toggle source
# File lib/fakefs/file.rb, line 490
def atime
  self.class.atime(@path)
end
autoclose?() click to toggle source
# File lib/fakefs/file.rb, line 553
def autoclose?
  @autoclose ? true : false
end
binmode?() click to toggle source
# File lib/fakefs/file.rb, line 527
def binmode?
  fail NotImplementedError
end
birthtime() click to toggle source
# File lib/fakefs/file.rb, line 609
def birthtime
  self.class.birthtime(@path)
end
chmod(mode_int) click to toggle source
# File lib/fakefs/file.rb, line 506
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 510
def chown(owner_int, group_int)
  return unless group_int && group_int != -1

  owner_int.is_a?(Fixnum) || fail(
    TypeError, "can't convert String into Integer")
  @file.uid = owner_int

  group_int.is_a?(Fixnum) || fail(
    TypeError, "can't convert String into Integer")
  @file.gid = group_int
end
close_on_exec=(_bool) click to toggle source
# File lib/fakefs/file.rb, line 531
def close_on_exec=(_bool)
  fail NotImplementedError
end
close_on_exec?() click to toggle source
# File lib/fakefs/file.rb, line 535
def close_on_exec?
  fail NotImplementedError
end
ctime() click to toggle source
# File lib/fakefs/file.rb, line 494
def ctime
  self.class.ctime(@path)
end
exists?() click to toggle source
# File lib/fakefs/file.rb, line 422
def exists?
  true
end
flock(*) click to toggle source
# File lib/fakefs/file.rb, line 498
def flock(*)
  fail NotImplementedError
end
ioctl(*) click to toggle source
# File lib/fakefs/file.rb, line 455
def ioctl(*)
  fail NotImplementedError
end
is_a?(klass) click to toggle source
# File lib/fakefs/file.rb, line 451
def is_a?(klass)
  RealFile.allocate.is_a?(klass)
end
lstat() click to toggle source
# File lib/fakefs/file.rb, line 467
def lstat
  self.class.lstat(@path)
end
mtime() click to toggle source
# File lib/fakefs/file.rb, line 502
def mtime
  self.class.mtime(@path)
end
read(length = nil, buf = '') click to toggle source
Calls superclass method
# File lib/fakefs/file.rb, line 614
def read(length = nil, buf = '')
  read_buf = super(length, buf)
  # change to binary only for ruby 1.9.3
  if read_buf.respond_to?(:force_encoding) && binary_mode?
    read_buf = read_buf.force_encoding('ASCII-8BIT')
  end
  read_buf
end
Also aliased as: sysread
read_nonblock() click to toggle source
# File lib/fakefs/file.rb, line 459
def read_nonblock
  fail NotImplementedError
end
readpartial(*) click to toggle source
# File lib/fakefs/file.rb, line 486
def readpartial(*)
  fail NotImplementedError
end
size() click to toggle source
# File lib/fakefs/file.rb, line 559
def size
  File.size(@path)
end
stat() click to toggle source
# File lib/fakefs/file.rb, line 463
def stat
  self.class.stat(@path)
end
sysread(length = nil, buf = '')
Alias for: read
sysseek(position, whence = SEEK_SET) click to toggle source
# File lib/fakefs/file.rb, line 471
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 478
def to_io
  self
end
to_path() click to toggle source
# File lib/fakefs/file.rb, line 539
def to_path
  @path
end
write(str) click to toggle source
Calls superclass method
# File lib/fakefs/file.rb, line 426
def write(str)
  val = super(str)
  @file.mtime = Time.now
  val
end
Also aliased as: syswrite
write_nonblock(*) click to toggle source
# File lib/fakefs/file.rb, line 482
def write_nonblock(*)
  fail NotImplementedError
end

Private Instance Methods

binary_mode?() click to toggle source
# File lib/fakefs/file.rb, line 629
def binary_mode?
  @mode.is_a?(String) && (@mode.include?('b') ||
                          @mode.include?('binary')) &&
    !@mode.include?('bom')
end
check_file_existence!() click to toggle source
# File lib/fakefs/file.rb, line 635
def check_file_existence!
  fail Errno::ENOENT, @path unless @file
end
check_modes!() click to toggle source
# File lib/fakefs/file.rb, line 625
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 655
def create_missing_file
  fail Errno::EISDIR, path if File.directory?(@path)

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

  unless dirname == '.'
    dir = FileSystem.find dirname

    fail Errno::ENOENT, path unless dir.is_a? FakeDir
  end

  @file = FileSystem.add(path, FakeFile.new)
end
file_creation_mode?() click to toggle source
# File lib/fakefs/file.rb, line 639
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 643
def mode_in?(list)
  list.any? do |element|
    @mode.include?(element)
  end if @mode.respond_to?(:include?)
end
mode_in_bitmask?(mask) click to toggle source
# File lib/fakefs/file.rb, line 649
def mode_in_bitmask?(mask)
  (@mode & mask) != 0 if @mode.is_a?(Integer)
end