summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-04-17 16:32:46 -0700
committerTim Smith <tsmith84@gmail.com>2020-04-17 16:43:28 -0700
commit0cf9f8817e5b493cf93949b69a580dd45b39e021 (patch)
tree9dc9df6e3b2ef017b70153fc8031de5c8bfc3d7d
parentd380678cb616e55682a3ca5a61a0be340f301ca0 (diff)
downloadchef-multi_package.tar.gz
Return empty hash from brew_info and avoid a bunch of nil checksmulti_package
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/provider/package/homebrew.rb11
-rw-r--r--spec/unit/provider/package/homebrew_spec.rb10
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