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.



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/fakefs/file.rb', line 227

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.



333
334
335
# File 'lib/fakefs/file.rb', line 333

def autoclose=(value)
  @autoclose = value
end

#pathObject (readonly)

Returns the value of attribute path.



225
226
227
# File 'lib/fakefs/file.rb', line 225

def path
  @path
end

Class Method Details

.basename(*args) ⇒ Object



113
114
115
# File 'lib/fakefs/file.rb', line 113

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

.const_missing(name) ⇒ Object



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

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



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fakefs/file.rb', line 159

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)


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

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



117
118
119
# File 'lib/fakefs/file.rb', line 117

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



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

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)


100
101
102
103
104
105
106
107
# File 'lib/fakefs/file.rb', line 100

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)
  parts * PATH_SEPARATOR
end


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fakefs/file.rb', line 139

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



185
186
187
# File 'lib/fakefs/file.rb', line 185

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



126
127
128
129
130
131
132
133
# File 'lib/fakefs/file.rb', line 126

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

.readlines(path) ⇒ Object



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

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


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

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

.size(path) ⇒ Object



67
68
69
# File 'lib/fakefs/file.rb', line 67

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

.size?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.stat(file) ⇒ Object



181
182
183
# File 'lib/fakefs/file.rb', line 181

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


177
178
179
# File 'lib/fakefs/file.rb', line 177

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

.symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/fakefs/file.rb', line 92

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

Instance Method Details

#atimeObject

Raises:

  • (NotImplementedError)


290
291
292
# File 'lib/fakefs/file.rb', line 290

def atime
  raise NotImplementedError
end

#autoclose?Boolean

Returns:

  • (Boolean)


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

def autoclose?
  @autoclose
end

#binmode?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def binmode?
  raise NotImplementedError
end

#chmod(mode_int) ⇒ Object

Raises:

  • (NotImplementedError)


294
295
296
# File 'lib/fakefs/file.rb', line 294

def chmod(mode_int)
  raise NotImplementedError
end

#chown(owner_int, group_int) ⇒ Object

Raises:

  • (NotImplementedError)


298
299
300
# File 'lib/fakefs/file.rb', line 298

def chown(owner_int, group_int)
  raise NotImplementedError
end

#close_on_exec=(bool) ⇒ Object

Raises:

  • (NotImplementedError)


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

def close_on_exec=(bool)
  raise NotImplementedError
end

#close_on_exec?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def close_on_exec?
  raise NotImplementedError
end

#ctimeObject



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

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

#exists?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/fakefs/file.rb', line 240

def exists?
  true
end

#flock(locking_constant) ⇒ Object

Raises:

  • (NotImplementedError)


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

def flock(locking_constant)
  raise NotImplementedError
end

#ioctl(integer_cmd, arg) ⇒ Object

Raises:

  • (NotImplementedError)


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

def ioctl(integer_cmd, arg)
  raise NotImplementedError
end

#lstatObject



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

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

#mtimeObject



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

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

#read_nonblock(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#readpartial(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (NotImplementedError)


286
287
288
# File 'lib/fakefs/file.rb', line 286

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

#sizeObject



341
342
343
# File 'lib/fakefs/file.rb', line 341

def size
  File.size(@path)
end

#statObject



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

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

#sysseek(position, whence = SEEK_SET) ⇒ Object



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

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

#to_ioObject



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

def to_io
  self
end

#to_pathObject

Raises:

  • (NotImplementedError)


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

def to_path
  raise NotImplementedError
end

#write_nonblock(string) ⇒ Object

Raises:

  • (NotImplementedError)


282
283
284
# File 'lib/fakefs/file.rb', line 282

def write_nonblock(string)
  raise NotImplementedError
end