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.



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fakefs/file.rb', line 242

def initialize(path, mode = READ_ONLY, perm = nil)
  @path = path
  @mode = mode
  @file = FileSystem.find(path)

  check_modes!

  file_creation_mode? ? create_missing_file : check_file_existence!

  super(@file.content, mode)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.basename(*args) ⇒ Object



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

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

.const_missing(name) ⇒ Object



82
83
84
# File 'lib/fakefs/file.rb', line 82

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

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



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

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)


86
87
88
89
90
91
92
93
# File 'lib/fakefs/file.rb', line 86

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



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

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



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

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)


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

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


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

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



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

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



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

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

.readlines(path) ⇒ Object



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

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


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

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

.rename(source, dest) ⇒ Object



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

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



70
71
72
# File 'lib/fakefs/file.rb', line 70

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

.size?(path) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/fakefs/file.rb', line 74

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

.stat(file) ⇒ Object



201
202
203
# File 'lib/fakefs/file.rb', line 201

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


197
198
199
# File 'lib/fakefs/file.rb', line 197

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

.symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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)


304
305
306
# File 'lib/fakefs/file.rb', line 304

def atime
  raise NotImplementedError
end

#binmode?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


329
330
331
# File 'lib/fakefs/file.rb', line 329

def binmode?
  raise NotImplementedError
end

#chmod(mode_int) ⇒ Object

Raises:

  • (NotImplementedError)


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

def chmod(mode_int)
  raise NotImplementedError
end

#chown(owner_int, group_int) ⇒ Object

Raises:

  • (NotImplementedError)


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

def chown(owner_int, group_int)
  raise NotImplementedError
end

#close_on_exec=(bool) ⇒ Object

Raises:

  • (NotImplementedError)


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

def close_on_exec=(bool)
  raise NotImplementedError
end

#close_on_exec?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


337
338
339
# File 'lib/fakefs/file.rb', line 337

def close_on_exec?
  raise NotImplementedError
end

#ctimeObject

Raises:

  • (NotImplementedError)


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

def ctime
  raise NotImplementedError
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  true
end

#flock(locking_constant) ⇒ Object

Raises:

  • (NotImplementedError)


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

def flock(locking_constant)
  raise NotImplementedError
end

#ioctl(integer_cmd, arg) ⇒ Object

Raises:

  • (NotImplementedError)


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

def ioctl(integer_cmd, arg)
  raise NotImplementedError
end

#lstatObject



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

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

#mtimeObject

Raises:

  • (NotImplementedError)


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

def mtime
  raise NotImplementedError
end

#read_nonblock(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#readpartial(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#statObject



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

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

#sysseek(position, whence = SEEK_SET) ⇒ Object



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

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

#to_ioObject



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

def to_io
  self
end

#to_pathObject

Raises:

  • (NotImplementedError)


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

def to_path
  raise NotImplementedError
end

#write_nonblock(string) ⇒ Object

Raises:

  • (NotImplementedError)


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

def write_nonblock(string)
  raise NotImplementedError
end