Box actions clean up the temporary file after unpackaging

This commit is contained in:
Mitchell Hashimoto 2010-02-22 22:42:31 -08:00
parent c7e21a0c94
commit 5b68f3dd10
4 changed files with 37 additions and 2 deletions

View File

@ -14,6 +14,11 @@ module Vagrant
end end
end end
def after_unpackage
logger.info "Cleaning up tempfile..."
File.unlink(@runner.temp_path) if @runner.temp_path && File.exist?(@runner.temp_path)
end
def with_tempfile def with_tempfile
logger.info "Creating tempfile for storing box file..." logger.info "Creating tempfile for storing box file..."
Tempfile.open(BASENAME, Env.tmp_path) do |tempfile| Tempfile.open(BASENAME, Env.tmp_path) do |tempfile|

View File

@ -5,9 +5,11 @@ module Vagrant
TAR_OPTIONS = [File::RDONLY, 0644, Tar::GNU] TAR_OPTIONS = [File::RDONLY, 0644, Tar::GNU]
def execute! def execute!
@runner.invoke_around_callback(:unpackage) do
setup_box_dir setup_box_dir
decompress decompress
end end
end
def setup_box_dir def setup_box_dir
if File.directory?(box_dir) if File.directory?(box_dir)

View File

@ -50,6 +50,25 @@ class DownloadBoxActionTest < Test::Unit::TestCase
end end
end end
context "after unpackaging" do
setup do
@temp_path = "foo"
@runner.stubs(:temp_path).returns(@temp_path)
File.stubs(:exist?).returns(true)
end
should "delete the temporary file if it exists" do
File.expects(:unlink).with(@temp_path).once
@action.after_unpackage
end
should "not delete anything if it doesn't exist" do
File.stubs(:exist?).returns(false)
File.expects(:unlink).never
@action.after_unpackage
end
end
context "copying URI file" do context "copying URI file" do
setup do setup do
@tempfile = mock("tempfile") @tempfile = mock("tempfile")

View File

@ -11,12 +11,21 @@ class UnpackageBoxActionTest < Test::Unit::TestCase
end end
context "executing" do context "executing" do
setup do
@runner.stubs(:invoke_around_callback).yields
end
should "execute the proper actions in the proper order" do should "execute the proper actions in the proper order" do
exec_seq = sequence("exec_seq") exec_seq = sequence("exec_seq")
@action.expects(:setup_box_dir).in_sequence(exec_seq) @action.expects(:setup_box_dir).in_sequence(exec_seq)
@action.expects(:decompress).in_sequence(exec_seq) @action.expects(:decompress).in_sequence(exec_seq)
@action.execute! @action.execute!
end end
should "execute it in a around block" do
@runner.expects(:invoke_around_callback).with(:unpackage).once
@action.execute!
end
end end
context "box directory" do context "box directory" do