diff options
author | Vivek Singh <vivek.singh@msystechnologies.com> | 2019-09-19 17:01:10 +0530 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2019-10-02 09:10:57 -0700 |
commit | e8fd15602bf2dad6822c0cd0164ccf30aa09183c (patch) | |
tree | 264ed08df5861fb91bc93bfb4ec201e4ee7f72c7 | |
parent | 7aa09027b604a5230cf28f059638782523d198ef (diff) | |
download | chef-e8fd15602bf2dad6822c0cd0164ccf30aa09183c.tar.gz |
Fix show_progress in remote_file is causing FloatDomainError: Infinity
- Add check to avoid total if zero or nil.
- Set @progress[resource] default value to -1 so that it can start with 0%.
Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r-- | lib/chef/formatters/doc.rb | 6 | ||||
-rw-r--r-- | spec/unit/formatters/doc_spec.rb | 18 |
2 files changed, 21 insertions, 3 deletions
diff --git a/lib/chef/formatters/doc.rb b/lib/chef/formatters/doc.rb index 19a6fafae6..7afbbcd6e5 100644 --- a/lib/chef/formatters/doc.rb +++ b/lib/chef/formatters/doc.rb @@ -290,11 +290,11 @@ class Chef end def resource_update_progress(resource, current, total, interval) - @progress[resource] ||= 0 + @progress[resource] ||= -1 - percent_complete = (current.to_f / total.to_f * 100).to_i + percent_complete = (current.to_f / total.to_f * 100).to_i unless total.to_f == 0.0 - if percent_complete > @progress[resource] + if percent_complete && percent_complete > @progress[resource] @progress[resource] = percent_complete diff --git a/spec/unit/formatters/doc_spec.rb b/spec/unit/formatters/doc_spec.rb index b8eccc1bc9..262276cfaa 100644 --- a/spec/unit/formatters/doc_spec.rb +++ b/spec/unit/formatters/doc_spec.rb @@ -76,6 +76,24 @@ describe Chef::Formatters::Base do expect(formatter.pretty_elapsed_time).to include("10 hours 10 minutes 10 seconds") end + it "shows nothing if total is nil" do + res = Chef::Resource::RemoteFile.new("canteloupe") + formatter.resource_update_progress(res, 35, nil, 10) + expect(out.string).to eq("") + end + + it "shows nothing if total is 0" do + res = Chef::Resource::RemoteFile.new("canteloupe") + formatter.resource_update_progress(res, 35, 0, 10) + expect(out.string).to eq("") + end + + it "shows nothing if current and total are 0" do + res = Chef::Resource::RemoteFile.new("canteloupe") + formatter.resource_update_progress(res, 0, 0, 10) + expect(out.string).to eq("") + end + it "shows the percentage completion of an action" do res = Chef::Resource::RemoteFile.new("canteloupe") formatter.resource_update_progress(res, 35, 50, 10) |