Permalink
Browse files

Use Readline when it's available.

  • Loading branch information...
1 parent a258173 commit 8c17b07a5c2adbc26ad48506cb01d4a94b741a3e @georgebrock georgebrock committed Aug 11, 2013
Showing with 48 additions and 4 deletions.
  1. +8 −2 lib/thor/line_editor.rb
  2. +9 −0 spec/helper.rb
  3. +31 −2 spec/line_editor_spec.rb
View
@@ -1,8 +1,14 @@
+require 'readline' rescue nil
+
class Thor
module LineEditor
def self.readline(prompt)
- $stdout.print(prompt)
- $stdin.gets
+ if defined? Readline
+ Readline.readline(prompt)
+ else
+ $stdout.print(prompt)
+ $stdin.gets
+ end
end
end
end
View
@@ -67,5 +67,14 @@ def destination_root
File.join(File.dirname(__FILE__), 'sandbox')
end
+ # This code was adapted from Ruby on Rails, available under MIT-LICENSE
+ # Copyright (c) 2004-2013 David Heinemeier Hansson
+ def silence_warnings
+ old_verbose, $VERBOSE = $VERBOSE, nil
+ yield
+ ensure
+ $VERBOSE = old_verbose
+ end
+
alias silence capture
end
View
@@ -1,7 +1,36 @@
require 'helper'
-describe Thor::LineEditor do
- describe '#readline' do
+describe Thor::LineEditor, 'on a system with Readline support' do
+ before do
+ @original_readline = ::Readline if defined? ::Readline
+ silence_warnings { ::Readline = double('Readline') }
+ end
+
+ after do
+ silence_warnings { ::Readline = @original_readline }
+ end
+
+ describe '.readline' do
+ it 'invokes the Readline library' do
+ expect(Readline).to receive(:readline).with('Enter your name ').and_return('George')
+ expect(Thor::LineEditor.readline('Enter your name ')).to eq('George')
+ end
+ end
+end
+
+describe Thor::LineEditor, 'on a system without Readline support' do
+ before do
+ if defined? ::Readline
+ @original_readline = ::Readline
+ Object.send(:remove_const, :Readline)
+ end
+ end
+
+ after do
+ silence_warnings { ::Readline = @original_readline }
+ end
+
+ describe '.readline' do
it 'uses $stdout and $stdin to prompt the user for input' do
expect($stdout).to receive(:print).with('Enter your name ')
expect($stdin).to receive(:gets).and_return('George')

0 comments on commit 8c17b07

Please sign in to comment.