From f1ac9b5776bbd49c9ce71b4ddf138bcc61b9b8f6 Mon Sep 17 00:00:00 2001 From: Baptiste Fontaine Date: Mon, 14 Dec 2015 14:57:17 +0100 Subject: [PATCH] pretty_duration: fixed for int arguments Without this the returned string is not as accurate if the method is called with an int larger than 120. Closes Homebrew/homebrew#47002. Signed-off-by: Baptiste Fontaine --- Library/Homebrew/test/test_utils.rb | 8 ++++++++ Library/Homebrew/utils.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/test/test_utils.rb b/Library/Homebrew/test/test_utils.rb index 0be6f50ebd..f533e3f906 100644 --- a/Library/Homebrew/test/test_utils.rb +++ b/Library/Homebrew/test/test_utils.rb @@ -48,6 +48,14 @@ class UtilTests < Homebrew::TestCase assert_predicate $?, :success? end + def test_pretty_duration + assert_equal "2 seconds", pretty_duration(1) + assert_equal "2 seconds", pretty_duration(2.5) + assert_equal "42 seconds", pretty_duration(42) + assert_equal "4.2 minutes", pretty_duration(252) + assert_equal "4.2 minutes", pretty_duration(252.45) + end + def test_plural assert_equal "", plural(1) assert_equal "s", plural(0) diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index c874ec03cc..b1a2420444 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -140,7 +140,7 @@ end def pretty_duration(s) return "2 seconds" if s < 3 # avoids the plural problem ;) return "#{s.to_i} seconds" if s < 120 - "%.1f minutes" % (s/60) + "%.1f minutes" % (s/60.0) end def plural(n, s = "s")