Class: FakeFS::File

Inherits:
StringIO
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode = READ_ONLY, _perm = nil) ⇒ File

Returns a new instance of File


409
410
411
412
413
414
415
416
417
418
419
420
# 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

Instance Attribute Details

#autocloseObject

Returns the value of attribute autoclose


551
552
553
# File 'lib/fakefs/file.rb', line 551

def autoclose
  @autoclose
end

#pathObject (readonly)

Returns the value of attribute path


407
408
409
# File 'lib/fakefs/file.rb', line 407

def path
  @path
end

Class Method Details

.absolute_path(file_name, dir_name = Dir.getwd) ⇒ Object


545
546
547
# 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) ⇒ Object


80
81
82
83
84
85
86
# 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) ⇒ Object


157
158
159
# File 'lib/fakefs/file.rb', line 157

def self.basename(*args)
  RealFile.basename(*args)
end

.binread(file, length = nil, offset = 0) ⇒ Object


305
306
307
# 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) ⇒ Object


601
602
603
604
605
606
607
# 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) ⇒ Object


274
275
276
# 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) ⇒ Object


286
287
288
289
290
291
292
293
294
295
296
297
298
299
# 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) ⇒ Object


119
120
121
# File 'lib/fakefs/file.rb', line 119

def self.const_missing(name)
  RealFile.const_get(name)
end

.ctime(path) ⇒ Object


72
73
74
75
76
77
78
# 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) ⇒ Object Also known as: unlink


244
245
246
247
248
249
250
251
252
# 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

.directory?(path) ⇒ Boolean

Returns:

  • (Boolean)

123
124
125
126
127
128
129
130
# 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) ⇒ Object


161
162
163
# File 'lib/fakefs/file.rb', line 161

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.

Returns:

  • (Boolean)

280
281
282
283
284
# 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) ⇒ Boolean Also known as: exists?, readable?, writable?

Returns:

  • (Boolean)

42
43
44
45
46
47
48
49
# 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

.expand_path(file_name, dir_string = FileSystem.current_dir.to_s) ⇒ Object


153
154
155
# 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) ⇒ Object


30
31
32
# File 'lib/fakefs/file.rb', line 30

def self.extname(path)
  RealFile.extname(path)
end

.file?(path) ⇒ Boolean

Returns:

  • (Boolean)

140
141
142
143
144
145
146
147
# 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) ⇒ Boolean Also known as: fnmatch

Returns:

  • (Boolean)

309
310
311
# File 'lib/fakefs/file.rb', line 309

def self.fnmatch?(pattern, path, flags = 0)
  RealFile.fnmatch?(pattern, path, flags)
end

.foreach(path, *args, &block) ⇒ Object


194
195
196
197
198
199
200
201
202
203
204
205
206
# 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) ⇒ Object


149
150
151
# File 'lib/fakefs/file.rb', line 149

def self.ftype(filename)
  File.lstat(filename).ftype
end

.join(*parts) ⇒ Object


34
35
36
# File 'lib/fakefs/file.rb', line 34

def self.join(*parts)
  RealFile.join(parts)
end

232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fakefs/file.rb', line 232

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


266
267
268
# File 'lib/fakefs/file.rb', line 266

def self.lstat(file)
  File::Stat.new(file, true)
end

.mtime(path) ⇒ Object


64
65
66
67
68
69
70
# File 'lib/fakefs/file.rb', line 64

def self.mtime(path)
  if exists?(path)
    FileSystem.find(path).mtime
  else
    fail Errno::ENOENT
  end
end

.path(file) ⇒ Object


38
39
40
# File 'lib/fakefs/file.rb', line 38

def self.path(file)
  RealFile.path(file)
end

.read(path, *args) ⇒ Object


170
171
172
173
174
175
176
177
178
179
180
181
182
# 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

.readlines(path) ⇒ Object


184
185
186
187
188
189
190
191
192
# 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

165
166
167
168
# File 'lib/fakefs/file.rb', line 165

def self.readlink(path)
  symlink = FileSystem.find(path)
  symlink.target
end

.realdirpath(*args) ⇒ Object


563
564
565
# File 'lib/fakefs/file.rb', line 563

def self.realdirpath(*args)
  RealFile.realdirpath(*args)
end

.realpath(*args) ⇒ Object


523
524
525
# File 'lib/fakefs/file.rb', line 523

def self.realpath(*args)
  RealFile.realpath(*args)
end

.rename(source, dest) ⇒ Object


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# 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) ⇒ Object


101
102
103
# File 'lib/fakefs/file.rb', line 101

def self.size(path)
  read(path).bytesize
end

.size?(path) ⇒ Boolean

Returns:

  • (Boolean)

105
106
107
# File 'lib/fakefs/file.rb', line 105

def self.size?(path)
  size(path) if exists?(path) && !size(path).zero?
end

.split(path) ⇒ Object


270
271
272
# File 'lib/fakefs/file.rb', line 270

def self.split(path)
  RealFile.split(path)
end

.stat(file) ⇒ Object


