Properly cleanup exported box

This commit is contained in:
Mitchell Hashimoto 2010-07-08 23:28:40 -07:00
parent 62a3adb7dd
commit 8bd64de61d
2 changed files with 33 additions and 1 deletions

View File

@ -2,6 +2,8 @@ module Vagrant
class Action
module VM
class Export
attr_reader :temp_dir
def initialize(app, env)
@app = app
@env = env
@ -16,11 +18,19 @@ module Vagrant
export
@app.call(env)
cleanup
end
def cleanup
if temp_dir && File.exist?(temp_dir)
FileUtils.rm_rf(temp_dir)
end
end
def setup_temp_dir
@env.logger.info "Creating temporary directory for export..."
@env["export.temp_dir"] = File.join(@env.env.tmp_path, Time.now.to_i.to_s)
@temp_dir = @env["export.temp_dir"] = File.join(@env.env.tmp_path, Time.now.to_i.to_s)
FileUtils.mkpath(@env["export.temp_dir"])
end

View File

@ -24,6 +24,7 @@ class ExportVMActionTest < Test::Unit::TestCase
@instance.expects(:setup_temp_dir).in_sequence(seq)
@instance.expects(:export).in_sequence(seq)
@app.expects(:call).with(@env).in_sequence(seq)
@instance.expects(:cleanup).in_sequence(seq)
@instance.call(@env)
end
@ -33,6 +34,7 @@ class ExportVMActionTest < Test::Unit::TestCase
@instance.expects(:setup_temp_dir).never
@instance.expects(:export).never
@app.expects(:call).with(@env).never
@instance.expects(:cleanup).never
@instance.call(@env)
assert @env.error?
@ -40,6 +42,25 @@ class ExportVMActionTest < Test::Unit::TestCase
end
end
context "cleaning up" do
setup do
@temp_dir = "foo"
@instance.stubs(:temp_dir).returns(@temp_dir)
File.stubs(:exist?).returns(true)
end
should "delete the temporary file if it exists" do
File.expects(:unlink).with(@temp_dir).once
@instance.cleanup
end
should "not delete anything if it doesn't exist" do
File.stubs(:exist?).returns(false)
File.expects(:unlink).never
@instance.cleanup
end
end
context "setting up the temporary directory" do
setup do
@time_now = Time.now.to_i.to_s
@ -60,6 +81,7 @@ class ExportVMActionTest < Test::Unit::TestCase
should "set to the environment" do
@instance.setup_temp_dir
assert_equal @temp_dir, @env["export.temp_dir"]
assert_equal @temp_dir, @instance.temp_dir
end
end