diff --git a/lib/vagrant/action/builtin/confirm.rb b/lib/vagrant/action/builtin/confirm.rb index d877f9dea..8f490e050 100644 --- a/lib/vagrant/action/builtin/confirm.rb +++ b/lib/vagrant/action/builtin/confirm.rb @@ -9,15 +9,25 @@ module Vagrant # For documentation, read the description of the {Confirm} class. # # @param [String] message The message to ask the user. - def initialize(app, env, message) + # @param [Symbol] force_key The key that if present and true in + # the environment hash will skip the confirmation question. + def initialize(app, env, message, force_key=nil) @app = app @message = message + @force_key = force_key end def call(env) - # Ask the user the message and store the result choice = nil - choice = env[:ui].ask(@message) + + # If we have a force key set and we're forcing, then set + # the result to "Y" + choice = "Y" if @force_key && env[@force_key] + + # If we haven't chosen yes, then ask the user via TTY + choice = env[:ui].ask(@message) if !choice + + # The result is only true if the user said "Y" env[:result] = choice && choice.upcase == "Y" @app.call(env) diff --git a/test/unit/vagrant/action/builtin/confirm_test.rb b/test/unit/vagrant/action/builtin/confirm_test.rb index 7c290e172..7f689582f 100644 --- a/test/unit/vagrant/action/builtin/confirm_test.rb +++ b/test/unit/vagrant/action/builtin/confirm_test.rb @@ -13,6 +13,21 @@ describe Vagrant::Action::Builtin::Confirm do end end + it "should set the result to true if force matches" do + force_key = :tubes + env[force_key] = true + described_class.new(app, env, message, force_key).call(env) + env[:result].should be + end + + it "should ask if force is not true" do + force_key = :tubes + env[force_key] = false + env[:ui].should_receive(:ask).with(message).and_return("nope") + described_class.new(app, env, message).call(env) + env[:result].should_not be + end + it "should set result to false if anything else is given" do env[:ui].should_receive(:ask).with(message).and_return("nope") described_class.new(app, env, message).call(env)