Skip to content

Commit

Permalink
Rerun the <ask> dialog if the script fails
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed May 10, 2021
1 parent 151687c commit 8cc89b3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/lib/autoinstall/ask/runner.rb
Expand Up @@ -75,8 +75,8 @@ def run
if result == :back
current_dialog = go_back
elsif result == :next
current_dialog.questions.each { |q| process_question(q) }
current_dialog = go_next
results = current_dialog.questions.map { |q| process_question(q) }
current_dialog = go_next unless results.any? { |r| r == :repeat }
end
end
end
Expand Down Expand Up @@ -179,6 +179,9 @@ def first?
# 3. Runs the script
#
# @param question [Question] Question to process
# @return [Symbol] Action to perform depending on this question.
# :next to continue; :repeat to run the question (and the whole dialog) again.
# @see #run_script
def process_question(question)
update_profile(question)
write_value(question)
Expand Down Expand Up @@ -210,13 +213,19 @@ def write_value(question)

# Runs the associated script if defined
#
# If the script fails but the `rerun_on_error` attribute is set to true,
# the dialog should run again.
#
# @param question [Question]
# @return [Symbol] Action to perform depending on this script.
# :next to continue; :repeat to run the question (and the whole dialog) again.
def run_script(question)
return unless question.script
return :next unless question.script

question.script.create_script_file
env = question.script.environment ? { "VAL" => question.value } : {}
script_runner.run(question.script, env: env)
result = script_runner.run(question.script, env: env)
(result || !question.script.rerun_on_error) ? :next : :repeat
end

# Returns a ScriptRunner instance to run scripts
Expand Down
3 changes: 2 additions & 1 deletion src/lib/autoinstall/script.rb
Expand Up @@ -422,7 +422,7 @@ def script_path

# Scripts that are used when processing the <ask-list> section
class AskScript < Y2Autoinstallation::ExecutedScript
attr_reader :environment
attr_reader :environment, :rerun_on_error

def self.type
"ask-scripts"
Expand All @@ -431,6 +431,7 @@ def self.type
def initialize(hash)
super
@environment = !!hash["environment"]
@rerun_on_error = !!hash["rerun_on_error"]
end

def to_hash
Expand Down
1 change: 1 addition & 0 deletions src/lib/autoinstall/script_runner.rb
Expand Up @@ -45,6 +45,7 @@ def run(script, env: {})

show_feedback(script)
report_error(script) unless result
result
end

private
Expand Down
36 changes: 35 additions & 1 deletion test/lib/autoinstall/ask/runner_test.rb
Expand Up @@ -104,14 +104,18 @@

context "when a script is set" do
let(:script) do
instance_double(Y2Autoinstallation::AskScript, environment: environment?)
instance_double(
Y2Autoinstallation::AskScript,
environment: environment?, rerun_on_error: rerun_on_error?
)
end

let(:script_runner) do
instance_double(Y2Autoinstall::ScriptRunner)
end

let(:environment?) { false }
let(:rerun_on_error?) { false }

let(:question1) do
Y2Autoinstall::Ask::Question.new("Question 1").tap do |question|
Expand All @@ -130,6 +134,36 @@
runner.run
end

context "and if it fails but the 'rerun_on_error' is set to 'true'" do
let(:rerun_on_error?) { true }

before do
allow(script).to receive(:create_script_file)
allow(script_runner).to receive(:run).with(script, env: {})
.and_return(false, true)
end

it "runs the dialog again" do
expect(ask_dialog1).to receive(:run).twice
runner.run
end
end

context "and if it fails but the 'rerun_on_error' is set to 'true'" do
let(:rerun_on_error?) { false }

before do
allow(script).to receive(:create_script_file)
allow(script_runner).to receive(:run).with(script, env: {})
.and_return(false)
end

it "runs the dialog again" do
expect(ask_dialog1).to receive(:run).once
runner.run
end
end

context "when the 'environment' attribute is set to 'true'" do
let(:environment?) { true }

Expand Down
8 changes: 8 additions & 0 deletions test/lib/autoinstall/script_runner_test.rb
Expand Up @@ -40,6 +40,10 @@
runner.run(script)
end

it "returns true" do
expect(runner.run(script)).to eq(true)
end

context "when a notification is defined" do
let(:spec) do
{ "notification" => "A script is running..." }
Expand Down Expand Up @@ -111,6 +115,10 @@
.with(/User script/, any_args)
runner.run(script)
end

it "returns false" do
expect(runner.run(script)).to eq(false)
end
end
end
end

0 comments on commit 8cc89b3

Please sign in to comment.