From 7e7563132c7220c3c30b19b069f6a174c60f6193 Mon Sep 17 00:00:00 2001 From: Adam Leff Date: Fri, 8 Sep 2017 16:36:21 -0400 Subject: [PATCH] Return nil when looking up non-existing products When calling `PRODUCT_MATRIX.lookup` for a product that does not exist, we'd try to call the `version` method on nil, resulting in an "undefined method" error. Now we return nil to the caller early before trying to manipulate it. Signed-off-by: Adam Leff --- lib/mixlib/install/product.rb | 3 +++ spec/unit/mixlib/install/product_spec.rb | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/lib/mixlib/install/product.rb b/lib/mixlib/install/product.rb index 3f3dc26e..f23dcffa 100644 --- a/lib/mixlib/install/product.rb +++ b/lib/mixlib/install/product.rb @@ -162,6 +162,9 @@ def products # # @return [Product] def lookup(key, version = :latest) + # return nil unless the product exists + return nil unless @product_map.key?(key) + product = @product_map[key] # We set the lookup version for the product to a very high number in # order to mimic :latest so that one does not need to handle this diff --git a/spec/unit/mixlib/install/product_spec.rb b/spec/unit/mixlib/install/product_spec.rb index 87d3c742..36258871 100644 --- a/spec/unit/mixlib/install/product_spec.rb +++ b/spec/unit/mixlib/install/product_spec.rb @@ -167,6 +167,10 @@ end end + it "returns nil when looking up a non-existent product" do + expect(PRODUCT_MATRIX.lookup("no-such-project")).to be_nil + end + it "returns nil for unset parameters" do expect(PRODUCT_MATRIX.lookup("chef").ctl_command).to be_nil end