From 0cf9f8817e5b493cf93949b69a580dd45b39e021 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Fri, 17 Apr 2020 16:32:46 -0700 Subject: Return empty hash from brew_info and avoid a bunch of nil checks Signed-off-by: Tim Smith --- lib/chef/provider/package/homebrew.rb | 11 +++-------- spec/unit/provider/package/homebrew_spec.rb | 10 +++++----- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/chef/provider/package/homebrew.rb b/lib/chef/provider/package/homebrew.rb index e3a0e471cc..d0321b2175 100644 --- a/lib/chef/provider/package/homebrew.rb +++ b/lib/chef/provider/package/homebrew.rb @@ -96,7 +96,7 @@ class Chef # convert the array of hashes into a hash where the key is the package name cmd_output = brew_cmd_output(command_array, allow_failure: true) - return nil if cmd_output.empty? # empty std_out == bad package queried + return {} if cmd_output.empty? # empty std_out == bad package queried Hash[Chef::JSONCompat.from_json(cmd_output).collect { |pkg| [pkg["name"], pkg] }] end @@ -110,8 +110,6 @@ class Chef # @return [Hash] Package information # def package_info(package_name) - return nil if brew_info.nil? # continue to raise the nil up the chain - # return the package hash if it's in the brew info hash return brew_info[package_name] if brew_info[package_name] @@ -135,9 +133,6 @@ class Chef def installed_version(i) p_data = package_info(i) - # nil means we couldn't find anything - return nil if p_data.nil? - if p_data["keg_only"] if p_data["installed"].empty? nil @@ -165,8 +160,8 @@ class Chef def available_version(i) p_data = package_info(i) - # nil means we couldn't find anything - return nil if p_data.nil? + # nothing is available + return nil if p_data.empty? p_data["versions"]["stable"] end diff --git a/spec/unit/provider/package/homebrew_spec.rb b/spec/unit/provider/package/homebrew_spec.rb index 8e4f754d07..62d8aee6f1 100644 --- a/spec/unit/provider/package/homebrew_spec.rb +++ b/spec/unit/provider/package/homebrew_spec.rb @@ -238,9 +238,9 @@ describe Chef::Provider::Package::Homebrew do expect(provider.brew_info).to have_key("vim") end - it "returns nil if brew_cmd_output_data returned empty stdout" do + it "returns empty hash if brew_cmd_output_data returned empty stdout" do allow(provider).to receive(:brew_cmd_output).and_return("") - expect(provider.brew_info).to be_nil + expect(provider.brew_info).to eq({}) end end @@ -282,9 +282,9 @@ describe Chef::Provider::Package::Homebrew do expect(provider.available_version("openssl")).to eql("1.1.1f") end - it "returns nil if brew_info returns nil" do - allow(provider).to receive(:brew_info).and_return(nil) - expect(provider.available_version("foo")).to be_nil + it "returns nil if the package is not installed" do + allow(provider).to receive(:brew_info).and_return(brew_info_data) + expect(provider.available_version("bogus")).to be_nil end end -- cgit v1.2.1