Skip to content

Commit

Permalink
Merge afd12fc into 3197fb8
Browse files Browse the repository at this point in the history
  • Loading branch information
ancorgs committed Jun 23, 2021
2 parents 3197fb8 + afd12fc commit 38f7446
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MissingValue < ::Installation::AutoinstIssues::Issue
# @param section [String] Section where it was detected
# @param attribute [String] Name of the missing attribute
# @param description [String] additional explanation; optional
# @param severity [Symbol] :warn, :fatal = abort the installation ; optional
# @param severity [Symbol] :warn, :error = abort the installation ; optional
def initialize(section, attr, description = "", severity = :warn)
textdomain "base"

Expand Down
11 changes: 7 additions & 4 deletions library/general/src/lib/y2issues.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@
#
# @example Registering an error
# list = Y2Issues::List.new
# list << Y2Issues::Issue.new("Could not read network configuration", severity: :fatal)
# list << Y2Issues::Issue.new("Could not read network configuration", severity: :error)
module Y2Issues
# Reports the errors to the user
#
# This is a helper method that offers an stable API on top of {Reporter}. Depending on
# Yast::Report settings, it may show a pop-up with the found issues and log them.
#
# @param [List] Issues list
# @param issues [List] Issues list
# @param warn [Symbol] what to do if the list of issues only contains warnings
# @param error [Symbol] what to do if the list of issues contains some error
# @return [Boolean] whether the process may continue, false means YaST is expected to abort
# @see Y2Issues::Reporter
def self.report(issues)
Reporter.new(issues).report
def self.report(issues, warn: :ask, error: :abort)
Reporter.new(issues).report(warn: warn, error: error)
end
end

Expand Down
14 changes: 8 additions & 6 deletions library/general/src/lib/y2issues/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Y2Issues
# specific information. See {InvalidValue} as an example.
#
# @example Create a new error
# Issue.new("Could not read network configuration", severity: :fatal)
# Issue.new("Could not read network configuration", severity: :error)
#
# @example Create an error from an specific location
# Issue.new(
Expand All @@ -42,25 +42,27 @@ class Issue
attr_reader :location
# @return [String] Error message
attr_reader :message
# @return [Symbol] Error severity (:warn, :fatal)
# @return [Symbol] Error severity (:warn, :error)
attr_reader :severity

# @param message [String] User-oriented message describing the problem
# @param location [String,nil] Where the error is located. Use a URI or
# a string to represent the error location. Use 'nil' if it
# does not exist an specific location.
# @param severity [Symbol] warning (:warn) or fatal (:fatal)
# @param severity [Symbol] warning (:warn) or error (:error)
def initialize(message, location: nil, severity: :warn)
@message = message
@location = location.is_a?(String) ? Location.parse(location) : location
@severity = severity
end

# Determines whether the error is fatal or not
# Determines whether the issue is an error
#
# @return [Boolean]
def fatal?
@severity == :fatal
def error?
@severity == :error
end

alias_method :fatal?, :error?
end
end
10 changes: 6 additions & 4 deletions library/general/src/lib/y2issues/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def initialize(issues = [])
@items = issues
end

# Determine whether any of the problem on the list is fatal
# Determine whether any of the issues on the list is an error
#
# @return [Boolean] true if any of them is a fatal problem
def fatal?
any?(&:fatal?)
# @return [Boolean]
def error?
any?(&:error?)
end

alias_method :fatal?, :error?

# Returns an array containing registered problems
#
# @return [Array<Issue>] List of problems
Expand Down
6 changes: 3 additions & 3 deletions library/general/src/lib/y2issues/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def to_plain
#
# @return [String] HTML representing the list of issues
def to_html
fatal, non_fatal = issues.partition(&:fatal?)
errors, warnings = issues.partition(&:error?)
parts = []
parts << error_text(fatal) unless fatal.empty?
parts << warning_text(non_fatal) unless non_fatal.empty?
parts << error_text(errors) unless errors.empty?
parts << warning_text(warnings) unless warnings.empty?

parts.join
end
Expand Down
101 changes: 79 additions & 22 deletions library/general/src/lib/y2issues/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

Yast.import "Label"
Yast.import "Report"
Yast.import "HTML"

module Y2Issues
# This class provides a mechanism to report YaST2 issues
Expand All @@ -37,45 +38,101 @@ class Reporter
def initialize(issues, report_settings: Yast::Report.Export)
textdomain "base"
@presenter = Presenter.new(issues)
@level = issues.fatal? ? :error : :warn
@level = issues.error? ? :error : :warn
@log, @show, @timeout = find_settings(report_settings, @level)
end

# Reports the issues to the user
#
# Depending on the given report settings, it may display a pop-up, and/or log the error.
def report
#
# In case of displaying the pop-up, the way to present the information and the possible return
# values are determined by the severity of the issues and the value of `warn` and `error`.
#
# If the value specified for the corresponding level is :abort, the pop-up contains the
# information and a single button to abort, the method returns false.
#
# If the value is :ask, the information is presented and the user is asked whether they want to
# continue or abort. The returned value depends on the answer.
#
# In the value is :continue (or any other symbol), the information is displayed with a button to
# simply close the pop-up and the method always returns true.
#
# @param warn [Symbol] what to do if the list of issues only contains warnings
# @param error [Symbol] what to do if the list of issues contains some error
# @return [Boolean] whether the process may continue, false means aborting
def report(warn: :ask, error: :abort)
log_issues if @log
show_issues if @show
return true unless @show

