4 # A utility class to manage the creation and automatic cleanup of temporary directories.
8 # Creates a uniquely named directory in the system tmp directory.
10 # If a block is passed to the constructor, the path to the created directory
11 # will be yielded to the block. The directory will then be deleted
12 # when this block returns.
16 # ScratchDir.new do |path|
17 # # Do some work in the new directory
18 # File.new( path + '/foobaz', 'w' ) do
21 # end # Scratch directory is deleted here
25 @path = File.join(Dir.tmpdir, Time.now.utc.strftime("ohloh_%Y%H%m%S#{rand(900) + 100}"))
34 FileUtils.rm_rf(@path)
43 STDOUT.puts "Created scratch direcory #{d}"
44 File.open(File.join(d, "test"), "w") do |io|
48 raise RuntimeError.new("Directory wasn't cleaned up") if FileTest.directory?(path)
49 STDOUT.puts "Test passed."