Skip to content
Browse files

Fix Guard on windows when using the Readline interactor

  • Loading branch information...
1 parent a6f0fa7 commit 1d0d1eb64f8428e3c920f064e10f30f838756626 @Maher4Ever committed Dec 24, 2011
Showing with 43 additions and 8 deletions.
  1. +11 −2 lib/guard/interactors/readline.rb
  2. +32 −6 spec/guard/interactors/readline_spec.rb
View
13 lib/guard/interactors/readline.rb
@@ -31,15 +31,15 @@ def initialize
# Start the interactor.
#
def start
- store_terminal_settings
+ store_terminal_settings if stty_exists?
super
end
# Stop the interactor.
#
def stop
super
- restore_terminal_settings
+ restore_terminal_settings if stty_exists?
end
# Read a line from stdin with Readline.
@@ -84,6 +84,15 @@ def prompt
private
+ # Detects whether or not the stty command exists
+ # on the user machine.
+ #
+ # @return [Boolean] the status of stty
+ #
+ def stty_exists?
+ system('hash', 'stty')
+ end
+
# Stores the terminal settings so we can resore them
# when stopping.
#
View
38 spec/guard/interactors/readline_spec.rb
@@ -16,18 +16,44 @@
end
describe '#start' do
- it 'stores the terminal settings' do
- subject.should_receive(:store_terminal_settings)
- subject.start
+ context 'when running on a system that has stty' do
+ before { subject.should_receive(:stty_exists?).and_return(true) }
+
+ it 'stores the terminal settings' do
+ subject.should_receive(:store_terminal_settings)
+ subject.start
+ end
+ end
+
+ context 'when running on a system without stty' do
+ before { subject.should_receive(:stty_exists?).and_return(false) }
+
+ it 'does not store the terminal settings' do
+ subject.should_not_receive(:store_terminal_settings)
+ subject.start
+ end
end
end
describe '#stop' do
before { subject.instance_variable_set(:@thread, Thread.current) }
- it 'restores the terminal settings' do
- subject.should_receive(:restore_terminal_settings)
- subject.stop
+ context 'when running on a system that has stty' do
+ before { subject.should_receive(:stty_exists?).and_return(true) }
+
+ it 'restores the terminal settings' do
+ subject.should_receive(:restore_terminal_settings)
+ subject.stop
+ end
+ end
+
+ context 'when running on a system without stty' do
+ before { subject.should_receive(:stty_exists?).and_return(false) }
+
+ it 'does not store the terminal settings' do
+ subject.should_not_receive(:restore_terminal_settings)
+ subject.stop
+ end
end
end

3 comments on commit 1d0d1eb

@netzpirat

Very nice. I've picked your cherry.

@Maher4Ever
Owner

Glad to know this fix is accepted, I was worried about the use of hash instead of which in stty_exists?... That's why I didn't open a pull-request,

@netzpirat

I think hash is perfect, since we're not interested in the path, we're interested only to know if it exit in the path. I've tested this on a Windows 7 VM and my OS X box and it worked fine in both environments (returns true on OS X and false on Windows).

Please sign in to comment.
Something went wrong with that request. Please try again.