Test Tempdir class cleans itself up
This commit is contained in:
parent
37c54c7b20
commit
f26024f771
|
@ -3,9 +3,6 @@ require 'tempfile'
|
||||||
|
|
||||||
# This class provides an easy way of creating a temporary
|
# This class provides an easy way of creating a temporary
|
||||||
# directory and having it removed when the application exits.
|
# directory and having it removed when the application exits.
|
||||||
#
|
|
||||||
# TODO: This class doesn't currently delete the temporary
|
|
||||||
# directory on exit.
|
|
||||||
class Tempdir
|
class Tempdir
|
||||||
attr_reader :path
|
attr_reader :path
|
||||||
|
|
||||||
|
@ -25,10 +22,22 @@ class Tempdir
|
||||||
@path = nil
|
@path = nil
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
# This deletes the temporary directory.
|
# This deletes the temporary directory.
|
||||||
def unlink
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue