Sha256: f29e33e75a0e0df794c2091ff8b27d428e4a587278b46e743751ed5aedf4f8ff

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'fileutils'

# A utility class to manage the creation and automatic cleanup of temporary directories.
module Scm
  class ScratchDir
    attr_reader :path

    # Creates a uniquely named directory in the system tmp directory.
    #
    # If a block is passed to the constructor, the path to the created directory
    # will be yielded to the block. The directory will then be deleted
    # when this block returns.
    #
    # Sample usage:
    #
    #   ScratchDir.new do |path|
    #     # Do some work in the new directory
    #     File.new( path + '/foobaz', 'w' ) do
    #       # ...
    #     end
    #   end # Scratch directory is deleted here
    #
    def initialize
      @path = `mktemp -d /tmp/ohloh_scm_XXXXXX`.strip
      if block_given?
        begin
          return yield(@path)
        ensure
          FileUtils.rm_rf(@path)
        end
      end
    end
  end

  if $0 == __FILE__
    path = nil

    ScratchDir.new do |d|
      path = d
      filename = File.join(d,"test")
      File.open(filename, "w") do |io|
        io.write "test"
      end
    end
    raise RuntimeError.new("Directory wasn't cleaned up") if FileTest.directory?(path)

    begin
      ScratchDir.new do |d|
        path = d
        STDOUT.puts "Created scratch direcory #{d}"
        raise RuntimeError.new("This error should not prevent cleanup")
      end
    rescue
    end
    raise RuntimeError.new("Directory wasn't cleaned up") if FileTest.directory?(path)

    STDOUT.puts "Tests passed."
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ohloh_scm-2.0.0 lib/ohloh_scm/scratch_dir.rb