Class: FakeFS::File

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

Defined Under Namespace

Classes: Stat

Constant Summary collapse

PATH_SEPARATOR =
'/'
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::NOCTTY   |
RealFile::SYNC
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.



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/fakefs/file.rb', line 256

def initialize(path, mode = READ_ONLY, perm = nil)
  @path = path
  @mode = 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=(value) ⇒ Object (writeonly)

Sets the attribute autoclose

Parameters:

  • value

    the value to set the attribute autoclose to.



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

def autoclose=(value)
  @autoclose = value
end

#pathObject (readonly)

Returns the value of attribute path.



254
255
256
# File 'lib/fakefs/file.rb', line 254

def path
  @path
end

Class Method Details

.basename(*args) ⇒ Object



125
126
127
# File 'lib/fakefs/file.rb', line 125

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

.const_missing(name) ⇒ Object



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

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

.ctime(path) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/fakefs/file.rb', line 59

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

.delete(file_name, *additional_file_names) ⇒ Object Also known as: unlink



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/fakefs/file.rb', line 188

def self.delete(file_name, *additional_file_names)
  if !exists?(file_name)
    raise Errno::ENOENT, "No such file or directory - #{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

.directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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



129
130
131
# File 'lib/fakefs/file.rb', line 129

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

.exist?(path) ⇒ Boolean Also known as: exists?, readable?, writable?

Returns:

  • (Boolean)


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

def self.exist?(path)
  !!FileSystem.find(path)
end

.expand_path(*args) ⇒ Object



121
122
123
# File 'lib/fakefs/file.rb', line 121

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

.extname(path) ⇒ Object



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

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

.file?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fakefs/file.rb', line 168

def self.link(source, dest)
  if directory?(source)
    raise Errno::EPERM, "Operation not permitted - #{source} or #{dest}"
  end

  if !exists?(source)
    raise Errno::ENOENT, "No such file or directory - #{source} or #{dest}"
  end

  if exists?(dest)
    raise Errno::EEXIST, "File exists - #{source} or #{dest}"
  end

  source = FileSystem.find(source)
  dest = FileSystem.add(dest, source.entry.clone)
  source.link(dest)

  0
end

.lstat(file) ⇒ Object



214
215
216
# File 'lib/fakefs/file.rb', line 214

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

.mtime(path) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/fakefs/file.rb', line 51

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

.read(path) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/fakefs/file.rb', line 138

def self.read(path)
  file = new(path)
  if file.exists?
    file.read
  else
    raise Errno::ENOENT
  end
end

.readlines(path) ⇒ Object



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

def self.readlines(path)
  read(path).split("\n")
end


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

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

.rename(source, dest) ⇒ Object



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

def self.rename(source, dest)
  if directory?(source) && file?(dest)
    raise Errno::ENOTDIR, "Not a directory - #{source} or #{dest}"
  elsif file?(source) && directory?(dest)
    raise Errno::EISDIR, "Is a directory - #{source} or #{dest}"
  end

  if target = FileSystem.find(source)
    FileSystem.add(dest, target.entry.clone)
    FileSystem.delete(source)
  else
    raise Errno::ENOENT,  "No such file or directory - #{source} or #{dest}"
  end

  0
end

.size(path) ⇒ Object



79
80
81
# File 'lib/fakefs/file.rb', line 79

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

.size?(path) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
# File 'lib/fakefs/file.rb', line 83

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

.stat(file) ⇒ Object



210
211
212
# File 'lib/fakefs/file.rb', line 210

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


206
207
208
# File 'lib/fakefs/file.rb', line 206

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

.symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
# File 'lib/fakefs/file.rb', line 104

def self.symlink?(path)
  if path.respond_to? :entry
    path.is_a? FakeSymlink
  else
    FileSystem.find(path).is_a? FakeSymlink
  end
end

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



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

def self.utime(atime, mtime, *paths)
  paths.each do |path|
    if exists?(path)
      FileSystem.find(path).mtime = mtime
    else
      raise Errno::ENOENT
    end
  end

  paths.size
end

Instance Method Details

#atimeObject

Raises:

  • (NotImplementedError)


319
320
321
# File 'lib/fakefs/file.rb', line 319

def atime
  raise NotImplementedError
end

#autoclose?Boolean

Returns:

  • (Boolean)


364
365
366
# File 'lib/fakefs/file.rb', line 364

def autoclose?
  @autoclose
end

#binmode?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


344
345
346
# File 'lib/fakefs/file.rb', line 344

def binmode?
  raise NotImplementedError
end

#chmod(mode_int) ⇒ Object

Raises:

  • (NotImplementedError)


323
324
325
# File 'lib/fakefs/file.rb', line 323

def chmod(mode_int)
  raise NotImplementedError
end

#chown(owner_int, group_int) ⇒ Object

Raises:

  • (NotImplementedError)


327
328
329
# File 'lib/fakefs/file.rb', line 327

def chown(owner_int, group_int)
  raise NotImplementedError
end

#close_on_exec=(bool) ⇒ Object

Raises:

  • (NotImplementedError)


348
349
350
# File 'lib/fakefs/file.rb', line 348

def close_on_exec=(bool)
  raise NotImplementedError
end

#close_on_exec?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


352
353
354
# File 'lib/fakefs/file.rb', line 352

def close_on_exec?
  raise NotImplementedError
end

#ctimeObject



331
332
333
# File 'lib/fakefs/file.rb', line 331

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  true
end

#flock(locking_constant) ⇒ Object

Raises:

  • (NotImplementedError)


335
336
337
# File 'lib/fakefs/file.rb', line 335

def flock(locking_constant)
  raise NotImplementedError
end

#ioctl(integer_cmd, arg) ⇒ Object

Raises:

  • (NotImplementedError)


284
285
286
# File 'lib/fakefs/file.rb', line 284

def ioctl(integer_cmd, arg)
  raise NotImplementedError
end

#lstatObject



296
297
298
# File 'lib/fakefs/file.rb', line 296

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

#mtimeObject



339
340
341
# File 'lib/fakefs/file.rb', line 339

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

#read_nonblock(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (NotImplementedError)


288
289
290
# File 'lib/fakefs/file.rb', line 288

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

#readpartial(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (NotImplementedError)


315
316
317
# File 'lib/fakefs/file.rb', line 315

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

#sizeObject



370
371
372
# File 'lib/fakefs/file.rb', line 370

def size
  File.size(@path)
end

#statObject



292
293
294
# File 'lib/fakefs/file.rb', line 292

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

#sysseek(position, whence = SEEK_SET) ⇒ Object



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

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

#to_ioObject



307
308
309
# File 'lib/fakefs/file.rb', line 307

def to_io
  self
end

#to_pathObject

Raises:

  • (NotImplementedError)


356
357
358
# File 'lib/fakefs/file.rb', line 356

def to_path
  raise NotImplementedError
end

#write_nonblock(string) ⇒ Object

Raises:

  • (NotImplementedError)


311
312
313
# File 'lib/fakefs/file.rb', line 311

def write_nonblock(string)
  raise NotImplementedError
end