module FakeFS::FileSystem

Public Instance Methods

add(path, object=FakeDir.new) click to toggle source
# File lib/fakefs/file_system.rb, line 35
def add(path, object=FakeDir.new)
  parts = path_parts(normalize_path(path))

  d = parts[0...-1].inject(fs) do |dir, part|
    dir[part] ||= FakeDir.new(part, dir)
  end

  object.name = parts.last
  object.parent = d
  d[parts.last] ||= object
end
chdir(dir, &blk) click to toggle source
# File lib/fakefs/file_system.rb, line 74
def chdir(dir, &blk)
  new_dir = find(dir)
  dir_levels.push dir if blk

  raise Errno::ENOENT, dir unless new_dir

  dir_levels.push dir if !blk
  blk.call if blk
ensure
  dir_levels.pop if blk
end
clear() click to toggle source
# File lib/fakefs/file_system.rb, line 13
def clear
  @dir_levels = nil
  @fs = nil
end
clone(path) click to toggle source

copies directories and files from the real filesystem into our fake one

# File lib/fakefs/file_system.rb, line 49
def clone(path)
  path    = File.expand_path(path)
  pattern = File.join(path, '**', '*')
  files   = RealFile.file?(path) ? [path] : [path] + RealDir.glob(pattern, RealFile::FNM_DOTMATCH)

  files.each do |f|
    if RealFile.file?(f)
      FileUtils.mkdir_p(File.dirname(f))
      File.open(f, File::WRITE_ONLY) do |g|
        g.print RealFile.open(f){|h| h.read }
      end
    elsif RealFile.directory?(f)
      FileUtils.mkdir_p(f)
    elsif RealFile.symlink?(f)
      FileUtils.ln_s()
    end
  end
end
current_dir() click to toggle source
# File lib/fakefs/file_system.rb, line 99
def current_dir
  find(normalize_path('.'))
end
delete(path) click to toggle source
# File lib/fakefs/file_system.rb, line 68
def delete(path)
  if node = FileSystem.find(path)
    node.delete
  end
end
dir_levels() click to toggle source
# File lib/fakefs/file_system.rb, line 5
def dir_levels
  @dir_levels ||= []
end
files() click to toggle source
# File lib/fakefs/file_system.rb, line 18
def files
  fs.values
end
find(path) click to toggle source
# File lib/fakefs/file_system.rb, line 22
def find(path)
  parts = path_parts(normalize_path(path))
  return fs if parts.empty? # '/'

  entries = find_recurser(fs, parts).flatten

  case entries.length
  when 0 then nil
  when 1 then entries.first
  else entries
  end
end
fs() click to toggle source
# File lib/fakefs/file_system.rb, line 9
def fs
  @fs ||= FakeDir.new('.')
end
normalize_path(path) click to toggle source
# File lib/fakefs/file_system.rb, line 90
def normalize_path(path)
  if Pathname.new(path).absolute?
    File.expand_path(path)
  else
    parts = dir_levels + [path]
    File.expand_path(File.join(*parts))
  end
end
path_parts(path) click to toggle source
# File lib/fakefs/file_system.rb, line 86
def path_parts(path)
  path.split(File::PATH_SEPARATOR).reject { |part| part.empty? }
end

Private Instance Methods

directories_under(dir) click to toggle source
# File lib/fakefs/file_system.rb, line 131
def directories_under(dir)
  children = dir.values.select{|f| f.is_a? FakeDir}
  ([dir] + children + children.map{|c| directories_under(c)}).flatten.uniq
end
find_recurser(dir, parts) click to toggle source
# File lib/fakefs/file_system.rb, line 105
def find_recurser(dir, parts)
  return [] unless dir.respond_to? :[]

  pattern , *parts = parts
  matches = case pattern
  when '**'
    case parts
    when ['*'], []
      parts = [] # end recursion
      directories_under(dir).map do |d|
        d.values.select{|f| f.is_a? FakeFile }
      end.flatten.uniq
    else
      directories_under(dir)
    end
  else
    dir.reject {|k,v| /\A#{pattern.gsub('?','.').gsub('*', '.*')}\Z/ !~ k }.values
  end

  if parts.empty? # we're done recursing
    matches
  else
    matches.map{|entry| find_recurser(entry, parts) }
  end
end