show_issues(warn, error)
end

private

attr_reader :level, :presenter
# Severity of the set of issues
#
# @return [Symbol] :warn if all the issues are just warnings, :error if any
# of the issues is an error
attr_reader :level

# @return [Presenter]
attr_reader :presenter

# Displays a pop-up containing the issues
#
# It can behave in two different ways depending if a fatal issue was found:
#
# * Ask the user if she/he wants to continue or abort the installation.
# * Display a message and only offer an 'Abort' button.
def show_issues
if level == :error
headline = :error
buttons = { abort: Yast::Label.AbortButton }
question = _("Please, correct these problems and try again.")
timeout = 0
# @return [Boolean] see {#report}
def show_issues(warn, error)
action = (level == :error) ? error : warn
case action
when :abort
show_issues_abort
when :ask
show_issues_ask
else
headline = :warning
buttons = :yes_no
question = _("Do you want to continue?")
timeout = @timeout
show_issues_continue
end
end

# @see #show_issues
#
# @return [Boolean] see {#report}, always false in this case
def show_issues_abort
buttons = { abort: Yast::Label.AbortButton }
question = _("Please, correct these problems and try again.")
popup(question, buttons, with_timeout: false)

false
end

# @see #show_issues
#
# @return [Boolean] see {#report}
def show_issues_ask
popup(_("Do you want to continue?"), :yes_no) == :yes
end

# @see #show_issues
#
# @return [Boolean] see {#report}, always true in this case
def show_issues_continue
popup("", :ok)
true
end

# Displays pop-up with information about the issues
def popup(footer, btns, with_timeout: true)
text = presenter.to_html
text += Yast::HTML.Para(footer) if footer && !footer.empty?
time = with_timeout ? @timeout : 0
Yast2::Popup.show(text, richtext: true, headline: header, buttons: btns, timeout: time)
end

# @see #popup
def header
return :error if level == :error

content = presenter.to_html + Yast::HTML.Para(question)
Yast2::Popup.show(
content, richtext: true, headline: headline, buttons: buttons, timeout: timeout
)
:warning
end

# Writes the issues
Expand Down
16 changes: 8 additions & 8 deletions library/general/test/y2issues/issue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
described_class.new(
"Something went wrong",
location: Y2Issues::Location.parse("file:/etc/hosts"),
severity: :fatal
severity: :error
)
end

it "creates an issue" do
expect(issue.message).to eq("Something went wrong")
expect(issue.location).to eq(Y2Issues::Location.parse("file:/etc/hosts"))
expect(issue.severity).to eq(:fatal)
expect(issue.severity).to eq(:error)
end

context "when location is given as a string" do
Expand All @@ -59,20 +59,20 @@
end
end

describe "#fatal?" do
context "when severity is :fatal" do
subject(:issue) { described_class.new("Something went wrong", severity: :fatal) }
describe "#error?" do
context "when severity is :error" do
subject(:issue) { described_class.new("Something went wrong", severity: :error) }

it "returns true" do
expect(issue.fatal?).to eq(true)
expect(issue.error?).to eq(true)
end
end

context "when severity is :fatal" do
context "when severity is :error" do
subject(:issue) { described_class.new("Something went wrong", severity: :warn) }

it "returns false" do
expect(issue.fatal?).to eq(false)
expect(issue.error?).to eq(false)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions library/general/test/y2issues/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@
end
end

describe "#fatal?" do
context "when contains some fatal error" do
let(:issue) { Y2Issues::Issue.new("Something went wrong", severity: :fatal) }
describe "#error?" do
context "when contains some error" do
let(:issue) { Y2Issues::Issue.new("Something went wrong", severity: :error) }

it "returns true" do
expect(list.fatal?).to eq(true)
expect(list.error?).to eq(true)
end
end

context "when does not contain any fatal error" do
context "when does not contain any error error" do
it "returns false" do
expect(list.fatal?).to eq(false)
expect(list.error?).to eq(false)
end

end
Expand Down
10 changes: 5 additions & 5 deletions library/general/test/y2issues/presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
let(:list) { Y2Issues::List.new }

describe "#to_html" do
context "when a fatal issue was found" do
context "when an error was found" do
before do
list << Y2Issues::Issue.new("Something is invalid", severity: :fatal)
list << Y2Issues::Issue.new("Something is invalid", severity: :error)
end

it "includes issues messages" do
issue = list.first
expect(presenter.to_html.to_s).to include "<li>#{issue.message}</li>"
end

it "includes an introduction to fatal issues qlist" do
it "includes an introduction to the errors list" do
expect(presenter.to_html.to_s).to include "Important issues"
end
end

context "when a non fatal issue was found" do
context "when a warning was found" do
before do
list << Y2Issues::Issue.new("Something is missing", severity: :warn)
end
Expand All @@ -51,7 +51,7 @@
expect(presenter.to_html.to_s).to include "<li>#{issue.message}</li>"
end

it "includes an introduction to non fatal issues list" do
it "includes an introduction to the warnings list" do
expect(presenter.to_html.to_s).to include "<p>Minor issues"
end
end
Expand Down
Loading

0 comments on commit 38f7446

Please sign in to comment.