Class: FakeFS::File

Inherits:
StringIO
  • Object
show all
Defined in:
lib/fakefs/file.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of File.



363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/fakefs/file.rb', line 363

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=(autoclose) ⇒ Object (writeonly)

Sets the attribute autoclose

Parameters:

  • value

    the value to set the attribute autoclose to.



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

def autoclose=(value)
  @autoclose = value
end

#pathObject (readonly)

Returns the value of attribute path.



361
362
363
# File 'lib/fakefs/file.rb', line 361

def path
  @path
end

Class Method Details

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



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

def self.absolute_path(file_name, dir_name = Dir.getwd)
  RealFile.absolute_path(file_name, dir_name)
end

.atime(path) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/fakefs/file.rb', line 71

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

.basename(*args) ⇒ Object



142
143
144
# File 'lib/fakefs/file.rb', line 142

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

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



278
279
280
# File 'lib/fakefs/file.rb', line 278

def self.binread(file, length = nil, offset = 0)
  contents = File.read(file, length, offset, :mode => 'rb:ASCII-8BIT')
end

.chmod(mode_int, filename) ⇒ Object



250
251
252
# File 'lib/fakefs/file.rb', line 250

def self.chmod(mode_int, filename)
  FileSystem.find(filename).mode = 0100000 + mode_int
end

.chown(owner_int, group_int, filename) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/fakefs/file.rb', line 262

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) ⇒ Object



108
109
110
# File 'lib/fakefs/file.rb', line 108

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

.ctime(path) ⇒ Object



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

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

.delete(*file_names) ⇒ Object Also known as: unlink



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/fakefs/file.rb', line 218

def self.delete(*file_names)
  file_names.each do |file_name|
    if !exists?(file_name)
      raise Errno::ENOENT, file_name
    end

    FileUtils.rm(file_name)
  end

  file_names.size
end

.directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
# File 'lib/fakefs/file.rb', line 112

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



146
147
148
# File 'lib/fakefs/file.rb', line 146

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)


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

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)


38
39
40
41
42
43
44
45
# File 'lib/fakefs/file.rb', line 38

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) ⇒ Object



138
139
140
# File 'lib/fakefs/file.rb', line 138

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)


129
130
131
132
133
134
135
136
# File 'lib/fakefs/file.rb', line 129

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


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fakefs/file.rb', line 198

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) ⇒ Object



242
243
244
# File 'lib/fakefs/file.rb', line 242

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

.mtime(path) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/fakefs/file.rb', line 55

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

.read(path, *args) ⇒ Object

Raises:

  • (Errno::ENOENT)


155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fakefs/file.rb', line 155

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)

  raise Errno::ENOENT if !file.exists?
  raise Errno::EISDIR, path if directory?(path)

  FileSystem.find(path).atime = Time.now
  file.seek(offset)
  file.read(length)
end

.readlines(path) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/fakefs/file.rb', line 169

def self.readlines(path)
  file = new(path)
  if file.exists?
    FileSystem.find(path).atime = Time.now
    file.readlines
  else
    raise Errno::ENOENT
  end
end


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

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

.realpath(*args) ⇒ Object



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

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

.rename(source, dest) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fakefs/file.rb', line 179

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)
    FileSystem.add(dest, target.entry.clone)
    FileSystem.delete(source)
  else
    raise Errno::ENOENT, "#{source} or #{dest}"
  end

  0
end

.size(path) ⇒ Object



92
93
94
# File 'lib/fakefs/file.rb', line 92

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

.size?(path) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
# File 'lib/fakefs/file.rb', line 96

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

.split(path) ⇒ Object



246
247
248
# File 'lib/fakefs/file.rb', line 246

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

.stat(file) ⇒ Object



238
239
240
# File 'lib/fakefs/file.rb', line 238

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


234
235
236
# File 'lib/fakefs/file.rb', line 234

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

.symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# File 'lib/fakefs/file.rb', line 121

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



274
275
276
# File 'lib/fakefs/file.rb', line 274

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

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



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fakefs/file.rb', line 79

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) ⇒ Object



526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/fakefs/file.rb', line 526

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) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.zero?(path)
  exists?(path) && size(path) == 0
end

Instance Method Details

#advise(advice, offset = 0, len = 0) ⇒ Object



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

def advise(advice, offset=0, len=0)
end

#atimeObject



441
442
443
# File 'lib/fakefs/file.rb', line 441

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

#autoclose?Boolean

Returns:

  • (Boolean)


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

def autoclose?
  @autoclose
end

#binmode?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def binmode?
  raise NotImplementedError
end

#chmod(mode_int) ⇒ Object



457
458
459
# File 'lib/fakefs/file.rb', line 457

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

#chown(owner_int, group_int) ⇒ Object



461
462
463
464
465
466
467
468
469
470
# File 'lib/fakefs/file.rb', line 461

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) ⇒ Object

Raises:

  • (NotImplementedError)


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

def close_on_exec=(bool)
  raise NotImplementedError
end

#close_on_exec?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def close_on_exec?
  raise NotImplementedError
end

#ctimeObject



445
446
447
# File 'lib/fakefs/file.rb', line 445

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

#exists?Boolean

Returns:

  • (Boolean)


376
377
378
# File 'lib/fakefs/file.rb', line 376

def exists?
  true
end

#flock(locking_constant) ⇒ Object

Raises:

  • (NotImplementedError)


449
450
451
# File 'lib/fakefs/file.rb', line 449

def flock(locking_constant)
  raise NotImplementedError
end

#ioctl(integer_cmd, arg) ⇒ Object

Raises:

  • (NotImplementedError)


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

def ioctl(integer_cmd, arg)
  raise NotImplementedError
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


402
403
404
# File 'lib/fakefs/file.rb', line 402

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

#lstatObject



418
419
420
# File 'lib/fakefs/file.rb', line 418

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

#mtimeObject



453
454
455
# File 'lib/fakefs/file.rb', line 453

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

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



542
543
544
545
546
547
548
# File 'lib/fakefs/file.rb', line 542

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

#read_nonblock(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (NotImplementedError)


410
411
412
# File 'lib/fakefs/file.rb', line 410

def read_nonblock(maxlen, outbuf = nil)
  raise NotImplementedError
end

#readpartial(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (NotImplementedError)


437
438
439
# File 'lib/fakefs/file.rb', line 437

def readpartial(maxlen, outbuf = nil)
  raise NotImplementedError
end

#sizeObject



517
518
519
# File 'lib/fakefs/file.rb', line 517

def size
  File.size(@path)
end

#statObject



414
415
416
# File 'lib/fakefs/file.rb', line 414

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

#sysreadObject



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

alias_method :sysread,  :read

#sysseek(position, whence = SEEK_SET) ⇒ Object



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

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

#syswriteObject



388
389
390
391
392
# File 'lib/fakefs/file.rb', line 388

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

#to_ioObject



429
430
431
# File 'lib/fakefs/file.rb', line 429

def to_io
  self
end

#to_pathObject



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

def to_path
  @path
end

#write(str) ⇒ Object



380
381
382
383
384
# File 'lib/fakefs/file.rb', line 380

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

#write_nonblock(string) ⇒ Object

Raises:

  • (NotImplementedError)


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

def write_nonblock(string)
  raise NotImplementedError
end