Added rescue/cleanup methods to box downloading to cleanup temporary files

This commit is contained in:
Mitchell Hashimoto 2010-03-03 23:00:04 -08:00
parent a7dbb26aa0
commit 35af1fa02b
2 changed files with 19 additions and 6 deletions

View File

@ -31,9 +31,15 @@ module Vagrant
end end
end end
def after_unpackage def cleanup
logger.info "Cleaning up tempfile..." if @runner.temp_path && File.exist?(@runner.temp_path)
File.unlink(@runner.temp_path) if @runner.temp_path && File.exist?(@runner.temp_path) logger.info "Cleaning up downloaded box..."
File.unlink(@runner.temp_path)
end
end
def rescue(exception)
cleanup
end end
def with_tempfile def with_tempfile

View File

@ -61,6 +61,13 @@ class DownloadBoxActionTest < Test::Unit::TestCase
end end
end end
context "rescue" do
should "call cleanup method" do
@action.expects(:cleanup).once
@action.rescue(nil)
end
end
context "tempfile" do context "tempfile" do
should "create a tempfile in the vagrant tmp directory" do should "create a tempfile in the vagrant tmp directory" do
Tempfile.expects(:open).with(Vagrant::Actions::Box::Download::BASENAME, Vagrant::Env.tmp_path).once Tempfile.expects(:open).with(Vagrant::Actions::Box::Download::BASENAME, Vagrant::Env.tmp_path).once
@ -77,7 +84,7 @@ class DownloadBoxActionTest < Test::Unit::TestCase
end end
end end
context "after unpackaging" do context "cleaning up" do
setup do setup do
@temp_path = "foo" @temp_path = "foo"
@runner.stubs(:temp_path).returns(@temp_path) @runner.stubs(:temp_path).returns(@temp_path)
@ -86,13 +93,13 @@ class DownloadBoxActionTest < Test::Unit::TestCase
should "delete the temporary file if it exists" do should "delete the temporary file if it exists" do
File.expects(:unlink).with(@temp_path).once File.expects(:unlink).with(@temp_path).once
@action.after_unpackage @action.cleanup
end end
should "not delete anything if it doesn't exist" do should "not delete anything if it doesn't exist" do
File.stubs(:exist?).returns(false) File.stubs(:exist?).returns(false)
File.expects(:unlink).never File.expects(:unlink).never
@action.after_unpackage @action.cleanup
end end
end end