Skip to content

Commit

Permalink
Show rendering errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Jul 2, 2021
1 parent 611358c commit 6c6d413
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib/autoinstall/profile_checker.rb
@@ -1,4 +1,4 @@
# Copyright (c) [2020] SUSE LLC
# Copyright (c) [2020-2021] SUSE LLC
#
# All Rights Reserved.
#
Expand Down
28 changes: 26 additions & 2 deletions src/modules/ProfileLocation.rb
Expand Up @@ -11,6 +11,7 @@
require "autoinstall/y2erb"
require "y2storage"
require "fileutils"
require "yast2/popup"

module Yast
class ProfileLocationClass < Module
Expand Down Expand Up @@ -154,8 +155,7 @@ def Process

# render erb template
if AutoinstConfig.filepath.end_with?(".erb")
res = Y2Autoinstallation::Y2ERB.render(localfile)
SCR.Write(path(".target.string"), localfile, res)
return false unless render_erb(localfile)
end
else
is_directory = true
Expand Down Expand Up @@ -257,6 +257,30 @@ def Process

publish function: :ProfileLocation, type: "void ()"
publish function: :Process, type: "boolean ()"

private

# Renders the ERB profile and saves the result
#
# An error popup is shown if there is an error while rendering the profile.
#
# @return [Boolean] true if everything was ok.
def render_erb(file)
res = nil

begin
res = Y2Autoinstallation::Y2ERB.render(file)
rescue StandardError => e
message = _("There was an error while rendering the ERB profile.")
details = e.message + "\n\n" + e.backtrace.join("\n")

Yast2::Popup.show(message, headline: :error, details: details)
return false
end

SCR.Write(path(".target.string"), file, res)
true
end
end

ProfileLocation = ProfileLocationClass.new
Expand Down
70 changes: 68 additions & 2 deletions test/ProfileLocation_test.rb
@@ -1,5 +1,24 @@
#!/usr/bin/env rspec

# Copyright (c) [2019-2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "test_helper"

Yast.import "ProfileLocation"
Expand All @@ -9,18 +28,19 @@
subject { Yast::ProfileLocation }

describe "#Process" do

before do
Yast::AutoinstConfig.scheme = "relurl"
Yast::AutoinstConfig.xml_tmpfile = "/tmp/123"
Yast::AutoinstConfig.filepath = "autoinst.xml"
Yast::AutoinstConfig.filepath = filepath
allow(Yast::InstURL).to receive(:installInf2Url).and_return(
"http://download.opensuse.org/distribution/leap/15.1/repo/oss/"
)
allow(Yast::SCR).to receive(:Read).and_return("test")
allow(Yast::Report).to receive(:Error) # test is already quite weak and some errors are shown
end

let(:filepath) { "autoinst.xml" }

context "when scheme is \"relurl\"" do
it "downloads AutoYaST configuration file with absolute path" do
expect(subject).to receive(:Get).with("http",
Expand Down Expand Up @@ -50,5 +70,51 @@
subject.Process
end
end

context "when the profile is an erb file" do
let(:filepath) { "autoinst.erb" }

before do
allow(Yast2::Popup).to receive(:show)

allow(subject).to receive(:Get).and_return("test")

allow(Yast::GPG).to receive(:encrypted_symmetric?).and_return(false)
end

context "and there is no error rendering the erb profile" do
before do
allow(Y2Autoinstallation::Y2ERB).to receive(:render)
.with(Yast::AutoinstConfig.xml_tmpfile).and_return("rendered content")

allow(Y2Autoinstallation::XmlChecks.instance).to receive(:valid_profile?).and_return(true)

allow(Yast::SCR).to receive(:Write)
end

it "does not show rendering errors" do
expect(Yast2::Popup).to_not receive(:show)

subject.Process
end
end

context "and there is some error rendering the erb profile" do
before do
allow(Y2Autoinstallation::Y2ERB).to receive(:render)
.with(Yast::AutoinstConfig.xml_tmpfile).and_raise StandardError
end

it "shows a rendering error" do
expect(Yast2::Popup).to receive(:show).with(/error while rendering/, anything)

subject.Process
end

it "returns false" do
expect(subject.Process).to eq(false)
end
end
end
end
end

0 comments on commit 6c6d413

Please sign in to comment.