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'.freeze, READ_WRITE = 'r+'.freeze, WRITE_ONLY = 'w'.freeze, READ_WRITE_TRUNCATE = 'w+'.freeze, APPEND_WRITE_ONLY = 'a'.freeze, APPEND_READ_WRITE = 'a+'.freeze ].freeze
- FILE_CREATION_MODES =
(MODES - [READ_ONLY, READ_WRITE]).freeze
- 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
- .birthtime(path) ⇒ Object
- .chmod(new_mode, filename) ⇒ Object
- .chown(owner_int, group_int, filename) ⇒ Object
- .const_missing(name) ⇒ Object
- .ctime(path) ⇒ Object
- .delete(*files) ⇒ 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?)
- .expand_path(file_name, dir_string = FileSystem.current_dir.to_s) ⇒ Object
- .extname(path) ⇒ Object
- .file?(path) ⇒ Boolean
- .fnmatch?(pattern, path, flags = 0) ⇒ Boolean (also: fnmatch)
- .foreach(path, *args, &block) ⇒ Object
- .ftype(filename) ⇒ Object
- .identical?(one_path, another_path) ⇒ Boolean
- .join(*parts) ⇒ Object
- .link(source, dest) ⇒ Object
- .lstat(file) ⇒ Object
- .mtime(path) ⇒ Object
- .path(file) ⇒ Object
- .read(path, *args) ⇒ Object
- .readable?(path) ⇒ Boolean
- .readlines(path, chomp: false) ⇒ Object
- .readlink(path) ⇒ Object
- .realdirpath(*args) ⇒ 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
- .writable?(path) ⇒ Boolean
- .write(filename, contents, offset = nil, open_args = {}) ⇒ Object
- .zero?(path) ⇒ Boolean (also: empty?)
Instance Method Summary collapse
- #advise(_advice, _offset = 0, _len = 0) ⇒ Object
- #atime ⇒ Object
- #autoclose? ⇒ Boolean
- #binmode? ⇒ Boolean
- #birthtime ⇒ Object
- #chmod(new_mode) ⇒ 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
- #string ⇒ Object
- #sysread ⇒ Object
- #sysseek(position, whence = SEEK_SET) ⇒ Object
- #syswrite ⇒ Object
- #to_io ⇒ Object
- #to_path ⇒ Object
- #write(*args) ⇒ Object
- #write_nonblock ⇒ Object
Constructor Details
#initialize(path, mode = READ_ONLY, _perm = nil) ⇒ File
Returns a new instance of File.
490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/fakefs/file.rb', line 490 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.
641 642 643 |
# File 'lib/fakefs/file.rb', line 641 def autoclose @autoclose end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
488 489 490 |
# File 'lib/fakefs/file.rb', line 488 def path @path end |
Class Method Details
.absolute_path(file_name, dir_name = Dir.getwd) ⇒ Object
637 638 639 |
# File 'lib/fakefs/file.rb', line 637 def self.absolute_path(file_name, dir_name = Dir.getwd) RealFile.absolute_path(file_name, dir_name) end |
.atime(path) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/fakefs/file.rb', line 92 def self.atime(path) if exists?(path) FileSystem.find(path).atime else raise Errno::ENOENT end end |
.basename(*args) ⇒ Object
173 174 175 |
# File 'lib/fakefs/file.rb', line 173 def self.basename(*args) RealFile.basename(*args) end |
.binread(file, length = nil, offset = 0) ⇒ Object
330 331 332 |
# File 'lib/fakefs/file.rb', line 330 def self.binread(file, length = nil, offset = 0) File.read(file, length, offset, mode: 'rb:ASCII-8BIT') end |
.birthtime(path) ⇒ Object
686 687 688 689 690 691 692 |
# File 'lib/fakefs/file.rb', line 686 def self.birthtime(path) if exists?(path) FileSystem.find(path).birthtime else raise Errno::ENOENT end end |
.chmod(new_mode, filename) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/fakefs/file.rb', line 293 def self.chmod(new_mode, filename) # chmod's mode can either be passed in in absolute mode, or symbolic mode # for reference: https://ruby-doc.org/stdlib-2.2.2/libdoc/fileutils/rdoc/FileUtils.html#method-c-chmod # if the mode is passed in symbolic mode we must convert it to absolute mode is_absolute_mode = new_mode.is_a? Numeric unless is_absolute_mode current_mode = FileSystem.find(filename).mode new_mode = convert_symbolic_chmod_to_absolute(new_mode, current_mode) end FileSystem.find(filename).mode = 0o100000 + new_mode end |
.chown(owner_int, group_int, filename) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/fakefs/file.rb', line 313 def self.chown(owner_int, group_int, filename) file = FileSystem.find(filename) if owner_int && owner_int != -1 owner_int.is_a?(Integer) || raise(TypeError, "can't convert String into Integer") file.uid = owner_int end if group_int && group_int != -1 group_int.is_a?(Integer) || raise(TypeError, "can't convert String into Integer") file.gid = group_int end end |
.const_missing(name) ⇒ Object
135 136 137 |
# File 'lib/fakefs/file.rb', line 135 def self.const_missing(name) RealFile.const_get(name) end |
.ctime(path) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/fakefs/file.rb', line 84 def self.ctime(path) if exists?(path) FileSystem.find(path).ctime else raise Errno::ENOENT end end |
.delete(*files) ⇒ Object Also known as: unlink
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/fakefs/file.rb', line 262 def self.delete(*files) files.each do |file| file_name = (file.class == FakeFS::File ? file.path : file.to_s) raise Errno::ENOENT, file_name unless exists?(file_name) FileUtils.rm(file_name) end files.size end |
.directory?(path) ⇒ Boolean
139 140 141 142 143 144 145 146 |
# File 'lib/fakefs/file.rb', line 139 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
177 178 179 |
# File 'lib/fakefs/file.rb', line 177 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.
307 308 309 310 311 |
# File 'lib/fakefs/file.rb', line 307 def self.executable?(filename) file = FileSystem.find(filename) return false unless file (file.mode - 0o100000) & 0o100 != 0 end |
.exist?(path) ⇒ Boolean Also known as: exists?
44 45 46 47 48 49 50 51 |
# File 'lib/fakefs/file.rb', line 44 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
169 170 171 |
# File 'lib/fakefs/file.rb', line 169 def self.(file_name, dir_string = FileSystem.current_dir.to_s) RealFile.(file_name, RealFile.(dir_string, Dir.pwd)) end |
.extname(path) ⇒ Object
32 33 34 |
# File 'lib/fakefs/file.rb', line 32 def self.extname(path) RealFile.extname(path) end |
.file?(path) ⇒ Boolean
156 157 158 159 160 161 162 163 |
# File 'lib/fakefs/file.rb', line 156 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) ⇒ Boolean Also known as: fnmatch
334 335 336 |
# File 'lib/fakefs/file.rb', line 334 def self.fnmatch?(pattern, path, flags = 0) RealFile.fnmatch?(pattern, path, flags) end |
.foreach(path, *args, &block) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/fakefs/file.rb', line 210 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 raise Errno::ENOENT end end |
.ftype(filename) ⇒ Object
165 166 167 |
# File 'lib/fakefs/file.rb', line 165 def self.ftype(filename) File.lstat(filename).ftype end |
.identical?(one_path, another_path) ⇒ Boolean
56 57 58 |
# File 'lib/fakefs/file.rb', line 56 def identical?(one_path, another_path) FileSystem.find(one_path) == FileSystem.find(another_path) end |
.join(*parts) ⇒ Object
36 37 38 |
# File 'lib/fakefs/file.rb', line 36 def self.join(*parts) RealFile.join(parts) end |
.link(source, dest) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/fakefs/file.rb', line 250 def self.link(source, dest) raise Errno::EPERM, "#{source} or #{dest}" if directory?(source) raise Errno::ENOENT, "#{source} or #{dest}" unless exists?(source) raise 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
285 286 287 |
# File 'lib/fakefs/file.rb', line 285 def self.lstat(file) File::Stat.new(file, true) end |
.mtime(path) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/fakefs/file.rb', line 76 def self.mtime(path) if exists?(path) FileSystem.find(path).mtime else raise Errno::ENOENT end end |
.path(file) ⇒ Object
40 41 42 |
# File 'lib/fakefs/file.rb', line 40 def self.path(file) RealFile.path(file) end |
.read(path, *args) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/fakefs/file.rb', line 186 def self.read(path, *args) = args[-1].is_a?(Hash) ? args.pop : {} length = args.empty? ? nil : args.shift offset = args.empty? ? 0 : args.shift file = new(path, **) raise Errno::ENOENT unless file.exists? raise Errno::EISDIR, path.to_s if directory?(path) FileSystem.find(path).atime = Time.now file.seek(offset) file.read(length) end |
.readable?(path) ⇒ Boolean
66 67 68 69 |
# File 'lib/fakefs/file.rb', line 66 def self.readable?(path) return false unless exist? path File.lstat(path).readable? end |
.readlines(path, chomp: false) ⇒ Object
200 201 202 203 204 205 206 207 208 |
# File 'lib/fakefs/file.rb', line 200 def self.readlines(path, chomp: false) file = new(path) if file.exists? FileSystem.find(path).atime = Time.now chomp ? file.readlines.map(&:chomp) : file.readlines else raise Errno::ENOENT end end |
.readlink(path) ⇒ Object
181 182 183 184 |
# File 'lib/fakefs/file.rb', line 181 def self.readlink(path) symlink = FileSystem.find(path) symlink.target end |
.realdirpath(*args) ⇒ Object
653 654 655 |
# File 'lib/fakefs/file.rb', line 653 def self.realdirpath(*args) RealFile.realdirpath(*args) end |
.realpath(*args) ⇒ Object
617 618 619 |
# File 'lib/fakefs/file.rb', line 617 def self.realpath(*args) RealFile.realpath(*args) end |
.rename(source, dest) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/fakefs/file.rb', line 224 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}" elsif !exist?(dirname(dest)) raise Errno::ENOENT, "#{source} or #{dest}" end if (target = FileSystem.find(source)) return 0 if source == dest if target.is_a?(FakeFS::FakeSymlink) File.symlink(target.target, dest) else FileSystem.add(dest, target.entry.clone) end FileSystem.delete(source) else raise Errno::ENOENT, "#{source} or #{dest}" end 0 end |
.size(path) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/fakefs/file.rb', line 113 def self.size(path) if directory?(path) 64 + (32 * FileSystem.find(path).entries.size) else read(path).bytesize end end |
.size?(path) ⇒ Boolean
121 122 123 |
# File 'lib/fakefs/file.rb', line 121 def self.size?(path) size(path) if exists?(path) && !size(path).zero? end |
.split(path) ⇒ Object
289 290 291 |
# File 'lib/fakefs/file.rb', line 289 def self.split(path) RealFile.split(path) end |
.stat(file) ⇒ Object
281 282 283 |
# File 'lib/fakefs/file.rb', line 281 def self.stat(file) File::Stat.new(file) end |
.sticky?(_path) ⇒ Boolean
Assume nothing is sticky.
61 62 63 |
# File 'lib/fakefs/file.rb', line 61 def sticky?(_path) false end |
.symlink(source, dest) ⇒ Object
277 278 279 |
# File 'lib/fakefs/file.rb', line 277 def self.symlink(source, dest) FileUtils.ln_s(source, dest) end |
.symlink?(path) ⇒ Boolean
148 149 150 151 152 153 154 |
# File 'lib/fakefs/file.rb', line 148 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
326 327 328 |
# File 'lib/fakefs/file.rb', line 326 def self.umask(*args) RealFile.umask(*args) end |
.utime(atime, mtime, *paths) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/fakefs/file.rb', line 100 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) ⇒ Boolean
71 72 73 74 |
# File 'lib/fakefs/file.rb', line 71 def self.writable?(path) return false unless exist? path File.lstat(path).writable? end |
.write(filename, contents, offset = nil, open_args = {}) ⇒ Object
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
# File 'lib/fakefs/file.rb', line 659 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.any? 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| # rubocop:disable Security/Open f.seek(offset) f.write(contents) end else open(*args) do |f| # rubocop:disable Security/Open f << contents end end contents.length end |
.zero?(path) ⇒ Boolean Also known as: empty?
125 126 127 |
# File 'lib/fakefs/file.rb', line 125 def self.zero?(path) exists?(path) && size(path) == 0 end |
Instance Method Details
#advise(_advice, _offset = 0, _len = 0) ⇒ Object
657 |
# File 'lib/fakefs/file.rb', line 657 def advise(_advice, _offset = 0, _len = 0); end |
#atime ⇒ Object
575 576 577 |
# File 'lib/fakefs/file.rb', line 575 def atime self.class.atime(@path) end |
#autoclose? ⇒ Boolean
643 644 645 |
# File 'lib/fakefs/file.rb', line 643 def autoclose? @autoclose ? true : false end |
#binmode? ⇒ Boolean
621 622 623 |
# File 'lib/fakefs/file.rb', line 621 def binmode? raise NotImplementedError end |
#birthtime ⇒ Object
694 695 696 |
# File 'lib/fakefs/file.rb', line 694 def birthtime self.class.birthtime(@path) end |
#chmod(new_mode) ⇒ Object
591 592 593 594 595 596 597 598 599 600 601 |
# File 'lib/fakefs/file.rb', line 591 def chmod(new_mode) # chmod's mode can either be passed in in absolute mode, or symbolic mode # for reference: https://ruby-doc.org/stdlib-2.2.2/libdoc/fileutils/rdoc/FileUtils.html#method-c-chmod # if the mode is passed in symbolic mode we must convert it to absolute mode is_absolute_mode = new_mode.is_a? Numeric unless is_absolute_mode current_mode = @file.mode new_mode = convert_symbolic_chmod_to_absolute(new_mode, current_mode) end @file.mode = 0o100000 + new_mode end |
#chown(owner_int, group_int) ⇒ Object
603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/fakefs/file.rb', line 603 def chown(owner_int, group_int) return unless group_int && group_int != -1 owner_int.is_a?(Integer) || raise( TypeError, "can't convert String into Integer" ) @file.uid = owner_int group_int.is_a?(Integer) || raise( TypeError, "can't convert String into Integer" ) @file.gid = group_int end |
#close_on_exec=(_bool) ⇒ Object
625 626 627 |
# File 'lib/fakefs/file.rb', line 625 def close_on_exec=(_bool) raise NotImplementedError end |
#close_on_exec? ⇒ Boolean
629 630 631 |
# File 'lib/fakefs/file.rb', line 629 def close_on_exec? raise NotImplementedError end |
#ctime ⇒ Object
579 580 581 |
# File 'lib/fakefs/file.rb', line 579 def ctime self.class.ctime(@path) end |
#exists? ⇒ Boolean
503 504 505 |
# File 'lib/fakefs/file.rb', line 503 def exists? true end |
#flock ⇒ Object
583 584 585 |
# File 'lib/fakefs/file.rb', line 583 def flock(*) raise NotImplementedError end |
#ioctl ⇒ Object
540 541 542 |
# File 'lib/fakefs/file.rb', line 540 def ioctl(*) raise NotImplementedError end |
#is_a?(klass) ⇒ Boolean
532 533 534 |
# File 'lib/fakefs/file.rb', line 532 def is_a?(klass) RealFile.allocate.is_a?(klass) end |
#lstat ⇒ Object
552 553 554 |
# File 'lib/fakefs/file.rb', line 552 def lstat self.class.lstat(@path) end |
#mtime ⇒ Object
587 588 589 |
# File 'lib/fakefs/file.rb', line 587 def mtime self.class.mtime(@path) end |
#read(length = nil, buf = '') ⇒ Object
698 699 700 701 702 703 704 705 706 |
# File 'lib/fakefs/file.rb', line 698 def read(length = nil, buf = '') read_buf = super(length, buf) if binary_mode? read_buf&.force_encoding('ASCII-8BIT') else read_buf&.force_encoding(Encoding.default_external) end read_buf end |
#read_nonblock ⇒ Object
544 545 546 |
# File 'lib/fakefs/file.rb', line 544 def read_nonblock raise NotImplementedError end |
#readpartial ⇒ Object
571 572 573 |
# File 'lib/fakefs/file.rb', line 571 def readpartial(*) raise NotImplementedError end |
#stat ⇒ Object
548 549 550 |
# File 'lib/fakefs/file.rb', line 548 def stat self.class.stat(@path) end |
#string ⇒ Object
536 537 538 |
# File 'lib/fakefs/file.rb', line 536 def string gets(nil) end |
#sysread ⇒ Object
514 |
# File 'lib/fakefs/file.rb', line 514 alias sysread read |
#sysseek(position, whence = SEEK_SET) ⇒ Object
556 557 558 559 |
# File 'lib/fakefs/file.rb', line 556 def sysseek(position, whence = SEEK_SET) seek(position, whence) pos end |
#syswrite ⇒ Object
515 516 517 518 519 |
# File 'lib/fakefs/file.rb', line 515 def write(*args) val = super(*args) @file.mtime = Time.now val end |
#to_io ⇒ Object
563 564 565 |
# File 'lib/fakefs/file.rb', line 563 def to_io self end |
#to_path ⇒ Object
633 634 635 |
# File 'lib/fakefs/file.rb', line 633 def to_path @path end |
#write(*args) ⇒ Object
507 508 509 510 511 |
# File 'lib/fakefs/file.rb', line 507 def write(*args) val = super(*args) @file.mtime = Time.now val end |
#write_nonblock ⇒ Object
567 568 569 |
# File 'lib/fakefs/file.rb', line 567 def write_nonblock(*) raise NotImplementedError end |