Extract code from signal handlers into methods

This will allow building other mechanisms to interact with Guard, for
example on JRuby, where signal handling tends to be unreliable.
This commit is contained in:
Nick Sieger 2011-05-06 12:29:24 -05:00
parent 815a81dd54
commit 70c15a7c94
2 changed files with 52 additions and 15 deletions

View File

@ -1,29 +1,41 @@
module Guard
module Interactor
extend self
def self.init_signal_traps
# Run all (Ctrl-\)
Signal.trap('QUIT') do
def run_all
::Guard.run do
::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :run_all) }
end
end
# Stop (Ctrl-C)
Signal.trap('INT') do
def stop
UI.info "Bye bye...", :reset => true
::Guard.listener.stop
::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :stop) }
abort("\n")
end
# Reload (Ctrl-Z)
Signal.trap('TSTP') do
def reload
::Guard.run do
::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :reload) }
end
end
def self.init_signal_traps
# Run all (Ctrl-\)
Signal.trap('QUIT') do
run_all
end
# Stop (Ctrl-C)
Signal.trap('INT') do
stop
end
# Reload (Ctrl-Z)
Signal.trap('TSTP') do
reload
end
end
end
end

View File

@ -0,0 +1,25 @@
require 'spec_helper'
describe Guard::Interactor do
subject { Guard::Interactor }
let(:guard) { mock "guard" }
before :each do
Guard.stub!(:guards).and_return([guard])
Guard.stub!(:listener).and_return(mock(:start => nil, :stop => nil))
end
it ".run_all should send :run_all to all guards" do
guard.should_receive(:run_all)
subject.run_all
end
it ".stop should send :stop to all guards" do
guard.should_receive(:stop)
lambda { subject.stop }.should raise_error(SystemExit)
end
it ".reload should send :reload to all guards" do
guard.should_receive(:reload)
subject.reload
end
end