262
263
264
# File 'lib/fakefs/file.rb', line 262

def self.stat(file)
  File::Stat.new(file)
end

.sticky?(_path) ⇒ Boolean

Assume nothing is sticky.

Returns:

  • (Boolean)

59
60
61
# File 'lib/fakefs/file.rb', line 59

def sticky?(_path)
  false
end

258
259
260
# File 'lib/fakefs/file.rb', line 258

def self.symlink(source, dest)
  FileUtils.ln_s(source, dest)
end

.symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)

132
133
134
135
136
137
138
# File 'lib/fakefs/file.rb', line 132

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


301
302
303
# File 'lib/fakefs/file.rb', line 301

def self.umask(*args)
  RealFile.umask(*args)
end

.utime(atime, mtime, *paths) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
# 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

.write(filename, contents, offset = nil, open_args = {}) ⇒ Object


572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# 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) ⇒ Boolean Also known as: empty?

Returns:

  • (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


569
570
# File 'lib/fakefs/file.rb', line 569

def advise(_advice, _offset = 0, _len = 0)
end

#atimeObject


490
491
492
# File 'lib/fakefs/file.rb', line 490

def atime
  self.class.atime(@path)
end

#autoclose?Boolean

Returns:

  • (Boolean)

553
554
555
# File 'lib/fakefs/file.rb', line 553

def autoclose?
  @autoclose ? true : false
end

#binmode?Boolean

Returns:

  • (Boolean)

527
528
529
# File 'lib/fakefs/file.rb', line 527

def binmode?
  fail NotImplementedError
end

#birthtimeObject


609
610
611
# File 'lib/fakefs/file.rb', line 609

def birthtime
  self.class.birthtime(@path)
end

#chmod(mode_int) ⇒ Object


506
507
508
# File 'lib/fakefs/file.rb', line 506

def chmod(mode_int)
  @file.mode = 0100000 + mode_int
end

#chown(owner_int, group_int) ⇒ Object


510
511
512
513
514
515
516
517
518
519
520
# 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) ⇒ Object


531
532
533
# File 'lib/fakefs/file.rb', line 531

def close_on_exec=(_bool)
  fail NotImplementedError
end

#close_on_exec?Boolean

Returns:

  • (Boolean)

535
536
537
# File 'lib/fakefs/file.rb', line 535

def close_on_exec?
  fail NotImplementedError
end

#ctimeObject


494
495
496
# File 'lib/fakefs/file.rb', line 494

def ctime
  self.class.ctime(@path)
end

#exists?Boolean

Returns:

  • (Boolean)

422
423
424
# File 'lib/fakefs/file.rb', line 422

def exists?
  true
end

#flockObject


498
499
500
# File 'lib/fakefs/file.rb', line 498

def flock(*)
  fail NotImplementedError
end

#ioctlObject


455
456
457
# File 'lib/fakefs/file.rb', line 455

def ioctl(*)
  fail NotImplementedError
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)

451
452
453
# File 'lib/fakefs/file.rb', line 451

def is_a?(klass)
  RealFile.allocate.is_a?(klass)
end

#lstatObject


467
468
469
# File 'lib/fakefs/file.rb', line 467

def lstat
  self.class.lstat(@path)
end

#mtimeObject


502
503
504
# File 'lib/fakefs/file.rb', line 502

def mtime
  self.class.mtime(@path)
end

#read(length = nil, buf = '') ⇒ Object


614
615
616
617
618
619
620
621
# 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

#read_nonblockObject


459
460
461
# File 'lib/fakefs/file.rb', line 459

def read_nonblock
  fail NotImplementedError
end

#readpartialObject


486
487
488
# File 'lib/fakefs/file.rb', line 486

def readpartial(*)
  fail NotImplementedError
end

#sizeObject


559
560
561
# File 'lib/fakefs/file.rb', line 559

def size
  File.size(@path)
end

#statObject


463
464
465
# File 'lib/fakefs/file.rb', line 463

def stat
  self.class.stat(@path)
end

#sysreadObject


433
# File 'lib/fakefs/file.rb', line 433

alias_method :sysread,  :read

#sysseek(position, whence = SEEK_SET) ⇒ Object


471
472
473
474
# File 'lib/fakefs/file.rb', line 471

def sysseek(position, whence = SEEK_SET)
  seek(position, whence)
  pos
end

#syswriteObject


434
435
436
437
438
# File 'lib/fakefs/file.rb', line 434

def write(str)
  val = super(str)
  @file.mtime = Time.now
  val
end

#to_ioObject


478
479
480
# File 'lib/fakefs/file.rb', line 478

def to_io
  self
end

#to_pathObject


539
540
541
# File 'lib/fakefs/file.rb', line 539

def to_path
  @path
end

#write(str) ⇒ Object


426
427
428
429
430
# File 'lib/fakefs/file.rb', line 426

def write(str)
  val = super(str)
  @file.mtime = Time.now
  val
end

#write_nonblockObject


482
483
484
# File 'lib/fakefs/file.rb', line 482

def write_nonblock(*)
  fail NotImplementedError
end