Sha256: f493a6a71706508a24802998670949853ecd31f34bed47ac34f27fb2cd1f1559

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

class Object
  # Copies a file
  def cp(source, dest)
    File.open(dest, "w") do |d|
      File.open(source, "r") do |s|
        while data = s.read(1024)
          d.write data
        end
      end
    end
  end

  # Creates each directory in path that does not exist.
  def mkdir_p(path)
    parts = File.expand_path(path).split %r[/|\\]
    name = parts.shift
    parts.each do |part|
      name = File.join name, part

      if File.file? name
        raise ArgumentError, "path component of #{path} is a file"
      end

      Dir.mkdir name unless File.directory? name
    end
  end

  # Recursively removes all files and directories in +path+
  # if +path+ is a directory. Removes the file if +path+ is
  # a file.
  def rm_r(*paths)
    paths.each do |path|
      path = File.expand_path path

      prefix = SPEC_TEMP_DIR
      unless path[0, prefix.size] == prefix
        raise ArgumentError, "#{path} is not prefixed by #{prefix}"
      end

      if File.directory? path
        Dir.entries(path).each { |x| rm_r "#{path}/#{x}" unless x =~ /^\.\.?$/ }
        Dir.rmdir path
      elsif File.exists? path
        File.delete path
      end
    end
  end

  # Creates a file +name+. Creates the directory for +name+
  # if it does not exist.
  def touch(name)
    mkdir_p File.dirname(name)

    File.open(name, "w") do |f|
      yield f if block_given?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mspec-1.5.13 lib/mspec/helpers/fs.rb