Persisting the dotfile middleware

This commit is contained in:
Mitchell Hashimoto 2010-07-05 03:30:33 +02:00
parent c365a7ffac
commit 3e51a07a80
3 changed files with 73 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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