Skip to content

Commit

Permalink
Avoid to crash when there is not a software section
Browse files Browse the repository at this point in the history
AutoinstFunction#base_product_name was crashing when given profile had
not a valid `software` section, which could happen due to several
reasons. For example, a not valid XML syntax or simply a user oversight.

It was failing with

> undefined method 'fetch' for nil:NilClass"

Related to bsc#1125959
  • Loading branch information
dgdavid committed Feb 22, 2019
1 parent 43c9175 commit f8a95af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/modules/AutoinstFunctions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def identify_product
# @return [Y2Packager::Product] a product if exactly one product matches
# the criteria, nil otherwise
def identify_product_by_patterns(profile)
software = profile.fetch("software", {})
software = profile["software"] || {}

identify_product do |product|
software.fetch("patterns", []).any? { |p| p =~ /#{product.name.downcase}-.*/ }
Expand All @@ -151,7 +151,7 @@ def identify_product_by_patterns(profile)
# @return [Y2Packager::Product] a product if exactly one product matches
# the criteria, nil otherwise
def identify_product_by_packages(profile)
software = profile.fetch("software", {})
software = profile["software"] || {}

identify_product do |product|
software.fetch("packages", []).any? { |p| p =~ /#{product.name.downcase}-release/ }
Expand Down Expand Up @@ -180,8 +180,10 @@ def identify_product_by_selection(profile)
# @param profile [Hash] AutoYaST profile
# @return [String] product name
def base_product_name(profile)
software = profile.fetch("software", {})
software.fetch("products", []).first
software = profile["software"] || {}
products = software["products"] || []

products.first
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/AutoinstFunctions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,12 @@ def base_product(name)
expect(subject.selected_product.name).to eql "SLED"
end
end

it "does not crash when there is not a software section" do
allow(Yast::Profile)
.to receive(:current)
.and_return("software" => nil)

expect(subject.selected_product).to be_nil
end
end

0 comments on commit f8a95af

Please sign in to comment.