class RSpec::Support::DirectoryMaker

@api private

Replacement for fileutils#mkdir_p because we don't want to require parts of stdlib in RSpec.

Public Class Methods

mkdir_p(path) click to toggle source

@api private

Implements nested directory construction

# File lib/rspec/support/directory_maker.rb, line 13
def self.mkdir_p(path)
  stack = generate_stack(path)
  path.split(File::SEPARATOR).each do |part|
    stack = generate_path(stack, part)
    begin
      Dir.mkdir(stack) unless directory_exists?(stack)
    rescue Errno::EEXIST => e
      raise e unless directory_exists?(stack)
    rescue Errno::ENOTDIR => e
      raise Errno::EEXIST, e.message
    end
  end
end

Private Class Methods

directory_exists?(dirname) click to toggle source
# File lib/rspec/support/directory_maker.rb, line 55
def self.directory_exists?(dirname)
  File.exist?(dirname) && File.directory?(dirname)
end
generate_path(stack, part) click to toggle source
# File lib/rspec/support/directory_maker.rb, line 37
def self.generate_path(stack, part)
  if stack == ''
    part
  elsif stack == File::SEPARATOR
    File.join('', part)
  else
    File.join(stack, part)
  end
end
generate_stack(path) click to toggle source
# File lib/rspec/support/directory_maker.rb, line 28
def self.generate_stack(path)
  if path.start_with?(File::SEPARATOR)
    File::SEPARATOR
  elsif path[1] == ':'
    ''
  else
    '.'
  end
end