From f26024f77121dec497ded56a9498b7637d4c879e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 24 Jun 2012 00:03:28 -0700 Subject: [PATCH] Test Tempdir class cleans itself up --- test/support/tempdir.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/support/tempdir.rb b/test/support/tempdir.rb index 21984757f..5c7d7dee6 100644 --- a/test/support/tempdir.rb +++ b/test/support/tempdir.rb @@ -3,9 +3,6 @@ require 'tempfile' # This class provides an easy way of creating a temporary # directory and having it removed when the application exits. -# -# TODO: This class doesn't currently delete the temporary -# directory on exit. class Tempdir attr_reader :path @@ -25,10 +22,22 @@ class Tempdir @path = nil end end + + # Setup a finalizer to delete the directory. This is the same way + # that Tempfile and friends do this... + @cleanup_proc = lambda do + FileUtils.rm_rf(@path) if File.directory?(@path) + end + + ObjectSpace.define_finalizer(self, @cleanup_proc) end # This deletes the temporary directory. def unlink - FileUtils.rm_rf(@path) + # Delete the directory + @cleanup_proc.call + + # Undefine the finalizer since we're all cleaned up + ObjectSpace.undefine_finalizer(self) end end