Test Tempdir class cleans itself up

This commit is contained in:
Mitchell Hashimoto 2012-06-24 00:03:28 -07:00
parent 37c54c7b20
commit f26024f771
1 changed files with 13 additions and 4 deletions

View File

@ -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