Parent

Included Modules

Files

FakeFS::Dir

Public Class Methods

[](pattern) click to toggle source
# File lib/fakefs/dir.rb, line 60
def self.[](pattern)
  glob(pattern)
end
_check_for_valid_file(path) click to toggle source
# File lib/fakefs/dir.rb, line 5
def self._check_for_valid_file(path)
  raise Errno::ENOENT, "No such file or directory - #{path}" unless FileSystem.find(path)
end
chdir(dir, &blk) click to toggle source
# File lib/fakefs/dir.rb, line 68
def self.chdir(dir, &blk)
  FileSystem.chdir(dir, &blk)
end
chroot(string) click to toggle source
# File lib/fakefs/dir.rb, line 72
def self.chroot(string)
  raise NotImplementedError
end
delete(string) click to toggle source
# File lib/fakefs/dir.rb, line 76
def self.delete(string)
  _check_for_valid_file(string)
  raise Errno::ENOTEMPTY, "Directory not empty - #{string}" unless FileSystem.find(string).values.empty?

  FileSystem.delete(string)
end
Also aliased as: rmdir, unlink
entries(dirname) click to toggle source
# File lib/fakefs/dir.rb, line 83
def self.entries(dirname)
  _check_for_valid_file(dirname)

  Dir.new(dirname).map { |file| File.basename(file) }
end
exists?(path) click to toggle source
# File lib/fakefs/dir.rb, line 64
def self.exists?(path)
  File.exists?(path) && File.directory?(path)
end
foreach(dirname, &block) click to toggle source
# File lib/fakefs/dir.rb, line 89
def self.foreach(dirname, &block)
  Dir.open(dirname) { |file| yield file }
end
getwd() click to toggle source
Alias for: pwd
glob(pattern, &block) click to toggle source
# File lib/fakefs/dir.rb, line 93
def self.glob(pattern, &block)
  files = [FileSystem.find(pattern) || []].flatten.map(&:to_s).sort
  block_given? ? files.each { |file| block.call(file) } : files
end
mkdir(string, integer = 0) click to toggle source
# File lib/fakefs/dir.rb, line 98
def self.mkdir(string, integer = 0)
  parent = string.split('/')
  parent.pop

  joined_parent_path = parent.join

  _check_for_valid_file(joined_parent_path) unless joined_parent_path == ""
  raise Errno::EEXIST, "File exists - #{string}" if File.exists?(string)

  FileUtils.mkdir_p(string)
end
mktmpdir(prefix_suffix = nil, tmpdir = nil) click to toggle source

This code has been borrowed from Rubinius

# File lib/fakefs/dir.rb, line 127
def self.mktmpdir(prefix_suffix = nil, tmpdir = nil)
  case prefix_suffix
  when nil
    prefix = "d"
    suffix = ""
  when String
    prefix = prefix_suffix
    suffix = ""
  when Array
    prefix = prefix_suffix[0]
    suffix = prefix_suffix[1]
  else
    raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
  end

  t = Time.now.strftime("%Y%m%d")
  n = nil

  begin
    path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
    path << "-#{n}" if n
    path << suffix
    mkdir(path, 0700)
  rescue Errno::EEXIST
    n ||= 0
    n += 1
    retry
  end

  if block_given?
    begin
      yield path
    ensure
      require 'fileutils'
      # This here was using FileUtils.remove_entry_secure instead of just
      # .rm_r. However, the security concerns that apply to
      # .rm_r/.remove_entry_secure shouldn't apply to a test fake
      # filesystem. :^)
      FileUtils.rm_r path
    end
  else
    path
  end
end
new(string) click to toggle source
# File lib/fakefs/dir.rb, line 9
def initialize(string)
  self.class._check_for_valid_file(string)

  @path = string
  @open = true
  @pointer = 0
  @contents = [ '.', '..', ] + FileSystem.find(@path).values
end
open(string, &block) click to toggle source
# File lib/fakefs/dir.rb, line 110
def self.open(string, &block)
  if block_given?
    Dir.new(string).each { |file| yield(file) }
  else
    Dir.new(string)
  end
end
pwd() click to toggle source
# File lib/fakefs/dir.rb, line 122
def self.pwd
  FileSystem.current_dir.to_s
end
Also aliased as: getwd
rmdir(string) click to toggle source
Alias for: delete
tmpdir() click to toggle source
# File lib/fakefs/dir.rb, line 118
def self.tmpdir
  '/tmp'
end

Public Instance Methods

close() click to toggle source
# File lib/fakefs/dir.rb, line 18
def close
  @open = false
  @pointer = nil
  @contents = nil
  nil
end
each(&block) click to toggle source
# File lib/fakefs/dir.rb, line 25
def each(&block)
  while f = read
    yield f
  end
end
path() click to toggle source
# File lib/fakefs/dir.rb, line 31
def path
  @path
end
pos() click to toggle source
# File lib/fakefs/dir.rb, line 35
def pos
  @pointer
end
pos=(integer) click to toggle source
# File lib/fakefs/dir.rb, line 39
def pos=(integer)
  @pointer = integer
end
read() click to toggle source
# File lib/fakefs/dir.rb, line 43
def read
  raise IOError, "closed directory" if @pointer == nil
  n = @contents[@pointer]
  @pointer += 1
  n.to_s.gsub(path + '/', '') if n
end
rewind() click to toggle source
# File lib/fakefs/dir.rb, line 50
def rewind
  @pointer = 0
end
seek(integer) click to toggle source
# File lib/fakefs/dir.rb, line 54
def seek(integer)
  raise IOError, "closed directory" if @pointer == nil
  @pointer = integer
  @contents[integer]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.