class MemFs::FileSystem

Attributes

registred_entries[RW]
root[RW]
working_directory[RW]

Public Class Methods

new() click to toggle source
# File lib/memfs/file_system.rb, line 85
def initialize
  clear!
end

Public Instance Methods

basename(path) click to toggle source
# File lib/memfs/file_system.rb, line 14
def basename(path)
  File.basename(path)
end
chdir(path) { || ... } click to toggle source
# File lib/memfs/file_system.rb, line 18
def chdir(path)
  destination = find_directory!(path)

  previous_directory = working_directory
  self.working_directory = destination

  yield if block_given?
ensure
  self.working_directory = previous_directory if block_given?
end
chmod(mode_int, file_name) click to toggle source
# File lib/memfs/file_system.rb, line 35
def chmod(mode_int, file_name)
  find!(file_name).mode = mode_int
end
chown(uid, gid, path) click to toggle source
# File lib/memfs/file_system.rb, line 39
def chown(uid, gid, path)
  entry = find!(path).dereferenced
  entry.uid = uid if uid && uid != -1
  entry.gid = gid if gid && gid != -1
end
clear!() click to toggle source
# File lib/memfs/file_system.rb, line 29
def clear!
  self.root = Fake::Directory.new('/')
  mkdir '/tmp'
  chdir '/'
end
dirname(path) click to toggle source
# File lib/memfs/file_system.rb, line 45
def dirname(path)
  File.dirname(path)
end
entries(path) click to toggle source
# File lib/memfs/file_system.rb, line 49
def entries(path)
  find_directory!(path).entry_names
end
find(path) click to toggle source
# File lib/memfs/file_system.rb, line 53
def find(path)
  if path == '/'
    root
  elsif dirname(path) == '.'
    working_directory.find(path)
  else
    root.find(path)
  end
end
find!(path) click to toggle source
# File lib/memfs/file_system.rb, line 63
def find!(path)
  find(path) || fail(Errno::ENOENT, path)
end
find_directory!(path) click to toggle source
# File lib/memfs/file_system.rb, line 67
def find_directory!(path)
  entry = find!(path).dereferenced

  fail Errno::ENOTDIR, path unless entry.is_a?(Fake::Directory)

  entry
end
find_parent!(path) click to toggle source
# File lib/memfs/file_system.rb, line 75
def find_parent!(path)
  parent_path = dirname(path)
  find_directory!(parent_path)
end
getwd() click to toggle source
# File lib/memfs/file_system.rb, line 80
def getwd
  working_directory.path
end
Also aliased as: pwd
mkdir(path, mode = 0777) click to toggle source
# File lib/memfs/file_system.rb, line 99
def mkdir(path, mode = 0777)
  fail Errno::EEXIST, path if find(path)
  directory = Fake::Directory.new(path)
  directory.mode = mode
  find_parent!(path).add_entry directory
end
paths() click to toggle source
# File lib/memfs/file_system.rb, line 106
def paths
  root.paths
end
pwd()
Alias for: getwd
rename(old_name, new_name) click to toggle source
# File lib/memfs/file_system.rb, line 110
def rename(old_name, new_name)
  file = find!(old_name)
  file.delete

  file.name = basename(new_name)
  find_parent!(new_name).add_entry(file)
end
rmdir(path) click to toggle source
# File lib/memfs/file_system.rb, line 118
def rmdir(path)
  directory = find!(path)

  fail Errno::ENOTEMPTY, path unless directory.empty?

  directory.delete
end
touch(*paths) click to toggle source
# File lib/memfs/file_system.rb, line 132
def touch(*paths)
  paths.each do |path|
    entry = find(path)

    unless entry
      entry = Fake::File.new(path)
      parent_dir = find_parent!(path)
      parent_dir.add_entry entry
    end

    entry.touch
  end
end