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}"))
36 FileUtils.rm_rf(@path)
47 STDOUT.puts "Created scratch direcory #{d}"
48 File.open(File.join(d, "test"), "w") do |io|
52 raise RuntimeError.new("Directory wasn't cleaned up") if FileTest.directory?(path)
57 STDOUT.puts "Created scratch direcory #{d}"
58 raise RuntimeError.new("This error should not prevent cleanup")
62 raise RuntimeError.new("Directory wasn't cleaned up") if FileTest.directory?(path)
64 STDOUT.puts "Tests passed."