class FakeFS::Dir

FakeFs Dir class

Attributes

path[R]

Public Class Methods

[](*pattern) click to toggle source
# File lib/fakefs/dir.rb, line 65
def self.[](*pattern)
  glob pattern
end
_check_for_valid_file(path) click to toggle source
# File lib/fakefs/dir.rb, line 9
def self._check_for_valid_file(path)
  raise Errno::ENOENT, path.to_s unless FileSystem.find(path)
end
chdir(dir, &blk) click to toggle source
# File lib/fakefs/dir.rb, line 73
def self.chdir(dir, &blk)
  FileSystem.chdir(dir, &blk)
end
children(dirname, _options = nil) click to toggle source
# File lib/fakefs/dir.rb, line 94
def self.children(dirname, _options = nil)
  entries(dirname) - ['.', '..']
end
chroot(_string) click to toggle source
# File lib/fakefs/dir.rb, line 77
def self.chroot(_string)
  raise NotImplementedError
end
delete(string) click to toggle source
# File lib/fakefs/dir.rb, line 81
def self.delete(string)
  _check_for_valid_file(string)
  raise Errno::ENOTEMPTY, string.to_s unless FileSystem.find(string).empty?

  FileSystem.delete(string)
end
Also aliased as: rmdir, unlink
each_child(dirname) { |file| ... } click to toggle source
# File lib/fakefs/dir.rb, line 98
def self.each_child(dirname, &_block)
  Dir.open(dirname) do |file|
    next if ['.', '..'].include?(file)
    yield file
  end
end
empty?(dirname) click to toggle source
# File lib/fakefs/dir.rb, line 106
def self.empty?(dirname)
  _check_for_valid_file(dirname)
  if File.directory?(dirname)
    Dir.new(dirname).count <= 2
  else
    false
  end
end
entries(dirname, _options = nil) click to toggle source
# File lib/fakefs/dir.rb, line 88
def self.entries(dirname, _options = nil)
  _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 69
def self.exists?(path)
  File.exist?(path) && File.directory?(path)
end
Also aliased as: exist?
foreach(dirname) { |file| ... } click to toggle source
# File lib/fakefs/dir.rb, line 116
def self.foreach(dirname, &_block)
  Dir.open(dirname) { |file| yield file }
end
glob(pattern, flags = 0, &block) click to toggle source
# File lib/fakefs/dir.rb, line 120
def self.glob(pattern, flags = 0, &block)
  matches_for_pattern = lambda do |matcher|
    [FileSystem.find(matcher, flags, true) || []].flatten.map do |e|
      if Dir.pwd.match(%r{\A/?\z}) ||
         !e.to_s.match(%r{\A#{Dir.pwd}/?})
        e.to_s
      else
        e.to_s.match(%r{\A#{Dir.pwd}/?}).post_match
      end
    end.sort
  end

  files =
    if pattern.is_a?(Array)
      pattern.map do |matcher|
        matches_for_pattern.call matcher
      end.flatten
    else
      matches_for_pattern.call pattern
    end

  block_given? ? files.each { |file| block.call(file) } : files
end
home(user = nil) click to toggle source
# File lib/fakefs/dir.rb, line 144
def self.home(user = nil)
  RealDir.home(user)
end
mkdir(string, _integer = 0) click to toggle source
# File lib/fakefs/dir.rb, line 148
def self.mkdir(string, _integer = 0)
  FileUtils.mkdir(string)
end
mktmpdir(prefix_suffix = nil, tmpdir = nil) { |path| ... } click to toggle source

This code has been borrowed from Rubinius

# File lib/fakefs/dir.rb, line 224
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, 0o700)
  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 13
def initialize(string)
  self.class._check_for_valid_file(string)

  @path     = FileSystem.normalize_path(string)
  @open     = true
  @pointer  = 0
  @contents = ['.', '..'] + FileSystem.find(@path).entries
  @inode    = FakeInode.new(self)
end
open(string) { |file| ... } click to toggle source
# File lib/fakefs/dir.rb, line 152
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 164
def self.pwd
  FileSystem.current_dir.to_s
end
Also aliased as: getwd
tmpdir() click to toggle source
# File lib/fakefs/dir.rb, line 160
def self.tmpdir
  '/tmp'
end

Private Class Methods

exist?(path)
Alias for: exists?
getwd()
Alias for: pwd
rmdir(string)
Alias for: delete

Public Instance Methods

close() click to toggle source
# File lib/fakefs/dir.rb, line 23
def close
  @open = false
  @pointer = nil
  @contents = nil
  nil
end
each() { |f| ... } click to toggle source
# File lib/fakefs/dir.rb, line 30
def each
  if block_given?
    while (f = read)
      yield f
    end
  else
    @contents.map { |entry| entry_to_relative_path(entry) }.each
  end
end
ino() click to toggle source
# File lib/fakefs/dir.rb, line 219
def ino
  @inode.inode_num
end
pos() click to toggle source
# File lib/fakefs/dir.rb, line 40
def pos
  @pointer
end
pos=(integer) click to toggle source
# File lib/fakefs/dir.rb, line 44
def pos=(integer)
  @pointer = integer
end
read() click to toggle source
# File lib/fakefs/dir.rb, line 48
def read
  raise IOError, 'closed directory' unless @pointer
  entry = @contents[@pointer]
  @pointer += 1
  entry_to_relative_path(entry) if entry
end
rewind() click to toggle source
# File lib/fakefs/dir.rb, line 55
def rewind
  @pointer = 0
end
seek(integer) click to toggle source
# File lib/fakefs/dir.rb, line 59
def seek(integer)
  raise IOError, 'closed directory' if @pointer.nil?
  @pointer = integer
  @contents[integer]
end

Private Instance Methods

entry_to_relative_path(entry) click to toggle source
# File lib/fakefs/dir.rb, line 271
def entry_to_relative_path(entry)
  filename = entry.to_s
  filename.start_with?("#{path}/") ? filename[path.size + 1..-1] : filename
end