summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-04-17 17:05:43 -0700
committerTim Smith <tsmith84@gmail.com>2020-04-17 17:05:43 -0700
commitc25eb20b2b1e4676118d7583b7f17394c9deec41 (patch)
treedbab2a32f38bed37b38c3a11df620ae4743a339e
parent0cf9f8817e5b493cf93949b69a580dd45b39e021 (diff)
downloadchef-c25eb20b2b1e4676118d7583b7f17394c9deec41.tar.gz
Use lamont's magic error handling
Tiny tweak to grab the first item so we actually get the hash back in the error handling. Given: ```ruby package %w{curl totally_junk_pkg} do action :upgrade end ``` ``` * No candidate version available for totally_junk_pkg ================================================================================ Error executing action `upgrade` on resource 'homebrew_package[curl, totally_junk_pkg]' ================================================================================ Chef::Exceptions::Package ------------------------- No candidate version available for totally_junk_pkg ``` Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/provider/package/homebrew.rb16
-rw-r--r--spec/unit/provider/package/homebrew_spec.rb5
2 files changed, 17 insertions, 4 deletions
diff --git a/lib/chef/provider/package/homebrew.rb b/lib/chef/provider/package/homebrew.rb
index d0321b2175..012bcdbb13 100644
--- a/lib/chef/provider/package/homebrew.rb
+++ b/lib/chef/provider/package/homebrew.rb
@@ -96,9 +96,21 @@ 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 {} if cmd_output.empty? # empty std_out == bad package queried
- Hash[Chef::JSONCompat.from_json(cmd_output).collect { |pkg| [pkg["name"], pkg] }]
+ if cmd_output.empty?
+ # we had some kind of failure so we need to iterate through each package to find them
+ package_name_array.each_with_object({}) do |package_name, hsh|
+ cmd_output = brew_cmd_output("info", "--json=v1", package_name, allow_failure: true)
+ if cmd_output.empty?
+ hsh[package_name] = {}
+ else
+ json = Chef::JSONCompat.from_json(cmd_output).first
+ hsh[json["name"]] = json
+ end
+ end
+ else
+ Hash[Chef::JSONCompat.from_json(cmd_output).collect { |pkg| [pkg["name"], pkg] }]
+ end
end
end
diff --git a/spec/unit/provider/package/homebrew_spec.rb b/spec/unit/provider/package/homebrew_spec.rb
index 62d8aee6f1..9975e72b02 100644
--- a/spec/unit/provider/package/homebrew_spec.rb
+++ b/spec/unit/provider/package/homebrew_spec.rb
@@ -238,9 +238,10 @@ describe Chef::Provider::Package::Homebrew do
expect(provider.brew_info).to have_key("vim")
end
- it "returns empty hash if brew_cmd_output_data returned empty stdout" do
+ it "returns empty hash for packages if they lack data" do
+ new_resource.package_name %w{bogus}
allow(provider).to receive(:brew_cmd_output).and_return("")
- expect(provider.brew_info).to eq({})
+ expect(provider.brew_info).to eq("bogus" => {})
end
end