Allow COnfirm calls to be forced by specifying a force_key
This commit is contained in:
parent
50d7b0aba4
commit
dffbf6eb09
|
@ -9,15 +9,25 @@ module Vagrant
|
||||||
# For documentation, read the description of the {Confirm} class.
|
# For documentation, read the description of the {Confirm} class.
|
||||||
#
|
#
|
||||||
# @param [String] message The message to ask the user.
|
# @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
|
@app = app
|
||||||
@message = message
|
@message = message
|
||||||
|
@force_key = force_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
# Ask the user the message and store the result
|
|
||||||
choice = nil
|
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"
|
env[:result] = choice && choice.upcase == "Y"
|
||||||
|
|
||||||
@app.call(env)
|
@app.call(env)
|
||||||
|
|
|
@ -13,6 +13,21 @@ describe Vagrant::Action::Builtin::Confirm do
|
||||||
end
|
end
|
||||||
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
|
it "should set result to false if anything else is given" do
|
||||||
env[:ui].should_receive(:ask).with(message).and_return("nope")
|
env[:ui].should_receive(:ask).with(message).and_return("nope")
|
||||||
described_class.new(app, env, message).call(env)
|
described_class.new(app, env, message).call(env)
|
||||||
|
|
Loading…
Reference in New Issue