Allow COnfirm calls to be forced by specifying a force_key

This commit is contained in:
Mitchell Hashimoto 2012-12-22 22:41:37 -08:00
parent 50d7b0aba4
commit dffbf6eb09
2 changed files with 28 additions and 3 deletions

View File

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

View File

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