Class: FakeFS::File
- Inherits:
-
StringIO
- Object
- StringIO
- FakeFS::File
- Defined in:
- lib/fakefs/file.rb
Overview
FakeFS File class inherit StringIO
Defined Under Namespace
Classes: Stat
Constant Summary collapse
- MODES =
[ READ_ONLY = 'r', READ_WRITE = 'r+', WRITE_ONLY = 'w', READ_WRITE_TRUNCATE = 'w+', APPEND_WRITE_ONLY = 'a', APPEND_READ_WRITE = 'a+' ]
- 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.const_defined?(:NOCTTY) ? RealFile::NOCTTY : 0) | (RealFile.const_defined?(:SYNC) ? RealFile::SYNC : 0)
- FILE_CREATION_BITMASK =
RealFile::CREAT
Instance Attribute Summary collapse
-
#autoclose ⇒ Object
Returns the value of attribute autoclose.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .absolute_path(file_name, dir_name = Dir.getwd) ⇒ Object
- .atime(path) ⇒ Object
- .basename(*args) ⇒ Object
- .binread(file, length = nil, offset = 0) ⇒ Object
- .chmod(mode_int, filename) ⇒ Object
- .chown(owner_int, group_int, filename) ⇒ Object
- .const_missing(name) ⇒ Object
- .ctime(path) ⇒ Object
- .delete(*file_names) ⇒ Object (also: unlink)
- .directory?(path) ⇒ Boolean
- .dirname(path) ⇒ Object
-
.executable?(filename) ⇒ Boolean
Not exactly right, returns true if the file is chmod +x for owner.
- .exist?(path) ⇒ Boolean (also: exists?, readable?, writable?)
- .expand_path(file_name, dir_string = FileSystem.current_dir.to_s) ⇒ Object
- .extname(path) ⇒ Object
- .file?(path) ⇒ Boolean
- .join(*parts) ⇒ Object
- .link(source, dest) ⇒ Object
- .lstat(file) ⇒ Object
- .mtime(path) ⇒ Object
- .read(path, *args) ⇒ Object
- .readlines(path) ⇒ Object
- .readlink(path) ⇒ Object
- .realpath(*args) ⇒ Object
- .rename(source, dest) ⇒ Object
- .size(path) ⇒ Object
- .size?(path) ⇒ Boolean
- .split(path) ⇒ Object
- .stat(file) ⇒ Object
-
.sticky?(_path) ⇒ Boolean
Assume nothing is sticky.
- .symlink(source, dest) ⇒ Object
- .symlink?(path) ⇒ Boolean
- .umask(*args) ⇒ Object
- .utime(atime, mtime, *paths) ⇒ Object
- .write(filename, contents, offset = nil, open_args = {}) ⇒ Object
- .zero?(path) ⇒ Boolean
Instance Method Summary collapse
- #advise(_advice, _offset = 0, _len = 0) ⇒ Object
- #atime ⇒ Object
- #autoclose? ⇒ Boolean
- #binmode? ⇒ Boolean
- #chmod(mode_int) ⇒ Object
- #chown(owner_int, group_int) ⇒ Object
- #close_on_exec=(_bool) ⇒ Object
- #close_on_exec? ⇒ Boolean
- #ctime ⇒ Object
- #exists? ⇒ Boolean
- #flock ⇒ Object
-
#initialize(path, mode = READ_ONLY, _perm = nil) ⇒ File
constructor
A new instance of File.
- #ioctl ⇒ Object
- #is_a?(klass) ⇒ Boolean
- #lstat ⇒ Object
- #mtime ⇒ Object
- #read(length = nil, buf = '') ⇒ Object
- #read_nonblock ⇒ Object
- #readpartial ⇒ Object
- #size ⇒ Object
- #stat ⇒ Object
- #sysread ⇒ Object
- #sysseek(position, whence = SEEK_SET) ⇒ Object
- #syswrite ⇒ Object
- #to_io ⇒ Object
- #to_path ⇒ Object
- #write(str) ⇒ Object
- #write_nonblock ⇒ Object
Constructor Details
#initialize(path, mode = READ_ONLY, _perm = nil) ⇒ File
Returns a new instance of File.
364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/fakefs/file.rb', line 364 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 |
Instance Attribute Details
#autoclose ⇒ Object
Returns the value of attribute autoclose
503 504 505 |
# File 'lib/fakefs/file.rb', line 503 def autoclose @autoclose end |
#path ⇒ Object (readonly)
Returns the value of attribute path
362 363 364 |
# File 'lib/fakefs/file.rb', line 362 def path @path end |
Class Method Details
.absolute_path(file_name, dir_name = Dir.getwd) ⇒ Object
497 498 499 |
# File 'lib/fakefs/file.rb', line 497 def self.absolute_path(file_name, dir_name = Dir.getwd) RealFile.absolute_path(file_name, dir_name) end |
.atime(path) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/fakefs/file.rb', line 76 def self.atime(path) if exists?(path) FileSystem.find(path).atime else fail Errno::ENOENT end end |
.basename(*args) ⇒ Object
147 148 149 |
# File 'lib/fakefs/file.rb', line 147 def self.basename(*args) RealFile.basename(*args) end |
.binread(file, length = nil, offset = 0) ⇒ Object
275 276 277 |
# File 'lib/fakefs/file.rb', line 275 def self.binread(file, length = nil, offset = 0) File.read(file, length, offset, mode: 'rb:ASCII-8BIT') end |
.chmod(mode_int, filename) ⇒ Object
245 246 247 |
# File 'lib/fakefs/file.rb', line 245 def self.chmod(mode_int, filename) FileSystem.find(filename).mode = 0100000 + mode_int end |
.chown(owner_int, group_int, filename) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/fakefs/file.rb', line 257 def self.chown(owner_int, group_int, filename) file = FileSystem.find(filename) if owner_int && owner_int != -1 owner_int.is_a?(Fixnum) || fail(TypeError, "can't convert String into Integer") file.uid = owner_int end if group_int && group_int != -1 group_int.is_a?(Fixnum) || fail(TypeError, "can't convert String into Integer") file.gid = group_int end end |
.const_missing(name) ⇒ Object
113 114 115 |
# File 'lib/fakefs/file.rb', line 113 def self.const_missing(name) RealFile.const_get(name) end |
.ctime(path) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/fakefs/file.rb', line 68 def self.ctime(path) if exists?(path) FileSystem.find(path).ctime else fail Errno::ENOENT end end |
.delete(*file_names) ⇒ Object Also known as: unlink
215 216 217 218 219 220 221 222 223 |
# File 'lib/fakefs/file.rb', line 215 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 |
.directory?(path) ⇒ Boolean
117 118 119 120 121 122 123 124 |
# File 'lib/fakefs/file.rb', line 117 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) ⇒ Object
151 152 153 |
# File 'lib/fakefs/file.rb', line 151 def self.dirname(path) RealFile.dirname(path) end |
.executable?(filename) ⇒ Boolean
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.
251 252 253 254 255 |
# File 'lib/fakefs/file.rb', line 251 def self.executable?(filename) file = FileSystem.find(filename) return false unless file (file.mode - 0100000) & 0100 != 0 end |
.exist?(path) ⇒ Boolean Also known as: exists?, readable?, writable?
38 39 40 41 42 43 44 45 |
# File 'lib/fakefs/file.rb', line 38 def self.exist?(path) if File.symlink?(path) referent = File.(File.readlink(path), File.dirname(path)) exist?(referent) else !FileSystem.find(path).nil? end end |
.expand_path(file_name, dir_string = FileSystem.current_dir.to_s) ⇒ Object
143 144 145 |
# File 'lib/fakefs/file.rb', line 143 def self.(file_name, dir_string = FileSystem.current_dir.to_s) RealFile.(file_name, RealFile.(dir_string, Dir.pwd)) end |
.extname(path) ⇒ Object
30 31 32 |
# File 'lib/fakefs/file.rb', line 30 def self.extname(path) RealFile.extname(path) end |
.file?(path) ⇒ Boolean
134 135 136 137 138 139 140 141 |
# File 'lib/fakefs/file.rb', line 134 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) ⇒ Object
34 35 36 |
# File 'lib/fakefs/file.rb', line 34 def self.join(*parts) RealFile.join(parts) end |
.link(source, dest) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/fakefs/file.rb', line 203 def self.link(source, dest) fail Errno::EPERM, "#{source} or #{dest}" if directory?(source) fail Errno::ENOENT, "#{source} or #{dest}" unless exists?(source) fail Errno::EEXIST, "#{source} or #{dest}" if exists?(dest) source = FileSystem.find(source) dest = FileSystem.add(dest, source.entry.clone) source.link(dest) 0 end |
.lstat(file) ⇒ Object
237 238 239 |
# File 'lib/fakefs/file.rb', line 237 def self.lstat(file) File::Stat.new(file, true) end |
.mtime(path) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/fakefs/file.rb', line 60 def self.mtime(path) if exists?(path) FileSystem.find(path).mtime else fail Errno::ENOENT end end |
.read(path, *args) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/fakefs/file.rb', line 160 def self.read(path, *args) = args[-1].is_a?(Hash) ? args.pop : {} length = args.size > 0 ? args.shift : nil offset = args.size > 0 ? args.shift : 0 file = new(path, ) 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 |
.readlines(path) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/fakefs/file.rb', line 174 def self.readlines(path) file = new(path) if file.exists? FileSystem.find(path).atime = Time.now file.readlines else fail Errno::ENOENT end end |
.readlink(path) ⇒ Object
155 156 157 158 |
# File 'lib/fakefs/file.rb', line 155 def self.readlink(path) symlink = FileSystem.find(path) symlink.target end |
.realpath(*args) ⇒ Object
475 476 477 |
# File 'lib/fakefs/file.rb', line 475 def self.realpath(*args) RealFile.realpath(*args) end |
.rename(source, dest) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/fakefs/file.rb', line 184 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)) FileSystem.add(dest, target.entry.clone) FileSystem.delete(source) else fail Errno::ENOENT, "#{source} or #{dest}" end 0 end |
.size(path) ⇒ Object
97 98 99 |
# File 'lib/fakefs/file.rb', line 97 def self.size(path) read(path).bytesize end |
.size?(path) ⇒ Boolean
101 102 103 104 105 106 107 |
# File 'lib/fakefs/file.rb', line 101 def self.size?(path) if exists?(path) && !size(path).zero? size(path) else nil end end |
.split(path) ⇒ Object
241 242 243 |
# File 'lib/fakefs/file.rb', line 241 def self.split(path) RealFile.split(path) end |
.stat(file) ⇒ Object
233 234 235 |
# File 'lib/fakefs/file.rb', line 233 def self.stat(file) File::Stat.new(file) end |
.sticky?(_path) ⇒ Boolean
Assume nothing is sticky.
55 56 57 |
# File 'lib/fakefs/file.rb', line 55 def sticky?(_path) false end |
.symlink(source, dest) ⇒ Object
229 230 231 |
# File 'lib/fakefs/file.rb', line 229 def self.symlink(source, dest) FileUtils.ln_s(source, dest) end |
.symlink?(path) ⇒ Boolean
126 127 128 129 130 131 132 |
# File 'lib/fakefs/file.rb', line 126 def self.symlink?(path) if path.respond_to? :entry path.is_a? FakeSymlink else FileSystem.find(path).is_a? FakeSymlink end end |
.umask(*args) ⇒ Object
271 272 273 |
# File 'lib/fakefs/file.rb', line 271 def self.umask(*args) RealFile.umask(*args) end |
.utime(atime, mtime, *paths) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/fakefs/file.rb', line 84 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 |
.write(filename, contents, offset = nil, open_args = {}) ⇒ Object
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/fakefs/file.rb', line 520 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) ⇒ Boolean
109 110 111 |
# File 'lib/fakefs/file.rb', line 109 def self.zero?(path) exists?(path) && size(path) == 0 end |
Instance Method Details
#advise(_advice, _offset = 0, _len = 0) ⇒ Object
517 518 |
# File 'lib/fakefs/file.rb', line 517 def advise(_advice, _offset = 0, _len = 0) end |
#atime ⇒ Object
442 443 444 |
# File 'lib/fakefs/file.rb', line 442 def atime self.class.atime(@path) end |
#autoclose? ⇒ Boolean
505 506 507 |
# File 'lib/fakefs/file.rb', line 505 def autoclose? @autoclose ? true : false end |
#binmode? ⇒ Boolean
479 480 481 |
# File 'lib/fakefs/file.rb', line 479 def binmode? fail NotImplementedError end |
#chmod(mode_int) ⇒ Object
458 459 460 |
# File 'lib/fakefs/file.rb', line 458 def chmod(mode_int) @file.mode = 0100000 + mode_int end |
#chown(owner_int, group_int) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/fakefs/file.rb', line 462 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) ⇒ Object
483 484 485 |
# File 'lib/fakefs/file.rb', line 483 def close_on_exec=(_bool) fail NotImplementedError end |
#close_on_exec? ⇒ Boolean
487 488 489 |
# File 'lib/fakefs/file.rb', line 487 def close_on_exec? fail NotImplementedError end |
#ctime ⇒ Object
446 447 448 |
# File 'lib/fakefs/file.rb', line 446 def ctime self.class.ctime(@path) end |
#exists? ⇒ Boolean
377 378 379 |
# File 'lib/fakefs/file.rb', line 377 def exists? true end |
#flock ⇒ Object
450 451 452 |
# File 'lib/fakefs/file.rb', line 450 def flock(*) fail NotImplementedError end |
#ioctl ⇒ Object
407 408 409 |
# File 'lib/fakefs/file.rb', line 407 def ioctl(*) fail NotImplementedError end |
#is_a?(klass) ⇒ Boolean
403 404 405 |
# File 'lib/fakefs/file.rb', line 403 def is_a?(klass) RealFile.allocate.is_a?(klass) end |
#lstat ⇒ Object
419 420 421 |
# File 'lib/fakefs/file.rb', line 419 def lstat self.class.lstat(@path) end |
#mtime ⇒ Object
454 455 456 |
# File 'lib/fakefs/file.rb', line 454 def mtime self.class.mtime(@path) end |
#read(length = nil, buf = '') ⇒ Object
548 549 550 551 552 553 554 555 |
# File 'lib/fakefs/file.rb', line 548 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 |
#read_nonblock ⇒ Object
411 412 413 |
# File 'lib/fakefs/file.rb', line 411 def read_nonblock fail NotImplementedError end |
#readpartial ⇒ Object
438 439 440 |
# File 'lib/fakefs/file.rb', line 438 def readpartial(*) fail NotImplementedError end |
#stat ⇒ Object
415 416 417 |
# File 'lib/fakefs/file.rb', line 415 def stat self.class.stat(@path) end |
#sysread ⇒ Object
388 |
# File 'lib/fakefs/file.rb', line 388 alias_method :sysread, :read |
#sysseek(position, whence = SEEK_SET) ⇒ Object
423 424 425 426 |
# File 'lib/fakefs/file.rb', line 423 def sysseek(position, whence = SEEK_SET) seek(position, whence) pos end |
#syswrite ⇒ Object
389 390 391 392 393 |
# File 'lib/fakefs/file.rb', line 389 def write(str) val = super(str) @file.mtime = Time.now val end |
#to_io ⇒ Object
430 431 432 |
# File 'lib/fakefs/file.rb', line 430 def to_io self end |
#to_path ⇒ Object
491 492 493 |
# File 'lib/fakefs/file.rb', line 491 def to_path @path end |
#write(str) ⇒ Object
381 382 383 384 385 |
# File 'lib/fakefs/file.rb', line 381 def write(str) val = super(str) @file.mtime = Time.now val end |
#write_nonblock ⇒ Object
434 435 436 |
# File 'lib/fakefs/file.rb', line 434 def write_nonblock(*) fail NotImplementedError end |