From 5d2e3d9f6f03b42ae6a35b7a356dc1f96f0631c0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Jul 2010 08:32:41 -0700 Subject: [PATCH] Box destroying middleware --- lib/vagrant/action/box/destroy.rb | 19 +++++++++++++++ lib/vagrant/action/builtin.rb | 7 ++++++ lib/vagrant/box.rb | 4 ++-- test/vagrant/action/box/destroy_test.rb | 30 ++++++++++++++++++++++++ test/vagrant/actions/box/destroy_test.rb | 2 +- test/vagrant/box_test.rb | 2 +- 6 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 lib/vagrant/action/box/destroy.rb create mode 100644 test/vagrant/action/box/destroy_test.rb diff --git a/lib/vagrant/action/box/destroy.rb b/lib/vagrant/action/box/destroy.rb new file mode 100644 index 000000000..36cdacafd --- /dev/null +++ b/lib/vagrant/action/box/destroy.rb @@ -0,0 +1,19 @@ +module Vagrant + class Action + module Box + class Destroy + def initialize(app, env) + @app = app + @env = env + end + + def call(env) + env.logger.info "Deleting box directory..." + FileUtils.rm_rf(env["box"].directory) + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index 778cb6b9c..af30dd8c0 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -92,6 +92,13 @@ module Vagrant end register :box_add, box_add + + # box_remove - Removes/deletes a box. + box_remove = Builder.new do + use Box::Destroy + end + + register :box_remove, box_remove end end end diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index 3e29a84f5..857d0d953 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -40,7 +40,7 @@ module Vagrant # box = Vagrant::Box.find("foo") # box.destroy # - class Box < Actions::Runner + class Box # The name of the box. attr_accessor :name @@ -138,7 +138,7 @@ module Vagrant # Beings the process of destroying this box. def destroy - execute!(Actions::Box::Destroy) + env.actions.run(:box_remove, { "box" => self }) end # Returns the directory to the location of this boxes content in the local diff --git a/test/vagrant/action/box/destroy_test.rb b/test/vagrant/action/box/destroy_test.rb new file mode 100644 index 000000000..72c44b00d --- /dev/null +++ b/test/vagrant/action/box/destroy_test.rb @@ -0,0 +1,30 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class DestroyBoxActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::Box::Destroy + @app, @env = mock_action_data + + @vm = mock("vm") + @env["vm"] = @vm + @env["box"] = Vagrant::Box.new(mock_environment, "foo") + + @internal_vm = mock("internal") + @vm.stubs(:vm).returns(@internal_vm) + + @instance = @klass.new(@app, @env) + end + + context "calling" do + setup do + @env.logger.stubs(:info) + end + + should "delete the box directory" do + seq = sequence("seq") + FileUtils.expects(:rm_rf).with(@env["box"].directory).in_sequence(seq) + @app.expects(:call).with(@env).once.in_sequence(seq) + @instance.call(@env) + end + end +end diff --git a/test/vagrant/actions/box/destroy_test.rb b/test/vagrant/actions/box/destroy_test.rb index 741aa7e0d..31fe8b81b 100644 --- a/test/vagrant/actions/box/destroy_test.rb +++ b/test/vagrant/actions/box/destroy_test.rb @@ -1,6 +1,6 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') -class DestroyBoxActionTest < Test::Unit::TestCase +class DestroyBoxActionsTest < Test::Unit::TestCase setup do @name = "foo" @dir = "foo" diff --git a/test/vagrant/box_test.rb b/test/vagrant/box_test.rb index 09294483b..2a1533528 100644 --- a/test/vagrant/box_test.rb +++ b/test/vagrant/box_test.rb @@ -134,7 +134,7 @@ class BoxTest < Test::Unit::TestCase context "destroying" do should "execute the destroy action" do - @box.expects(:execute!).with(Vagrant::Actions::Box::Destroy).once + @box.env.actions.expects(:run).with(:box_remove, { "box" => @box }) @box.destroy end end