From 3e51a07a80cc2311aaf995a99a5d428608b50496 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 5 Jul 2010 03:30:33 +0200 Subject: [PATCH] Persisting the dotfile middleware --- lib/vagrant/action/builtin.rb | 1 + lib/vagrant/action/vm/persist.rb | 22 ++++++++++++ test/vagrant/action/vm/persist_test.rb | 50 ++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 lib/vagrant/action/vm/persist.rb create mode 100644 test/vagrant/action/vm/persist_test.rb diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index ab6a6dfd5..dd5a3c5b1 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -7,6 +7,7 @@ module Vagrant def self.builtin! up = Builder.new do use VM::Import + use VM::Persist use VM::Customize use VM::ForwardPorts use VM::ShareFolders diff --git a/lib/vagrant/action/vm/persist.rb b/lib/vagrant/action/vm/persist.rb new file mode 100644 index 000000000..73f37852b --- /dev/null +++ b/lib/vagrant/action/vm/persist.rb @@ -0,0 +1,22 @@ +module Vagrant + class Action + module VM + class Persist + def initialize(app, env) + @app = app + + # Error the environment if the dotfile is not valid + env.error!(:dotfile_error, :env => env.env) if File.exist?(env.env.dotfile_path) && + !File.file?(env.env.dotfile_path) + end + + def call(env) + env.logger.info "Persisting the VM UUID (#{env["vm"].uuid})" + env.env.update_dotfile + + @app.call(env) + end + end + end + end +end diff --git a/test/vagrant/action/vm/persist_test.rb b/test/vagrant/action/vm/persist_test.rb new file mode 100644 index 000000000..555563a37 --- /dev/null +++ b/test/vagrant/action/vm/persist_test.rb @@ -0,0 +1,50 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class PersistVMActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::VM::Persist + @app, @env = mock_action_data + + @vm = mock("vm") + @vm.stubs(:uuid).returns("123") + @env["vm"] = @vm + end + + context "initializing" do + setup do + File.stubs(:file?).returns(true) + File.stubs(:exist?).returns(true) + @dotfile_path = "foo" + @env.env.stubs(:dotfile_path).returns(@dotfile_path) + end + + should "error environment if dotfile exists but is not a file" do + File.expects(:file?).with(@env.env.dotfile_path).returns(false) + @klass.new(@app, @env) + assert @env.error? + assert_equal :dotfile_error, @env.error.first + end + + should "initialize properly if dotfiles doesn't exist" do + File.expects(:exist?).with(@env.env.dotfile_path).returns(false) + @klass.new(@app, @env) + assert !@env.error? + end + end + + context "with an instance" do + setup do + File.stubs(:file?).returns(true) + File.stubs(:exist?).returns(true) + @instance = @klass.new(@app, @env) + end + + should "persist the dotfile then continue chain" do + update_seq = sequence("update_seq") + @env.env.expects(:update_dotfile).in_sequence(update_seq) + @app.expects(:call).with(@env).in_sequence(update_seq) + + @instance.call(@env) + end + end +end