Skip to content

Commit

Permalink
Fix: NoMethodError on decrement when output is non-TTY
Browse files Browse the repository at this point in the history
Why This Change Is Necessary
========================================================================

- [x] Bug Fix
- [ ] New Feature

Calling decrement when the output is not TTY enabled causes a no method
error as `bar_update_string` returns `nil` where a string is expected.

How These Changes Address the Issue
========================================================================

This change ensures a string is returned so there is no runtime error
and adds a test case.

Side Effects Caused By This Change
========================================================================

- [ ] This Causes a Breaking Change
- [x] This Does Not Cause Any Known Side Effects

Checklist
========================================================================

- [x] I have run `rubocop` against the codebase
- [x] I have added tests to cover my changes
- [x] All new and existing tests passed

------------------------------------------------------------------------
Github Issue URLs:
  * #147
  • Loading branch information
jfelchner committed Aug 3, 2018
2 parents 50016df + 6e582e3 commit 7b23c06
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ inherit_from:

# Put your project-specifc Rubocop config here

Layout/ClassStructure:
Enabled: false

Layout/ClosingParenthesisIndentation:
Enabled: false

Expand Down
5 changes: 3 additions & 2 deletions lib/ruby-progressbar/calculators/length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def length_override=(other)

# This code was copied and modified from Rake, available under MIT-LICENSE
# Copyright (c) 2003, 2004 Jim Weirich
# rubocop:disable Lint/RescueWithoutErrorClass
# rubocop:disable Style/RescueStandardError
def terminal_width
return 80 unless unix?

Expand All @@ -48,7 +48,7 @@ def terminal_width
rescue
80
end
# rubocop:enable Lint/RescueWithoutErrorClass
# rubocop:enable Style/RescueStandardError

# rubocop:disable Lint/DuplicateMethods
begin
Expand All @@ -68,6 +68,7 @@ def dynamic_width
dynamic_width_via_system_calls
end
end
# rubocop:enable Lint/DuplicateMethods

def dynamic_width_via_output_stream_object
_rows, columns = output.winsize
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-progressbar/outputs/non_tty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def bar_update_string
output_string = formatted_string[last_update_length..-1]
self.last_update_length = formatted_string.length

output_string
output_string.to_s
end

def default_format
Expand Down
2 changes: 0 additions & 2 deletions lib/ruby-progressbar/refinements/enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class ProgressBar
module Refinements
module Enumerator
refine ::Enumerator do
# rubocop:disable Metrics/BlockNesting
def with_progressbar(options = {}, &block)
chain = ::Enumerator.new do |yielder|
progress_bar = ProgressBar.create(options.merge(:starting_at => 0, :total => size))
Expand All @@ -18,7 +17,6 @@ def with_progressbar(options = {}, &block)

chain.each(&block)
end
# rubocop:enable Metrics/BlockNesting
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby-progressbar/time.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable Style/InlineComment
class ProgressBar
class Time
TIME_MOCKING_LIBRARY_METHODS = [
Expand Down Expand Up @@ -28,3 +29,4 @@ def unmocked_time_method
attr_accessor :time
end
end
# rubocop:enable Style/InlineComment
30 changes: 30 additions & 0 deletions spec/ruby-progressbar/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,36 @@ class ProgressBar
end
end

context 'decrementing the bar' do
it 'displays the bar with the correct progress' do
progressbar = ProgressBar::Base.new(:output => output,
:length => 20,
:starting_at => 1,
:total => 6,
:throttle_rate => 0.0)

progressbar.decrement

expect(output_string).to eql " \r" \
"Progress: |= |\r" \
"Progress: | |\r"
end

context 'for non-TTY enabled devices' do
it 'does nothing' do
progressbar = ProgressBar::Base.new(:output => non_tty_output,
:length => 20,
:starting_at => 2,
:total => 6,
:throttle_rate => 0.0)
progressbar.decrement

expect(non_tty_output_string).to eql "\n" \
"Progress: |=="
end
end
end

it 'can be converted into a hash' do
Timecop.freeze(::Time.utc(2012, 7, 26, 18, 0, 0))

Expand Down

0 comments on commit 7b23c06

Please sign in to comment.