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 142 def self.basename(*args) RealFile.basename(*args) end
binread(file, length = nil, offset = 0)
click to toggle source
# File lib/fakefs/file.rb, line 274 def self.binread(file, length = nil, offset = 0) contents = File.read(file) if length contents.slice(offset, length) else contents end end
chmod(mode_int, filename)
click to toggle source
# File lib/fakefs/file.rb, line 246 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 258 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 107 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 212 def self.delete(file_name, *additional_file_names) if !exists?(file_name) raise Errno::ENOENT, 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 111 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 146 def self.dirname(path) RealFile.dirname(path) end
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 252 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 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
expand_path(file_name, dir_string=FileSystem.current_dir.to_s)
click to toggle source
# File lib/fakefs/file.rb, line 137 def self.expand_path(file_name, dir_string=FileSystem.current_dir.to_s) dir_string = FileSystem.find(dir_string).to_s RealFile.expand_path(file_name, dir_string) 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 128 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
link(source, dest)
click to toggle source
# File lib/fakefs/file.rb, line 192 def self.link(source, dest) if directory?(source) raise Errno::EPERM, "#{source} or #{dest}" end if !exists?(source) raise Errno::ENOENT, "#{source} or #{dest}" end if exists?(dest) raise Errno::EEXIST, "#{source} or #{dest}" end source = FileSystem.find(source) dest = FileSystem.add(dest, source.entry.clone) source.link(dest) 0 end
lstat(file)
click to toggle source
# File lib/fakefs/file.rb, line 238 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 355 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 155 def self.read(path, *args) file = new(path) raise Errno::ENOENT if !file.exists? raise Errno::EISDIR, path if directory?(path) FileSystem.find(path).atime = Time.now file.read end
readlines(path)
click to toggle source
# File lib/fakefs/file.rb, line 165 def self.readlines(path) file = new(path) if file.exists? FileSystem.find(path).atime = Time.now file.readlines else raise Errno::ENOENT end end
readlink(path)
click to toggle source
# File lib/fakefs/file.rb, line 150 def self.readlink(path) symlink = FileSystem.find(path) symlink.target end
realpath(*args)
click to toggle source
# File lib/fakefs/file.rb, line 465 def self.realpath(*args) RealFile.realpath(*args) end
rename(source, dest)
click to toggle source
# File lib/fakefs/file.rb, line 175 def self.rename(source, dest) if directory?(source) && file?(dest) raise Errno::ENOTDIR, "#{source} or #{dest}" elsif file?(source) && directory?(dest) raise Errno::EISDIR, "#{source} or #{dest}" end if target = FileSystem.find(source) FileSystem.add(dest, target.entry.clone) FileSystem.delete(source) else raise Errno::ENOENT, "#{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).bytesize end
size?(path)
click to toggle source
# File lib/fakefs/file.rb, line 95 def self.size?(path) if exists?(path) && !size(path).zero? size(path) else nil end end
split(path)
click to toggle source
# File lib/fakefs/file.rb, line 242 def self.split(path) return RealFile.split(path) end
stat(file)
click to toggle source
# File lib/fakefs/file.rb, line 234 def self.stat(file) File::Stat.new(file) end
symlink(source, dest)
click to toggle source
# File lib/fakefs/file.rb, line 230 def self.symlink(source, dest) FileUtils.ln_s(source, dest) end
symlink?(path)
click to toggle source
# File lib/fakefs/file.rb, line 120 def self.symlink?(path) if path.respond_to? :entry path.is_a? FakeSymlink else FileSystem.find(path).is_a? FakeSymlink end end
umask(*args)
click to toggle source
# File lib/fakefs/file.rb, line 270 def self.umask(*args) RealFile.umask(*args) 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
write(filename, contents, offset = nil)
click to toggle source
# File lib/fakefs/file.rb, line 512 def self.write(filename, contents, offset = nil) if offset open(filename, 'a') do |f| f.seek(offset) f.write(contents) end else open(filename, 'w') do |f| f << contents end end contents.length end
zero?(path)
click to toggle source
# File lib/fakefs/file.rb, line 103 def self.zero?(path) exists?(path) && size(path) == 0 end
Public Instance Methods
advise(advice, offset=0, len=0)
click to toggle source
# File lib/fakefs/file.rb, line 509 def advise(advice, offset=0, len=0) end
atime()
click to toggle source
# File lib/fakefs/file.rb, line 433 def atime self.class.atime(@path) end
autoclose=(autoclose)
click to toggle source
# File lib/fakefs/file.rb, line 497 def autoclose=(autoclose) @autoclose = autoclose end
autoclose?()
click to toggle source
# File lib/fakefs/file.rb, line 489 def autoclose? @autoclose end
binmode?()
click to toggle source
# File lib/fakefs/file.rb, line 469 def binmode? raise NotImplementedError end
chmod(mode_int)
click to toggle source
# File lib/fakefs/file.rb, line 449 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 453 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 473 def close_on_exec=(bool) raise NotImplementedError end
close_on_exec?()
click to toggle source
# File lib/fakefs/file.rb, line 477 def close_on_exec? raise NotImplementedError end
ctime()
click to toggle source
# File lib/fakefs/file.rb, line 437 def ctime self.class.ctime(@path) end
exists?()
click to toggle source
# File lib/fakefs/file.rb, line 368 def exists? true end
flock(locking_constant)
click to toggle source
# File lib/fakefs/file.rb, line 441 def flock(locking_constant) raise NotImplementedError end
ioctl(integer_cmd, arg)
click to toggle source
# File lib/fakefs/file.rb, line 398 def ioctl(integer_cmd, arg) raise NotImplementedError end
is_a?(klass)
click to toggle source
# File lib/fakefs/file.rb, line 394 def is_a?(klass) RealFile.allocate.is_a?(klass) end
lstat()
click to toggle source
# File lib/fakefs/file.rb, line 410 def lstat self.class.lstat(@path) end
mtime()
click to toggle source
# File lib/fakefs/file.rb, line 445 def mtime self.class.mtime(@path) end
read(length = nil, buf = "")
click to toggle source
Calls superclass method
# File lib/fakefs/file.rb, line 528 def read(length = nil, buf = "") read_buf = super(length, buf) if read_buf.respond_to?(:force_encoding) && binary_mode? #change to binary only for ruby 1.9.3 read_buf = read_buf.force_encoding('ASCII-8BIT') end read_buf end
Also aliased as: sysread
read_nonblock(maxlen, outbuf = nil)
click to toggle source
# File lib/fakefs/file.rb, line 402 def read_nonblock(maxlen, outbuf = nil) raise NotImplementedError end
readpartial(maxlen, outbuf = nil)
click to toggle source
# File lib/fakefs/file.rb, line 429 def readpartial(maxlen, outbuf = nil) raise NotImplementedError end
size()
click to toggle source
# File lib/fakefs/file.rb, line 503 def size File.size(@path) end
stat()
click to toggle source
# File lib/fakefs/file.rb, line 406 def stat self.class.stat(@path) end
sysseek(position, whence = SEEK_SET)
click to toggle source
# File lib/fakefs/file.rb, line 414 def sysseek(position, whence = SEEK_SET) seek(position, whence) pos end
to_io()
click to toggle source
# File lib/fakefs/file.rb, line 421 def to_io self end
to_path()
click to toggle source
# File lib/fakefs/file.rb, line 481 def to_path @path end
write(str)
click to toggle source
Calls superclass method
# File lib/fakefs/file.rb, line 372 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 425 def write_nonblock(string) raise NotImplementedError end
Private Instance Methods
binary_mode?()
click to toggle source
# File lib/fakefs/file.rb, line 542 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 546 def check_file_existence! raise Errno::ENOENT, @path unless @file end
check_modes!()
click to toggle source
# File lib/fakefs/file.rb, line 538 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 564 def create_missing_file raise Errno::EISDIR, 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, path end end @file = FileSystem.add(path, FakeFile.new) end end
file_creation_mode?()
click to toggle source
# File lib/fakefs/file.rb, line 550 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 554 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 558 def mode_in_bitmask?(mask) (@mode & mask) != 0 if @mode.is_a?(Integer) end